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