-
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
lib: make Error
objects instantiation less vulnerable to prototype pollution
#46065
base: main
Are you sure you want to change the base?
lib: make Error
objects instantiation less vulnerable to prototype pollution
#46065
Conversation
Review requested:
|
const kIsNodeError = Symbol('kIsNodeError'); | ||
|
||
const isWindows = process.platform === 'win32'; | ||
|
||
const messages = new SafeMap(); | ||
const codes = {}; | ||
const codes = ObjectCreate(null); |
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.
const codes = ObjectCreate(null); | |
const codes = { __proto__: null }; |
this is equally robust and avoids a function call.
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 do prefer this, but I know Antoine prefers the other, and the diff is pretty "6 of one".
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.
Why the preference?
Even setting aside that Object.create is a regretted addition to the JS language, it's still got the potential overhead of a function call, as compared to the very straightforward syntax that node already uses with objects initialized with more than one property. Why is "no properties" a special case that requires a function call?
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.
it's still got the potential overhead of a function call
I don't think that should be a concern here because lib/internal/errors.js
is already a part of the snapshot, so this code won't get executed at startup, rather the context would get deserialized from the snapshot.
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.
ok, fair - it’s still indirection, and it’s using an API call for something that’s available with syntax.
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 is a style question that’s out of scope for this PR. If someone feels strongly about this, they should open a new PR that updates our eslint to enforce a particular style.
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.
yes thanks, i'm sure that won't get bogged down in blocks, i'll get right on that
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.
opened here: #46083
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.
(that was landed, so this can be resolved)
* @returns {T} | ||
*/ | ||
function assignOwnProperties(obj, ...sources) { | ||
const descriptors = ObjectCreate(null); |
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.
const descriptors = ObjectCreate(null); | |
const descriptors = { __proto__: null }; |
Benchmak CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1287/ Results
|
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 do not think we should do this. It increases the implementation complexity signficantly and reduces the performance of a critical part of the application.
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.
The slowdown is not acceptable in this specific case.
Errors are already a slow path for us given all the modifications we do to them, and we should not slow them down further.
Having a more robust
Error
instantiation makes sure the user would get the actual error rather than an unrelated one if the prototype was altered somewhere else.Before this change:
After this change: