fix: Potential fix for code scanning alert no. 14: Uncontrolled data used in path expression#11
Merged
Merged
Conversation
…in path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Contributor
Reviewer's GuideStrengthens the S3 FileStore path-safety helper to defensively validate each path segment and enforce a robust absolute-path containment check, ensuring all object operations are protected against directory traversal and path escape attacks. 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 found 1 issue, and left some high level feedback:
- The new
safePathnow rejects empty path segments and always returns an absolute path, which is a behavior change from the previous implementation; double-check callers that may rely on passing empty segments or on getting back a path relative tofs.baseDir. - You’re recomputing
filepath.Abs(fs.baseDir)on everysafePathcall; consider normalizing and storing an absolutebaseDironce inNewFileStoreto avoid repeated work and to simplify the containment check.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `safePath` now rejects empty path segments and always returns an absolute path, which is a behavior change from the previous implementation; double-check callers that may rely on passing empty segments or on getting back a path relative to `fs.baseDir`.
- You’re recomputing `filepath.Abs(fs.baseDir)` on every `safePath` call; consider normalizing and storing an absolute `baseDir` once in `NewFileStore` to avoid repeated work and to simplify the containment check.
## Individual Comments
### Comment 1
<location path="internal/services/s3/store.go" line_range="30-31" />
<code_context>
- if !strings.HasPrefix(cleaned, fs.baseDir+string(filepath.Separator)) && cleaned != fs.baseDir {
- return "", fmt.Errorf("path traversal detected: %s", cleaned)
+ cleanParts := make([]string, 0, len(parts))
+ for _, part := range parts {
+ if part == "" {
+ return "", fmt.Errorf("invalid empty path segment")
+ }
</code_context>
<issue_to_address>
**question (bug_risk):** Rejecting empty path segments may be a breaking behavior change compared to the previous implementation.
`filepath.Join` previously ignored empty segments, but the new logic returns an error when `part == ""`, changing semantics for callers that may legitimately pass empty strings (e.g., conditional appends). If this behavior change isn’t deliberate, consider skipping empty segments or normalizing `parts` before this check. If it is deliberate, please verify that no existing callers rely on the old behavior.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
skyoo2003
added a commit
that referenced
this pull request
Apr 20, 2026
The previous security fix (alert #11) rejected all path separators in every component passed to safePath, but S3 keys legitimately contain "/" for nested objects (e.g. "a/b/c/file.txt"). Now only accountID and bucket are validated as simple names; the key may contain "/" and is still protected against path traversal via filepath.Clean + prefix check.
…with symlink resolution
…use resolved baseDir
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/14
Best fix: strengthen
safePathto validate each untrusted path segment before joining and then perform a robust relative-path containment check after resolving absolute paths.In
internal/services/s3/store.go:safePath(parts ...string)to reject:..traversal semantics (part == ".."or starts with"../"patterns after cleaning)baseDir+ validated parts.baseDirand candidate to absolute paths and verify containment usingfilepath.Rel(reject if rel starts with..or is absolute).a/b/cby preserving valid subpaths), but reject traversal/escape inputs deterministically.No changes are required in
provider.gofor this finding; centralizing enforcement insafePathfixes all call paths (GetObject,PutObject,DeleteObject, etc.).Suggested fixes powered by Copilot Autofix. Review carefully before merging.