-
Notifications
You must be signed in to change notification settings - Fork 30k
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
assert: do not read Node.js modules #18322
Conversation
lib/assert.js
Outdated
// Skip Node.js modules! | ||
if (!filename.includes('/') && !filename.includes('\\')) { | ||
return; | ||
} |
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.
would it be better to check explicitly against process.binding('natives')
here?
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 switched to use that instead. In general: it is possible to show the native code as well, I just think it is not the right thing to do because it might confuse someone getting a error message of e.g. Reflect.apply(fn, this, foobar)
.
Generally lgtm but left a comment that I'd like to see addressed before signing off. |
PTAL |
lib/assert.js
Outdated
@@ -31,7 +31,9 @@ const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn'); | |||
const { inspect } = require('util'); | |||
const { EOL } = require('os'); | |||
|
|||
const codeCache = new Map(); | |||
const nativeModules = | |||
Object.keys(process.bindung('native')).map((e) => `${e}.js`); |
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.
Umm...there is a typo process.bindung
here..
lib/assert.js
Outdated
const filename = call.getFileName(); | ||
|
||
// Skip Node.js modules! | ||
if (nativeModules[filename]) { |
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.
You can check the existence of an native module via something like require('native_module').exists('assert')
, also require('native_module').getSource(id)
actually returns the source if you want to use it..
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 explicitly want to exclude native source code. I think it could cause more confusion than helping anyone in case users use it like e.g.
const assert = require('assert');
const EventEmitter = require('events');
const e = new EventEmitter();
// Listen and verify the input
e.once('hello', assert);
// Emit some data
e.emit('hello', null);
lib/assert.js
Outdated
closeSync(fd); | ||
} | ||
} | ||
// TODO: fix the "generatedMessage property" |
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.
Can you put your handle in the TODO like TODO(BridgeAR)
?
Since the TODO was mentioned, I just went ahead and pushed another commit that fixed that as well. I did not want to open another PR to prevent rebasing all the time due to conflicting changes after one PR landed. |
f3e8895
to
acb1cbd
Compare
Rebased due to conflicts (that git did not realize?!). |
e.emit('hello', false); | ||
} catch (err) { | ||
const frames = err.stack.split('\n'); | ||
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/); |
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.
This might be affected by internal changes of events and assert...can you look for the frame that contains __filename
instead of using hard-coded offsets?
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.
Hm, I am not sure I can follow. I explicitly tried to write the test in a way that it will not be affected by internal changes. It should keep on working no matter if the lines are moved or if the specific code is moved to another file. It depends on the thrown error with the information about the filename etc.
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.
Uh, so this frame is guaranteed to belong to where EventEmitter.emit
is defined and that should be a safe target...never mind then, sorry for the noise.
} | ||
); | ||
|
||
// Do not try to check Node.js modules. |
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.
This might deserve a separate test file since test-assert.js
is already 1000+ lines long..
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 personally would like to keep this test together with the other simple assert tests. I am fine to move all of those to another file though and I agree that this file is a bit long.
Prevent reading a Node.js module. That could theoretically lead to false errors being thrown otherwise.
The generatedMessage was wrong in case simple assert was used and a message was auto generated. This fixed the TODO.
acb1cbd
to
16f1e48
Compare
Rebased again due to conflicts. |
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.
LGTM
Prevent reading a Node.js module. That could theoretically lead to false errors being thrown otherwise. PR-URL: nodejs#18322 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
The generatedMessage was wrong in case simple assert was used and a message was auto generated. This fixed the TODO. PR-URL: nodejs#18322 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` + | ||
'ok(failed(badly));'; | ||
try { | ||
writeFileSync(filename, data); |
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.
Sorry to be so late to this, but can you explain why this is writing a file in project root? Is the idea to make sure that assert
doesn't try to parse an events.js
file when it needs a stack line or something from the internal events
module? I don't completely understand what's going on here, but I'd like to.
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.
Oh, I just saw this right now. What this test makes sure is that if assert.ok
is triggered inside of Node.js core code and that Node.js core file has the same name as a user file in the root directory (in this example events.js
) it would have read the users file and that is clearly wrong.
Prevent reading a Node.js module. That could theoretically lead to false errors being thrown otherwise. PR-URL: nodejs#18322 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
The generatedMessage was wrong in case simple assert was used and a message was auto generated. This fixed the TODO. PR-URL: nodejs#18322 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Prevent reading a Node.js module. That could theoretically lead to
false errors being thrown otherwise.
Update: I also added another commit that addresses a TODO about the
generatedMessage
when using simple assert.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
assert