fix: enhance NAR URL normalization and fix potential path traversal [backport #856]#859
Conversation
…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)
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 URL handling by addressing potential path traversal vulnerabilities. The Highlights
Changelog
Activity
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 effectively addresses a path traversal vulnerability in nar.URL.Normalize(). The changes are robust, replacing manual string manipulation with stricter regular expression-based validation. The function signature was correctly updated to return an error, and all call sites have been diligently updated to handle this new error case. Additionally, the use of filepath.Base() when creating temporary files adds a valuable layer of defense-in-depth. The accompanying test changes are thorough and correctly validate the new behavior, including the error conditions. Overall, this is an excellent and well-executed security fix.
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:
(cherry picked from commit e3119d4)