Repository navigation
dns: do not deny queries in permission audit mode #67905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RafaelGSS
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
RafaelGSS:do-not-deny-queries-in-permission-audit-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
test/parallel/test-permission-audit-dns-does-not-deny.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const { isMainThread } = require('worker_threads'); | ||
|
|
||
| if (!isMainThread) { | ||
| common.skip('This test only works on a main thread'); | ||
| } | ||
|
|
||
| const assert = require('assert'); | ||
| const dgram = require('dgram'); | ||
| const { spawn } = require('child_process'); | ||
| const dnstools = require('../common/dns'); | ||
|
|
||
| // In audit mode, dns.resolve*(), Resolver, dns.reverse() and | ||
| // dns.lookupService() must publish the denial on node:permission-model:net | ||
| // and carry on with the query instead of failing with ERR_ACCESS_DENIED. | ||
|
|
||
| const server = dgram.createSocket('udp4'); | ||
|
|
||
| server.on('message', (msg, { address, port }) => { | ||
| const parsed = dnstools.parseDNSPacket(msg); | ||
| const { domain, type } = parsed.questions[0]; | ||
| const answer = type === 'PTR' ? | ||
| { type: 'PTR', domain, ttl: 123, value: 'example.org' } : | ||
| { type: 'A', domain, ttl: 123, address: '1.2.3.4' }; | ||
| server.send(dnstools.writeDNSPacket({ | ||
| id: parsed.id, | ||
| questions: parsed.questions, | ||
| answers: [answer], | ||
| }), port, address); | ||
| }); | ||
|
|
||
| const childScript = ` | ||
| const dc = require('node:diagnostics_channel'); | ||
| const dns = require('node:dns'); | ||
| const msgs = []; | ||
| dc.subscribe('node:permission-model:net', (m) => msgs.push({ | ||
| permission: m.permission, | ||
| resource: m.resource, | ||
| })); | ||
| const results = {}; | ||
| const resolver = new dns.promises.Resolver(); | ||
| resolver.setServers([process.env.DNS_SERVER]); | ||
| dns.setServers([process.env.DNS_SERVER]); | ||
|
|
||
| const code = (e) => (e ? e.code : null); | ||
| const run = async () => { | ||
| await new Promise((resolve) => { | ||
| dns.resolve4('example.org', (e, a) => { | ||
| results.resolve4 = { code: code(e), value: a }; | ||
| resolve(); | ||
| }); | ||
| }); | ||
| try { | ||
| results.resolverResolve4 = { | ||
| code: null, | ||
| value: await resolver.resolve4('example.org'), | ||
| }; | ||
| } catch (e) { | ||
| results.resolverResolve4 = { code: code(e) }; | ||
| } | ||
| await new Promise((resolve) => { | ||
| dns.reverse('127.0.0.1', (e, a) => { | ||
| results.reverse = { code: code(e), value: a }; | ||
| resolve(); | ||
| }); | ||
| }); | ||
| await new Promise((resolve) => { | ||
| dns.lookupService('127.0.0.1', 22, (e) => { | ||
| results.lookupService = { code: code(e) }; | ||
| resolve(); | ||
| }); | ||
| }); | ||
| console.log('RESULT ' + JSON.stringify(results)); | ||
| console.log('AUDIT ' + JSON.stringify(msgs)); | ||
| }; | ||
| run(); | ||
| `; | ||
|
|
||
| server.bind(0, '127.0.0.1', common.mustCall(() => { | ||
| const { port } = server.address(); | ||
| const env = { ...process.env, DNS_SERVER: `127.0.0.1:${port}` }; | ||
| const child = spawn( | ||
| process.execPath, | ||
| ['--permission-audit', '-e', childScript], | ||
| { env }); | ||
| let stdout = ''; | ||
| let stderr = ''; | ||
| child.stdout.setEncoding('utf8').on('data', (d) => stdout += d); | ||
| child.stderr.setEncoding('utf8').on('data', (d) => stderr += d); | ||
| child.on('close', common.mustCall((status) => { | ||
| server.close(); | ||
| assert.strictEqual(status, 0, stderr); | ||
| const lines = stdout.split('\n'); | ||
| const resultLine = lines.find((l) => l.startsWith('RESULT ')); | ||
| const auditLine = lines.find((l) => l.startsWith('AUDIT ')); | ||
| assert.ok(resultLine, stdout); | ||
| assert.ok(auditLine, stdout); | ||
| const results = JSON.parse(resultLine.slice('RESULT '.length)); | ||
| const msgs = JSON.parse(auditLine.slice('AUDIT '.length)); | ||
|
|
||
| assert.deepStrictEqual(results.resolve4, | ||
| { code: null, value: ['1.2.3.4'] }); | ||
| assert.deepStrictEqual(results.resolverResolve4, | ||
| { code: null, value: ['1.2.3.4'] }); | ||
| assert.deepStrictEqual(results.reverse, | ||
| { code: null, value: ['example.org'] }); | ||
| // The system resolver answers lookupService(), whatever the outcome is. | ||
| assert.notStrictEqual(results.lookupService.code, 'ERR_ACCESS_DENIED'); | ||
|
|
||
| for (const resource of ['example.org', '127.0.0.1']) { | ||
| assert.ok( | ||
| msgs.some((m) => m.permission === 'Net' && m.resource === resource), | ||
| `no Net message for ${resource}: ${JSON.stringify(msgs)}`); | ||
| } | ||
| })); | ||
| })); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer that this lives in its own file so we can lint it.