Skip to content

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

Merged
skyoo2003 merged 2 commits into
mainfrom
alert-autofix-15
Apr 20, 2026
Merged

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

Conversation

@skyoo2003
Copy link
Copy Markdown
Owner

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

Use a safer containment check in internal/services/s3/store.go by validating with filepath.Rel between canonical absolute paths, instead of strings.HasPrefix.

Best fix (without changing functionality):

  1. In safePath, build candidate path with filepath.Join.
  2. Canonicalize both fs.baseDir and candidate with filepath.Abs.
  3. Compute rel, err := filepath.Rel(baseAbs, candAbs).
  4. Reject if rel == ".." or starts with ".."+separator (or is absolute), otherwise accept.
  5. Return canonical candidate path.

This preserves support for nested object keys while preventing traversal outside baseDir in a platform-correct way.

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>
@github-actions github-actions Bot added the services AWS service implementations label Apr 19, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors FileStore.safePath to use absolute paths and filepath.Rel for secure, platform-correct containment checks instead of a fragile strings.HasPrefix prefix check, improving protection against path traversal while preserving existing behavior for nested object keys.

File-Level Changes

Change Details Files
Harden safePath path traversal protection by using absolute canonical paths and filepath.Rel-based containment checking instead of string prefix comparison.
  • Remove strings import and any reliance on strings.HasPrefix for validating resolved paths under the base directory.
  • Resolve fs.baseDir to an absolute canonical baseAbs path, returning a wrapped error if resolution fails.
  • Build the candidate path by joining baseAbs with the provided parts and canonicalize it with filepath.Abs, returning a wrapped error on failure.
  • Compute the relative path from baseAbs to candidateAbs with filepath.Rel and reject candidates that escape the base directory (.., ..+separator prefixes, or absolute rel paths), returning a detailed traversal error.
  • Return the canonical absolute candidate path on success, preserving support for nested object keys while enforcing directory containment.
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

@skyoo2003 skyoo2003 changed the title Potential fix for code scanning alert no. 15: Uncontrolled data used in path expression fix: Potential fix for code scanning alert no. 15: 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 left some high level feedback:

  • Consider computing and storing the absolute base directory once in NewFileStore (e.g., fs.baseAbs) instead of calling filepath.Abs(fs.baseDir) on every safePath invocation to avoid repeated work and potential inconsistencies if baseDir changes.
  • The traversal check len(rel) >= 3 && rel[:3] == ".."+string(filepath.Separator) is a bit brittle; using strings.HasPrefix(rel, ".."+string(filepath.Separator)) would be clearer and avoid manual length/slice handling.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider computing and storing the absolute base directory once in `NewFileStore` (e.g., `fs.baseAbs`) instead of calling `filepath.Abs(fs.baseDir)` on every `safePath` invocation to avoid repeated work and potential inconsistencies if `baseDir` changes.
- The traversal check `len(rel) >= 3 && rel[:3] == ".."+string(filepath.Separator)` is a bit brittle; using `strings.HasPrefix(rel, ".."+string(filepath.Separator))` would be clearer and avoid manual length/slice handling.

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.

@github-actions github-actions Bot added the bug Something isn't working label Apr 20, 2026
@skyoo2003 skyoo2003 merged commit 8c9c0d5 into main Apr 20, 2026
9 checks passed
@skyoo2003 skyoo2003 deleted the alert-autofix-15 branch April 20, 2026 16:08
skyoo2003 added a commit that referenced this pull request Apr 29, 2026
Scope the override to next's postcss dependency and pin to an exact
version to reduce risk of unintended side effects.
skyoo2003 added a commit that referenced this pull request Apr 29, 2026
)

* fix: override postcss to >=8.5.10 to resolve XSS vulnerability (dependabot #10)

PostCSS <8.5.10 has an XSS vulnerability via unescaped <style> in its
CSS Stringify Output. Next.js 16.2.4 depends on postcss@8.4.31 which
is vulnerable. Add npm overrides to force postcss@8.5.12.

* fix: narrow postcss override to next, pin to 8.5.12 (dependabot #10)

Scope the override to next's postcss dependency and pin to an exact
version to reduce risk of unintended side effects.
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