Packages / happy-dom / Security
Happy DOM: VM Context Escape can lead to Remote Code Execution
Affected releases · happy-dom
- 19.x and older0.0.1 – 19.0.2No fix, no longer updated
19.x has no fix yet; the newest fixed line:
npm install happy-dom@20.0.0Not affected
21.0.0 – 21.0.4
5 releases, latest
Affected
19.0.0 – 19.0.2
3 releases
Affected
18.0.0 – 18.0.1
2 releases
Affected
17.0.0 – 17.6.3
51 releases
Affected
16.0.0 – 16.8.1
29 releases
Affected
15.0.0 – 15.11.7
42 releases
Affected
14.0.0 – 14.12.3
44 releases
Affected
13.0.0 – 13.10.1
53 releases
Affected
12.0.0 – 12.10.3
28 releases
Affected
11.0.0 – 11.2.0
11 releases
Affected
10.0.0 – 10.11.2
35 releases
Affected
9.0.0 – 9.20.3
60 releases
Affected
8.0.0 – 8.9.0
33 releases
Affected
7.0.0 – 7.8.1
40 releases
Affected
6.0.0 – 6.0.4
5 releases
Affected
5.0.0 – 5.4.0
9 releases
Affected
4.0.0 – 4.1.0
4 releases
Affected
3.0.0 – 3.2.2
6 releases
Affected
2.0.0 – 2.55.0
106 releases
Affected
1.0.0 – 1.18.2
58 releases
Affected
0.0.1 – 0.14.1
68 releases
Is your project affected?
Paste your package.json, or type the range you declare for happy-dom. It is read in your browser and sent nowhere.
What happens
Escape of VM Context gives access to process level functionality
Summary
Happy DOM v19 and lower contains a security vulnerability that puts the owner system at the risk of RCE (Remote Code Execution) attacks.
A Node.js VM Context is not an isolated environment, and if the user runs untrusted JavaScript code within the Happy DOM VM Context, it may escape the VM and get access to process level functionality.
It seems like what the attacker can get control over depends on if the process is using ESM or CommonJS. With CommonJS the attacker can get hold of the require() function to import modules.
Happy DOM has JavaScript evaluation enabled by default. This may not be obvious to the consumer of Happy DOM and can potentially put the user at risk if untrusted code is executed within the environment.
Reproduce
CommonJS (Possible to get hold of require)
const { Window } = require('happy-dom');
const window = new Window({ console });
window.document.write(`
<script>
const process = this.constructor.constructor('return process')();
const require = process.mainModule.require;
console.log('Files:', require('fs').readdirSync('.').slice(0,3));
</script>
`);ESM (Not possible to get hold of import or require)
const { Window } = require('happy-dom');
const window = new Window({ console });
window.document.write(`
<script>
const process = this.constructor.constructor('return process')();
console.log('PID:', process.pid);
</script>
`);Potential Impact
Server-Side Rendering (SSR)
const { Window } = require('happy-dom');
const window = new Window();
window.document.innerHTML = userControlledHTML;Testing Frameworks
Any test suite using Happy-DOM with untrusted content may be at risk
Attack Scenarios
Data Exfiltration: Access to environment variables, configuration files, secrets
Lateral Movement: Network access for connecting to internal systems. Happy DOM already gives access to the network by fetch, but has protections in place (such as CORS and header validation etc.).
Code Execution: Child process access for running arbitrary commands
Persistence: File system access
Recommended Immediate Actions
Update Happy DOM to v20 or above
This version has JavaScript evaluation disabled by default
This version will output a warning if JavaScript is enabled in an insecure environment
Run Node.js with the "--disallow-code-generation-from-strings" if you need JavaScript evaluation enabled
This makes sure that evaluation can't be used at process level to escape the VM
eval()andFunction()can still be used within the Happy DOM VM without any known security riskHappy DOM v20 and above will output a warning if this flag is not in use
If you can't update Happy DOM right now, it's recommended to disable JavaScript evaluation, unless you completely trust the content within the environment
Technical Root Cause
All classes and functions inherit from Function. By walking the constructor chain it's possible to get hold of Function at process level. As Function can evaluate code from strings, it's possible to execute code at process level.
Running Node with the "--disallow-code-generation-from-strings" flag protects against this.