Skip to content

feat: remove empty directories after file deletion in local storage [backport #722] - #725

Merged
kalbasit merged 3 commits into
release-0.8from
security-fix
Feb 4, 2026
Merged

kalbasit merged 3 commits into
release-0.8from
security-fix

Conversation

@kalbasit

@kalbasit kalbasit commented Feb 4, 2026

Copy link
Copy Markdown
Owner

This change addresses a path injection vulnerability (alert #7) where unvalidated hashes could be used to construct file paths, potentially allowing access to arbitrary files.

Hash validation has been added to pkg/helper/filepath.go using a regex that ensures hashes only contain alphanumeric characters.

The return signatures of NarInfoFilePath and NarFilePath have been updated to return an error if the hash is invalid. All call sites in pkg/storage/local, pkg/storage/s3, and pkg/nar have been updated to handle these errors.

A validation helper ValidateHash and a new error ErrInvalidHash were introduced in pkg/helper/filepath.go to support these checks.

(cherry picked from commit 2eb0a8c)

This change addresses a path injection vulnerability (alert #7) where unvalidated
hashes could be used to construct file paths, potentially allowing access to
arbitrary files.

Hash validation has been added to `pkg/helper/filepath.go` using a regex
that ensures hashes only contain alphanumeric characters.

The return signatures of `NarInfoFilePath` and `NarFilePath` have been updated
to return an error if the hash is invalid. All call sites in `pkg/storage/local`,
`pkg/storage/s3`, and `pkg/nar` have been updated to handle these errors.

A validation helper `ValidateHash` and a new error `ErrInvalidHash` were introduced
in `pkg/helper/filepath.go` to support these checks.

(cherry picked from commit 2eb0a8c)
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request go Pull requests that update go code labels Feb 4, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kalbasit, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the application's security posture by introducing robust validation for hash inputs used in file path generation. By converting previously panic-inducing or unchecked path constructions into error-returning functions, it ensures that invalid or malicious hash inputs are caught and handled gracefully across various storage operations, preventing potential path injection attacks.

Highlights

  • Security Enhancement: Addressed a path injection vulnerability by implementing strict validation for hashes used in file path construction.
  • Hash Validation: Introduced a regular expression (^[a-z0-9]+$) to validate hashes, ensuring they only contain alphanumeric characters.
  • Error Handling Refinement: Modified NarInfoFilePath, NarFilePath, and FilePathWithSharding functions to return errors instead of panicking or returning only a string, allowing for explicit error handling.
  • Error Propagation: Updated all call sites in pkg/storage/local, pkg/storage/s3, and pkg/nar to properly handle the new error returns from file path generation functions.
  • New Helper and Error Type: Added a ValidateHash helper function and a new ErrInvalidHash error type in pkg/helper/filepath.go.
Changelog
  • pkg/helper/filepath.go
    • Introduced ErrInvalidHash, hashRegexp, and ErrInputTooShort for better error handling and hash validation.
    • Added ValidateHash function to enforce alphanumeric hash format.
    • Changed NarInfoFilePath, NarFilePath, and FilePathWithSharding signatures to return (string, error) instead of string, incorporating hash validation and error propagation.
  • pkg/helper/filepath_test.go
    • Updated tests for NarInfoFilePath, NarFilePath, and FilePathWithSharding to assert error returns for invalid inputs (e.g., too short or non-alphanumeric hashes) instead of panics.
    • Added require import from testify for more concise error checking in tests.
  • pkg/nar/url.go
    • Modified URL.ToFilePath() to return (string, error) to correctly propagate errors from underlying helper functions.
  • pkg/storage/local/local.go
    • Updated all methods (HasNarInfo, GetNarInfo, PutNarInfo, DeleteNarInfo, HasNar, GetNar, PutNar, DeleteNar) that use helper.NarInfoFilePath or nar.URL.ToFilePath() to handle the newly introduced error returns.
  • pkg/storage/s3/s3.go
    • Modified narInfoPath and narPath methods to return (string, error) to incorporate hash validation errors.
    • Updated all methods (HasNarInfo, GetNarInfo, PutNarInfo, DeleteNarInfo, HasNar, GetNar, PutNar, DeleteNar) that call s.narInfoPath() or s.narPath() to handle the new error returns.
Activity
  • The changes in this pull request were cherry-picked from commit 2eb0a8c9dddd6da9da87ad3c002045cd4aa9db57.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@kalbasit kalbasit changed the title feat: remove empty directories after file deletion in local storage [… feat: remove empty directories after file deletion in local storage [backport #722] Feb 4, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an important security fix by validating hashes used to construct file paths, effectively preventing a path injection vulnerability. The changes are well-implemented, replacing panics with proper error handling and updating all call sites accordingly. The tests have also been updated to reflect these changes and include cases for invalid hashes. I've included a low-priority suggestion regarding code duplication, keeping in mind the repository's guidelines on acceptable duplication. Overall, this is a great improvement.

@kalbasit
kalbasit enabled auto-merge (squash) February 4, 2026 01:46
@kalbasit
kalbasit disabled auto-merge February 4, 2026 02:01
@kalbasit
kalbasit merged commit 9ae3ae2 into release-0.8 Feb 4, 2026
11 checks passed
@kalbasit
kalbasit deleted the security-fix branch February 4, 2026 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant