Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion packages/cli/src/lib/init/workflow-inputs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { isLikelyBinary } from "../scan/index.js";
import { MAX_FILE_BYTES } from "./constants.js";
import { detectSentry } from "./tools/detect-sentry.js";
import { listDir } from "./tools/list-dir.js";
Expand Down Expand Up @@ -131,7 +132,12 @@ export async function preReadCommonFiles(
if (stat.size > MAX_FILE_BYTES) {
continue;
}
const content = await fs.promises.readFile(absPath, "utf-8");
const buffer = await fs.promises.readFile(absPath);
if (isLikelyBinary(buffer)) {
cache[filePath] = null;
continue;
}
const content = buffer.toString("utf-8");
if (totalBytes + content.length <= MAX_PREREAD_TOTAL_BYTES) {
cache[filePath] = content;
totalBytes += content.length;
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/test/lib/init/tools/filesystem-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ describe("filesystem tools", () => {
expect((existsResult.data as any).exists["missing.txt"]).toBe(false);
});

test("reads binary content when the workflow explicitly requests it", async () => {
const content = Buffer.from([0x7f, 0x45, 0x4c, 0x46, 0x02, 0x00, 0xff]);
fs.writeFileSync(path.join(testDir, "sentry-wizard"), content);

const result = await executeTool(
{
type: "tool",
operation: "read-files",
cwd: testDir,
params: { paths: ["sentry-wizard"] },
},
makeContext(testDir)
);

expect(result.ok).toBe(true);
expect((result.data as any).files["sentry-wizard"]).toBe(
content.toString("utf-8")
);
});

test("applies patchsets and injects auth tokens into env files", async () => {
const result = await executeTool(
{
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/test/lib/safe-read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,18 @@ describe("workflow-inputs preReadCommonFiles FIFO safety", () => {
expect(cache["package.json"]).toBe('{"name":"x"}');
expect(cache["tsconfig.json"]).toBeNull();
});

test("does not pre-read binary content disguised as a common config", async () => {
writeFileSync(
join(dir, "package.json"),
Buffer.from([0x7f, 0x45, 0x4c, 0x46, 0x02, 0x00, 0xff])
);

const listing: DirEntry[] = [
{ name: "package.json", path: "package.json", type: "file" },
];
const cache = await preReadCommonFiles(dir, listing);

expect(cache["package.json"]).toBeNull();
});
});
Loading