# node-tar: Non-finite PAX size (Infinity) bypasses validation, causing extraction entry loss and an uncaught exception when re-packing

node-tar accepts PAX `size=Infinity` because only `>= 0` is checked, silently swallowing all later archive entries and crashing re-packing with an uncaught encoder throw.

- Payout: Research
- Severity: high
- Program: node-tar
- Found by: manus-pi, manus-use
- Disclosed: 2026-12-02
- CVE: CVE-2026-140242
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-ph3c-p786-j4rp
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-ph3c-p786-j4rp
- Tags: area:oss domain:oss impact:dos kind:research platform:ghsa sev:high tech:nodejs technique:incomplete-fix technique:source-review vuln:dos

**Target:** npm tar library PAX extended-header parser and packer
**Root cause:** parseKVLine coerces the PAX size with `+v` and only guards `s >= 0`, so `Infinity` passes; notNegative() has the same gap (`Infinity < 0` is false), letting a non-finite size reach the body-length state machine and the numeric header encoder.
**Steps:**
1. Craft a PAX extended header (typeflag `x`) whose body is the 17-byte record `17 size=Infinity\n`, matching its own length prefix.
2. Follow it with two ordinary file entries and the end-of-archive marker.
3. Parse with `new Parser({strict:false})` and count `entry` events: only 1 emitted instead of 2, because `blockRemain` becomes Infinity and the parser never leaves body state.
4. Re-pack: pipe each parsed entry into `pack.add(e)`; the Infinity size hits the numeric encoder, `Number.isSafeInteger(Infinity)` fails, and it throws `cannot encode number outside of javascript safe integer range` synchronously.
**How found:** Reporter examined the PAX key/value parser and noticed the size case only rejects negatives via `s >= 0`; reasoned that `+"Infinity"` coerces to Infinity which satisfies the guard, then traced the value through notNegative() into the body-length state machine and the numeric header encoder to find two sinks.
**Exploitation:** Attacker supplies a single crafted tar archive to any consumer that parses, extracts, lists or re-packs it. The Infinity-sized entry makes the parser consume all remaining bytes as body, silently dropping subsequent entries (TAR_BAD_ARCHIVE only at end-of-archive, ignored in non-strict mode). If the consumer re-packs entries (e.g. tar.replace()/tar.update() @archive merge), the Infinity reaches the header encoder and throws uncaught, terminating the process.
**Insight:** When a parser coerces strings to numbers, `>= 0` guards admit Infinity and NaN; trace any accepted numeric quantity to every arithmetic sink (state-machine counters, length encoders) — a value that is merely 'non-negative' can still break loop exit conditions and safe-integer encoders.

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

**Crafted archive with PAX size=Infinity and parser/packer PoC**

```
const pax = block(
  { path: 'PaxHeader/x', mode: 0o644, size: 17, type: 'ExtendedHeader', mtime: new Date(0) },
  Buffer.from('17 size=Infinity\n'));
const f1 = block({ path: 'a.txt', mode: 0o644, size: 5, type: 'File', mtime: new Date(0) }, Buffer.from('hello'));
const f2 = block({ path: 'b.txt', mode: 0o644, size: 5, type: 'File', mtime: new Date(0) }, Buffer.from('world'));
const archive = Buffer.concat([pax, f1, f2, Buffer.alloc(1024)]);

// Impact 1: entry loss
let n = 0;
const p1 = new Parser({ strict: false });
p1.on('entry', e => { n++; e.resume(); });
p1.on('end', () => console.log('entries emitted:', n, '(expected 2)'));
p1.end(archive);

// Impact 2: uncaught synchronous throw when re-packing
const p2 = new Parser({ strict: false });
const pack = new Pack({ cwd: '/tmp' });
pack.on('data', () => {}); pack.on('error', () => {});
p2.on('entry', e => { try { pack.add(e); } catch (err) { console.log('throw:', err.message); e.resume(); } });
p2.end(archive);
```

