Skip to content

Commit f15ebc2

Browse files
authored
feat: add support for COREPACK_INTEGRITY_KEYS=0 (#470)
1 parent 6efa349 commit f15ebc2

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,9 @@ same major line. Should you need to upgrade to a new major, use an explicit
296296
- `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` are supported through
297297
[`node-proxy-agent`](https://github.com/TooTallNate/node-proxy-agent).
298298

299-
- `COREPACK_INTEGRITY_KEYS` can be set to an empty string to instruct Corepack
300-
to skip integrity checks, or a JSON string containing custom keys.
299+
- `COREPACK_INTEGRITY_KEYS` can be set to an empty string or `0` to
300+
instruct Corepack to skip integrity checks, or to a JSON string containing
301+
custom keys.
301302

302303
## Troubleshooting
303304

sources/corepackUtils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
283283

284284
if (!build[1]) {
285285
const registry = getRegistryFromPackageManagerSpec(spec);
286-
if (registry.type === `npm` && !registry.bin && process.env.COREPACK_INTEGRITY_KEYS !== ``) {
286+
if (registry.type === `npm` && !registry.bin && !shouldSkipIntegrityCheck()) {
287287
if (signatures! == null || integrity! == null)
288288
({signatures, integrity} = (await npmRegistryUtils.fetchTarballURLAndSignature(registry.package, version)));
289289

@@ -432,3 +432,8 @@ export async function runVersion(locator: Locator, installSpec: InstallSpec & {s
432432
// the stack trace of the package manager.
433433
process.nextTick(Module.runMain, binPath);
434434
}
435+
436+
export function shouldSkipIntegrityCheck() {
437+
return process.env.COREPACK_INTEGRITY_KEYS === ``
438+
|| process.env.COREPACK_INTEGRITY_KEYS === `0`;
439+
}

sources/npmRegistryUtils.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {UsageError} from 'clipanion';
2-
import {createVerify} from 'crypto';
1+
import {UsageError} from 'clipanion';
2+
import {createVerify} from 'crypto';
33

4-
import defaultConfig from '../config.json';
4+
import defaultConfig from '../config.json';
55

6-
import * as httpUtils from './httpUtils';
6+
import {shouldSkipIntegrityCheck} from './corepackUtils';
7+
import * as httpUtils from './httpUtils';
78

89
// load abbreviated metadata as that's all we need for these calls
910
// see: https://github.com/npm/registry/blob/cfe04736f34db9274a780184d1cdb2fb3e4ead2a/docs/responses/package-metadata.md
@@ -63,7 +64,7 @@ export async function fetchLatestStableVersion(packageName: string) {
6364

6465
const {version, dist: {integrity, signatures}} = metadata;
6566

66-
if (process.env.COREPACK_INTEGRITY_KEYS !== ``) {
67+
if (!shouldSkipIntegrityCheck()) {
6768
verifySignature({
6869
packageName, version,
6970
integrity, signatures,

tests/corepackUtils.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {describe, it, expect} from '@jest/globals';
2+
3+
import {shouldSkipIntegrityCheck} from '../sources/corepackUtils';
4+
5+
describe(`corepack utils shouldSkipIntegrityCheck`, () => {
6+
it(`should return false if COREPACK_INTEGRITY_KEYS env is not set`, () => {
7+
delete process.env.COREPACK_INTEGRITY_KEYS;
8+
expect(shouldSkipIntegrityCheck()).toBe(false);
9+
});
10+
11+
it(`should return true if COREPACK_INTEGRITY_KEYS env is set to 0`, () => {
12+
process.env.COREPACK_INTEGRITY_KEYS = `0`;
13+
expect(shouldSkipIntegrityCheck()).toBe(true);
14+
});
15+
16+
it(`should return true if COREPACK_INTEGRITY_KEYS env is set to an empty string`, () => {
17+
process.env.COREPACK_INTEGRITY_KEYS = ``;
18+
expect(shouldSkipIntegrityCheck()).toBe(true);
19+
});
20+
21+
it(`should return false if COREPACK_INTEGRITY_KEYS env is set to any other value`, () => {
22+
process.env.COREPACK_INTEGRITY_KEYS = JSON.stringify({foo: `bar`});
23+
expect(shouldSkipIntegrityCheck()).toBe(false);
24+
});
25+
});

0 commit comments

Comments
 (0)