fix: missing auth token in registries with paths included (#7337)

close #5970
close #2933

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
Nacho Aldama
2023-11-21 22:03:11 +01:00
committed by GitHub
parent 45bdc79b17
commit 23039a6d60
3 changed files with 28 additions and 1 deletions

View File

@@ -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

View File

@@ -24,6 +24,9 @@ function getMaxParts (uris: string[]) {
}
function getAuthHeaderByURI (authHeaders: Record<string, string>, 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<string, string>, maxParts: numb
return getAuthHeaderByURI(authHeaders, maxParts, urlWithoutPort)
}
return undefined
}
}

View File

@@ -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')
})