mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-11 02:29:48 -04:00
Replaces the dual `authConfig` (raw .npmrc) + `authInfos` (parsed auth) + `sslConfigs` (parsed SSL) pattern with a single structured `configByUri: Record<string, RegistryConfig>` field on Config.
### New types (`@pnpm/types`)
- **`RegistryConfig`** — per-registry config: `{ creds?: Creds, tls?: TlsConfig }`
- **`Creds`** — auth credentials: `{ authToken?, basicAuth?, tokenHelper? }`
- **`TlsConfig`** — TLS config: `{ cert?, key?, ca? }`
### Key changes
- Rewrite `createGetAuthHeaderByURI` to accept `Record<string, RegistryConfig>` instead of raw .npmrc key-value pairs
- Eliminate duplicate auth parsing between `getAuthHeadersFromConfig` and `getNetworkConfigs`
- Remove `authConfig` from the install pipeline (`StrictInstallOptions`, `HeadlessOptions`), replaced by `configByUri`
- Remove `sslConfigs` from Config — SSL fields now live in `configByUri[uri].tls`
- Remove `authConfig['registry']` mutation in `extendInstallOptions` (default registry now passed directly to `createGetAuthHeaderByURI`)
- `authConfig` remains on Config only for raw .npmrc access (config commands, error reporting, config inheritance)
### Security
- tokenHelper in project .npmrc now throws instead of being silently stripped
- tokenHelper execution uses `shell: false` to prevent shell metacharacter injection
- Basic auth uses `Buffer.from().toString('base64')` instead of `btoa()` for Unicode safety
- Dispatcher only creates custom agents when entries actually have TLS fields
109 lines
2.7 KiB
TypeScript
109 lines
2.7 KiB
TypeScript
import {
|
|
AuthMissingSeparatorError,
|
|
type Creds,
|
|
parseCreds,
|
|
TokenHelperUnsupportedCharacterError,
|
|
} from '../src/parseCreds.js'
|
|
|
|
describe('parseCreds', () => {
|
|
test('empty object', () => {
|
|
expect(parseCreds({})).toBeUndefined()
|
|
})
|
|
|
|
test('authToken', () => {
|
|
expect(parseCreds({
|
|
authToken: 'example auth token',
|
|
})).toStrictEqual({
|
|
authToken: 'example auth token',
|
|
} as Creds)
|
|
})
|
|
|
|
test('authPairBase64', () => {
|
|
expect(parseCreds({
|
|
authPairBase64: btoa('foo:bar'),
|
|
})).toStrictEqual({
|
|
basicAuth: {
|
|
username: 'foo',
|
|
password: 'bar',
|
|
},
|
|
} as Creds)
|
|
|
|
expect(parseCreds({
|
|
authPairBase64: btoa('foo:bar:baz'),
|
|
})).toStrictEqual({
|
|
basicAuth: {
|
|
username: 'foo',
|
|
password: 'bar:baz',
|
|
},
|
|
} as Creds)
|
|
})
|
|
|
|
test('authPairBase64 must have a separator', () => {
|
|
expect(() => parseCreds({
|
|
authPairBase64: btoa('foo'),
|
|
})).toThrow(new AuthMissingSeparatorError())
|
|
})
|
|
|
|
test('authUsername and authPassword', () => {
|
|
expect(parseCreds({
|
|
authUsername: 'foo',
|
|
authPassword: btoa('bar'),
|
|
})).toStrictEqual({
|
|
basicAuth: {
|
|
username: 'foo',
|
|
password: 'bar',
|
|
},
|
|
} as Creds)
|
|
|
|
expect(parseCreds({
|
|
authUsername: 'foo',
|
|
})).toBeUndefined()
|
|
|
|
expect(parseCreds({
|
|
authPassword: 'bar',
|
|
})).toBeUndefined()
|
|
})
|
|
|
|
test('tokenHelper', () => {
|
|
expect(parseCreds({
|
|
tokenHelper: 'example-token-helper --foo --bar baz',
|
|
})).toStrictEqual({
|
|
tokenHelper: ['example-token-helper', '--foo', '--bar', 'baz'],
|
|
} as Creds)
|
|
|
|
expect(parseCreds({
|
|
tokenHelper: './example-token-helper.sh --foo --bar baz',
|
|
})).toStrictEqual({
|
|
tokenHelper: ['./example-token-helper.sh', '--foo', '--bar', 'baz'],
|
|
} as Creds)
|
|
|
|
expect(parseCreds({
|
|
tokenHelper: 'node ./example-token-helper.js --foo --bar baz',
|
|
})).toStrictEqual({
|
|
tokenHelper: ['node', './example-token-helper.js', '--foo', '--bar', 'baz'],
|
|
} as Creds)
|
|
|
|
expect(parseCreds({
|
|
tokenHelper: './example-token-helper.sh',
|
|
})).toStrictEqual({
|
|
tokenHelper: ['./example-token-helper.sh'],
|
|
} as Creds)
|
|
})
|
|
|
|
test('tokenHelper does not support environment variable', () => {
|
|
expect(() => parseCreds({
|
|
tokenHelper: 'example-token-helper $MY_VAR',
|
|
})).toThrow(new TokenHelperUnsupportedCharacterError('$'))
|
|
})
|
|
|
|
test('tokenHelper does not support quotations', () => {
|
|
expect(() => parseCreds({
|
|
tokenHelper: 'example-token-helper "hello world"',
|
|
})).toThrow(new TokenHelperUnsupportedCharacterError('"'))
|
|
|
|
expect(() => parseCreds({
|
|
tokenHelper: "example-token-helper 'hello world'",
|
|
})).toThrow(new TokenHelperUnsupportedCharacterError("'"))
|
|
})
|
|
})
|