Skip to content

Commit 4327ff8

Browse files
committed
tools,doc: add guards against prototype pollution when creating proxies
PR-URL: nodejs#43391 Reviewed-By: James M Snell <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
1 parent e0b41a5 commit 4327ff8

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

doc/contributing/primordials.md

+24
Original file line numberDiff line numberDiff line change
@@ -738,3 +738,27 @@ class SomeClass {
738738
ObjectDefineProperty(SomeClass.prototype, 'readOnlyProperty', kEnumerableProperty);
739739
console.log(new SomeClass().readOnlyProperty); // genuine data
740740
```
741+
742+
### Defining a `Proxy` handler
743+
744+
When defining a `Proxy`, the handler object could be at risk of prototype
745+
pollution when using a plain object literal:
746+
747+
```js
748+
// User-land
749+
Object.prototype.get = () => 'Unrelated user-provided data';
750+
751+
// Core
752+
const objectToProxy = { someProperty: 'genuine value' };
753+
754+
const proxyWithPlainObjectLiteral = new Proxy(objectToProxy, {
755+
has() { return false; },
756+
});
757+
console.log(proxyWithPlainObjectLiteral.someProperty); // Unrelated user-provided data
758+
759+
const proxyWithNullPrototypeObject = new Proxy(objectToProxy, {
760+
__proto__: null,
761+
has() { return false; },
762+
});
763+
console.log(proxyWithNullPrototypeObject.someProperty); // genuine value
764+
```

lib/internal/debugger/inspect.js

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function createAgentProxy(domain, client) {
117117
};
118118

119119
return new Proxy(agent, {
120+
__proto__: null,
120121
get(target, name) {
121122
if (name in target) return target[name];
122123
return function callVirtualMethod(params) {

lib/internal/http2/core.js

+1
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ function trackAssignmentsTypedArray(typedArray) {
982982
}
983983

984984
return new Proxy(typedArray, {
985+
__proto__: null,
985986
get(obj, prop, receiver) {
986987
if (prop === 'copyAssigned') {
987988
return copyAssigned;

lib/internal/modules/cjs/loader.js

+4
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ const wrapper = [
217217
];
218218

219219
let wrapperProxy = new Proxy(wrapper, {
220+
__proto__: null,
221+
220222
set(target, property, value, receiver) {
221223
patched = true;
222224
return ReflectSet(target, property, value, receiver);
@@ -725,6 +727,8 @@ function emitCircularRequireWarning(prop) {
725727
// A Proxy that can be used as the prototype of a module.exports object and
726728
// warns when non-existent properties are accessed.
727729
const CircularRequirePrototypeWarningProxy = new Proxy({}, {
730+
__proto__: null,
731+
728732
get(target, prop) {
729733
// Allow __esModule access in any case because it is used in the output
730734
// of transpiled code to determine whether something comes from an

test/parallel/test-eslint-avoid-prototype-pollution.js

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ new RuleTester({
4545
'ReflectDefineProperty({}, "key", { "__proto__": null })',
4646
'ObjectDefineProperty({}, "key", { \'__proto__\': null })',
4747
'ReflectDefineProperty({}, "key", { \'__proto__\': null })',
48+
'new Proxy({}, otherObject)',
49+
'new Proxy({}, someFactory())',
50+
'new Proxy({}, { __proto__: null })',
51+
'new Proxy({}, { __proto__: null, ...{} })',
4852
],
4953
invalid: [
5054
{
@@ -183,5 +187,21 @@ new RuleTester({
183187
code: 'StringPrototypeSplit("some string", /some regex/)',
184188
errors: [{ message: /looks up the Symbol\.split property/ }],
185189
},
190+
{
191+
code: 'new Proxy({}, {})',
192+
errors: [{ message: /null-prototype/ }]
193+
},
194+
{
195+
code: 'new Proxy({}, { [`__proto__`]: null })',
196+
errors: [{ message: /null-prototype/ }]
197+
},
198+
{
199+
code: 'new Proxy({}, { __proto__: Object.prototype })',
200+
errors: [{ message: /null-prototype/ }]
201+
},
202+
{
203+
code: 'new Proxy({}, { ...{ __proto__: null } })',
204+
errors: [{ message: /null-prototype/ }]
205+
},
186206
]
187207
});

tools/eslint-rules/avoid-prototype-pollution.js

+17
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ module.exports = {
128128
...createUnsafeStringMethodReport(context, 'StringPrototypeReplaceAll', 'Symbol.replace'),
129129
...createUnsafeStringMethodReport(context, 'StringPrototypeSearch', 'Symbol.search'),
130130
...createUnsafeStringMethodReport(context, 'StringPrototypeSplit', 'Symbol.split'),
131+
132+
'NewExpression[callee.name="Proxy"][arguments.1.type="ObjectExpression"]'(node) {
133+
for (const { key, value } of node.arguments[1].properties) {
134+
if (
135+
key != null && value != null &&
136+
((key.type === 'Identifier' && key.name === '__proto__') ||
137+
(key.type === 'Literal' && key.value === '__proto__')) &&
138+
value.type === 'Literal' && value.value === null
139+
) {
140+
return;
141+
}
142+
}
143+
context.report({
144+
node,
145+
message: 'Proxy handler must be a null-prototype object'
146+
});
147+
}
131148
};
132149
},
133150
};

0 commit comments

Comments
 (0)