-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for non-own property access (#1280)
Add a test to check for non-own property access which could be manipulated by prototype pollution. Fix such accesses to check if it is an own property first. Because this is standard Node.js behavior this is considered a hardening measure (rather than a security fix).
- Loading branch information
1 parent
3182149
commit c766179
Showing
6 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @overview Provides testing utilities. | ||
* @license MIT | ||
*/ | ||
|
||
import * as arbitrary from "../../_arbitraries.js"; | ||
import * as pollution from "./_pollution.js"; | ||
|
||
export { arbitrary, pollution }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @overview Contains helpers to detect non-own property access. | ||
* @license MIT | ||
*/ | ||
|
||
import assert from "node:assert/strict"; | ||
|
||
/** | ||
* A store for non-own property accesses by proxy. | ||
*/ | ||
const pollutionState = new Map(); | ||
|
||
/** | ||
* Check whether or not a value is nil (`null` or `undefined`). | ||
* | ||
* @param {any} value The value to check. | ||
* @returns {boolean} `true` if `value` is nil, `false` otherwise. | ||
*/ | ||
function isNil(value) { | ||
return value === null || value === undefined; | ||
} | ||
|
||
/** | ||
* Check whether or not a value is a primitive value. | ||
* | ||
* @param {any} value The value to check. | ||
* @returns {boolean} `true` if `value` is primitive, `false` otherwise. | ||
*/ | ||
function isPrimitive(value) { | ||
return typeof value === "number" || typeof value === "string"; | ||
} | ||
|
||
/** | ||
* Check whether or not a value can be wrapped by a `Proxy`. | ||
* | ||
* @param {any} value The value to check. | ||
* @returns {boolean} `true` if `value` is proxyable, `false` otherwise. | ||
*/ | ||
function isProxyable(value) { | ||
return !(isNil(value) || isPrimitive(value)); | ||
} | ||
|
||
/** | ||
* Wrap the provided target in in order to monitor it for access to properties | ||
* not present on the target. | ||
* | ||
* @param {any} target The value to wrap. | ||
* @returns {any} The wrapped value (or original if wrapping isn't possible). | ||
*/ | ||
export function wrap(target) { | ||
if (!isProxyable(target)) { | ||
return target; | ||
} | ||
|
||
const nonOwnAccesses = new Set(); | ||
const proxy = new Proxy(target, { | ||
get(target, property, _proxy) { | ||
if (!Object.hasOwn(target, property)) { | ||
nonOwnAccesses.add(property); | ||
} | ||
|
||
// Return normal lookup to ensure normal test execution. | ||
return target[property]; | ||
}, | ||
}); | ||
|
||
pollutionState.set(proxy, nonOwnAccesses); | ||
return proxy; | ||
} | ||
|
||
/** | ||
* Check if non-own property access was detected on the given wrapped object. | ||
* | ||
* @param {any} wrapped A `wrap`ped value. | ||
* @throws {Error} If non-own property access was detected. | ||
*/ | ||
export function check(wrapped) { | ||
if (!isProxyable(wrapped)) { | ||
return; | ||
} | ||
|
||
assert.ok(pollutionState.has(wrapped), "target not found"); | ||
const nonOwnAccesses = pollutionState.get(wrapped); | ||
|
||
// Remove the proxy from the state so re-use of it does not result in errors | ||
// from one test to affect other tests. (Also just to reduce memory usage.) | ||
pollutionState.delete(wrapped); | ||
|
||
const actual = nonOwnAccesses.size; | ||
const expected = 0; | ||
const propertiesList = Array.from(nonOwnAccesses.values()).join(", "); | ||
|
||
assert.equal( | ||
actual, | ||
expected, | ||
`Non-own access to ${actual} property(s) detected: ${propertiesList}`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters