Skip to content

A spy satellite simulator in your browser, except the data is real. Live open source spatial intelligence on a photorealistic 3D globe.

Merge origin/main into fix/realtime-debug-log-hardening

Sameh Khamisa948274326e8
diff --cc CHANGELOG.md
index 9a1ada795,99a421a5e..da600f5b0
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@@ -58,8 -133,8 +133,10 @@@
- Expose portable radio, camera-type and regional source helpers; keep HTTP transport separate from record normalization.
+- The Realtime debug-log endpoint is bounded on every axis it was not: an always-on per-client rate limit, asynchronous appends through a serialized queue instead of a synchronous write on the request path, and rotation of the log file at 32 MB keeping one prior generation. The 8 MB cap applied to a single request body and never to the file those requests accumulated into, so any local page could grow it for as long as the dev server ran. A malformed record and a failed write are now told apart, 400 from 500, and neither answer carries the error text.
+
+ - Three proxy paths no longer relay upstream or JS error text to the client. The HUD summary passed OpenAI's own `error.message` through whenever upstream was not ok, carrying request ids and quota wording; the Realtime token route passed through non-success response bodies and echoed JS errors, which can expose upstream details; and a failed CCTV media fetch stored the raw errno as the camera's health message, which reaches the screen through `GET /api/cctv/health` rather than through the sanitized response beside it. Logs now name the failure and the upstream status without the text.
+
- Separate vessel records and feed acquisition from rendering while preserving selection, partial-feed retention, sea-surface placement and request cancellation.
- Separate military-flight records and acquisition from rendering while preserving ground-model ownership, source units and follow behavior.
diff --cc src/tooling/localServices.test.mjs
index 3ed1ef504,ce9d000b6..7073957ae
--- a/src/tooling/localServices.test.mjs
+++ b/src/tooling/localServices.test.mjs
@@@ -284,78 -285,55 +285,131 @@@ test('Realtime service configuration se
assert.doesNotMatch(response.body, /server-fixture|voice\.example/);
});
+ test('OpenAI routes answer generically when the upstream or the request fails', async (t) => {
+ env(t, 'OPENAI_API_KEY', 'fixture-upstream-secret');
+ env(t, 'GEV_RATELIMIT_OPENAI_PER_MIN', undefined);
+ const leak =
+ 'fixture-upstream-secret req_fixture_1234 org-fixture quota exhausted';
+
+ // `data.error.message` is OpenAI's own wording — request ids, organization
+ // hints, quota phrasing — and was relayed verbatim whenever upstream was not ok.
+ t.mock.method(globalThis, 'fetch', async () =>
+ Response.json({ error: { message: leak } }, { status: 429 }),
+ );
+ const summary = await request(
+ install(openAiRealtimeProxy()).get('/api/openai/hud-summary'),
+ { method: 'POST', body: JSON.stringify({ context: {} }) },
+ );
+ assert.equal(summary.json().error, 'OpenAI HUD summary request failed');
+ assert.equal(summary.body.includes('req_fixture_1234'), false);
+ assert.equal(summary.body.includes('fixture-upstream-secret'), false);
+
+ // An HTTP error from the client-secret endpoint carries the same upstream
+ // detail, while a successful response must still pass the ephemeral secret.
+ t.mock.restoreAll();
+ t.mock.method(globalThis, 'fetch', async () =>
+ Response.json({ error: { message: leak } }, { status: 429 }),
+ );
+ const rejectedToken = await request(
+ install(openAiRealtimeProxy()).get('/api/realtime/token'),
+ );
+ assert.equal(rejectedToken.status, 429);
+ assert.equal(
+ rejectedToken.headers['content-type'],
+ 'application/json; charset=utf-8',
+ );
+ assert.deepEqual(rejectedToken.json(), {
+ error: 'Failed to create Realtime token',
+ });
+ assert.equal(rejectedToken.body.includes('req_fixture_1234'), false);
+ assert.equal(rejectedToken.body.includes('fixture-upstream-secret'), false);
+
+ // A network fault surfaced a resolver message naming the upstream host.
+ t.mock.restoreAll();
+ t.mock.method(globalThis, 'fetch', async () => {
+ throw Error(`getaddrinfo ENOTFOUND api.openai.com ${leak}`);
+ });
+ const token = await request(
+ install(openAiRealtimeProxy()).get('/api/realtime/token'),
+ );
+ assert.equal(token.status, 502);
+ assert.deepEqual(token.json(), { error: 'Failed to create Realtime token' });
+ assert.equal(token.body.includes('api.openai.com'), false);
+ assert.equal(token.body.includes('fixture-upstream-secret'), false);
+ });
++
+test('the debug-log sink stays bounded, rate limited, and quiet about failures', async (t) => {
+ const sourceRoot = root(t);
+ const handler = install(openAiRealtimeProxy({ sourceRoot })).get(
+ '/api/realtime/debug-log',
+ );
+ const file = path.join(sourceRoot, '.gev-logs/realtime-conversations.jsonl');
+ const write = (record) =>
+ request(handler, { method: 'POST', body: JSON.stringify(record) });
+
+ // A malformed record is the caller's fault and a 400; neither answer carries
+ // the error text, which for a write failure is an errno and an absolute path.
+ const malformed = await request(handler, { method: 'POST', body: '{nope' });
+ assert.equal(malformed.status, 400);
+ assert.deepEqual(malformed.json(), {
+ error: 'Failed to write Realtime debug log',
+ });
+
+ // The limiter is always on, unlike the opt-in one the cost-bearing routes
+ // share: 120/min per IP, far above what a voice session writes.
+ let limited = null;
+ let accepted = 0;
+ for (let n = 0; n < 130 && !limited; n += 1) {
+ const response = await write({ n });
+ if (response.status === 429) limited = response;
+ else if (response.status === 204) accepted += 1;
+ }
+ assert.ok(limited, 'the sink refuses a caller past its per-minute ceiling');
+ assert.equal(limited.headers['retry-after'], '5');
+ assert.deepEqual(limited.json(), { error: 'Rate limit exceeded' });
+ // The limiter counts requests rather than successful writes, so the malformed
+ // record above already spent one of the 120 slots.
+ assert.equal(accepted, 119);
+
+ // Every accepted record is on disk and parses: the queue serializes appends,
+ // so none was lost or truncated by the ones beside it.
+ const lines = readFileSync(file, 'utf8').split('\n').filter(Boolean);
+ assert.equal(lines.length, accepted);
+ for (const line of lines) assert.doesNotThrow(() => JSON.parse(line));
+ assert.ok(lines.every((line) => JSON.parse(line).loggedAt));
+});
+
+test('the debug log rotates instead of growing without bound', async (t) => {
+ const sourceRoot = root(t);
+ const handler = install(openAiRealtimeProxy({ sourceRoot })).get(
+ '/api/realtime/debug-log',
+ );
+ const file = path.join(sourceRoot, '.gev-logs/realtime-conversations.jsonl');
+
+ // 8 MB bounds one request body; nothing bounded the file until now, so a
+ // single page could grow it for as long as the dev server ran. Each record
+ // here is ~1 MB, well inside the body cap.
+ const pad = 'x'.repeat(1024 * 1024);
+ for (let n = 0; n < 40; n += 1) {
+ assert.equal(
+ (
+ await request(handler, {
+ method: 'POST',
+ body: JSON.stringify({ n, pad }),
+ })
+ ).status,
+ 204,
+ );
+ }
+
+ const live = statSync(file).size;
+ const previous = statSync(`${file}.1`).size;
+ assert.ok(live <= 32 * 1024 * 1024, `live log under the ceiling (${live})`);
+ assert.ok(
+ live + previous <= 64 * 1024 * 1024,
+ 'both generations together stay within twice the ceiling',
+ );
+ // Unrotated, these records would be ~42 MB in one file.
+ assert.ok(live + previous < 42 * 1024 * 1024);
+ assert.ok(!existsSync(`${file}.2`), 'exactly one generation is retained');
+});