Skip to content

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

Merged
skyoo2003 merged 2 commits into
mainfrom
alert-autofix-4
Apr 19, 2026
Merged

fix: Potential fix for code scanning alert no. 4: Uncontrolled data used in path expression#21
skyoo2003 merged 2 commits into
mainfrom
alert-autofix-4

Conversation

@skyoo2003
Copy link
Copy Markdown
Owner

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, update codePath to:

  1. Reject empty accountID/functionName.
  2. Reject any value containing /, \, or .. (single-component policy).
  3. Resolve both base and target via filepath.Abs and handle errors.
  4. Keep a strict “within base directory” check before returning path.

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.

…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

Tightens 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

Change Details Files
Harden codePath to validate path components and perform safer containment checks against the base code directory.
  • Introduce validPathComponent helper to enforce that account and function identifiers are single, non-traversing path components with no separators or '..'
  • Reject empty accountID or functionName and return descriptive errors for invalid path components before constructing paths
  • Resolve both the base code directory and the computed code path to absolute paths with error handling instead of ignoring resolution errors
  • Replace prefix-based containment check with a filepath.Rel-based check that rejects paths that resolve outside the base directory
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. 4: Uncontrolled data used in path expression fix: Potential fix for code scanning alert no. 4: 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 14:30
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:

  • 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.
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>

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
Extract validPathComponent helper using strings.ContainsAny,
switch containment check from HasPrefix to filepath.Rel for
robustness, and allow equality (rel == ".").
@github-actions github-actions Bot added the bug Something isn't working label Apr 19, 2026
@skyoo2003
Copy link
Copy Markdown
Owner Author

@sourcery-ai review

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 reviewed your changes and they look great!


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.

@skyoo2003 skyoo2003 merged commit 1794ead into main Apr 19, 2026
10 checks passed
@skyoo2003 skyoo2003 deleted the alert-autofix-4 branch April 19, 2026 19:02
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