fix: Potential fix for code scanning alert no. 4: Uncontrolled data used in path expression#21
Merged
Conversation
…n path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Contributor
Reviewer's GuideTightens validation and containment checks for Lambda function code paths by enforcing single-component identifiers, resolving absolute paths safely, and using a robust relative-path containment check to prevent path traversal. 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 manual checks for
/,\, and..inaccountIDandfunctionNameare a bit repetitive; consider extracting a small helper (e.g.,validatePathComponent) or usingstrings.ContainsAnyfor the separator checks to reduce duplication and make the validation rules clearer. - The containment check uses
HasPrefix(absCleaned, absBase+string(filepath.Separator)), which excludes the case whereabsCleaned == absBase; if storing a zip directly undercodeDiris ever expected, consider explicitly allowing equality instead of requiring a subdirectory.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The manual checks for `/`, `\`, and `..` in `accountID` and `functionName` are a bit repetitive; consider extracting a small helper (e.g., `validatePathComponent`) or using `strings.ContainsAny` for the separator checks to reduce duplication and make the validation rules clearer.
- The containment check uses `HasPrefix(absCleaned, absBase+string(filepath.Separator))`, which excludes the case where `absCleaned == absBase`; if storing a zip directly under `codeDir` is ever expected, consider explicitly allowing equality instead of requiring a subdirectory.
## Individual Comments
### Comment 1
<location path="internal/services/lambda/store.go" line_range="164-175" />
<code_context>
- absBase, _ := filepath.Abs(s.codeDir)
- absCleaned, _ := filepath.Abs(cleaned)
+
+ absBase, err := filepath.Abs(s.codeDir)
+ if err != nil {
+ return "", fmt.Errorf("resolve base code directory: %w", err)
+ }
+ absCleaned, err := filepath.Abs(cleaned)
+ if err != nil {
+ return "", fmt.Errorf("resolve code path: %w", err)
+ }
+
if !strings.HasPrefix(absCleaned, absBase+string(filepath.Separator)) {
return "", fmt.Errorf("path traversal detected: %s", cleaned)
}
</code_context>
<issue_to_address>
**🚨 suggestion (security):** Using `HasPrefix` for directory containment can be fragile; `filepath.Rel` would be more robust.
String prefix checks can fail if path normalization or casing changes (especially on Windows) or if separator assumptions differ. A safer approach is to compute the relative path from the base and ensure it doesn’t escape:
```go
rel, err := filepath.Rel(absBase, absCleaned)
if err != nil {
return "", fmt.Errorf("resolve relative code path: %w", err)
}
if strings.HasPrefix(rel, ".."+string(filepath.Separator)) || rel == ".." {
return "", fmt.Errorf("path traversal detected: %s", cleaned)
}
```
This checks containment without relying on absolute-path string prefixes.
```suggestion
absBase, err := filepath.Abs(s.codeDir)
if err != nil {
return "", fmt.Errorf("resolve base code directory: %w", err)
}
absCleaned, err := filepath.Abs(cleaned)
if err != nil {
return "", fmt.Errorf("resolve code path: %w", err)
}
rel, err := filepath.Rel(absBase, absCleaned)
if err != nil {
return "", fmt.Errorf("resolve relative code path: %w", err)
}
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("path traversal detected: %s", cleaned)
}
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Extract validPathComponent helper using strings.ContainsAny, switch containment check from HasPrefix to filepath.Rel for robustness, and allow equality (rel == ".").
Owner
Author
|
@sourcery-ai review |
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/4
Best fix: validate untrusted path components as single safe names before path construction, then keep robust containment verification.
In
internal/services/lambda/store.go, updatecodePathto:accountID/functionName./,\, or..(single-component policy).filepath.Absand handle errors.This preserves existing behavior (store code under
codeDir/accountID/functionName/code.zip) while preventing traversal through crafted names.Suggested fixes powered by Copilot Autofix. Review carefully before merging.