fix: enhance NAR URL normalization and fix potential path traversal#856
fix: enhance NAR URL normalization and fix potential path traversal#856
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Summary of ChangesHello @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 enhances the security and robustness of NAR (Nix Archive) URL handling. It refactors the Highlights
Changelog
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a critical security fix for a path traversal vulnerability in nar.URL.Normalize. The Normalize function now correctly returns an error for invalid hashes containing path traversal elements, instead of returning the original invalid URL. This change is propagated throughout the codebase, with call sites updated to handle the new error return. Additionally, path traversal protection has been added when creating temporary NAR files by using filepath.Base to sanitize filenames derived from NAR hashes. The changes are well-implemented and significantly improve the security of the application. My review includes one suggestion to refine the HTTP status code for normalization errors to provide more accurate client feedback.
|
Draft as I need to do this after #842 |
5c1f697 to
35f7680
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #856 +/- ##
=====================================
Coverage 3.96% 3.96%
=====================================
Files 6 6
Lines 429 429
=====================================
Hits 17 17
Misses 409 409
Partials 3 3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
35f7680 to
decb5cb
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a path traversal vulnerability in nar.URL.Normalize() by replacing a fragile string-based implementation with a robust, regex-based validation approach. The change to return an error from Normalize() is a good design choice, and all call sites have been updated accordingly. The additional use of filepath.Base when creating temporary files provides another layer of security. My feedback includes suggestions to simplify the new regular expressions for better clarity and to improve error handling in the test server for increased robustness.
The previous implementation of Normalize() was vulnerable to path traversal if the NAR hash contained ".." or other malicious sequences. This change refactors Normalize() to use stricter regular expressions for hash validation and returns an error if the hash is invalid. Additionally, it refactors ensureNarFile in pkg/cache/cache.go to use a new helper createOrUpdateNarFile, reducing code duplication. Key changes: - Introduced narHashLenientRegexp and narNormalizedHashRegexp for robust hash validation. - Modified nar.URL.Normalize() to return (URL, error). - Updated all callers in pkg/server, pkg/storage, and testdata to handle the new error return. - Added filepath.Base() to temp file creation in pkg/cache/cache.go for enhanced security.
decb5cb to
3fd977b
Compare
|
Backport failed for Please cherry-pick the changes locally and resolve any conflicts. git fetch origin release-0.8
git worktree add -d .worktree/backport-856-to-release-0.8 origin/release-0.8
cd .worktree/backport-856-to-release-0.8
git switch --create backport-856-to-release-0.8
git cherry-pick -x e3119d4b6d548fe25f8f52c12ef3a5e79e9448a0 |
…backport #856] The previous implementation of Normalize() was vulnerable to path traversal if the NAR hash contained ".." or other malicious sequences. This change refactors Normalize() to use stricter regular expressions for hash validation and returns an error if the hash is invalid. Additionally, it refactors ensureNarFile in pkg/cache/cache.go to use a new helper createOrUpdateNarFile, reducing code duplication. Key changes: - Introduced narHashLenientRegexp and narNormalizedHashRegexp for robust hash validation. - Modified nar.URL.Normalize() to return (URL, error). - Updated all callers in pkg/server, pkg/storage, and testdata to handle the new error return. - Added filepath.Base() to temp file creation in pkg/cache/cache.go for enhanced security. (cherry picked from commit e3119d4)
…backport #856] (#859) The previous implementation of Normalize() was vulnerable to path traversal if the NAR hash contained ".." or other malicious sequences. This change refactors Normalize() to use stricter regular expressions for hash validation and returns an error if the hash is invalid. Additionally, it refactors ensureNarFile in pkg/cache/cache.go to use a new helper createOrUpdateNarFile, reducing code duplication. Key changes: - Introduced narHashLenientRegexp and narNormalizedHashRegexp for robust hash validation. - Modified nar.URL.Normalize() to return (URL, error). - Updated all callers in pkg/server, pkg/storage, and testdata to handle the new error return. - Added filepath.Base() to temp file creation in pkg/cache/cache.go for enhanced security. (cherry picked from commit e3119d4)

The previous implementation of Normalize() was vulnerable to path
traversal if the NAR hash contained ".." or other malicious sequences.
This change refactors Normalize() to use stricter regular expressions
for hash validation and returns an error if the hash is invalid.
Additionally, it refactors ensureNarFile in pkg/cache/cache.go to use a
new helper createOrUpdateNarFile, reducing code duplication.
Key changes: