CVE-2026-140235 — Tar — CWE-20 · CWE-436
### 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 ```ts // 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 ne
### 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 ```ts // 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 ne
🎯 What is this vulnerability?
Root cause node-tar (versions 7.5.0 – 8.0.0) parses PAX records with a helper called parseKV.
PAX records are length‑delimited key/value pairs that may contain arbitrary data, including newline characters (\n). parseKV naïvely splits the record on every newline, treating each line as a separate key/value pair.
If a newline appears inside a value, the parser:
1. Drops the original field – the key/value pair that contained the newline is lost.
2. Treats the trailing fragment (the part after the newline) as a new key/value pair.
When the forged key is one of the special tar header fields (path, size, linkpath, etc.), the library ends up creating an entry with a forged attribute that can point outside the intended extraction directory.
CWE mapping
| CWE | Description |
|---|---|
| CWE‑20 | Improper Input Validation – the library accepts arbitrary PAX data without sanitising newlines. |
| CWE‑436 | Improper Handling of Path Traversal – forged path values can escape the extraction root when preservePaths is enabled. |
Impact of the missing check
- Integrity: The attacker can overwrite arbitrary files on disk (e.g., configuration files, web‑root files).
- Privilege Escalation: If the Node.js process runs as a privileged user (root, system, or a user with write access to critical directories), the attacker can modify binaries or scripts.
- Remote Code Execution (RCE): By writing a malicious script or binary into a web‑accessible directory, the attacker can later execute it.
- Availability: Not a primary concern; the flaw does not crash the extractor, but corrupted archives could cause downstream failures.
⚡ Attack Chain
Assumptions
* The target application uses node-tar ≥ 7.5.0 and < 8.0.0.
* The application accepts user‑supplied tar archives (file upload, API endpoint, etc.).
*preservePathsis enabled (the default isfalse).
* The Node.js process runs with write access to the extraction root or higher.
1. Discovery & Reconnaissance
| Step | Technique | Tool / Command |
|---|---|---|
| Identify vulnerable library | Search package.json or npm ls output for node-tar | npm ls node-tar |
| Determine version | Inspect the resolved version | npm ls node-tar → [email protected] |
Confirm preservePaths usage | Review source code or configuration | Search for preservePaths: true in the codebase |
If the application is a black‑box web service, a simple upload of a tar file and observation of the response can reveal whether the extraction succeeded or failed.
2. Exploitation
2.1 Crafting the malicious tar
The PAX record format is:
<length> <key>=<value>\nparseKV splits on \n.
To exploit, we embed a newline inside the value of a key that controls the extraction path.
Example payload – a tar archive that writes /tmp/evil.txt into the extraction root when preservePaths is true.
# Create a benign file that will be extracted
echo "Hello world" > /tmp/hello.txt
# Build a malicious tar with a PAX record that contains a newline in the value
# The record will be interpreted as:
# path=../../../../../../../../tmp/evil.txt
# (the rest of the line after the newline is ignored)
tar --format=ustar \
--pax-option="path=../../../../../../../../tmp/evil.txt\n" \
-cf malicious.tar /tmp/hello.txtThe\ninside thepath=value forcesparseKVto split the record, dropping the originalpathfield and turning the trailing fragment into a forged path.
2.2 Delivering the archive
Assume the application exposes an upload endpoint /api/upload that accepts a tar file and extracts it with:
const tar = require('node-tar');
tar.extract({
file: req.file.path,
cwd: '/var/www/app/uploads',
preservePaths: true
});PoC request (illustrative)
curl -X POST http://target.example.com/api/upload \
-F "[email protected]"If the endpoint is protected by authentication, the attacker must first obtain valid credentials or exploit an unauthenticated upload path.
2.3 Result
When node-tar processes malicious.tar:
path value resolves to /tmp/evil.txt (outside /var/www/app/uploads).preservePaths is true, the library does not sanitize the path./tmp/evil.txt is created (or overwritten) with the contents of hello.txt.If the attacker writes a malicious script instead of hello.txt, they can achieve RCE.
3. Post‑Exploitation
| Goal | Technique | Outcome |
|---|---|---|
| Write arbitrary files | Use the same mechanism to write a PHP/Node.js script into the web root | The attacker can later access the script via HTTP |
| Privilege Escalation | If the Node.js process runs as root, overwrite /etc/passwd or a system binary | Gain root shell or modify system configuration |
| Persistence | Deploy a cron job or systemd unit via the same path traversal | Maintain long‑term access |
| Data Exfiltration | Write a script that reads sensitive files and sends them to the attacker | Steal credentials, secrets, or intellectual property |
The impact is proportional to the privileges of the Node.js process and the sensitivity of the files that can be overwritten.
🌍 Real World Impact
| Impact | CVSS Vector | Explanation |
|---|---|---|
| Confidentiality (VC:L) | VC:L | The attacker can read files only if they can write them first; no direct read‑only leak. |
| Integrity (VI:H) | VI:H | Arbitrary file overwrite is a high‑integrity impact. |
| Availability (VA:L) | VA:L | Not a primary concern; the flaw does not crash the extractor. |
| Privilege Escalation | – | Possible if the process runs with elevated privileges. |
| RCE | – | Achievable by writing a malicious script into a web‑accessible directory. |
Practical consequences
/var/www/html/ and then execute it. config.yaml, .env) and alter service behavior. 🛡️ Remediation & Mitigation
1. Patch
| Affected Versions | Fixed In |
|---|---|
node-tar 7.5.0 – 8.0.0 | 8.0.0 (and later) |
Upgrade recommendation
npm install node-tar@^8.0.0If the project uses a lockfile (package-lock.jsonoryarn.lock), runnpm audit fixoryarn upgradeto apply the patch.
2. Workarounds (if upgrade is not immediately possible)
| Mitigation | How to implement |
|---|---|
Disable preservePaths | Set preservePaths: false when calling tar.extract. |
| Sanitize paths manually | Before extraction, strip .. components and enforce a whitelist of allowed directories. |
| Use a safe extraction library | Replace node-tar with a library that validates PAX records (e.g., tar-stream with custom validation). |
| Restrict extraction root | Mount the extraction directory with chroot or use a container with a dedicated filesystem. |
| File system permissions | Run the Node.js process with the least privileges required (e.g., non‑root). |
3. Detection
| Indicator | Log pattern | IoC |
|---|---|---|
| Suspicious extraction paths | tar: extracting: ../../../../etc/passwd | Path containing .. or absolute / in extraction logs |
| Failed extraction due to path traversal | tar: error: path traversal detected | Error messages from node-tar |
| Unexpected file creation outside root | Created file: /tmp/evil.txt | Files appearing in /tmp or other non‑expected directories after an upload |
If the application logs extraction events (e.g., viawinstonorbunyan), search for the above patterns.
4. Monitoring
/evil.php). ⚠️ Urgency
node-tar 7.5.0 – 8.0.0 is in use and preservePaths is enabled or the application accepts user‑supplied tar archives.
If the application does not accept tar uploads, the risk is lower but still present if an internal user can supply a malicious archive; consider disabling preservePaths and applying the patch as a best practice.Bottom line: The flaw is a classic path‑traversal via malformed PAX records. It allows an attacker to overwrite arbitrary files when
preservePaths is enabled. Upgrade to node-tar ≥ 8.0.0, disable preservePaths if possible, and monitor for suspicious extraction activity.[🤖 CVE Professor AI Security Engine]