From 62af176273bf30095e9d5b8afa4a2bca1c3b1bc5 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 15:19:47 -0600 Subject: [PATCH] fix(design-code-proxy): parse backend timeout via parseProxyMillis (#4225) --- bin/design-code-proxy.mjs | 13 +++++--- bin/design-code-proxy.test.mjs | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/bin/design-code-proxy.mjs b/bin/design-code-proxy.mjs index b0f387d2b1..05e6ddf0e7 100755 --- a/bin/design-code-proxy.mjs +++ b/bin/design-code-proxy.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import http from 'node:http'; import https from 'node:https'; -import { parseProxyPort } from './proxy-port.mjs'; +import { parseProxyMillis, parseProxyPort } from './proxy-port.mjs'; import { createRequestBodyCollector } from './request-body.mjs'; import { createStreamDecoder } from './stream-decode.mjs'; @@ -31,10 +31,13 @@ const httpsAgent = new https.Agent({ keepAlive: true, keepAliveMsecs: 1000, maxS // slow-but-alive streams are never killed. Pinned at 60s — generous for a // slow first byte from a local language-server backend — and overridable // via DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS (tests pin it short). -const BACKEND_TIMEOUT_MS = (() => { - const raw = Number(process.env.DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS); - return Number.isFinite(raw) && raw > 0 ? Math.floor(raw) : 60_000; -})(); +// Strict parse via the house helper (#4225): the previous hand-rolled +// Number() guard admitted values above 2^31-1, which Node's timer layer +// silently truncates to 2147483647 ms (~24.8 days) — defeating this +// timeout entirely — and non-decimal spellings like 0x10 (16 ms). +// parseProxyMillis refuses anything outside integer 1..2147483647 at +// startup, naming the variable; unset/empty keeps the 60s default. +const BACKEND_TIMEOUT_MS = parseProxyMillis('DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS', 60_000); const DESIGN_SYSTEM_PROMPT = `You are an elite Code & Systems Design Architect. Your core expertise is in software architecture, clean code design, UI/UX design engineering, design tokens, component abstractions, domain-driven design, and maintainable systems patterns. Provide elegant, production-grade architectures and idiomatic design implementations.`; diff --git a/bin/design-code-proxy.test.mjs b/bin/design-code-proxy.test.mjs index 53d0b06523..e792884ea0 100644 --- a/bin/design-code-proxy.test.mjs +++ b/bin/design-code-proxy.test.mjs @@ -444,3 +444,62 @@ test('a primary that stalls mid-stream has its client stream torn down by the ti assert.equal(await Promise.race([read, bounded]), 'torn-down'); assert.equal(fallback.calls, 0); }); + +// #4225: DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS goes through the strict +// parseProxyMillis helper. Overflow and non-decimal spellings must +// refuse to start (naming the variable) instead of silently truncating +// to ~24.8 days or 16 ms; a valid value is honoured (the #2097 tests +// above already pin short values through startProxy). +const spawnProxyRaw = async (extraEnv) => { + const port = await freePort(); + const env = { ...process.env }; + delete env.OPEN_DESIGN_API_KEY; + delete env.AMR_API_KEY; + const child = spawn(process.execPath, [path.join(binDir, 'design-code-proxy.mjs')], { + env: { ...env, DESIGN_CODE_PROXY_PORT: String(port), ...extraEnv }, + stdio: ['ignore', 'pipe', 'pipe'] + }); + let log = ''; + child.stdout.on('data', d => { log += d; }); + child.stderr.on('data', d => { log += d; }); + return { child, port, log: () => log }; +}; + +const waitForExit = (child, ms = 5000) => new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(new Error('proxy did not exit in time')), ms); + child.on('exit', code => { clearTimeout(timer); resolve(code); }); +}); + +test('a backend timeout above 2^31-1 refuses to start, naming the variable (#4225)', async () => { + const { child, log } = await spawnProxyRaw({ DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS: '2147483648' }); + const code = await waitForExit(child); + assert.notEqual(code, 0); + assert.match(log(), /DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS/); + assert.match(log(), /2147483648/); +}); + +test('non-decimal backend timeout spellings refuse to start (#4225)', async () => { + for (const bad of ['0x10', '1e5']) { + const { child, log } = await spawnProxyRaw({ DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS: bad }); + const code = await waitForExit(child); + assert.notEqual(code, 0, `${bad} should refuse to start`); + assert.match(log(), /DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS/); + } +}); + +test('a valid backend timeout value is honoured (#4225)', async () => { + const { child, port } = await spawnProxyRaw({ DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS: '5000' }); + try { + const deadline = Date.now() + 10000; + for (;;) { + try { + const res = await fetch(`http://127.0.0.1:${port}/health`); + if (res.ok) break; + } catch { /* not up yet */ } + if (Date.now() > deadline) throw new Error('proxy with a valid timeout did not start'); + await new Promise(r => setTimeout(r, 50)); + } + } finally { + child.kill('SIGKILL'); + } +});