From 1f72d72a0046101337f26bacbe8d34fc4f4e47b7 Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Tue, 3 Nov 2026 07:55:50 -0600 Subject: [PATCH] fix(antigravity-direct): parse PORT and TIMEOUT_MS through the house helpers (#2823) Both env vars were read with bare Number(): 'abc'/'18941.9'/'99999' reached listen() as an anonymous ERR_SOCKET_BAD_PORT, '0' bound an ephemeral port while endpoint.json advertised :0, and '0x4a35' bound 18997. A bad timeout started healthy and 502'd every request (or overflowed a 32-bit timer silently). ANTIGRAVITY_DIRECT_PORT now goes through parseProxyPort and ANTIGRAVITY_DIRECT_TIMEOUT_MS through parseProxyMillis, the same sibling helpers all ten other bin/* proxies use, so an invalid value exits at startup naming the variable and its value. Defaults are unchanged (18751 / 300000). Tests extend bin/antigravity-direct.test.mjs: the six bad-port and three bad-timeout shapes are rejected with no Node stack, and a valid port with a 300ms timeout still serves and times a hanging upstream out at ~300ms. --- bin/antigravity-direct.mjs | 10 +++- bin/antigravity-direct.test.mjs | 95 +++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/bin/antigravity-direct.mjs b/bin/antigravity-direct.mjs index c1da805495..4d27b4af55 100755 --- a/bin/antigravity-direct.mjs +++ b/bin/antigravity-direct.mjs @@ -55,11 +55,17 @@ import path from 'node:path'; import os from 'node:os'; import crypto from 'node:crypto'; import zlib from 'node:zlib'; +import { parseProxyPort, parseProxyMillis } from './proxy-port.mjs'; const HOST = process.env.ANTIGRAVITY_DIRECT_HOST || '127.0.0.1'; -const PORT = Number(process.env.ANTIGRAVITY_DIRECT_PORT || 18751); +// #2823: both env values go through the house helpers, like every other +// bin/* proxy. A bare Number() let 'abc'/'18941.9'/'99999' reach listen() +// as an anonymous ERR_SOCKET_BAD_PORT, and let '0'/'0x4a35' bind a port +// nobody configured; a bad timeout started healthy and 502'd every +// request. Both now fail at startup naming the variable and its value. +const PORT = parseProxyPort('ANTIGRAVITY_DIRECT_PORT', 18751); const UPSTREAM = (process.env.ANTIGRAVITY_DIRECT_UPSTREAM || 'https://daily-cloudcode-pa.googleapis.com').replace(/\/$/, ''); -const TIMEOUT_MS = Number(process.env.ANTIGRAVITY_DIRECT_TIMEOUT_MS || 300000); +const TIMEOUT_MS = parseProxyMillis('ANTIGRAVITY_DIRECT_TIMEOUT_MS', 300000); const PROJECT = 'aicode-consumers'; // The CLI's OAuth client id (public identifier, appears in its sign-in URLs). diff --git a/bin/antigravity-direct.test.mjs b/bin/antigravity-direct.test.mjs index 2d85a7fe62..172252f1fa 100644 --- a/bin/antigravity-direct.test.mjs +++ b/bin/antigravity-direct.test.mjs @@ -170,3 +170,98 @@ test('specific-function tool_choice with no declared tools is a 400', async () = assert.equal(body.error.type, 'invalid_request_error'); assert.equal(generateCalls().length, beforeCount); }); + +// --- #2823: env parsing is strict ----------------------------------------- +// A bad ANTIGRAVITY_DIRECT_PORT used to reach listen() as an anonymous +// ERR_SOCKET_BAD_PORT (or bind 0 / a hex-decoded port), and a bad +// ANTIGRAVITY_DIRECT_TIMEOUT_MS started healthy then 502'd every +// request. Both must now fail at startup, naming the variable and value. + +const stubSecretTool = () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'antigravity-direct-env-')); + const file = path.join(dir, 'secret-tool'); + const expiry = new Date(Date.now() + 3600e3).toISOString(); + fs.writeFileSync(file, '#!/usr/bin/env node\nconsole.log(JSON.stringify({ token: { access_token: "stub-access-token", refresh_token: "stub-refresh-token", expiry: ' + JSON.stringify(expiry) + ' }, auth_method: "consumer" }));\n'); + fs.chmodSync(file, 0o755); + return { dir, file }; +}; + +const spawnProxy = (env) => { + const stub = stubSecretTool(); + const child = spawn(process.execPath, [path.join(binDir, 'antigravity-direct.mjs')], { + env: { + ...process.env, + ANTIGRAVITY_DIRECT_HOST: '127.0.0.1', + ANTIGRAVITY_DIRECT_UPSTREAM: 'http://127.0.0.1:1', + SECRET_TOOL_PATH: stub.file, + XDG_STATE_HOME: stub.dir, + ...env + }, + stdio: ['ignore', 'ignore', 'pipe'] + }); + let stderr = ''; + child.stderr.on('data', (c) => { stderr += c; }); + const exited = new Promise((resolve) => child.on('exit', (code, signal) => resolve({ code, signal, stderr }))); + const cleanup = () => fs.rmSync(stub.dir, { recursive: true, force: true }); + return { child, exited, cleanup }; +}; + +const rejectedCase = (name, raw, pattern) => { + test(`${name}=${raw} is rejected at startup naming the variable`, async () => { + const { child, exited, cleanup } = spawnProxy({ [name]: raw }); + const timer = setTimeout(() => child.kill('SIGKILL'), 15000); + const { code, stderr } = await exited; + clearTimeout(timer); + cleanup(); + assert.equal(code, 1, `expected exit 1, got ${code}; stderr=${stderr}`); + assert.match(stderr, new RegExp(`${name}="${raw}"`.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))); + assert.match(stderr, pattern); + // A Node listen/timer error would name none of the above. + assert.doesNotMatch(stderr, /ERR_SOCKET_BAD_PORT|RangeError|TimeoutOverflowWarning/); + }); +}; + +for (const raw of ['abc', '18941abc', '18941.9', '0', '99999', '0x4a35']) { + rejectedCase('ANTIGRAVITY_DIRECT_PORT', raw, /not a valid port \(expected an integer 1-65535\)/); +} +for (const raw of ['abc', '-5000', '3000000000']) { + rejectedCase('ANTIGRAVITY_DIRECT_TIMEOUT_MS', raw, /not a valid timeout \(expected an integer 1-2147483647 ms\)/); +} + +test('valid port with a 300ms timeout serves and times a hanging upstream out', async () => { + const hanging = http.createServer(() => {}); // accepts, never responds + const hangingPort = await freePort(); + await new Promise(r => hanging.listen(hangingPort, '127.0.0.1', r)); + const port = await freePort(); + const { child, cleanup } = spawnProxy({ + ANTIGRAVITY_DIRECT_PORT: String(port), + ANTIGRAVITY_DIRECT_TIMEOUT_MS: '300', + ANTIGRAVITY_DIRECT_UPSTREAM: 'http://127.0.0.1:' + hangingPort + }); + try { + const deadline = Date.now() + 30000; + for (;;) { + try { + const r = await fetch('http://127.0.0.1:' + port + '/healthz'); + if (r.ok) break; + } catch {} + if (Date.now() > deadline) throw new Error('proxy did not come up'); + await new Promise(r => setTimeout(r, 100)); + } + const started = Date.now(); + const res = await fetch('http://127.0.0.1:' + port + '/v1/chat/completions', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model: 'gemini-3.8-flash', messages: [{ role: 'user', content: 'hi' }] }) + }); + const body = await res.json(); + const elapsed = Date.now() - started; + assert.equal(res.status, 502); + assert.match(JSON.stringify(body), /upstream timeout after 300ms/); + assert.ok(elapsed >= 250 && elapsed < 5000, `expected ~300ms timeout, took ${elapsed}ms`); + } finally { + child.kill('SIGKILL'); + hanging.close(); + cleanup(); + } +});