fix(config): normalize registry URL trailing slash in nerfDart to prevent scope key mismatch#9555
Open
heunghingwan wants to merge 1 commit into
Open
fix(config): normalize registry URL trailing slash in nerfDart to prevent scope key mismatch#9555heunghingwan wants to merge 1 commit into
heunghingwan wants to merge 1 commit into
Conversation
…vent scope key mismatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
nerfDart()in@npmcli/configusesnew URL('.', url)to compute a scope key (e.g.//host/path/) for credential lookup. URL resolution treats a path without a trailing slash as a file, so the last segment is discarded:nerfDartoutputhttps://host/npm///host/npm/https://host/npm//host/(segment lost)getCredentialsByURIonly checks this single computed key with no backtracking. This means a registry URL with a path component (e.g.https://host/npm) produces the scope key//host/, which collapses auth to the entire host and can collide with other registries on the same host at different paths.Meanwhile,
npm-registry-fetch(the HTTP layer) uses its own path-backtracking logic and handles this correctly.Fix
Introduce
nerfDartURI()— a wrapper aroundnerfDart()that ensures the URL pathname always ends with/before computing the scope key. Since all callers (getCredentialsByURI,setCredentialsByURI,clearCredentialsByURI, and the auth validation invalidate) receive registry base URLs (not package/API URLs), this normalization is safe.Updated all 4
nerfDart()call sites inworkspaces/config/lib/index.jsto usenerfDartURI().Before vs After
Both
https://host/npm/andhttps://host/npmnow produce the same key//host/npm/.Impact
.npmrcfiles with auth stored at an incorrect collapsed scope (//host/:_authTokeninstead of//host/path/:_authToken) will neednpm loginto regenerate the key at the correct scope.