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?
| * @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 }; |


Having a more robust
Errorinstantiation 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: