Skip to content

Replace archiver with tar-fs and tar-stream - #1652

Open
cristianrgreco wants to merge 8 commits into
mainfrom
replace-archiver-with-tar-fs
Open

cristianrgreco wants to merge 8 commits into
mainfrom
replace-archiver-with-tar-fs

Conversation

@cristianrgreco

@cristianrgreco cristianrgreco commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Replaces archiver with tar-fs (already a runtime dependency, used for image builds) and tar-stream (already in the tree through tar-fs) for copying files, directories and content into containers.

archiver@7 pulls in archiver-utils, whose glob@10 and minimatch@9 carry advisories that fail npm audit for every consumer (#1641). There is no fix in range: archiver-utils has had no commits since October 2024, and archiver@8 dropped it but is ESM-only, which breaks Jest users (#1365, #1366). tar-fs and tar-stream are CommonJS.

createArchive (generic-container/create-archive.ts) builds one tar stream:

  • files and directories are added with tar-fs.pack, all writing into one shared tar-stream pack (pack, finalize: false)
  • content is added with pack.entry. A Readable is buffered first, as archiver does, since a tar header needs the size up front

tar-fs does the directory walking and header encoding. The helper sets a few options so archives match what archiver produced:

Option Why
umask: 0 tar-fs applies the process umask, archiver does not; without it a 666 file is copied as 644
top-level children as entries tar-fs otherwise adds an entry for the copied directory itself, which changes the target's permissions (copying into /tmp made it 700)
dereference: true for files a source file that is a symlink is copied as the file it points to
uid/gid 0 as archiver wrote them
targets normalized: \ to /, drive letter and leading / removed, .. and // resolved as archiver did. A Windows user building a target with path.join("/app", "x") gets \app\x, which would otherwise be copied as a file literally named \app\x
directories 0755 on Windows as archiver did, because fs.stat on Windows doesn't report usable directory permissions
ignore children for files a directory passed to withCopyFilesToContainer is copied as an empty directory, as archiver did, instead of being walked
content before directories archiver added content synchronously and directory entries asynchronously, so a directory entry won over content with the same target
strict: false FIFOs and devices inside a directory are skipped (tar-fs always skips sockets), which is what archiver intended (in practice it hung on them, see below). Checked in the scenario run rather than a test, since creating a FIFO needs mkfifo

This also removes archiver, @types/archiver, their Dependabot ignore entries, and their subtree (archiver-utils, glob, minimatch, zip-stream, compress-commons, readdir-glob, …) from the runtime dependencies. npm audit --omit=dev goes from 5 moderate findings to 0. require("archiver") took about 60 ms against about 6 ms for tar-fs (#1577).

Earlier attempts were closed over complexity: #1246 moved content to the filesystem, and #1370 owned the tar walking and headers. Here, content stays in memory and tar-fs owns the walking.

Closes #1641

Verification

  • npm run format, npm run lint, npm run knip, npm run check-compiles: pass

  • generic-container.test.ts -t "copy|Copy": 20 passed (13 existing copy and ownership tests, 7 new). The missing-directory test pins an intended behaviour change: it resolves on archiver and rejects with ENOENT here

  • Windows-style targets (path.win32.join("/tmp", "nested", "test.txt"), C:\tmp\drive\test.txt): the new test fails before the normalization (cat exits 1, the files don't exist) and passes with it

  • File and content targets ending in /: the new test fails without the check (promise resolved "undefined" instead of rejecting, and Docker replaced the existing /tmp/dir with the file) and passes with it, leaving the directory in place

  • New tests cover a directory with hidden files, an empty directory, a symlink and a 666 file; the target directory's permissions staying unchanged; and Readable content. Evidence that they pin the existing behaviour:

    • on main's archiver code: the directory, target-permission, Readable and ownership tests pass. The failing-Readable test hangs there, because that is an archiver bug (see the scenario table)
    • with umask: 0 removed: fails, expected '644' to be '666'
    • with the root directory entry added back: fails, expected '755' to be '1777'
  • Comparing archives built by archiver and by createArchive from the same inputs (files, mode overrides, symlinked source, nested and empty directories, dotfiles, symlinks inside a directory including a broken one, 600/666/755/777/setgid modes, string/Buffer/Readable content, relative symlinks): extracted trees identical in content, type, mode, size and link target

  • Ran 20 user scenarios through both archiver (main) and createArchive against a real container and compared the resulting files, types, modes, owners, link targets and checksums:

    Scenario Result
    relative file and directory sources; targets without a leading slash, with a trailing slash, and / same
    unicode and long (>100 char) paths, unicode content, mode 0 same
    directory source that is a symlink; hard links and symlinks inside a directory; empty directory source same
    directory passed as a file; directory and content with the same target before start same
    missing file source same: rejects with ENOENT
    2000 files in 20 directories / 100 files in one call same; 339 ms vs 567 ms / 36 ms vs 44 ms
    FIFO inside a directory archiver hangs forever; now skipped
    unreadable file inside a directory, Readable content that errors archiver hangs forever and raises an unhandled error; now rejects with the error
    missing directory source archiver silently copies nothing; now rejects with ENOENT, as a missing file already did
  • Jest 30 CommonJS runtime against the built package: copying files, directories, string and Readable content works. The build also loads with require and ESM import.

  • Archiving a 500 MB directory locally: 412 ms and 118 MB peak RSS, against 561 ms and 158 MB with archiver. Both stream without holding file contents in memory

  • Publish dry run: https://github.com/testcontainers/testcontainers-node/actions/runs/43711875356

On Windows, libuv's fs.stat reports directories as 0666, or 0444 when read-only, with no execute bit (src/win/fs.c). tar-fs only checks the platform when extracting, so without the Windows line, directories copied from Windows would have no execute bit and couldn't be entered by a non-root user in the container. archiver forced 0755, and Docker's own client also adds execute bits when packing on Windows (chmodTarEntry). The line is checked against those sources but hasn't been run on Windows: CI has no Windows runner with Linux containers.

Not breaking

No public API changes, and the copy methods' types are unchanged. Behaviour matches archiver except:

  • calls where archiver hung, or silently ignored a missing directory, now reject with the underlying error
  • a file or content target ending in / (or empty) rejects with Target "…" must be a file path. archiver hung on it for files and threw for content. Without the check, Docker replaces an existing directory at that path with the file. This also covers a directory passed as a file with such a target, which archiver copied as an empty directory
  • mode on copyDirectoriesToContainer for a started container is now applied, as it already was for withCopyDirectoriesToContainer. archiver was never passed it there
  • absolute symlinks inside a copied directory are stored as they are, as docker cp does. archiver rewrote them relative to the host directory, so a link to an absolute path inside the copied directory used to resolve in the container and now doesn't, while a link to a system path such as /etc/hosts no longer depends on how deep the host directory was. Relative symlinks, the usual case, are unchanged

Runtime dependencies stay CommonJS. tar-stream becomes a direct dependency of testcontainers at the same range @testcontainers/k3s already uses.

archiver 7 pulls in archiver-utils, whose glob and minimatch carry advisories, and archiver 8 is ESM-only. Build copy archives with tar-fs, already a runtime dependency, and tar-stream instead.
@cristianrgreco cristianrgreco added dependencies Pull requests that update a dependency file patch Backward compatible bug fix labels Nov 15, 2026
…rs-node into replace-archiver-with-tar-fs

# Conflicts:
#	package-lock.json
…nd special files

Copy a directory passed as a file as an empty directory, add content before directories so a directory entry wins over content with the same target, and skip FIFOs and sockets instead of failing.
…rs-node into replace-archiver-with-tar-fs

# Conflicts:
#	package-lock.json
#	packages/testcontainers/package.json
@cristianrgreco
cristianrgreco marked this pull request as ready for review November 19, 2026 13:42
zigordev added a commit to zigordev/toolkit that referenced this pull request Nov 23, 2026
)

## What

Step 4.1 of the build plan: the new package `@zigordev/test`, private
for now.

- `@zigordev/test/postgres`: one PostgreSQL 18 container per test run,
started through Testcontainers. A global setup migrates a template
database once, and `useTestDatabase()` gives each test file its own
copy, dropped after the file.
- `@zigordev/test/app`: the harness for API tests. `useApp()` builds an
app once per file and closes it after. `captureLogs()` keeps the log
lines and finds events. `expectProblem()` checks an error answer: the
status, `application/problem+json`, the code, and exactly the fields of
the platform's error format.
- Tests that need a container live in `*.db.test.ts`, in a Vitest
project named `<name>:db` from a `vitest.db.config.ts`. `pnpm check`
runs them. `pnpm check:quick`, which the push hook runs, now leaves them
out with `--project '!*:db'`.

## Why

The test rules ask for API tests on a real PostgreSQL, and for every
test file to get its own copy of the migrated database. Without one
shared package, each service would write its own container start, copies
and clean-up. Steps 4.2 and 4.3 build on it.

## The override, and the owner's exception

Testcontainers 12.2.2, the newest release, brings `glob` 10.5.0 through
`archiver` 7 and `archiver-utils`, with the flaw
[GHSA-3g3m-2fjr-6pw4](GHSA-3g3m-2fjr-6pw4),
medium, published 2026-10-30. Record 0033 was followed in its order:

1. **Update the package that brings it in.** Not possible yet. The
maintainer's own fix, testcontainers/testcontainers-node#1652, replaces
`archiver`. It was still open on 2026-11-23.
2. **Do without it.** Not possible. Testcontainers loads `archiver` when
it starts: an override that removed it failed every test with
`MODULE_NOT_FOUND`. `archiver` 8 drops `archiver-utils`, but brings
`minimatch` 10.2.6, which has its own flaw, GHSA-g3hv-75wg-m249.
3. **Replace the flawed package.** `archiver-utils>glob: 14.0.6` under
`overrides`. `pnpm audit` then finds nothing, and every test passes.

**This is an exception to the one-month rule.** `glob` 14 is a new major
version, first released on 2026-10-27, so the override would have waited
until 2026-11-27. On 2026-11-23 the owner was asked "Let toolkit#64 wait
until 2026-11-27, keeping the one-month rule?" and answered, in their
words: "Merge it now".

Why the risk is low: Testcontainers uses `archiver`, and through it
`glob`, only to copy files into a container, which nothing here does.
Every test of the package passes with `glob` 14, on this Mac and in CI.

When the line goes: when a release of Testcontainers drops `archiver`.
From 2026-11-27 it is no longer an exception. The README of
`@zigordev/test` and the new table "The overrides in force" on the
handbook page Dependency updates both say so.

## How it was tested

- `pnpm check` passes: 665 tests, 99.3% of lines covered, in 29 seconds
on this Mac with Docker Desktop.
- The database tests ask the server for its version, and pass only on
PostgreSQL 18. They prove that copies never see each other's rows, that
five copies can be made at once, that a copy drops while a client still
holds it, that the template refuses clients, that a client left open by
`prepare` does not block a copy, and that a failing `prepare` stops its
container.
- `vitest list --project '!*:db'` lists 536 tests and none from a
`*.db.test.ts` file.
- The package was packed and installed into an empty folder outside the
repo, with the README's settings. Its example ran there: a copy of the
template, and `expectProblem` on an answer. That trial found that
Testcontainers also needs `protobufjs: false` under `allowBuilds`, and
the README now names all three.

## Observability

Nothing runs in production. A test run starts one PostgreSQL container
and Testcontainers' clean-up container, and both are removed when the
run ends.

## Docs

- `packages/test/README.md`, with the five headings.
- A new handbook page, `docs/handbook/tests.md`, listed as page 14 of
the reading order.
- `CLAUDE.md`: the new folder, and what `check:quick` leaves out.
- `docs/handbook/dependency-updates.md`: a table of the overrides in
force, and when each goes.

## Follow-ups

- Remove the override once Testcontainers drops `archiver`.
- A repo that installs `@zigordev/test` needs three lines under
`allowBuilds` and, until then, the override line. The README says so.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file patch Backward compatible bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

archiver@7 pulls in a glob/minimatch with known advisories (npm audit fails)

1 participant