Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented Jan 23, 2018

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), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

assert

@nodejs-github-bot nodejs-github-bot added assert Issues and PRs related to the assert subsystem. errors Issues and PRs related to JavaScript errors originated in Node.js core. labels Jan 23, 2018
@BridgeAR
Copy link
Member Author

lib/assert.js Outdated
// Skip Node.js modules!
if (!filename.includes('/') && !filename.includes('\\')) {
return;
}
Copy link
Member

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?

Copy link
Member Author

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).

@jasnell
Copy link
Member

jasnell commented Jan 24, 2018

Generally lgtm but left a comment that I'd like to see addressed before signing off.

@BridgeAR
Copy link
Member Author

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`);
Copy link
Member

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]) {
Copy link
Member

@joyeecheung joyeecheung Jan 31, 2018

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..

Copy link
Member Author

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"
Copy link
Member

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)?

@BridgeAR
Copy link
Member Author

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.

@BridgeAR
Copy link
Member Author

@BridgeAR BridgeAR force-pushed the simple-assert-internal branch from f3e8895 to acb1cbd Compare January 31, 2018 17:44
@BridgeAR
Copy link
Member Author

Rebased due to conflicts (that git did not realize?!).

CI https://ci.nodejs.org/job/node-test-pull-request/12852/

e.emit('hello', false);
} catch (err) {
const frames = err.stack.split('\n');
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@joyeecheung joyeecheung Feb 3, 2018

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.
Copy link
Member

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..

Copy link
Member Author

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.
@BridgeAR BridgeAR force-pushed the simple-assert-internal branch from acb1cbd to 16f1e48 Compare February 2, 2018 17:22
@BridgeAR
Copy link
Member Author

BridgeAR commented Feb 2, 2018

Rebased again due to conflicts.

CI https://ci.nodejs.org/job/node-test-pull-request/12910/

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Feb 3, 2018
BridgeAR added a commit to BridgeAR/node that referenced this pull request Feb 6, 2018
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>
BridgeAR added a commit to BridgeAR/node that referenced this pull request Feb 6, 2018
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>
@BridgeAR
Copy link
Member Author

BridgeAR commented Feb 6, 2018

Landed in 3e910fb and a27f48d

@BridgeAR BridgeAR closed this Feb 6, 2018
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
'ok(failed(badly));';
try {
writeFileSync(filename, data);
Copy link
Member

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.

Copy link
Member Author

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.

MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
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>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
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>
@BridgeAR BridgeAR deleted the simple-assert-internal branch April 1, 2019 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. errors Issues and PRs related to JavaScript errors originated in Node.js core.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants