The use of __proto__ in process.mainModule.__proto__.require() bypasses the permission system in Node v19.6.1
Node's experimental permission policy system checked require() on process.mainModule but not the same method reached through the object's prototype. An attacker who could run a script under a restrictive policy could load modules the policy did not permit.
- Triaged in 6 days
- Fixed in 4 months
- Disclosed after 5 months
process.mainModule.require() correctly works with permission system in Node v19.6.1. But the use of __proto__ in process.mainModule.__proto__.require() can bypass the check.
Description and STR
Consider the following policy.json:
{
"resources": {
"./proc.js": {
"integrity": true
}
}
}
The policy only allows proc.js file to be loaded without any dependencies.
However with the following proc.js
const os = process.mainModule.__proto__.require("os")
console.log(process.version)
console.log(os.version())
We get the output:
└─$ ../node-v19.6.1-linux-x64/bin/node --experimental-policy=policy.json proc.js
v19.6.1
#1 SMP PREEMPT Debian 5.16.18-1kali1 (2022-04-01)
(node:2720) ExperimentalWarning: Policies are experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
Therefore os dependency can be loaded and os.version executed even if unspecified in permission system.
Impact
Bypass the permission system
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the policy check was attached to the process.mainModule.require property rather than to the underlying loader, so the same function reached via the prototype chain skipped the check.
Impact
A script running under the experimental policy could load and execute modules the policy did not allow, bypassing the permission system.
What the attacker needed
Ability to run a script under a Node policy; Node v19.6.1 with experimental policies
How it was found
- Create a policy file that permits only one script and no dependencies.
- Write a script that loads a module through the prototype of process.mainModule instead of calling require directly.
- Run Node with the experimental policy flag and observe the module loading despite the policy.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Node can be told, through a policy file, which files and modules a script is allowed to load. The check was applied to one way of calling the module loader but not to an equivalent way reached through the object's prototype, so a script could load a module the policy meant to block.
What is Prototype pollution?
Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.