Skip to content

Commit 86988e6

Browse files
Pick-catsteipete
authored andcommitted
fix(models): reject oversized piped auth input
readPipedStdin accumulated piped stdin without a size limit, letting `models auth paste-token` and `paste-api-key` buffer arbitrarily large input before any validation. Add a 1 MiB cap matching the existing Matrix recovery-key and config-patch stdin limits, and reject oversized input at the stream boundary. Co-Authored-By: nebulacoder-v8.0 <[email protected]>
1 parent 40e7447 commit 86988e6

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎src/commands/models/auth.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,21 @@ describe("modelsAuthLoginCommand", () => {
14731473
expect(mocks.updateConfig).not.toHaveBeenCalled();
14741474
});
14751475

1476+
it("rejects oversized piped auth input before buffering it", async () => {
1477+
const runtime = createRuntime();
1478+
restoreStdin?.();
1479+
const oversized = "x".repeat(1024 * 1024 + 1);
1480+
restoreStdin = withPipedStdin(oversized);
1481+
1482+
await expect(modelsAuthPasteApiKeyCommand({ provider: "openai" }, runtime)).rejects.toThrow(
1483+
"Piped auth input exceeds 1048576 bytes.",
1484+
);
1485+
1486+
expect(mocks.clackPassword).not.toHaveBeenCalled();
1487+
expect(mocks.upsertAuthProfileWithLock).not.toHaveBeenCalled();
1488+
expect(mocks.updateConfig).not.toHaveBeenCalled();
1489+
});
1490+
14761491
it("writes pasted API keys to the requested agent store", async () => {
14771492
const runtime = createRuntime();
14781493
useCoderAgentConfig();

‎src/commands/models/auth.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,20 @@ const password = async (params: Parameters<typeof clackPassword>[0]) =>
130130
const select = async <T>(params: Parameters<typeof clackSelect<T>>[0]) =>
131131
guardCancel(await clackSelect(styleSelectParams(params)));
132132

133+
const MODELS_AUTH_STDIN_MAX_BYTES = 1024 * 1024;
134+
133135
async function readPipedStdin(): Promise<string> {
134-
process.stdin.setEncoding("utf8");
135-
let input = "";
136+
const chunks: Buffer[] = [];
137+
let totalBytes = 0;
136138
for await (const chunk of process.stdin) {
137-
input += String(chunk);
139+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
140+
totalBytes += buffer.byteLength;
141+
if (totalBytes > MODELS_AUTH_STDIN_MAX_BYTES) {
142+
throw new Error(`Piped auth input exceeds ${MODELS_AUTH_STDIN_MAX_BYTES} bytes.`);
143+
}
144+
chunks.push(buffer);
138145
}
139-
return input;
146+
return Buffer.concat(chunks, totalBytes).toString("utf8");
140147
}
141148

142149
async function readPastedSecret(params: {

0 commit comments

Comments
 (0)