|
| 1 | +// Real behavior proof: OpenAI ChatGPT OAuth token response read is bounded. |
| 2 | +// |
| 3 | +// exchangeAuthorizationCode and refreshAccessToken previously buffered the entire |
| 4 | +// response body with response.json(). The fix uses readProviderJsonResponse with |
| 5 | +// a 1 MiB cap so a runaway token endpoint cannot OOM the CLI login flow. |
| 6 | +// |
| 7 | +// This proof starts a local HTTP server, feeds it an oversized JSON body, and |
| 8 | +// shows readProviderJsonResponse rejects before buffering the whole payload. |
| 9 | + |
| 10 | +import http from "node:http"; |
| 11 | +import { fileURLToPath } from "node:url"; |
| 12 | +import path from "node:path"; |
| 13 | + |
| 14 | +const repoRoot = path.dirname(path.dirname(path.dirname(fileURLToPath(import.meta.url)))); |
| 15 | +const { readProviderJsonResponse } = await import( |
| 16 | + path.join(repoRoot, "src/plugin-sdk/provider-http.js") |
| 17 | +); |
| 18 | + |
| 19 | +const PORT = 0; |
| 20 | +const HUGE_SIZE = 8 * 1024 * 1024; |
| 21 | +let sendOversized = false; |
| 22 | + |
| 23 | +const server = http.createServer((req, res) => { |
| 24 | + res.writeHead(200, { "Content-Type": "application/json" }); |
| 25 | + if (sendOversized) { |
| 26 | + res.write('{"access_token":"a","refresh_token":"r","expires_in":3600,"padding":"'); |
| 27 | + const chunk = "x".repeat(64 * 1024); |
| 28 | + let sent = 0; |
| 29 | + function sendChunk(): void { |
| 30 | + if (sent >= HUGE_SIZE) { |
| 31 | + res.end('"}'); |
| 32 | + return; |
| 33 | + } |
| 34 | + res.write(chunk); |
| 35 | + sent += chunk.length; |
| 36 | + setImmediate(sendChunk); |
| 37 | + } |
| 38 | + sendChunk(); |
| 39 | + } else { |
| 40 | + res.end(JSON.stringify({ access_token: "a", refresh_token: "r", expires_in: 3600 })); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +await new Promise<void>((resolve) => { |
| 45 | + server.listen(PORT, "127.0.0.1", () => { |
| 46 | + resolve(); |
| 47 | + }); |
| 48 | +}); |
| 49 | +const address = server.address(); |
| 50 | +const port = address && typeof address === "object" ? address.port : 0; |
| 51 | + |
| 52 | +console.log("=== Proof: OpenAI ChatGPT OAuth token JSON bound ===\n"); |
| 53 | +console.log(`Local token server listening on port ${port}\n`); |
| 54 | + |
| 55 | +// Normal path: small token JSON parses fine. |
| 56 | +const normalResp = await fetch(`http://127.0.0.1:${port}/token`); |
| 57 | +const normalData = await readProviderJsonResponse<{ |
| 58 | + access_token: string; |
| 59 | + refresh_token: string; |
| 60 | + expires_in: number; |
| 61 | +}>(normalResp, "OpenAI Codex token exchange", { maxBytes: 1024 * 1024 }); |
| 62 | +console.log(`Normal token response: access_token=${normalData.access_token.slice(0, 8)}...`); |
| 63 | + |
| 64 | +// Oversized path: bounded read rejects before buffering everything. |
| 65 | +sendOversized = true; |
| 66 | +const oversizedResp = await fetch(`http://127.0.0.1:${port}/token-oversized`); |
| 67 | +const startMem = process.memoryUsage.rss(); |
| 68 | +const start = performance.now(); |
| 69 | + |
| 70 | +try { |
| 71 | + await readProviderJsonResponse<Record<string, unknown>>( |
| 72 | + oversizedResp, |
| 73 | + "OpenAI Codex token exchange", |
| 74 | + { maxBytes: 1024 * 1024 }, |
| 75 | + ); |
| 76 | + console.log("\nFAIL: oversized token body should have been rejected."); |
| 77 | + process.exitCode = 1; |
| 78 | +} catch (err) { |
| 79 | + const duration = performance.now() - start; |
| 80 | + const endMem = process.memoryUsage.rss(); |
| 81 | + const message = err instanceof Error ? err.message : String(err); |
| 82 | + console.log(`\nOversized token body rejected: ${message}`); |
| 83 | + console.log(`Duration: ${duration.toFixed(1)} ms`); |
| 84 | + console.log(`RSS delta: ${((endMem - startMem) / 1024 / 1024).toFixed(1)} MB`); |
| 85 | + |
| 86 | + if (message.includes("JSON response exceeds") && duration < 5000 && endMem - startMem < 64 * 1024 * 1024) { |
| 87 | + console.log("\nPASS: OAuth token JSON read is bounded; oversized bodies fail fast without OOM."); |
| 88 | + } else { |
| 89 | + console.log("\nFAIL: did not fail fast or consumed too much memory."); |
| 90 | + process.exitCode = 1; |
| 91 | + } |
| 92 | +} finally { |
| 93 | + server.close(); |
| 94 | + await new Promise<void>((resolve) => { |
| 95 | + server.once("close", () => { |
| 96 | + resolve(); |
| 97 | + }); |
| 98 | + }); |
| 99 | +} |
0 commit comments