Skip to content

fix: reject symlinks with rooted/absolute targets in Untar, for #8213 (#8625) [skip ci] - #8625

Merged
rfay merged 7 commits into
mainfrom
20260727_rfay_fix_untar_symlink_escape
Jul 29, 2026
Merged

rfay merged 7 commits into
mainfrom
20260727_rfay_fix_untar_symlink_escape

Conversation

@rfay

@rfay rfay commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

The Issue

Follow-up to CVE-2026-32885 / GHSA-9hq4-hm3j-jmph.

The ZipSlip fix in 05cbe29 (#8213) added a symlink-target escape check to Untar, but for an absolute (rooted) Linkname it validated a copy rebased under dest while creating the symlink on disk with the raw, un-rebased target. The value that was validated was not the value that was written: a malicious archive could plant a host-absolute symlink that passed the check, and a follow-up regular-file entry that traversed that symlink could overwrite arbitrary files outside dest (e.g. $HOME rc-files), reachable via ddev add-on get <repo|url> and ddev import-db/import-files.

Attempting to fix this by rebasing absolute targets under dest (rather than rejecting them) turned out to be its own recurring source of bugs: a Windows CI run surfaced a second escape, because filepath.IsAbs is host-OS-dependent and doesn't recognize a POSIX-style rooted path (/var/www/...) as absolute on Windows, so that rebasing was silently skipped there too.

How This PR Solves The Issue

Rather than continue trying to safely rebase absolute symlink targets under dest, Untar now rejects any rooted/absolute Linkname outright — a dev-environment archive has no legitimate need to plant one. isArchiveAbsolutePath detects a rooted target in any spelling (POSIX /..., bare-rooted \..., or a native drive-lettered/UNC path), regardless of which OS is running, and Untar returns an error for it instead of trying to place it somewhere safe. Only relative symlink targets are extracted, resolved against and confined to the symlink's own parent directory, as before.

Manual Testing Instructions

  1. Build a tar archive containing a tar.TypeSymlink entry with a rooted Linkname (e.g. an absolute path outside the extraction directory), optionally followed by a tar.TypeReg entry whose name traverses that symlink.
  2. Run archive.Untar(tarball, dest, "").
  3. Before this PR: the symlink is created pointing at the raw absolute target, and a follow-up entry traversing it can write
    outside dest. After this PR: Untar returns an error for the rooted symlink entry and nothing is written outside dest.

Automated Testing Overview

In pkg/archive/archive_test.go, TestUntarPathTraversal:

  • absolute_symlink_target_with_traversal / absolute_symlink_target_rejected: any rooted symlink target — including one shaped like a container path (/var/www/html/...) that a prior version of this fix special-cased as allowed — is now rejected outright.
  • absolute_symlink_write_through_escape: regression test reproducing the advisory's PoC (planted rooted symlink + traversing regular-file entry). Verified this fails against the pre-fix code (host file overwritten) and now passes: Untar errors out on the rooted symlink entry before the write-through can happen.

go test ./pkg/archive/... and make staticrequired pass. Cross-compiled and vetted for GOOS=windows (no Windows machine available locally); verification of the actual Windows path handling depends on the ddev-windows-mutagen Buildkite pipeline.

Release/Deployment Notes

Security fix for GHSA-9hq4-hm3j-jmph. Behavior change: tar archives containing a symlink with a rooted/absolute target will now fail to extract with an explicit error, instead of being silently rebased under the destination directory. No code elsewhere in the repository was found to depend on absolute symlink targets surviving extraction.


Moved from a private fork because merging a security advisory ignores the configured merge strategy and commit message:

@rfay
rfay requested a review from a team as a code owner July 28, 2026 15:59
@rfay
rfay requested a review from stasadev July 28, 2026 15:59

@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.

I see the error during import. Although it's err: mkdir file exists rather than archive entry escapes destination directory, but it's fine.


Test with an absolute symlink:

Prepare an archive:

rm -rf $HOME/tmp/outside-vault $HOME/tmp/pack-a $HOME/tmp/pack-b $HOME/tmp/breakout-archive.tar $HOME/tmp/breakout-archive.tar.gz

mkdir -p $HOME/tmp/outside-vault $HOME/tmp/pack-a/data $HOME/tmp/pack-b/breakout
echo SAFE-CONTENT > $HOME/tmp/outside-vault/secret.txt

# ordinary files that should extract normally
echo "hello from archive" > $HOME/tmp/pack-a/data/hello.txt
echo "just some info"     > $HOME/tmp/pack-a/data/info.txt

# the malicious pair
ln -s $HOME/tmp/outside-vault $HOME/tmp/pack-a/breakout
echo TAMPERED-BY-TARBALL > $HOME/tmp/pack-b/breakout/secret.txt

# benign files + symlink first, write-through entry last
tar -cf  $HOME/tmp/breakout-archive.tar -C $HOME/tmp/pack-a data breakout
tar -rf  $HOME/tmp/breakout-archive.tar -C $HOME/tmp/pack-b breakout/secret.txt
gzip -f  $HOME/tmp/breakout-archive.tar

tar -tvf $HOME/tmp/breakout-archive.tar.gz

Test it (before):

$ rm -rf foobar && ddev import-files --source=$HOME/tmp/breakout-archive.tar.gz --target=foobar
Successfully imported files for test-project

$ tree -a foobar
foobar
├── breakout -> /home/stas/tmp/outside-vault
└── data
    ├── hello.txt
    └── info.txt

3 directories, 2 files

Test it (after):

$ rm -rf foobar && ddev import-files --source=$HOME/tmp/breakout-archive.tar.gz --target=foobar
Error: failed to import files for test-project: failed to extract provided archive: symlink target "/home/stas/tmp/outside-vault" in archive entry "breakout" is absolute, which is not allowed

$ tree -a foobar
foobar
└── data
    ├── hello.txt
    └── info.txt

2 directories, 2 files

Test with a relative symlink (confirming that previous fix works as expected):

Prepare an archive:

rm -rf $HOME/tmp/outside-vault $HOME/tmp/rel-pack-a $HOME/tmp/rel-pack-b $HOME/tmp/relative-escape.tar $HOME/tmp/relative-escape.tar.gz

mkdir -p $HOME/tmp/outside-vault $HOME/tmp/rel-pack-a/data $HOME/tmp/rel-pack-b/breakout
echo SAFE-CONTENT > $HOME/tmp/outside-vault/secret.txt

# ordinary file that would extract normally
echo "hello from archive" > $HOME/tmp/rel-pack-a/data/hello.txt

# relative symlink climbing out of the project to an absolute-ish host location
ln -s ../../../../../../../../home/stas/tmp/outside-vault $HOME/tmp/rel-pack-a/breakout
echo TAMPERED-BY-TARBALL > $HOME/tmp/rel-pack-b/breakout/secret.txt

# benign file + relative symlink first, write-through entry last
tar -cf  $HOME/tmp/relative-escape.tar -C $HOME/tmp/rel-pack-a data breakout
tar -rf  $HOME/tmp/relative-escape.tar -C $HOME/tmp/rel-pack-b breakout/secret.txt
gzip -f  $HOME/tmp/relative-escape.tar

tar -tvf $HOME/tmp/relative-escape.tar.gz

Test it (before):

$ rm -rf foobar && ddev import-files --source=$HOME/tmp/relative-escape.tar.gz --target=foobar
Error: failed to import files for test-project: failed to extract provided archive: symlink target "../../../../../../../../home/stas/tmp/outside-vault" in archive entry "breakout" escapes destination directory

$ tree -a foobar
foobar
└── data
    └── hello.txt

2 directories, 1 file

Test it (after):

$ rm -rf foobar && ddev import-files --source=$HOME/tmp/relative-escape.tar.gz --target=foobar
Error: failed to import files for test-project: failed to extract provided archive: symlink target "../../../../../../../../home/stas/tmp/outside-vault" in archive entry "breakout" escapes destination directory

$ tree -a foobar
foobar
└── data
    └── hello.txt

2 directories, 1 file

@github-actions

github-actions Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

rfay added a commit that referenced this pull request Jul 28, 2026
…basing

Tar Linkname strings are POSIX-style regardless of the host OS. On
Windows, filepath.IsAbs("/var/www/html/...") returns false (no drive
letter), so that absolute-looking target skipped rebasing entirely and
was written to disk raw and unconstrained by dest, per the
ddev-windows-mutagen CI failure on
#8625.

Separately, a genuinely native-Windows absolute Linkname (e.g. a
drive-lettered path) would have been naively concatenated onto dest
via filepath.Join, producing an invalid path with an embedded drive
letter/colon.

isArchiveAbsolutePath now recognizes POSIX-style ("/"-prefixed),
bare-rooted ("\"-prefixed), and native (drive-lettered/UNC) absolute
targets uniformly. rebaseUnderDest strips any volume name and leading
slashes before joining onto dest, so absolute targets of either style
are always rebased into a single valid path under dest, on every
platform.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@rfay rfay changed the title fix: rebase absolute symlink targets on-disk in Untar, not just for the escape check fix: reject symlinks with rooted/absolute targets in Untar Jul 28, 2026
@rfay
rfay requested a review from stasadev July 28, 2026 17:19
@rfay

rfay commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

Changed it to reject rooted tarballs, thanks for re-review. Much more satisfying.

@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.

Thank you!

The error is now clear, I tested it again and updated my comment.

@rfay

rfay commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

The all-project-types / test job failed: https://github.com/ddev/ddev/actions/runs/30381950072/job/90411352224

Prepare() failed on TestSite.Prepare() site=testpkgmagento2, err=failed to GetCachedArchive, err=archive extraction of /home/runner/.ddev/testcache/tarballs/157d05f0_testpkgmagento2_code_no_media.magento2.4.6-p4.tgz failed err=symlink target "/var/www/html/lib/web/underscore.js" in archive entry "./pub/static/adminhtml/Magento/backend/en_US/underscore.js" is absolute, which is not allowed

This is a real, deterministic hit, not flakiness. The testpkgmagento2_code_no_media.magento2.4.6-p4.tgz fixture (from github.com/ddev/test-magento2, unchanged for ~2 years) contains two symlinks with absolute targets:

./pub/static/adminhtml/Magento/backend/en_US/underscore.js -> /var/www/html/lib/web/underscore.js
./pub/static/frontend/Magento/luma/en_US/underscore.js     -> /var/www/html/lib/web/underscore.js

These were baked in whenever the fixture was originally tarred up from a live project directory, and until this PR Untar rebased absolute symlink targets rather than rejecting them, so they passed silently. The new outright-rejection behavior in pkg/archive/archive.go now correctly refuses them, so this test is the only one in the matrix that fails, because it's the only cached fixture with an absolute symlink target in it.

I don't think this says anything bad about the fix itself — it's arguably the fix working as intended, just surfacing a 2-year-old quirk in this one test fixture. @rfay is going to regenerate/update the test artifact to drop the absolute symlinks.

rfay and others added 7 commits July 28, 2026 17:00
…he escape check

## The Issue

Follow-up to CVE-2026-32885 (GHSA-9hq4-hm3j-jmph). The ZipSlip fix in
05cbe29 validated absolute symlink targets by rebasing them under
`dest` (`filepath.Join(dest, file.Linkname)`), but then created the
symlink with the raw, un-rebased `file.Linkname` via `os.Symlink`. The
value that was validated was not the value that was written, so the
on-disk symlink could point at an arbitrary absolute host path even
though the check passed. A follow-up regular-file entry traversing
that planted symlink could then overwrite arbitrary files outside
`dest` (e.g. shell rc-files), reachable via `ddev add-on get` and
`ddev import-db`/`import-files`.

## How This PR Solves The Issue

`Untar`'s `tar.TypeSymlink` case now creates the symlink using
`resolvedTarget` (the rebased path) whenever the original target is
absolute, instead of the raw `file.Linkname`. Relative targets are
unchanged, since their resolution already matches what was validated.

## Manual Testing Instructions

1. Build an archive containing a `tar.TypeSymlink` entry with an
   absolute `Linkname` pointing outside the extraction directory,
   followed by a `tar.TypeReg` entry whose name traverses that symlink.
2. Run `archive.Untar(tarball, dest, "")`.
3. Before this fix, the file outside `dest` is overwritten. After this
   fix, the symlink is rebased under `dest`, so the write lands inside
   `dest` instead.

## Automated Testing Overview

- Extended `TestUntarPathTraversal/absolute_symlink_container_path_allowed`
  to assert the created symlink target is actually rebased under
  `dest`, not just that `Untar` returns no error.
- Added `TestUntarPathTraversal/absolute_symlink_write_through_escape`,
  a regression test reproducing the advisory's PoC (planted absolute
  symlink + traversing regular-file entry); fails against the old code
  and passes with this fix.

## Release/Deployment Notes

Security fix; no user-facing behavior change for legitimate archives.
Absolute symlink targets in tar archives (e.g. container paths like
`/var/www/html/...`) are still permitted, but are now consistently
confined under the extraction destination on disk.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…basing

Tar Linkname strings are POSIX-style regardless of the host OS. On
Windows, filepath.IsAbs("/var/www/html/...") returns false (no drive
letter), so that absolute-looking target skipped rebasing entirely and
was written to disk raw and unconstrained by dest, per the
ddev-windows-mutagen CI failure on
#8625.

Separately, a genuinely native-Windows absolute Linkname (e.g. a
drive-lettered path) would have been naively concatenated onto dest
via filepath.Join, producing an invalid path with an embedded drive
letter/colon.

isArchiveAbsolutePath now recognizes POSIX-style ("/"-prefixed),
bare-rooted ("\"-prefixed), and native (drive-lettered/UNC) absolute
targets uniformly. rebaseUnderDest strips any volume name and leading
slashes before joining onto dest, so absolute targets of either style
are always rebased into a single valid path under dest, on every
platform.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…asing

Rebasing absolute symlink targets under dest was the root cause of the
escape bugs in this function (GHSA-9hq4-hm3j-jmph and the Windows
filepath.IsAbs gap it exposed). A dev-environment archive extracted by
Untar has no legitimate need to plant an absolute symlink target, so
rather than continue trying to safely place one under dest, reject any
absolute Linkname outright and only allow relative targets, which are
resolved against and confined to the symlink's parent directory as
before.

This removes isArchiveAbsolutePath's now-unnecessary companion
(pathUnderDest) along with the absolute-target branch entirely,
closing the whole class of bugs rather than patching around it.

Updated TestUntarPathTraversal accordingly: the container-path test
now expects rejection instead of asserting a rebased target, and the
write-through regression test now asserts Untar itself returns an
error for the planted absolute symlink.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
The comment asserted Linkname is always POSIX-style regardless of host
OS, then justified checking for non-POSIX (backslash/drive-letter)
forms anyway. Linkname comes from the archive being extracted and
cannot be trusted to follow that convention, so all three forms need
checking explicitly, regardless of which OS is running. No logic
change.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Magento 2.4.9 requires MariaDB 12.3 (recommended) or 11.8 per Adobe's
system requirements; the magento2ConfigOverrideAction was still
hardcoding the 2.4.7-era MariaDB 10.6, which no longer matches the
supported version or the rebuilt test-magento2 fixture.
…Rs [skip buildkite]

An inline `git commit -m "$(cat <<'EOF' ... EOF)"` heredoc just failed
silently when chained with `&&`, even though this pattern was already
warned against for PR-initial commits. Generalize the rule so it's not
missed on ordinary commits.
@rfay
rfay force-pushed the 20260727_rfay_fix_untar_symlink_escape branch from a3a05d6 to 92ea701 Compare July 28, 2026 23:01
@rfay

rfay commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

I updated the test-magento2 test fixture to current 2.4.9, which also required a minor change to magento.go. I think this is probably OK, but may be reviewed with slight suspicion. The files tarball now is explicitly created without the rooted symlink.

@rfay
rfay requested a review from stasadev July 28, 2026 23:03
@rfay rfay changed the title fix: reject symlinks with rooted/absolute targets in Untar fix: reject symlinks with rooted/absolute targets in Untar, for #8213 Jul 29, 2026
@rfay rfay changed the title fix: reject symlinks with rooted/absolute targets in Untar, for #8213 fix: reject symlinks with rooted/absolute targets in Untar, for #8213 (#8625) [skip ci] Jul 29, 2026
@rfay
rfay merged commit 7034963 into main Jul 29, 2026
60 of 61 checks passed
@rfay
rfay deleted the 20260727_rfay_fix_untar_symlink_escape branch July 29, 2026 21:53
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