fix: Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression#16
Merged
fix: Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression#16
Conversation
…n path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideHardens FileStore.safePath to defensively validate individual path components and enforce containment under baseDir using filepath.Rel-based path semantics instead of string-prefix checks, mitigating path traversal/code scanning issues. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider normalizing
fs.baseDir(e.g.,filepath.Abs+filepath.Clean) when constructingFileStoreso thatfilepath.Join,filepath.Clean, andfilepath.Relall operate on a consistent, absolute base path and the containment check cannot be bypassed by variations inbaseDir(e.g., trailing separators or relative components). - In the
safePathcomponent validation loop, consider usingos.IsPathSeparatoror checking againstfilepath.Separatorinstead of hardcoding"/"and"\\"so the separator validation remains correct and portable across platforms.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider normalizing `fs.baseDir` (e.g., `filepath.Abs` + `filepath.Clean`) when constructing `FileStore` so that `filepath.Join`, `filepath.Clean`, and `filepath.Rel` all operate on a consistent, absolute base path and the containment check cannot be bypassed by variations in `baseDir` (e.g., trailing separators or relative components).
- In the `safePath` component validation loop, consider using `os.IsPathSeparator` or checking against `filepath.Separator` instead of hardcoding `"/"` and `"\\"` so the separator validation remains correct and portable across platforms.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
…tore Address code review feedback: apply filepath.Clean to baseDir at construction time for consistent absolute paths, and replace hardcoded "/" and "\\" with os.IsPathSeparator for cross-platform correctness.
…tection The safePath function rejected all components containing path separators, but S3 object keys like "photos/a.jpg" legitimately contain slashes. Split objectPath to validate accountID/bucket strictly (no separators), then allow slashes in the key while still enforcing baseDir containment via filepath.Rel.
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.
Potential fix for https://github.com/skyoo2003/devcloud/security/code-scanning/9
General fix: validate untrusted path components before building paths, and enforce containment using path semantics (not just string prefix). For this code, the best minimal fix is to harden
safePathininternal/services/s3/store.go:parts:.or../or\)filepath.Rel(fs.baseDir, cleaned)and reject if:rel == ".."or starts with".."+separator(escapes root)filepath.IsAbs(rel)(unexpected absolute rel)This preserves existing behavior for valid names while preventing traversal patterns and making the sanitizer recognizable and sounder for CodeQL.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.