Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: COREPACK_INTEGRITY_KEYS should support 0 and false values #470

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ same major line. Should you need to upgrade to a new major, use an explicit
- `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` are supported through
[`node-proxy-agent`](https://github.com/TooTallNate/node-proxy-agent).

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

## Troubleshooting

Expand Down
7 changes: 6 additions & 1 deletion sources/corepackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s

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

Expand Down Expand Up @@ -432,3 +432,8 @@ export async function runVersion(locator: Locator, installSpec: InstallSpec & {s
// the stack trace of the package manager.
process.nextTick(Module.runMain, binPath);
}

export function shouldSkipIntegrityCheck() {
return process.env.COREPACK_INTEGRITY_KEYS === ``
|| process.env.COREPACK_INTEGRITY_KEYS === `0`;
}
11 changes: 6 additions & 5 deletions sources/npmRegistryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {UsageError} from 'clipanion';
import {createVerify} from 'crypto';
import {UsageError} from 'clipanion';
import {createVerify} from 'crypto';

import defaultConfig from '../config.json';
import defaultConfig from '../config.json';

import * as httpUtils from './httpUtils';
import {shouldSkipIntegrityCheck} from './corepackUtils';
import * as httpUtils from './httpUtils';

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

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

if (process.env.COREPACK_INTEGRITY_KEYS !== ``) {
if (!shouldSkipIntegrityCheck()) {
verifySignature({
packageName, version,
integrity, signatures,
Expand Down
25 changes: 25 additions & 0 deletions tests/corepackUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {describe, it, expect} from '@jest/globals';

import {shouldSkipIntegrityCheck} from '../sources/corepackUtils';

describe(`corepack utils shouldSkipIntegrityCheck`, () => {
it(`should return false if COREPACK_INTEGRITY_KEYS env is not set`, () => {
delete process.env.COREPACK_INTEGRITY_KEYS;
expect(shouldSkipIntegrityCheck()).toBe(false);
});

it(`should return true if COREPACK_INTEGRITY_KEYS env is set to 0`, () => {
process.env.COREPACK_INTEGRITY_KEYS = `0`;
expect(shouldSkipIntegrityCheck()).toBe(true);
});

it(`should return true if COREPACK_INTEGRITY_KEYS env is set to an empty string`, () => {
process.env.COREPACK_INTEGRITY_KEYS = ``;
expect(shouldSkipIntegrityCheck()).toBe(true);
});

it(`should return false if COREPACK_INTEGRITY_KEYS env is set to any other value`, () => {
process.env.COREPACK_INTEGRITY_KEYS = JSON.stringify({foo: `bar`});
expect(shouldSkipIntegrityCheck()).toBe(false);
});
});