# node-tar parses PAX extended headers by newline-splitting instead of length-delimiting, causing record dropping and forged-field injection

node-tar splits PAX headers on newlines instead of length-delimited records, so newline-containing filenames drop real records and let forged path/size/linkpath fields be injected.

- Payout: Research
- Severity: high
- Program: node-tar
- Found by: president-xd, eros938, hash3liZer
- Disclosed: 2026-12-02
- CVE: CVE-2026-140235
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-rgjp-p5j7-w36x
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-rgjp-p5j7-w36x
- Tags: area:oss domain:oss impact:integrity kind:research platform:ghsa sev:high tech:nodejs technique:parser-differential technique:source-review vuln:misconfiguration vuln:path-traversal

**Target:** npm tar package PAX extended header parser (src/pax.ts)
**Root cause:** parseKV splits the PAX header body on every '\n' and validates each fragment's length prefix, rather than consuming length-delimited records; a record whose value contains a newline fails the length check and is dropped, while a post-newline fragment forming a valid '<len> key=value' line is accepted as a real field.
**Steps:**
1. Create files whose names embed a newline plus a forged record tail, e.g. `B`*95 + '\n' + '24 path=forged-name.txt', `C`*95 + '\n' + '10 size=0', `D`*95 + '\n' + '14 linkpath=a`.
2. Pack with a compliant packer: `tar --format=posix -cf gnu.tar -C src .`
3. Read the archive with node-tar `t()`: observe pathlen=100 truncated names, forged size=0, forged linkpath="a", and pathlen=15 "forged-name.txt".
4. Extract with node-tar `x()`: 'forged-name.txt' is materialized on disk; A/C/D appear under truncated names.
5. Compare with GNU tar 1.35 / Python 3.13, which read true long names and correct attributes.
**How found:** Code reading of src/pax.ts revealed the maintainer's own `XXX` comment noting values with \n fail the naive line-by-line parse; the researcher then constructed newline-embedded filenames and compared node-tar against GNU tar and Python tarfile to confirm record dropping and forged-field acceptance.
**Exploitation:** A single attacker-controlled filename (or hand-crafted archive) packed by any length-compliant packer causes node-tar consumers to assign arbitrary path, size, linkpath, mode, uid/gid or mtime to an entry, defeating name/attribute-based inspection and feeding the path-reservation deadlock and meta-entry desync issues; with preservePaths the forged path may be absolute or contain '..'.
**Escalation:** Forged linkpath on a plain File is an injection route into the path-reservation deadlock (advisory 01); forged size feeds the meta-entry desync (advisory 02).
**Insight:** When auditing archive/format parsers, check whether length-delimited records are consumed by declared length or by delimiter splitting; any in-code 'XXX' about newline handling in a length-prefixed format is a strong signal for parser-differential injection.

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

**Python script crafting newline filenames with forged PAX tails**

```
python3 - <<'EOF'
import os; d='/tmp/paxnl/src'; os.makedirs(d, exist_ok=True)
open(os.path.join(d,'A'*95+'\nTAIL1.txt'),'w').write('PAX-DEPENDENT\n')          # legit newline name
open(os.path.join(d,'B'*95+'\n'+'24 path=forged-name.txt'),'w').write('INJECTED\n')  # forged path
open(os.path.join(d,'C'*95+'\n'+'10 size=0'),'w').write('EIGHTBYTS')              # forged size (true 9)
open(os.path.join(d,'D'*95+'\n'+'14 linkpath=a'),'w').write('X')                 # forged linkpath on a File
EOF
tar --format=posix -cf /tmp/paxnl/gnu.tar -C /tmp/paxnl/src .
```

**Vulnerable newline-splitting PAX parser code**

```
const parseKV = (str) =>
  str.replace(/\n$/, '').split('\n')          // <- splits INSIDE length-valid record values
     .reduce(parseKVLine, Object.create(null))
const parseKVLine = (set, line) => {
  const n = parseInt(line, 10)
  // XXX Values with \n in them will fail this.
  // Refactor to not be a naive line-by-line parse.
  if (n !== Buffer.byteLength(line) + 1) return set   // record dropped when its value contained '\n'
  line = line.slice((n + ' ').length)
```

