Skip to content

fix: prevent path traversal (ZipSlip) in Untar and Unzip - #8213

Merged
rfay merged 1 commit into
ddev:mainfrom
rfay:20260310_tar_traversal
Mar 23, 2026
Merged

rfay merged 1 commit into
ddev:mainfrom
rfay:20260310_tar_traversal

Conversation

@rfay

@rfay rfay commented Mar 10, 2026

Copy link
Copy Markdown
Member

The Issue

filepath.Join(dest, file.Name) resolves ../ sequences, allowing a crafted archive entry to write files outside the intended destination directory. Symlink targets in tar archives had the same issue.

The affected functions are used in ddev import-db, ddev import-files, and all CMS-specific file import handlers — a realistic attack vector when a developer receives a database dump or file archive from an untrusted or compromised source.

How This PR Solves The Issue

Added path containment checks to both Untar() and Unzip() in pkg/archive/archive.go:

  1. After computing fullPath, verify filepath.Clean(fullPath) is prefixed by filepath.Clean(dest)+/. Any entry that escapes the destination returns an error immediately.
  2. For symlinks in tar archives, resolve the link target (relative targets resolved against the symlink parent directory; absolute targets used as-is) and apply the same containment check.

Manual Testing Instructions

Run the new tests:

go test -v -run 'TestUntarPathTraversal|TestUnzipPathTraversal' ./pkg/archive/

Automated Testing Overview

  • TestUntarPathTraversal: three subtests covering ../ in file path, ../ in symlink target, and absolute symlink target outside dest
  • TestUnzipPathTraversal: verifies zip traversal is blocked
  • All existing archive tests continue to pass

Release/Deployment Notes

Any archive containing entries that escape the destination directory will now return an error instead of writing outside it. Legitimate archives are unaffected.

@github-actions

github-actions Bot commented Mar 10, 2026 •

Copy link
Copy Markdown

…x2xq-qhjf-5mvg

## The Issue

- Fixes GHSA-x2xq-qhjf-5mvg

`filepath.Join(dest, file.Name)` resolves `../` sequences, allowing a
crafted archive entry to write files outside the intended destination
directory. Symlink targets in tar archives had the same issue.

The affected functions are used in `ddev import-db`, `ddev import-files`,
and all CMS-specific file import handlers — a realistic attack vector when
a developer receives a database dump or file archive from an untrusted or
compromised source.

## How This PR Solves The Issue

Added path containment checks to both `Untar()` and `Unzip()` in
`pkg/archive/archive.go`:

1. After computing `fullPath`, verify `filepath.Clean(fullPath)` is
   prefixed by `filepath.Clean(dest)+/`. Any entry that escapes the
   destination returns an error immediately.
2. For symlinks in tar archives, resolve the link target (relative targets
   resolved against the symlink's parent directory; absolute targets used
   as-is) and apply the same containment check.

## Manual Testing Instructions

Run the new tests:

```
go test -v -run "TestUntarPathTraversal|TestUnzipPathTraversal" ./pkg/archive/
```

## Automated Testing Overview

- `TestUntarPathTraversal`: three subtests covering `../` in file path,
  `../` in symlink target, and absolute symlink target outside dest
- `TestUnzipPathTraversal`: verifies zip traversal is blocked
- All existing archive tests continue to pass

## Release/Deployment Notes

Any archive containing entries that escape the destination directory will
now return an error instead of writing outside it. Legitimate archives are
unaffected.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@rfay
rfay force-pushed the 20260310_tar_traversal branch from c1461e4 to 257cdba Compare March 10, 2026 17:04
@rfay
rfay marked this pull request as ready for review March 11, 2026 19:05
@rfay
rfay requested a review from a team as a code owner March 11, 2026 19:05
@rfay

rfay commented Mar 11, 2026

Copy link
Copy Markdown
Member Author

I think this is probably OK. I'm running TestDdevFullSiteSetup manually, as all the tarballs in that could have been broken.

@rfay
rfay requested a review from stasadev March 11, 2026 19:06

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@rfay
rfay merged commit 05cbe29 into ddev:main Mar 23, 2026
35 checks passed
@rfay
rfay deleted the 20260310_tar_traversal branch March 23, 2026 14:17
rfay added a commit that referenced this pull request Mar 27, 2026
… them

PurgeDirectory and PurgeDirectoryExcept used os.Stat (via util.Chmod)
which follows symlinks. If a symlink's target was already removed earlier
in the loop, the stat would fail with ENOENT.

Use os.Lstat instead and skip chmod for symlinks — symlinks have no
meaningful permissions of their own and os.RemoveAll removes the symlink
entry itself without following it.

This surfaced in TestDdevImportFilesDir for TYPO3: the v13.4.19
files.tgz contains test.txt as a symlink to README.txt. After #8213
added TypeSymlink handling to Untar, symlinks are now extracted to the
host. When README.txt was removed before test.txt in the purge loop,
test.txt became a dangling symlink and the subsequent util.Chmod failed.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
rfay added a commit that referenced this pull request Mar 27, 2026
… them [skip buildkite]

PurgeDirectory and PurgeDirectoryExcept used os.Stat (via util.Chmod)
which follows symlinks. If a symlink's target was already removed earlier
in the loop, the stat would fail with ENOENT.

Use os.Lstat instead and skip chmod for symlinks — symlinks have no
meaningful permissions of their own and os.RemoveAll removes the symlink
entry itself without following it.

This surfaced in TestDdevImportFilesDir for TYPO3: the v13.4.19
files.tgz contains test.txt as a symlink to README.txt. After #8213
added TypeSymlink handling to Untar, symlinks are now extracted to the
host. When README.txt was removed before test.txt in the purge loop,
test.txt became a dangling symlink and the subsequent util.Chmod failed.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
rfay added a commit that referenced this pull request Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants