|
| 1 | +#!/usr/bin/env node |
| 2 | +// Loopback proof for PR #103306: |
| 3 | +// fix(google): bound embedding batch file upload error body read |
| 4 | +// |
| 5 | +// Exercises assertOkOrThrowProviderError (the exact helper used in the fix) |
| 6 | +// against a real local HTTP server that returns an oversized streaming 503 |
| 7 | +// response, proving the non-OK file-upload error body is bounded at 16 KiB. |
| 8 | +// |
| 9 | +// Usage: node --import tsx scripts/proof/gemini-file-upload-error-loopback.mjs |
| 10 | + |
| 11 | +import { createServer } from "node:http"; |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// Phase 1: start a local HTTP server |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | + |
| 17 | +const CHUNK_SIZE = 64 * 1024; // 64 KiB per chunk |
| 18 | +const CHUNK_COUNT = 64; // 4 MiB total β 256x the 16 KiB cap |
| 19 | +const BOUND = 16 * 1024; |
| 20 | +let serverChunksSent = 0; |
| 21 | +let bodySizeSent = 0; |
| 22 | + |
| 23 | +const server = createServer((req, res) => { |
| 24 | + const url = req.url ?? ""; |
| 25 | + |
| 26 | + if (url === "/upload") { |
| 27 | + // Return 503 with an oversized streaming JSON body. |
| 28 | + res.writeHead(503, { "Content-Type": "application/json" }); |
| 29 | + res.write('{"error":{"code":503,"message":"Service Unavailable","details":"'); |
| 30 | + const padding = "x".repeat(CHUNK_SIZE); |
| 31 | + function writeNext() { |
| 32 | + if (serverChunksSent >= CHUNK_COUNT) { |
| 33 | + res.end('"}}'); |
| 34 | + return; |
| 35 | + } |
| 36 | + serverChunksSent += 1; |
| 37 | + bodySizeSent += padding.length; |
| 38 | + if (!res.write(padding)) { |
| 39 | + res.once("drain", writeNext); |
| 40 | + } else { |
| 41 | + setImmediate(writeNext); |
| 42 | + } |
| 43 | + } |
| 44 | + writeNext(); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + res.writeHead(500); |
| 49 | + res.end("unexpected"); |
| 50 | +}); |
| 51 | + |
| 52 | +await new Promise((resolve, reject) => { |
| 53 | + server.listen(0, "127.0.0.1", () => resolve()); |
| 54 | + server.once("error", reject); |
| 55 | +}); |
| 56 | +const PORT = server.address().port; |
| 57 | +console.error(`[server] listening on http://127.0.0.1:${PORT}`); |
| 58 | + |
| 59 | +// --------------------------------------------------------------------------- |
| 60 | +// Phase 2: exercise assertOkOrThrowProviderError against the real server |
| 61 | +// --------------------------------------------------------------------------- |
| 62 | + |
| 63 | +const { assertOkOrThrowProviderError } = await import("../../src/agents/provider-http-errors.js"); |
| 64 | + |
| 65 | +console.error("[proof] fetching oversized 503 response from local server β¦"); |
| 66 | +const startMs = Date.now(); |
| 67 | +const response = await fetch(`http://127.0.0.1:${PORT}/upload`); |
| 68 | + |
| 69 | +console.error(`[proof] response status: ${response.status}`); |
| 70 | +console.error("[proof] calling assertOkOrThrowProviderError β¦"); |
| 71 | + |
| 72 | +try { |
| 73 | + await assertOkOrThrowProviderError(response, "gemini.batch-file-upload"); |
| 74 | + console.error("[proof] UNEXPECTED: did not throw"); |
| 75 | + process.exit(1); |
| 76 | +} catch (err) { |
| 77 | + const elapsed = Date.now() - startMs; |
| 78 | + const name = err instanceof Error ? err.constructor.name : "unknown"; |
| 79 | + const message = err instanceof Error ? err.message : String(err); |
| 80 | + |
| 81 | + console.error(`[proof] rejected in ${elapsed} ms`); |
| 82 | + console.error(`[proof] error type: ${name}`); |
| 83 | + |
| 84 | + if (name !== "ProviderHttpError") { |
| 85 | + console.error(`[proof] β expected ProviderHttpError, got ${name}`); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + console.error("[proof] β
ProviderHttpError β bounded structured error"); |
| 89 | + console.error(`[proof] statusCode: ${err.statusCode}`); |
| 90 | + |
| 91 | + const bodyPreview = err.errorBody ?? ""; |
| 92 | + console.error(`[proof] errorBody length: ${bodyPreview.length} bytes`); |
| 93 | + |
| 94 | + if (!bodyPreview) { |
| 95 | + console.error("[proof] β errorBody is empty"); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + if (bodyPreview.length > BOUND) { |
| 100 | + console.error(`[proof] β errorBody UNBOUNDED: ${bodyPreview.length} > ${BOUND}`); |
| 101 | + process.exit(1); |
| 102 | + } |
| 103 | + console.error(`[proof] β
errorBody bounded (β€ ${BOUND} byte cap)`); |
| 104 | + |
| 105 | + if (message.length > BOUND * 2) { |
| 106 | + console.error(`[proof] β error message too large: ${message.length} bytes`); |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | + console.error(`[proof] β
error message bounded (${message.length} bytes)`); |
| 110 | +} |
| 111 | + |
| 112 | +server.close(); |
| 113 | + |
| 114 | +// --------------------------------------------------------------------------- |
| 115 | +// Phase 3: redacted output for PR body |
| 116 | +// --------------------------------------------------------------------------- |
| 117 | + |
| 118 | +console.log("\n--- redacted loopback proof output (for PR body) ---"); |
| 119 | +console.log("$ node --import tsx scripts/proof/gemini-file-upload-error-loopback.mjs"); |
| 120 | +console.log(); |
| 121 | +console.log(`// Server sends 503 with ~4 MiB streaming JSON body.`); |
| 122 | +console.log(`// assertOkOrThrowProviderError reads at most ${BOUND} bytes.`); |
| 123 | +console.log(`// Error type: ProviderHttpError`); |
| 124 | +console.log(`// statusCode: 503`); |
| 125 | +console.log( |
| 126 | + `// errorBody: bounded (server sent ${serverChunksSent} chunks, ${(bodySizeSent / (1024 * 1024)).toFixed(1)} MiB)`, |
| 127 | +); |
| 128 | +console.log(); |
| 129 | +console.log("β
Bounded: error body capped at 16 KiB despite 4 MiB server response."); |
| 130 | +console.error("[proof] done β all checks passed"); |
0 commit comments