Summary
A code injection vulnerability in Happy DOM's ECMAScriptModuleCompiler allows arbitrary JavaScript execution when specially crafted export { } declarations are processed in ES module scripts.
If an attacker can influence module source code consumed by happy-dom, they may achieve remote code execution in the host environment where Happy DOM is running.
Affected context
This vulnerability appears in projects that embed or depend on Happy DOM versions 15.10.0 through 20.8.7, particularly in test runners, server-side rendering tools, or automation frameworks that execute untrusted or semi-trusted ES modules through Happy DOM.
It is most relevant where:
- ES module scripts are dynamically loaded or transformed via
ECMAScriptModuleCompiler - User-controlled or third‑party content can influence
export { ... }declarations - Executed module code runs with meaningful access to the file system, network, or sensitive process capabilities
Exploit preconditions
- Attack surface: ES module scripts processed by Happy DOM's
ECMAScriptModuleCompiler. - Required access: Ability to provide or modify module source code (e.g., plugin ecosystem, test fixtures, user‑generated scripts, or compromised package).
- Trigger point: Parsing and compilation of an ES module that contains an
export { }declaration with injected expressions. - Why exploitation is possible:
- The compiler interpolates export specifiers directly into generated JavaScript as executable expressions.
- Sanitization is incomplete: while some quoting is filtered, backticks are not stripped, enabling template literal expressions to be injected.
- Generated code executes in the same context as the host application using Happy DOM, giving the payload access to that environment's APIs and resources.
Why this happens
The root cause is unsafe code generation where untrusted fragments from export { } declarations are treated as benign identifiers but are directly embedded into dynamically constructed JavaScript.
This matches CWE-94: Improper Control of Generation of Code ('Code Injection'), in which input that should be handled as data is instead used to construct executable code.
The quote filter only partially escapes characters and fails to neutralize backticks, so an attacker can wrap arbitrary JavaScript in template literals or expressions that are then evaluated when the generated module code runs.
Potential impact
- Execution of arbitrary JavaScript code within the process hosting Happy DOM
- Access to and modification of in‑memory data, including test secrets and configuration
- File system read/write operations if the environment exposes such APIs
- Outbound or internal network requests (e.g., data exfiltration, SSRF) via available network libraries
- Lateral movement within CI/CD or build pipelines that rely on Happy DOM for testing or rendering
- Compromise of downstream build artifacts or test outputs used in supply-chain processes
General mitigation guidance
Mitigation focuses on eliminating unsafe code generation paths and treating module components as data, not executable fragments.
Recommended patterns include:
- Avoid direct string interpolation of parsed identifiers or specifiers into generated code; use structured AST transformations or safe code emitters.
- Apply strict validation and canonicalization of export specifiers, ensuring they conform to identifier syntax rather than arbitrary expressions.
- Fully neutralize or reject characters that can introduce new execution contexts (such as backticks, semicolons, or template literal delimiters) when dynamic code generation is unavoidable.
- Prefer static module resolution and compilation pipelines where possible, instead of on‑the‑fly code generation based on runtime input.
- Run Happy DOM workloads with least privilege (restricted file system and network access, limited environment variables) to reduce impact if code execution occurs.
- Limit or sandbox user‑supplied or plugin‑supplied scripts, for example via dedicated processes or restricted JavaScript runtimes, instead of executing them directly in the host process.
How teams detect issues like this
Detection is typically achieved by focusing on unsafe dynamic code generation and module compilation logic:
- Static analysis:
- Rules that flag dynamic construction of JavaScript source using string concatenation or template literals, especially in compilers or loaders.
- Patterns that look for insertion of user-controlled values into
export/importstatements or other syntactic constructs. - Checks for incomplete sanitization routines that ignore backticks or other execution-enabling characters.
- Code review:
- Manual review of module compilation, bundling, or transformation utilities, with attention to how identifiers and specifiers are handled.
- Examination of any quote-filtering or escaping functions to confirm they neutralize all code execution vectors, not just quotes.
- Runtime and policy enforcement:
- Logging and monitoring of dynamically compiled code paths, including source snippets and callers.
- CI policies that forbid or heavily scrutinize new usages of
eval,Function, or equivalent dynamic compilation flows in libraries like Happy DOM. - Automated tools:
- Use of SAST tools and linters configured with rules for dynamic code evaluation and unsafe string interpolation in JavaScript/TypeScript codebases.

