fix: Arbitrary file access during archive extraction a filepath.Join Path Traversal #6762
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.
devtron/util/helper.go
Line 186 in 680616b
devtron/util/helper.go
Lines 197 to 198 in 680616b
devtron/util/helper.go
Lines 206 to 213 in 680616b
Extracting files from a malicious zip file, or similar type of archive, is at risk of directory traversal attacks if filenames from the archive are not properly validated. archive paths. zip archives contain archive entries representing each file in the archive. These entries include a file path for the entry, but these file paths are not restricted and may contain unexpected special elements such as the directory traversal element (
..
). If these file paths are used to create a filesystem path, then a file operation may happen in an unexpected location. This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files.fix the "Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')" vulnerability, we must ensure that files extracted from the tar archive cannot escape the intended extraction directory (
chartDir
). The best way to do this is to:filepath.Clean
...
elements that would traverse outside the target directory.filepath.Join(chartDir, cleanedName)
.chartDir
(i.e., it has not escaped via symlinks or traversal).We should perform these checks before any filesystem operation (directory creation, file writing, etc.) that uses the archive entry name.
Required changes:
ExtractTarGz
.path/filepath
,strings
are already imported).Checklist:
Does this PR introduce a user-facing change?
Summary by Bito
This PR fixes a critical security vulnerability by preventing directory traversal attacks in the archive extraction process. It introduces a safeJoin function in util/helper.go to validate file paths and ensure all filesystem operations remain within the intended directory.