Skip to content

Add File.extract archive API - #13354

Closed
dagger-codex[bot] wants to merge 1 commit into
mainfrom
codex/file-extract-api
Closed

dagger-codex[bot] wants to merge 1 commit into
mainfrom
codex/file-extract-api

Conversation

@dagger-codex

@dagger-codex dagger-codex Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Mark (<@529485027071754273>, Discord user ID 529485027071754273).

Fixes #13353.

Adds File.extract(stripComponents: Int = 0): Directory! for extracting archive File objects into first-class Dagger directories.

Implemented behavior:

  • auto-detects zip by magic bytes, otherwise treats the input as tar with plain, gzip, or xz compression detection
  • supports .tar, .tar.gz/.tgz, .tar.xz, and .zip content
  • applies stripComponents uniformly to tar and zip entries, skipping entries with too few components
  • rejects absolute paths, NUL paths, root-escaping .. paths, and root-escaping symlink targets
  • preserves regular file modes/mtimes and symlinks where supported by the archive format

Generated the GraphQL reference, Go, TypeScript, Python, Rust, PHP, and Elixir SDK artifacts.

Tests:

  • go test ./core ./core/schema
  • dagger call engine-dev generate export --path .
  • dagger call go-sdk generate export --path .
  • dagger call docs references export --path .
  • dagger call typescript-sdk client-library export --path .
  • dagger call python-sdk client-library export --path .
  • dagger call rust-sdk apiclient export --path .
  • dagger call php-sdk api export --path .
  • dagger call elixir-sdk client-library export --path .

@dagger-codex
dagger-codex Bot requested review from a team as code owners June 7, 2026 21:45
@dagger-codex
dagger-codex Bot force-pushed the codex/file-extract-api branch from b88138a to 1c47f6b Compare June 8, 2026 11:46
Add a File.extract API that turns supported archive files into directories. Support tar, tar.gz, tar.xz, and zip archives with path-safety checks and stripComponents handling.

Regenerate client and reference artifacts for the new field.

Signed-off-by: Solomon Hykes <[email protected]>
@dagger-codex
dagger-codex Bot force-pushed the codex/file-extract-api branch from 1c47f6b to fa494c2 Compare June 8, 2026 12:03
@TomChv

TomChv commented Jun 10, 2026

Copy link
Copy Markdown
Member

Nice work — the feature is functional, the lazy/persisted wiring matches the existing DirectoryChownLazy pattern, SDK codegen is regenerated everywhere, and the path-traversal tests are solid. My main concern is a design one: most of this 592-line file reimplements extraction logic (including the security-sensitive parts) that we already have hardened in the repo.

🔴 Main design issue: we already have tar extraction

core/archive_extract.go hand-rolls tar/gzip/xz extraction plus the path-traversal and symlink-escape protection. We already do exactly this in core/directory.go:2419 (unpackArchiveFile) using chrootarchive.Untar + compression.DecompressStream from github.com/moby/go-archive — both already direct dependencies (core/directory.go:37-39).

chrootarchive.Untar extracts inside a chroot, which is the canonical defense against symlink-escape/TOCTOU during extraction. Reimplementing that by hand (strippedArchivePath, validateSymlinkTarget, per-entry RootPathWithoutFinalSymlink) is a security surface I'd rather not maintain ourselves.

The genuinely new requirements are narrow:

  • ZIP — go-archive doesn't support it, so keep the hand-rolled zip branch.
  • stripComponents — Untar doesn't strip, but this can be a thin pre-processing pass on top.
  • pure-Go xz — see dependency note below.

Suggestion: reuse compression.DecompressStream + chrootarchive.Untar for the tar path; keep custom code only for the zip branch and strip-components. That deletes most of the file and the entire bespoke security surface.

🟠 New dependency github.com/ulikunitz/xz

Brand new module (not in main). compression.DecompressStream already handles xz, but by shelling out to the xz binary. So a pure-Go lib is defensible if the engine image doesn't ship xz — but that tradeoff should be stated explicitly in the PR description, since ulikunitz/xz is a low-activity single-maintainer module being added to the engine's dependency surface.

🟠 Format coverage gap

Because it doesn't reuse DecompressStream, openTarArchive (core/archive_extract.go:187) only sniffs gzip and xz. It silently misses zstd (.tar.zst, very common today) and bzip2 (.tar.bz2) — both free via DecompressStream. As written, a .tar.zst is passed raw to tar.NewReader and fails.

🟠 Behavior differences vs. our existing extraction (please confirm these are intentional)

  • Ownership dropped — uid/gid from the archive aren't applied.
  • setuid/setgid/sticky bits stripped via & os.ModePerm (:356, :524).
  • Device/char/fifo nodes → hard error — extractTarStream default: (:270) fails the whole extraction instead of skipping; a rootfs tarball with /dev nodes won't extract.
  • No xattrs (existing path uses BestEffortXattrs).

🟡 Code smells

  • :402 — case mode.IsRegular() || mode.Type() == 0: is X || X (IsRegular() is Type() == 0); second clause is dead.
  • :29 — maxZipEntrySize = uint64(1<<63 - 1) → use math.MaxInt64.
  • Source file is opened 2–3× (hasZipHeader at :145, then openTarArchive/zip.OpenReader); one open + buffered peek would do.
  • stripComponents < 0 is validated in three places (schema/file.go:264, archive_extract.go:71, :540).
  • No overall extraction-size limit (decompression bomb) — same as existing code, so probably out of scope, but worth a conscious note.

🟡 Testing

Tests are unit-only. Our convention for new schema fields is an end-to-end integration test — e.g. File.search is covered in core/integration/file_test.go. Could you add a TestFileExtract there that round-trips through the actual extract resolver + SDK?

TL;DR

  1. Reuse compression.DecompressStream + chrootarchive.Untar for the tar path; keep custom code only for zip + strip-components. This also fixes zstd/bzip2 and likely removes the xz dep.
  2. If keeping the hand-rolled approach, justify the ulikunitz/xz dependency and confirm the ownership/setuid/device-node decisions.
  3. Fix the IsRegular() || Type()==0 redundancy and the triple validation.
  4. Add an integration test.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 7 days with no activity.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add API to extract archive File objects into directories

2 participants