Skip to content

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

Merged
skyoo2003 merged 3 commits intomainfrom
alert-autofix-13
Apr 20, 2026
Merged

fix: Potential fix for code scanning alert no. 13: Uncontrolled data used in path expression#12
skyoo2003 merged 3 commits intomainfrom
alert-autofix-13

Conversation

@skyoo2003
Copy link
Copy Markdown
Owner

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

To fix this safely without changing functionality, harden safePath in internal/services/s3/store.go so it:

  1. Resolves both baseDir and candidate path to absolute paths (filepath.Abs).
  2. Uses filepath.Rel(baseAbs, candidateAbs) to verify containment.
  3. Rejects paths where rel == ".." or starts with ".."+separator (escape).
  4. Keeps returning normalized absolute path for downstream file ops.

This is the best single fix because it preserves current API behavior and all call sites, while replacing a fragile prefix check with a standard, cross-platform path containment validation pattern. No provider-side routing changes are required for this specific sink hardening.

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

…in 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 (collapsed on small PRs)

Reviewer's Guide

Hardens the FileStore.safePath implementation to use absolute paths and filepath.Rel for robust directory containment checks, addressing a path traversal/code scanning alert without changing the public API.

File-Level Changes

Change Details Files
Strengthen path traversal protection in FileStore.safePath using absolute paths and relative path containment checks.
  • Resolve fs.baseDir to an absolute path at the start of safePath and handle resolution errors explicitly.
  • Join path components using the absolute base directory instead of the raw baseDir value.
  • Normalize the joined path and resolve it again to an absolute candidate path, returning a wrapped error if resolution fails.
  • Use filepath.Rel between the absolute base directory and the candidate path to determine whether the candidate escapes the base directory.
  • Treat any relative result equal to ".." or starting with ".." plus the OS path separator as a traversal attempt and return a descriptive error including the candidate path.
  • Return the normalized absolute candidate path instead of the previously returned cleaned path to keep downstream file operations on absolute, validated paths.
internal/services/s3/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. 13: Uncontrolled data used in path expression fix: Potential fix for code scanning alert no. 13: 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:51
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:

  • Consider resolving fs.baseDir to an absolute path once in NewFileStore and storing it on the struct, rather than calling filepath.Abs on every safePath invocation to avoid repeated filesystem/OS work on hot paths.
  • Since joined is already built from baseAbs, calling filepath.Abs on cleaned is redundant; you can simplify by dropping the second Abs call and using cleaned directly as the candidate path for Rel and the return value.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider resolving `fs.baseDir` to an absolute path once in `NewFileStore` and storing it on the struct, rather than calling `filepath.Abs` on every `safePath` invocation to avoid repeated filesystem/OS work on hot paths.
- Since `joined` is already built from `baseAbs`, calling `filepath.Abs` on `cleaned` is redundant; you can simplify by dropping the second `Abs` call and using `cleaned` directly as the candidate path for `Rel` and the return value.

## Individual Comments

### Comment 1
<location path="internal/services/s3/store.go" line_range="41-45" />
<code_context>
+		return "", fmt.Errorf("resolve candidate path: %w", err)
+	}
+
+	rel, err := filepath.Rel(baseAbs, candidateAbs)
+	if err != nil {
+		return "", fmt.Errorf("resolve relative path: %w", err)
+	}
+	if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
+		return "", fmt.Errorf("path traversal detected: %s", candidateAbs)
 	}
</code_context>
<issue_to_address>
**🚨 issue (security):** Path traversal check does not account for symlinks inside the base directory.

`Abs` + `Rel` blocks `..` traversal but not symlinks under `baseAbs` that point outside it, so the isolation guarantee can be broken. For stronger confinement, resolve symlinks (e.g. with `filepath.EvalSymlinks`) on `baseAbs` and/or `candidateAbs` and run the `Rel` check on those resolved paths.
</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/s3/store.go Outdated
Keep the HEAD version which uses absolute path resolution for stronger
path traversal protection.
@github-actions github-actions Bot added the bug Something isn't working label Apr 20, 2026
Resolve baseDir symlinks once in NewFileStore via EvalSymlinks, remove
per-call filepath.Abs overhead in safePath, and resolve candidate
symlinks before containment check to prevent escape via symlinks.
@skyoo2003 skyoo2003 merged commit e23c787 into main Apr 20, 2026
10 checks passed
@skyoo2003 skyoo2003 deleted the alert-autofix-13 branch April 20, 2026 10:52
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