Safe extraction of .tar.zst archives into a directory, with whatever is available: the zstd PHP extension, a
small bundled native binary, a zstd executable, or pure PHP.
Which method extracts an archive must never change the result. Every method produces the same files, or
rejects the same archives, as decompressing with the reference zstd implementation (libzstd / the zstd CLI)
and then applying this library's tar rules.
-
Decompression follows libzstd. Valid input gives identical output. Input libzstd rejects is rejected too: bad checksums, truncation, trailing data, unknown frames, reserved bits, required dictionaries, and windows over 128MB (zstd's default
--memorylimit, so--long=27at most). The bundled binary and ext-zstd use libzstd itself. The PHP decoder mirrors its checks, and ConformanceTest runs it against the zstd CLI on its fixtures and 150 corrupted variants of them. -
Tar extraction has one set of rules, implemented twice: in Rust for the bundled binary, in PHP for the other methods. Every bad-actor archive in BadActorTest goes through every method, and all must reject it the same way.
-
Gaps: three cases can differ from libzstd, none of which zstd itself produces:
- In frames without a checksum, corrupted data that libzstd doesn't detect decodes to garbage. Which garbage depends on libzstd's internal decoding path (fast and slow paths differ), so it can't be reproduced exactly. Where that is known to happen (Huffman streams not consumed exactly, which libzstd's fast path lets through), the PHP decoder rejects the input instead of returning different content.
- The pure PHP decoder can only verify checksums on PHP 8.1+, where ext-hash has XXH64. zstd adds checksums by default.
- A frame declaring a content size of up to 128KB but a window over 128MB. libzstd's streaming decoder (bundled binary, zstd CLI) only skips the window limit when it gets the whole frame in one read, so whether it is rejected depends on where it sits in the file. ext-zstd and the PHP decoder always accept it.
Archives should be authenticated (signed) before extraction anyway.
Any other difference is a bug.
composer require composer/zstdRequires PHP 7.2.5+. The pure PHP decoder needs a 64-bit PHP build.
use Composer\Zstd\Extractor;
use Composer\Zstd\ExtractionException;
$extractor = new Extractor();
try {
// the destination must not exist or be an empty directory
$extractor->extract('/path/to/package.tar.zst', '/path/to/destination');
} catch (ExtractionException $e) {
// invalid or unsafe archive, or I/O error; the destination was cleaned up
}
$extractor->getLastMethod(); // which method was used
$extractor->getUnavailableReasons(); // why others were skipped
// e.g. for a diagnose command: warn when only the slow pure PHP fallback is left
if ($extractor->getAvailableMethods()[0] === Extractor::METHOD_PHP) {
// suggest installing zstd or ext-zstd
}Constructor arguments, all optional:
$cacheDir: Composer's cache directory. When the bundled binary cannot run in place (from inside a phar), it is copied to thezstdsubdirectory. Defaults to the directory Composer itself uses (COMPOSER_CACHE_DIR,COMPOSER_HOME/cache,%LOCALAPPDATA%/Composer,~/Library/Caches/composer,$XDG_CACHE_HOME/composer…, resolved likeComposer\Factory::getCacheDir()).$methods: methods to try, in order. The first available one is used.$bundledPath: anotherzstd-extractbinary to use instead of the bundled one.$zstdCliPath: azstdexecutable to use instead of looking one up inPATH.$maxSize: the most an archive may decompress to, in bytes (the size of the tar stream). Defaults to 1024MB (Extractor::DEFAULT_MAX_SIZE). Larger archives are rejected, by every method.$binaryDownloader:function (string $name, string $sha256): string, returning the contents of the binary for this platform when it is not shipped, e.g. because a phar leaves the binaries out to save space. Its result must matchbin/SHA256SUMSand is kept in the cache directory like a copied binary (see below).
Extractor::validateFile($archive) checks that a file is a complete zstd stream without decompressing it, e.g. to
detect a truncated download. It throws an ExtractionException otherwise.
Composer\Zstd\ZstdDecoder::decompress(string $data): string can also be used on its own.
Default order (getMethodOrder()):
| method | used when | how |
|---|---|---|
bundled |
proc_open works and a bundled binary fits the platform |
streaming decompression (libzstd) and extraction (Rust), in a separate process |
ext-zstd |
the zstd extension is loaded | libzstd in-process, tar extraction in PHP |
zstd-cli |
a zstd 1.4+ executable is in PATH |
zstd -d in a separate process, tar extraction in PHP |
php |
always (64-bit PHP) | pure PHP decoder, tar extraction in PHP |
prepare() picks the first available one. The bundled binary is only picked once it is known to work. From a
phar this means it was copied to the cache directory safely (see below) and ran. Otherwise the next method is
used.
A method that runs and rejects the archive ends the extraction, and the next methods are not tried.
getAvailableMethods() lists what works in the current environment, in order. For the bundled binary this
includes copying it if needed and running it once. When only php is left, extraction is several times slower.
259 popular packages (630MB unpacked, about 59k files), archive to unpacked directory, arm64 Linux, 12 cores:
| method | one at a time | 10 in parallel |
|---|---|---|
bundled |
4.1-5.1s | 1.3s |
ext-zstd (0.15.2, libzstd 1.5.5) |
5.4-7.3s | n/a, in-process |
zstd-cli |
6.3-7.5s | 4.7s |
php |
12.1s (8.0s with opcache JIT) | n/a, in-process |
for comparison: unzip of the zip dists |
10.8s | 2.7s |
Writing the files from PHP takes about 4.5s of these numbers, and it cannot run in parallel. So only the bundled binary, which writes the files in its own process, scales with parallelism. ext-zstd itself decompresses everything in about 1.1s.
prepare() returns an Extraction. For process based methods it exposes the command, so you can run several
extractions in parallel with your own process runner and pass each result back to complete(). With Composer's
ProcessExecutor, for example:
$extraction = $extractor->prepare($archive, $destination);
$command = $extraction->getCommand();
if ($command === null) {
$extraction->run(); // in-process method
return \React\Promise\resolve(null);
}
return $process->executeAsync($command)->then(static function (Process $result) use ($extraction): void {
// throws an ExtractionException if the archive was rejected; if the command could not run at all,
// falls back to the next available method
$extraction->complete($result->getExitCode(), $result->getOutput(), $result->getErrorOutput());
});Commands from getCommand() refer to the bundled binary by path. When extract()/run() start it themselves on
Linux, they use the open file handle instead (see below), which a generic process runner cannot pass on.
ext-zstd only receives archives whose framing was validated in PHP first. zstd_uncompress() handles a single
frame, and depending on the extension version it silently ignores trailing data or returns partial output for
truncated input.
The zstd CLI gets the archive as is. Depending on how it was built, it also decompresses gzip, xz, lz4 and
legacy (pre-1.0) zstd streams, which the other methods reject. Only the zstd format is covered by the core
principle.
Neither can limit its output on its own:
zstd_uncompress()only runs once the frame headers and block counts show that the output fits in$maxSize. When they don't (no declared size, and enough blocks to possibly go over), the pure PHP decoder measures the actual size first.zstdis stopped once its output goes over$maxSize. A command fromgetCommand()has no such limit, the size is only checked bycomplete().
- It is not faster.
tar --zstd -xftook 6.4-7.9s one at a time on the same benchmark, since it runs zstd as a second process. - Its behavior varies between GNU tar, bsdtar and busybox: symlinks,
..and absolute paths are handled differently, as are what counts as an error and how far extraction gets before one. That would break the core principle.
bin/ contains one self-contained zstd-extract executable per platform:
| file | runs on |
|---|---|
zstd-extract-linux-x86_64 |
Linux x86_64, any libc |
zstd-extract-linux-aarch64 |
Linux arm64, any libc, 4K to 64K pages |
zstd-extract-darwin |
macOS 11+, universal x86_64 + arm64 |
zstd-extract-windows-x86_64.exe |
Windows x64, and Windows 11 arm64 through emulation |
They are built from native/ on GitHub Actions: a small Rust program, which has no dependencies besides its standard
library, plus the vendored zstd decoder, compiled from C. Only the FFI calls into zstd and libc (RELRO setup, closing
files to catch write errors) use unsafe; the archive parsing and every filesystem operation are safe Rust. On Linux,
they link statically against musl (static-pie, RELRO, stack protector and _FORTIFY_SOURCE for the C part), inside a
toolchain container pinned by digest (native/build-linux.sh). macOS links the system library dynamically as macOS has
no static libc. Windows uses MSVC with a static C runtime and ASLR; /GS and Control Flow Guard cover the C part, CFG
also this crate's Rust code, but not Rust's prebuilt standard library. Where no binary runs, e.g. Windows 10 on arm64 or
other architectures, the next method is used. musl is used instead of glibc because it is MIT licensed, where a
statically linked glibc would bring LGPL obligations. It is also as fast, and the binary half the size. The builds are
reproducible: every build runs twice and fails unless both results are identical. Two checks build on that:
- The binaries workflow rebuilds them for every change to
native/orbin/, and fails unless the committedbin/is byte-identical to the fresh build. - The release workflow rebuilds them again from the release tag. If the result is
identical to
bin/, it signs a build provenance attestation from that run, covering the build steps, and attaches the files to the release.
As the files are identical, that attestation covers the committed copies as well:
gh attestation verify bin/zstd-extract-linux-x86_64 --repo composer/zstdEach binary comes with a CycloneDX SBOM, bin/<binary>.cdx.json, listing what is linked into it: the zstd
decoder, the Rust standard library with the crates it is built from (found in the binary, versions from the
toolchain's lockfile), on Linux musl and libunwind with their exact Alpine package versions, and on Windows the
Microsoft C runtime.
Dependency scanners cannot see these otherwise, as static binaries carry no package metadata. The SBOMs are
generated by the build, compared like the binaries, and attested by the release workflow:
gh attestation verify bin/zstd-extract-linux-x86_64 --repo composer/zstd --predicate-type https://cyclonedx.org/bomzstd-extract --version prints the zstd and Rust versions as well, and on Linux the musl version.
The Linux toolchain is the ghcr.io/composer/zstd-builder image, built from native/Dockerfile and pinned by
digest, so any release's Linux binaries can be rebuilt identically. The macOS and Windows toolchains come from the
runner images, so those builds are only reproducible until GitHub updates them. Older releases cannot be rebuilt
identically for these two later on, but their attestations stay valid.
When this package is installed normally, the binary in vendor/composer/zstd/bin/ runs in place. Anyone able to
replace it could equally modify the PHP code next to it.
From inside a phar the binary cannot be executed directly, so it is copied out first. A downloaded binary takes the
same path, its hash checked against bin/SHA256SUMS, which then has to be shipped:
- The copy goes to
<cacheDir>/zstd/, which must be owned by the current user, and is made private (0700) if group or others can write to it. Every ancestor directory must be owned by the user or root, and be sticky if it is writable by others (like/tmp). Otherwise the method is skipped. These ownership checks don't apply on Windows, where the default cache directory under%LOCALAPPDATA%is private to the user. - The file is named after its SHA-256, written to a random temporary name and renamed into place.
- It is then opened and its hash (and owner, except on Windows) are verified through that open handle.
- On Linux,
extract()andrun()execute the binary from that same handle (/proc/self/fd/N), so even replacing the file afterwards has no effect. Otherwise, including commands fromgetCommand(), it runs by path from the private directory.
So only the current user (or root) could swap the file between the check and execution, and they can already do anything that process can.
- The destination must not exist or be an empty directory. On failure it is removed (or emptied if it existed).
- Supported entries: regular files, directories and symlinks, with ustar name prefixes, GNU long names and link
names, and pax
path/linkpath. Hardlinks, devices, FIFOs, GNU sparse entries (typeS), paxsizeoverrides and base-256 encoded sizes (which tar uses from 8GB) are rejected. pax sparse files are not recognized, they extract as their stored data like with tar implementations that don't support them. - Every header checksum is verified.
- The decompressed tar stream may not exceed
$maxSize(1024MB by default). The bundled binary stops writing at that point. The other methods hold the decompressed data in memory (ext-zstd and the PHP decoder the compressed archive too), so PHP'smemory_limitcan end the extraction earlier. - Entry names may not be absolute or contain
..components, backslashes or control characters..and empty components are dropped. Names must be shorter than 4096 bytes and have at most 128 components. On Windows,<>:"|?*, trailing dots or spaces and reserved device names (CON,NUL,COM1…) are also rejected. - Files are created exclusively (
O_EXCL, which never follows a symlink). When a name appears twice the last entry wins, but only a regular file is ever replaced. Directories are only traversed when they are real directories, never through symlinks. - Symlink targets must be relative and contain no backslashes or control characters. Symlinks are created after all files and directories, so nothing is ever written through one. Once all of them exist, each one is resolved on the real filesystem, following other links like the OS would. If any step leaves the destination, or there are more than 40 hops, all symlinks are removed and extraction fails. The same happens when verifying all symlinks takes more than a million path components, which no real archive comes close to. Targets that don't exist yet are resolved lexically.
- File modes are
0666or0777(if any execute bit is set) minus the umask. Owners and timestamps are not restored.
On Windows, symlinks are written as plain files containing the target path, by every method. git does the same
with core.symlinks=false, and so does Composer today when it extracts zip archives with ZipArchive on Windows,
so this keeps the current behavior.
Real alternatives exist, but each has a catch:
- Symbolic links need Developer Mode or admin rights, so the result would depend on the machine.
- Directory junctions need no privileges, but they store absolute paths and only work for directories. Composer extracts into a temporary directory and then moves it, which would leave junctions pointing at the old location.
native/build-linux.sh # needs Docker; build-darwin.sh on macOS, build-windows.cmd on Windows
ZSTD_EXTRACT_BINARY=$PWD/native/build/zstd-extract-linux-$(uname -m) vendor/bin/phpunit
# or a quick local build with the toolchain from native/rust-toolchain.toml:
cargo build --release --manifest-path native/Cargo.toml
ZSTD_EXTRACT_BINARY=$PWD/native/target/release/zstd-extract vendor/bin/phpunit
vendor/bin/phpstan analyseTests run against every method available locally:
| variable | effect |
|---|---|
ZSTD_EXTRACT_BINARY |
binary for bundled, defaults to bin/ |
ZSTD_CLI |
zstd for zstd-cli and ConformanceTest, defaults to PATH |
ZSTD_TEST_REQUIRE_METHODS |
comma-separated methods that must be available; CI sets it so nothing is silently skipped |
- Change
native/and push. The binaries workflow fails, sincebin/no longer matches the build. - Take the binaries from that run, as the failure message explains:
gh run download <run-id> --repo composer/zstd --name zstd-extract-binaries --dir bin/ - Commit them with their SBOMs and
bin/SHA256SUMS, the binaries as executable:git add bin/ && git update-index --chmod=+x $(git ls-files 'bin/zstd-extract-*' | grep -v json). The next run must then find them identical.
The same applies when GitHub updates a macOS or Windows runner image and the toolchain output changes: CI shows it, and the binaries are refreshed the same way.
The Linux toolchain only changes on purpose. To update it (new gcc or musl packages, or a new Rust or Alpine
release in native/Dockerfile), run the builder workflow, put the image digest it
prints into native/build-linux.sh, and refresh the binaries as above. The Rust version is also pinned in
native/rust-toolchain.toml for macOS and Windows, keep both the same.
Publish the GitHub release soon after the last CI run on the tagged commit, before a macOS or Windows runner image
update can change the toolchains. The release workflow rebuilds, compares, attests, and uploads the binaries, their
SBOMs, SHA256SUMS and license files. If the rebuild differs from bin/, it fails before attesting or uploading anything. Refresh the
binaries as above and tag again.
gh release download v1.0.0 --repo composer/zstd --pattern 'zstd-extract-*'
gh attestation verify zstd-extract-linux-x86_64 --repo composer/zstdTo update zstd itself, run native/update-zstd.sh <version> <tarball sha256>.
composer/zstd is licensed under the MIT License, see the LICENSE file for details.
The bundled binaries also contain:
- zstd (decoder only), used under its BSD license (zstd is dual-licensed BSD/GPLv2):
bin/LICENSE.zstd - the Rust standard library, used under its MIT license (it is dual-licensed MIT/Apache-2.0), including Unicode
data tables under the Unicode license:
bin/LICENSE.rust. The crates it is built from, listed in the SBOMs, are all available under MIT as well. Its compiler-builtins part contains code under Apache-2.0 with the LLVM exception, which waives the notice requirements for binaries - on Linux, musl libc, MIT licensed:
bin/LICENSE.musl, and LLVM's libunwind, whose LLVM exception waives the notice requirements for binaries - on Windows, the statically linked Microsoft C runtime, which Microsoft's Visual C++ license allows to be redistributed as part of an application, without notice requirements
These notices ship with the package in bin/, and are attached to each GitHub release next to the binaries.
When redistributing the binaries, e.g. inside a phar, keep LICENSE, bin/LICENSE.zstd, bin/LICENSE.rust and
bin/LICENSE.musl with them.