fix(storage): Implement path containment to prevent traversal attacks#8658
Merged
bshaffer merged 7 commits intogoogleapis:mainfrom Oct 21, 2025
Merged
Conversation
65dd722 to
35c863f
Compare
bshaffer
reviewed
Oct 15, 2025
Contributor
bshaffer
left a comment
There was a problem hiding this comment.
Great job! Some minor suggestions is all I have
35c863f to
dd44f2e
Compare
999ac1c to
25f4a5b
Compare
bshaffer
approved these changes
Oct 21, 2025
Contributor
|
great job @thiyaguk09 ! thank you |
|
Unfortunately this change has broken BC for us across a number of projects, where we can already guarantee that $path is safe from user injection. I think it does not actually provide the benefit it claims, and in some cases would reduce security. IMO it should be reverted - please see #8690 for full writeup. |
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.
This pull request significantly enhances the security of the
downloadToFilemethod by implementing comprehensive path validation. The changes prevent directory traversal attacks, ensuring that files can only be written to designated locations and mitigating the risk of malicious file overwrites or unauthorized access to the file system. This is achieved through checks for relative path components, absolute paths, and Windows drive specifications, complemented by new and updated test cases.Highlights
downloadToFile()method to prevent directory traversal attacks.downloadToFile()method now normalizes path separators and explicitly checks for '..' segments, paths starting with '/', and Windows drive letters (e.g., 'C:').RuntimeExceptionis thrown with a clear message if an invalid or malicious path is detected during the download operation.__DIR__orphp://temp, aligning with the new security checks and preserving the original intent of verifying file system interactions.Changelog
downloadToFileto prevent directory traversal.\to/for consistent checking...as a distinct path segment, paths starting with/, and paths beginning with a Windows drive letter (e.g.,C:).RuntimeExceptionwith a specific message upon detection of a disallowed path.testDownloadsToFileShouldNotCreateFileWhenObjectNotFoundto use a simple relative filename for the download path.testDownloadsToFileShouldBlockRelativeTraversalto confirm..in paths triggers the expectedRuntimeException.testDownloadsToFileShouldBlockAbsolutePathto confirm absolute paths trigger the expectedRuntimeException.testDownloadsToFileShouldNotCreateFileWhenObjectNotFoundto use a simple relative filename for the download path.testDownloadsToFileShouldBlockRelativeTraversalto confirm..in paths triggers the expectedRuntimeException.testDownloadsToFileShouldBlockAbsolutePathto confirm absolute paths trigger the expectedRuntimeException.