Permissions policies can be bypassed via process.mainModule
Node's experimental permission policy feature was meant to restrict which built-in modules a script could load, but a script could reach any module through process.mainModule.require instead of the normal require. An attacker running code under a policy could therefore escape the intended sandbox and load modules that were not whitelisted.
- Triaged in 2 days
- Fixed in 4 months
- Disclosed after 5 months
Summary:
Permissions policies module can be bypassed via process.mainModule.require
Description:
Permission policies allow to run a script with a specific set of authorized node js built-in modules.
However, the script could access non authorized modules by calling process.mainModule.require()
Steps To Reproduce:
- Create
escape.jsfile:
console.log(process.mainModule.require("os").cpus());
- Create
policy.jsonfile:
{
"onerror": "exit",
"scopes": {
"file:": {
"integrity": true,
"dependencies": {}
}
}
}
- Run:
node --experimental-policy=policy.json escape.js
- You will see your os cpus listed in the console even though the
escape.jsfile does not have the permission to import the nodeosmodule
Impact:
Permission policies are supposed to enforce imported modules to a limited whitelist. This vulnerability allow a script to include any non-whitelisted module.
If you modify escape.js to use top level require statement, like this:
const os = require("os");
console.log(os.cpus());
and run again:
node --experimental-policy=policy.json escape.js
you'll now see this error:
Error [ERR_MANIFEST_DEPENDENCY_MISSING]: Manifest resource escape.js does not list os as a dependency specifier for conditions: require, node, node-addons
which is the expected behavior and should be enforced as well when using process.mainModule.require
Impact
Any project that uses permission policies for arbitrary code execution are vulnerable to sandbox escaping. This example show a non-critical permission gain (listing the machine cpu), but an attacker could do much more damage by accessing internal file system, running child processes, ...
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 enforcement only hooked the standard require path and did not cover the equivalent module-loading route exposed through process.mainModule.
Impact
A script running under a permission policy could load any non-whitelisted module, so projects relying on policies for sandboxing arbitrary code could be escaped, with the example showing only a minor gain but potentially allowing file system access or child processes.
What the attacker needed
Node run with the experimental policy flag; Attacker controls the script being run
How it was found
- Create a small script that loads a built-in module through process.mainModule.require and prints its output.
- Create a policy file that does not list that module as a dependency.
- Run the script with the experimental policy flag pointing at the policy file.
- Observe the module's output appears even though the policy did not allow it.
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.
A permission policy is a list that says which built-in pieces of Node a script is allowed to use, a bit like a guest list. The check only looked at the normal way of asking for a module, so a script could ask through a different door and get anything it wanted. That means the guest list could be ignored.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.