Fix path traversal vulnerability in cache.go#1795
Open
antitree wants to merge 1 commit intochainguard-dev:mainfrom
Open
Fix path traversal vulnerability in cache.go#1795antitree wants to merge 1 commit intochainguard-dev:mainfrom
antitree wants to merge 1 commit intochainguard-dev:mainfrom
Conversation
Replace unsafe filepath.Clean + strings.HasPrefix validation with Go 1.24's os.Root for secure path resolution. This prevents path traversal attacks including symlink-based attacks. The previous validation could be bypassed with paths like "/cache-evil/file" which would pass the prefix check for root "/cache". 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Author
|
Aims to address https://github.com/chainguard-dev/internal-dev/issues/1593 |
tstromberg
suggested changes
Aug 5, 2025
tstromberg
left a comment
There was a problem hiding this comment.
To prevent a future regression, this should get a test.
| } | ||
|
|
||
| // Return the absolute path for use by other functions | ||
| return filepath.Join(root, relativePath), nil |
There was a problem hiding this comment.
"root" isn't necessarily an absolute path; if this comment is to be accurate, you should use filepath.Abs.
| return "", fmt.Errorf("cache file %s is not within root %s", cacheFile, cleanroot) | ||
| relativePath := filepath.Join(repoDir, dir, filename) | ||
|
|
||
| // Use os.Root for secure path validation to prevent path traversal |
There was a problem hiding this comment.
Look out for other calls to filepath.Join here to see whether or not they are similarly vulnerable - https://github.com/chainguard-dev/apko/pull/1795/files#diff-085f287e1519fcab77329f1fededeae7b771c1e18f0522e4c4f1fdbe3c202d3bR376 looks suspicious.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pkg/apk/apk/cache.gofilepath.Clean+strings.HasPrefixvalidation with Go 1.24'sos.RootSecurity Issue
The previous validation in
cachePathFromURL()could be bypassed:Attack example: With root="/cache", a malicious path like "/cache-evil/file" would pass the prefix check.
Solution
Implemented Go 1.24's
os.Rootwhich provides:Test Plan
🤖 Generated with Claude Code