Skip to content

feat: Add NAR URL normalization function and support dashes/underscores in hashes#836

Merged
kalbasit merged 2 commits intomainfrom
add-nar-normalization-function-support-dashes-underscores
Feb 11, 2026
Merged

feat: Add NAR URL normalization function and support dashes/underscores in hashes#836
kalbasit merged 2 commits intomainfrom
add-nar-normalization-function-support-dashes-underscores

Conversation

@kalbasit
Copy link
Owner

@kalbasit kalbasit commented Feb 11, 2026

Add Normalize() method to nar.URL to handle NAR URLs with embedded narinfo hash
prefixes. Some upstream caches (like nix-serve) serve NAR URLs with the narinfo
hash as a prefix (e.g., "narinfo-hash-actual-hash"). This method strips the prefix
by identifying the first separator (dash or underscore) and removing everything
before it.

Changes:

  • pkg/nar/url.go: Add Normalize() method and supporting logic
  • pkg/nar/hash.go: Update narHashPattern to allow dashes and underscores ([a-z0-9_-]+)
  • pkg/nar/url_test.go: Add TestNormalize with three test cases (no prefix, dash-separated, underscore-separated)
  • testdata/nar7.go: Update to use prefixed hash format for testing

This enables parsing and normalizing NAR URLs from sources that include hash
prefixes, which will be applied consistently across cache, storage, and test
layers in subsequent commits.

Part of #806
Closes #820

Co-Authored-By: Claude Haiku 4.5 noreply@anthropic.com

…es in hashes

Add Normalize() method to nar.URL to handle NAR URLs with embedded narinfo hash
prefixes. Some upstream caches (like nix-serve) serve NAR URLs with the narinfo
hash as a prefix (e.g., "narinfo-hash-actual-hash"). This method strips the prefix
by identifying the first separator (dash or underscore) and removing everything
before it.

Changes:
- pkg/nar/url.go: Add Normalize() method and supporting logic
- pkg/nar/hash.go: Update narHashPattern to allow dashes and underscores ([a-z0-9_-]+)
- pkg/nar/url_test.go: Add TestNormalize with three test cases (no prefix, dash-separated, underscore-separated)
- testdata/nar7.go: Update to use prefixed hash format for testing

This enables parsing and normalizing NAR URLs from sources that include hash
prefixes, which will be applied consistently across cache, storage, and test
layers in subsequent commits.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 11, 2026
@dosubot
Copy link

dosubot bot commented Feb 11, 2026

Related Documentation

Checked 7 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@dosubot dosubot bot added enhancement New feature or request go Pull requests that update go code labels Feb 11, 2026
@gemini-code-assist
Copy link
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 introduces functionality to robustly handle NAR URLs that may contain an informational hash prefix, as seen in some upstream caches like nix-serve. By adding a Normalize() method to the nar.URL type and updating the hash pattern to recognize separators, the system can now consistently parse and standardize NAR hashes, ensuring compatibility and correct storage across various components.

Highlights

  • NAR URL Normalization Function: Implemented a Normalize() method for nar.URL to strip narinfo hash prefixes from NAR URLs, standardizing the hash for storage. This addresses URLs served by caches like nix-serve that include a prefix (e.g., "narinfo-hash-actual-hash").
  • Hash Pattern Expansion: Modified the narHashPattern regular expression to allow dashes (-) and underscores (_) in NAR hashes, accommodating the format of prefixed hashes.
  • Comprehensive Testing: Added new test cases in pkg/nar/url_test.go for the Normalize() method, covering scenarios with no prefix, dash-separated prefixes, and underscore-separated prefixes.
Changelog
  • pkg/nar/hash.go
    • Updated the narHashPattern regular expression to include dashes and underscores.
  • pkg/nar/url.go
    • Added a new Normalize() method to the URL struct for stripping narinfo hash prefixes.
    • Implemented logic within Normalize() to identify and remove prefixes separated by dashes or underscores.
  • pkg/nar/url_test.go
    • Added TestNormalize with multiple test cases to validate the new Normalize() method's behavior.
    • Included tests for URLs with no prefix, dash-separated prefixes, and underscore-separated prefixes.
Activity
  • No human activity (comments, reviews, etc.) has been recorded on this pull request yet.
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.

Copy link
Contributor

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

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 a Normalize() function to handle NAR URLs with prefixes and updates the NAR hash pattern to allow dashes and underscores. While the feature is useful, the implementation of the Normalize function has a critical flaw. Its logic is complex and uses a weak heuristic that can incorrectly modify valid NAR hashes that happen to contain a separator. This could lead to data corruption or invalid hash errors downstream. I've provided a much simpler and more robust implementation that uses a stronger heuristic based on the known length of narinfo hashes. Additionally, the accompanying tests are not comprehensive enough to catch these issues, so I've recommended adding more test cases for edge scenarios.

@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.41%. Comparing base (184373f) to head (c2924c9).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #836   +/-   ##
=======================================
  Coverage   85.41%   85.41%           
=======================================
  Files           2        2           
  Lines         480      480           
=======================================
  Hits          410      410           
  Misses         65       65           
  Partials        5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Implement 32-character prefix heuristic for identifying narinfo hash prefixes.
- Sanitize the hash using filepath.Clean and check for path traversal sequences.
- Add comprehensive test cases for edge cases and path traversal attempts.

This addresses critical security and logic issues identified in PR comments.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Feb 11, 2026
@kalbasit kalbasit added the backport release-0.8 Backport PR automatically label Feb 11, 2026
@kalbasit kalbasit enabled auto-merge (squash) February 11, 2026 17:15
@kalbasit kalbasit merged commit 7e1c28e into main Feb 11, 2026
18 checks passed
@kalbasit kalbasit deleted the add-nar-normalization-function-support-dashes-underscores branch February 11, 2026 17:25
@kalbasit
Copy link
Owner Author

Backport failed for release-0.8, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release-0.8
git worktree add -d .worktree/backport-836-to-release-0.8 origin/release-0.8
cd .worktree/backport-836-to-release-0.8
git switch --create backport-836-to-release-0.8
git cherry-pick -x 20012d5c911eda931990e2076e37c4c6bc2d5be2 c2924c9a11dc9c9b792460f6f1da3eb16524d5b9

kalbasit added a commit that referenced this pull request Feb 12, 2026
…es in hashes [backport #836]

Add Normalize() method to nar.URL to handle NAR URLs with embedded narinfo hash
prefixes. Some upstream caches (like nix-serve) serve NAR URLs with the narinfo
hash as a prefix (e.g., "narinfo-hash-actual-hash"). This method strips the prefix
by identifying the first separator (dash or underscore) and removing everything
before it.

Changes:
- pkg/nar/url.go: Add Normalize() method and supporting logic
- pkg/nar/hash.go: Update narHashPattern to allow dashes and underscores ([a-z0-9_-]+)
- pkg/nar/url_test.go: Add TestNormalize with three test cases (no prefix, dash-separated, underscore-separated)
- testdata/nar7.go: Update to use prefixed hash format for testing

This enables parsing and normalizing NAR URLs from sources that include hash
prefixes, which will be applied consistently across cache, storage, and test
layers in subsequent commits.

Part of #806
Closes #820

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
(cherry picked from commit 7e1c28e)
kalbasit added a commit that referenced this pull request Feb 12, 2026
…es in hashes [backport #836]

Add Normalize() method to nar.URL to handle NAR URLs with embedded narinfo hash
prefixes. Some upstream caches (like nix-serve) serve NAR URLs with the narinfo
hash as a prefix (e.g., "narinfo-hash-actual-hash"). This method strips the prefix
by identifying the first separator (dash or underscore) and removing everything
before it.

Changes:
- pkg/nar/url.go: Add Normalize() method and supporting logic
- pkg/nar/hash.go: Update narHashPattern to allow dashes and underscores ([a-z0-9_-]+)
- pkg/nar/url_test.go: Add TestNormalize with three test cases (no prefix, dash-separated, underscore-separated)
- testdata/nar7.go: Update to use prefixed hash format for testing

This enables parsing and normalizing NAR URLs from sources that include hash
prefixes, which will be applied consistently across cache, storage, and test
layers in subsequent commits.

Part of #806
Closes #820

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
(cherry picked from commit 7e1c28e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport release-0.8 Backport PR automatically 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