Skip to content

Commit 1e19fc0

Browse files
cklinCopilot
andcommitted
Check lockfile registry URLs before committing
Reject non-public registry URLs, credentials, and insecure schemes with a dependency-free Node script. Run the check after lockfile formatting in lint-staged and before installing dependencies in CI. Cover accepted and rejected URLs with focused tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0757eb7c-b8a7-4586-af2f-9fd2c88ffbc2
1 parent 835c78a commit 1e19fc0

4 files changed

Lines changed: 129 additions & 1 deletion

File tree

‎.github/workflows/main.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ jobs:
7575
cache: 'npm'
7676
cache-dependency-path: extensions/ql-vscode/package-lock.json
7777

78+
- name: Check lockfile URLs
79+
working-directory: extensions/ql-vscode
80+
run: npm run check:lockfile-urls
81+
7882
- name: Install dependencies
7983
working-directory: extensions/ql-vscode
8084
run: |

‎extensions/ql-vscode/package.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,7 @@
20632063
"generate": "npm-run-all -p generate:*",
20642064
"generate:schemas": "vite-node scripts/generate-schemas.ts",
20652065
"generate:chromium-version": "vite-node scripts/generate-chromium-version.ts",
2066+
"check:lockfile-urls": "node scripts/check-lockfile-urls.cjs",
20662067
"check-types": "find . -type f -name \"tsconfig.json\" -not -path \"./node_modules/*\" -not -path \"*/.vscode-test/*\" | sed -r 's|/[^/]+$||' | sort | uniq | xargs -I {} sh -c \"echo Checking types in {} && cd {} && npx tsc --noEmit\"",
20672068
"postinstall": "patch-package",
20682069
"prepare": "cd ../.. && husky"
@@ -2192,9 +2193,13 @@
21922193
}
21932194
},
21942195
"lint-staged": {
2195-
"./**/*.{json,css,scss}": [
2196+
"./**/!(package-lock).{json,css,scss}": [
21962197
"prettier --write"
21972198
],
2199+
"./**/package-lock.json": [
2200+
"prettier --write",
2201+
"node scripts/check-lockfile-urls.cjs"
2202+
],
21982203
"./**/*.{ts,tsx}": [
21992204
"prettier --write",
22002205
"eslint --fix"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const { readFileSync } = require("node:fs");
2+
const { resolve } = require("node:path");
3+
4+
const paths = process.argv.slice(2);
5+
if (paths.length === 0) {
6+
paths.push(resolve(__dirname, "../package-lock.json"));
7+
}
8+
9+
for (const path of paths) {
10+
const lockfile = JSON.parse(readFileSync(path, "utf8"));
11+
if (
12+
!lockfile.packages ||
13+
typeof lockfile.packages !== "object" ||
14+
Array.isArray(lockfile.packages)
15+
) {
16+
throw new Error(`${path}: expected a lockfile with a packages object`);
17+
}
18+
19+
for (const [name, entry] of Object.entries(lockfile.packages)) {
20+
if (!Object.hasOwn(entry, "resolved")) {
21+
continue;
22+
}
23+
24+
let url;
25+
try {
26+
if (typeof entry.resolved !== "string") {
27+
throw new TypeError("Expected a URL string");
28+
}
29+
url = new URL(entry.resolved);
30+
} catch (error) {
31+
if (!(error instanceof TypeError)) {
32+
throw error;
33+
}
34+
console.error(`${path}: ${name}: invalid resolved URL`);
35+
process.exitCode = 1;
36+
continue;
37+
}
38+
39+
if (
40+
url.protocol !== "https:" ||
41+
url.host !== "registry.npmjs.org" ||
42+
url.username ||
43+
url.password
44+
) {
45+
console.error(
46+
`${path}: ${name}: resolved URL must use https://registry.npmjs.org without credentials (received ${url.protocol}//${url.host})`,
47+
);
48+
process.exitCode = 1;
49+
}
50+
}
51+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { spawnSync } from "child_process";
2+
import { writeFileSync } from "fs";
3+
import { resolve, join } from "path";
4+
import type { DirResult } from "tmp";
5+
import { dirSync } from "tmp-promise";
6+
7+
describe("lockfile URL check", () => {
8+
let directory: DirResult;
9+
const script = resolve(__dirname, "../../../scripts/check-lockfile-urls.cjs");
10+
11+
beforeEach(() => {
12+
directory = dirSync({ unsafeCleanup: true });
13+
});
14+
15+
afterEach(() => {
16+
directory.removeCallback();
17+
});
18+
19+
function check(packages: Record<string, unknown>) {
20+
const path = join(directory.name, "package-lock.json");
21+
writeFileSync(path, JSON.stringify({ lockfileVersion: 3, packages }));
22+
return spawnSync(process.execPath, [script, path], { encoding: "utf8" });
23+
}
24+
25+
it("accepts public registry URLs and entries without resolved URLs", () => {
26+
const result = check({
27+
"": { name: "test" },
28+
"node_modules/example": {
29+
resolved: "https://registry.npmjs.org/example/-/example-1.0.0.tgz",
30+
},
31+
"node_modules/@scope/example": {
32+
resolved:
33+
"https://registry.npmjs.org/@scope/example/-/example-1.0.0.tgz",
34+
},
35+
});
36+
37+
expect(result.status).toBe(0);
38+
expect(result.stderr).toBe("");
39+
});
40+
41+
it.each([
42+
"https://pkgs.dev.azure.com/example/package.tgz",
43+
"https://registry.npmjs.org.example.com/package.tgz",
44+
"http://registry.npmjs.org/package.tgz",
45+
"https://registry.npmjs.org:8443/package.tgz",
46+
"file:../example",
47+
"not-a-url",
48+
null,
49+
])("rejects an invalid download location: %s", (resolved) => {
50+
const result = check({ "node_modules/example": { resolved } });
51+
52+
expect(result.status).toBe(1);
53+
expect(result.stderr).toContain("node_modules/example");
54+
});
55+
56+
it("rejects credentials without exposing them in diagnostics", () => {
57+
const result = check({
58+
"node_modules/example": {
59+
resolved:
60+
"https://user:example-password@registry.npmjs.org/package.tgz",
61+
},
62+
});
63+
64+
expect(result.status).toBe(1);
65+
expect(result.stderr).toContain("without credentials");
66+
expect(result.stderr).not.toContain("example-password");
67+
});
68+
});

0 commit comments

Comments
 (0)