Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions test/node-tar-dockerfile-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
{
file: "agents/pi/Dockerfile.base",
installsPatchDownloader: true,
installsWithNpm: false,
installsWithNpm: true,
patchCount: 2,
},
{
Expand All @@ -64,8 +64,18 @@
"Dockerfile.base",
"agents/hermes/Dockerfile.base",
"agents/langchain-deepagents-code/Dockerfile.base",
"agents/pi/Dockerfile.base",
] as const;
const reviewedNodeBases = new Set<string>(NODE_BASES_REQUIRING_BUNDLED_NPM_TAR_PATCH);
const npmConsumerPattern =
/\bnpm\s+(?:--?[\w-]+(?:=\S+)?\s+(?:\S+\s+)?)*(?:ci|install)\b/gu;
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed

function npmConsumerPositions(source: string): number[] {
const executableSource = source.replace(/^\s*#.*$/gmu, (comment) =>
" ".repeat(comment.length),
);
return [...executableSource.matchAll(npmConsumerPattern)].map((match) => match.index);
}

function nodeBaseReferences(source: string): string[] {
return [
Expand Down Expand Up @@ -207,12 +217,7 @@
aptInstallCleanup < firstPatchRun,
file,
).toBe(installsPatchDownloader);
const executableSource = source.replace(/^\s*#.*$/gmu, (comment) =>
" ".repeat(comment.length),
);
const npmConsumers = [...executableSource.matchAll(/\bnpm\s+(?:ci|install)\b/gu)].map(
(match) => match.index,
);
const npmConsumers = npmConsumerPositions(source);
expect(npmConsumers.length > 0, file).toBe(installsWithNpm);
expect(
npmConsumers.every((index) => index > lastPatchRun),
Expand All @@ -227,7 +232,7 @@
{ file: "Dockerfile.base", installsWithNpm: true },
{ file: "agents/hermes/Dockerfile.base", installsWithNpm: true },
{ file: "agents/langchain-deepagents-code/Dockerfile.base", installsWithNpm: false },
{ file: "agents/pi/Dockerfile.base", installsWithNpm: false },
{ file: "agents/pi/Dockerfile.base", installsWithNpm: true },
])(
"patches tar before and after upgrading the complete npm tree in $file",
({ file, installsWithNpm }) => {
Expand All @@ -252,12 +257,7 @@
expect(upgradeRun, file).toBeGreaterThan(patchRuns[0]!.commandStart);
expect(patchRuns[1]!.commandStart, file).toBeGreaterThan(upgradeRun);

const executableSource = source.replace(/^\s*#.*$/gmu, (comment) =>
" ".repeat(comment.length),
);
const npmConsumers = [...executableSource.matchAll(/\bnpm\s+(?:ci|install)\b/gu)].map(
(match) => match.index,
);
const npmConsumers = npmConsumerPositions(source);
expect(npmConsumers.length > 0, file).toBe(installsWithNpm);
expect(
npmConsumers.every((index) => index > patchRuns[1]!.commandStart),
Expand Down
26 changes: 26 additions & 0 deletions test/patch-bundled-npm-tar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ describe("npm bundled node-tar remediation", () => {
expect(commands).toEqual(["curl", "tar", "npm", "npx", "cleanup"]);
});

it("rejects mismatched tar@7.5.21 archive bytes before extraction or npm-tree mutation (#9933)", () => {
const target = fixture("11.18.0", "7.5.19");
const commands: string[] = [];

expect(() =>
patchBundledNpmTarFromRegistry(target.npmRoot, {
commandRunner(command, args) {
commands.push(command);
expect(command).toBe("curl");
expect(args).toContain(FIXED_TAR_TARBALL);
const outputIndex = args.indexOf("--output");
expect(outputIndex).toBeGreaterThanOrEqual(0);
fs.writeFileSync(args[outputIndex + 1]!, "mismatched archive bytes\n");
},
}),
).toThrow("npm bundled tar replacement integrity mismatch");

expect(commands).toEqual(["curl"]);
expect(fs.existsSync(path.join(target.npmRoot, "node_modules", "tar", "old.js"))).toBe(true);
expect(
fs.existsSync(path.join(target.npmRoot, "node_modules", "tar", "lib", "fixed.js")),
).toBe(false);
expect(fs.readdirSync(path.join(target.npmRoot, "node_modules"))).toEqual(["tar"]);
expect(() => verifyBundledNpmTar(target.npmRoot)).toThrow("bundles affected tar@7.5.19");
});

it("is idempotent when npm already bundles a safe release", () => {
const target = fixture("10.9.7", FIXED_TAR_VERSION);
expect(patchBundledNpmTar(target)).toMatchObject({ state: "fixed" });
Expand Down
Loading