diff --git a/.changeset/three-penguins-trade.md b/.changeset/three-penguins-trade.md new file mode 100644 index 0000000000..9f7f20d30f --- /dev/null +++ b/.changeset/three-penguins-trade.md @@ -0,0 +1,6 @@ +--- +"@pnpm/network.auth-header": patch +"pnpm": patch +--- + +Fix missing auth tokens in registries with paths specified (e.g. //npm.pkg.github.com/pnpm). #5970 #2933 diff --git a/network/auth-header/src/index.ts b/network/auth-header/src/index.ts index bdd5ec4d48..240c032cfc 100644 --- a/network/auth-header/src/index.ts +++ b/network/auth-header/src/index.ts @@ -24,6 +24,9 @@ function getMaxParts (uris: string[]) { } function getAuthHeaderByURI (authHeaders: Record, maxParts: number, uri: string): string | undefined { + if (!uri.endsWith('/')) { + uri += '/' + } const nerfed = nerfDart(uri) const parts = nerfed.split('/') for (let i = Math.min(parts.length, maxParts) - 1; i >= 3; i--) { @@ -35,4 +38,4 @@ function getAuthHeaderByURI (authHeaders: Record, maxParts: numb return getAuthHeaderByURI(authHeaders, maxParts, urlWithoutPort) } return undefined -} \ No newline at end of file +} diff --git a/network/auth-header/test/getAuthHeaderByURI.ts b/network/auth-header/test/getAuthHeaderByURI.ts index 851dfbba1a..706bc7263e 100644 --- a/network/auth-header/test/getAuthHeaderByURI.ts +++ b/network/auth-header/test/getAuthHeaderByURI.ts @@ -43,3 +43,21 @@ test('getAuthHeaderByURI() when default ports are specified', () => { test('returns undefined when the auth header is not found', () => { expect(createGetAuthHeaderByURI({ allSettings: {}, userSettings: {} })('http://reg.com')).toBe(undefined) }) + +test('getAuthHeaderByURI() when the registry has pathnames', () => { + const getAuthHeaderByURI = createGetAuthHeaderByURI({ + allSettings: { + '//npm.pkg.github.com/pnpm/:_authToken': 'abc123', + }, + userSettings: {}, + }) + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/foo')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/foo/')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/foo/-/foo-1.0.0.tgz')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/foo/-/foo-1.0.0.tgz')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/foo/-/foo-1.0.0.tgz')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/foo/-/foo-1.0.0.tgz')).toBe('Bearer abc123') + expect(getAuthHeaderByURI('https://npm.pkg.github.com/pnpm/foo/-/foo-1.0.0.tgz')).toBe('Bearer abc123') +}) \ No newline at end of file