Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
67 changes: 53 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 @@ const dockerfiles = [
{
file: "agents/pi/Dockerfile.base",
installsPatchDownloader: true,
installsWithNpm: false,
installsWithNpm: true,
patchCount: 2,
},
{
Expand All @@ -64,9 +64,58 @@ const pinnedBaseDockerfiles = [
"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);

interface ShellToken {
end: number;
value: string;
}

function isShellTokenBoundary(character: string): boolean {
return character === " " || character === "\t" || character === "\r" || character === "\n";
}

function readShellToken(source: string, start: number): ShellToken | undefined {
let cursor = start;
while (cursor < source.length && isShellTokenBoundary(source[cursor]!)) cursor += 1;
const tokenStart = cursor;
while (
cursor < source.length &&
!isShellTokenBoundary(source[cursor]!) &&
!";&|".includes(source[cursor]!)
) {
cursor += 1;
}
const value = source.slice(tokenStart, cursor);
return value.length === 0 ? undefined : { end: cursor, value };
}

function npmSubcommand(source: string, start: number): ShellToken | undefined {
const token = readShellToken(source, start);
const prefix = token?.value === "--prefix" ? readShellToken(source, token.end) : undefined;
return token?.value === "--prefix"
? prefix === undefined
? undefined
: readShellToken(source, prefix.end)
: token?.value.startsWith("--prefix=") === true
? readShellToken(source, token.end)
: token;
}

function npmConsumerPositions(source: string): number[] {
const executableSource = source
.replace(/^\s*#.*$/gmu, (comment) => " ".repeat(comment.length))
.replace(/\\\s*\n/gu, (continuation) => " ".repeat(continuation.length));
return [...executableSource.matchAll(/\bnpm\b/gu)]
.filter((match) => {
const subcommand = npmSubcommand(executableSource, match.index + match[0].length);
return subcommand?.value === "ci" || subcommand?.value === "install";
})
.map((match) => match.index);
Comment thread
apurvvkumaria marked this conversation as resolved.
Resolved
}

function nodeBaseReferences(source: string): string[] {
return [
...new Set(
Expand Down Expand Up @@ -207,12 +256,7 @@ describe("node-tar image remediation contract", () => {
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 +271,7 @@ describe("reviewed npm image remediation contract", () => {
{ 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 +296,7 @@ describe("reviewed npm image remediation contract", () => {
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