Skip to content

fix: Potential fix for code scanning alert no. 7: Uncontrolled data used in path expression#18

Merged
skyoo2003 merged 3 commits into
mainfrom
alert-autofix-7
Apr 19, 2026
Merged

fix: Potential fix for code scanning alert no. 7: Uncontrolled data used in path expression#18
skyoo2003 merged 3 commits into
mainfrom
alert-autofix-7

Conversation

@skyoo2003
Copy link
Copy Markdown
Owner

Potential fix for https://github.com/skyoo2003/devcloud/security/code-scanning/7

General fix: ensure user-derived path components are validated as safe path components, and enforce canonical containment checks using filepath.Rel (or equivalent), while handling all path resolution errors.

Best fix here without changing functionality:

  1. In internal/services/lambda/store.go, harden codePath:
    • Reject accountID / functionName if they are absolute, contain separators, or contain ...
    • Build candidate path as before.
    • Resolve s.codeDir and candidate to absolute paths with error checks.
    • Use filepath.Rel(absBase, absCandidate) and reject if it starts with .. or is absolute.
    • Return the absolute candidate path (safe canonical path).
  2. No behavioral change to API contract; only invalid/path-traversal names now fail deterministically.

No changes are required in provider.go for this specific sink once store-layer validation is robust (defense-in-depth at sink boundary).

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…n path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 19, 2026

Reviewer's Guide

Hardens Lambda code storage path construction to validate user-derived path components and enforce robust canonical containment checks against the configured code directory, returning a fully resolved safe absolute path or a deterministic error on invalid/traversal input.

File-Level Changes

Change Details Files
Harden Lambda function code path construction against path traversal by validating components and enforcing canonical containment within codeDir with proper error handling.
  • Introduce an isSafeComponent helper to reject empty, relative, absolute, or separator-containing accountID and functionName values.
  • Construct the code.zip path as before via filepath.Join and filepath.Clean, but then resolve both base directory and candidate path to absolute paths with error checks.
  • Use filepath.Rel to compute the relative path from the base directory to the candidate, rejecting results that escape the base (".." prefix) or are absolute.
  • Return the canonical absolute code path instead of the cleaned but potentially relative path.
internal/services/lambda/store.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions Bot added the services AWS service implementations label Apr 19, 2026
@skyoo2003 skyoo2003 changed the title Potential fix for code scanning alert no. 7: Uncontrolled data used in path expression fix: Potential fix for code scanning alert no. 7: Uncontrolled data used in path expression Apr 19, 2026
@skyoo2003 skyoo2003 self-assigned this Apr 19, 2026
@skyoo2003 skyoo2003 marked this pull request as ready for review April 19, 2026 18:49
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Returning absCleaned instead of the previously relative cleaned value changes the function’s output semantics; double-check callers aren’t relying on codePath returning a path relative to s.codeDir before making this behavior change.
  • The isSafeComponent helper rejects any value containing "..", which is stricter than just blocking ".." as a component and may unexpectedly reject valid names (e.g., "user..1"); consider tightening the check to disallow only path-segment traversal rather than any substring.
  • The new path validation logic (clean + Abs + Rel checks) is fairly intricate; consider adding a short comment or extracting it into a reusable helper to clarify the invariants (e.g., which inputs are considered valid components and why) and reduce the chance of future regressions.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Returning `absCleaned` instead of the previously relative `cleaned` value changes the function’s output semantics; double-check callers aren’t relying on `codePath` returning a path relative to `s.codeDir` before making this behavior change.
- The `isSafeComponent` helper rejects any value containing `".."`, which is stricter than just blocking `".."` as a component and may unexpectedly reject valid names (e.g., `"user..1"`); consider tightening the check to disallow only path-segment traversal rather than any substring.
- The new path validation logic (clean + Abs + Rel checks) is fairly intricate; consider adding a short comment or extracting it into a reusable helper to clarify the invariants (e.g., which inputs are considered valid components and why) and reduce the chance of future regressions.

## Individual Comments

### Comment 1
<location path="internal/services/lambda/store.go" line_range="157" />
<code_context>
-// It validates the result stays under codeDir to prevent path traversal.
+// It validates path components and ensures the resolved path stays under codeDir.
 func (s *LambdaStore) codePath(accountID, functionName string) (string, error) {
+	isSafeComponent := func(v string) bool {
+		if v == "" || v == "." || v == ".." {
+			return false
+		}
+		if filepath.IsAbs(v) {
+			return false
+		}
+		if strings.Contains(v, "/") || strings.Contains(v, "\\") || strings.Contains(v, "..") {
+			return false
+		}
</code_context>
<issue_to_address>
**suggestion (bug_risk):** The `strings.Contains(v, "..")` check may be stricter than necessary and disallow valid IDs with consecutive dots.

This condition rejects any value containing ".." anywhere (e.g. "user..1"), which may be valid depending on how IDs are defined. If the aim is only to block path traversal, `filepath.Clean` + `Rel` plus the explicit `v == ".."` check already cover that. Consider dropping the `strings.Contains(v, "..")` check and relying on the path normalization instead, unless you explicitly want to forbid all such IDs.

```suggestion
		if strings.Contains(v, "/") || strings.Contains(v, "\\") {
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread internal/services/lambda/store.go Outdated
…ing check, return cleaned path

- Extract isSafePathComponent as a package-level helper with a doc comment
  explaining why ".." as a substring is allowed (traversal is caught by Rel)
- Drop the overly strict strings.Contains(v, "..") check that rejected valid
  names like "user..1"
- Return cleaned (relative) instead of absCleaned to preserve original output
  semantics for callers
- Apply the same isSafePathComponent + filepath.Rel containment check in
  DeleteFunction for consistency
Keep isSafePathComponent (loosened ".." substring check per review),
main's separate field-specific error messages, slog.Debug logging in
DeleteFunction, and filepath.IsAbs(rel) defense-in-depth check.
@github-actions github-actions Bot added the bug Something isn't working label Apr 19, 2026
@skyoo2003 skyoo2003 merged commit 21a192e into main Apr 19, 2026
10 checks passed
@skyoo2003 skyoo2003 deleted the alert-autofix-7 branch April 19, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working services AWS service implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant