Prevent path traversal in LocalStore.getFilePath#5464
Open
immanuwell wants to merge 1 commit into
Open
Conversation
getFilePath constructed file paths with filepath.Join but never verified the result stayed within basePath. A name like "../../../etc/passwd" would resolve outside the state directory, making GetReader, GetWriter, CreateExclusive, Delete, and Exists all vulnerable to path traversal. Fix: after filepath.Join (which calls filepath.Clean to resolve ".." components), check that the result starts with basePath+separator using the same containment pattern already established in pkg/fileutils/contained.go. Update the #nosec G304 comments to reflect that the check is now actually enforced. Closes stacklok#4736
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5464 +/- ##
==========================================
- Coverage 69.17% 69.14% -0.03%
==========================================
Files 634 634
Lines 64473 64486 +13
==========================================
- Hits 44596 44592 -4
- Misses 16577 16591 +14
- Partials 3300 3303 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
LocalStore.getFilePathusedfilepath.Jointo build file paths but never verified the result stayed withinbasePath. A name like../../../etc/passwdwould escape the state directory, makingGetReader,GetWriter,CreateExclusive,Delete, andExistsall vulnerable to path traversalfilepath.Join(which callsfilepath.Cleanto resolve..components) using thestrings.HasPrefix(path, basePath+separator)pattern already established inpkg/fileutils/contained.gogetFilePathto return(string, error)so callers can propagate the error#nosec G304comments to accurately reflect that containment is now actually enforcedlocal_test.gowith tests covering path traversal rejection across all five affected methods, normal operations, and a check that written files stay insidebasePathFixes #4736
Type of change
Test plan
task test)task lint-fix)New test
TestLocalStore_PathTraversalPreventedexercises five traversal patterns (../escape,../../etc/passwd,../../../root/.ssh/authorized_keys,./../escape,subdir/../../escape) against all five public methods and asserts each returns a "path traversal detected" error.