Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ Then check the routes above against `https://omni.syberlabs.io`.
address. To undo a bad release, `npx wrangler rollback` restores the prior
version.

### Logs

`wrangler.jsonc` turns on Workers Logs (`observability.enabled`) with request
logs off (`observability.logs.invocation_logs: false`). Cloudflare then keeps
no log per request, the entry that would carry its method, full URL and
request metadata, which can include the client address. From the app's own
code, Workers Logs keeps only the allowlisted event lines below, for 3 days
on the Workers Free plan and 7 on Paid. Cloudflare's docs do not say what
metadata it attaches to such a line, so check one from the deployed Worker
before counting on it carrying no URL or address.

The lines are JSON, one per event, written only through
`src/core/services/server/telemetry.ts`; a test fails on any other console
call under `src/app/api` or `src/core/services/server`.
`telemetry/events.json` names every event and every field an event may
carry. Error detail is the error's class name, never its message. Errors go
to `console.error`, the rest to `console.log`. vinext and React can still
write their own error lines, and the runtime still records an uncaught
exception; neither is on the allowlist.

Turning request logs on would keep full URLs and can keep client addresses.
Do it only after syberlabs.io's privacy page says the preview keeps them.

### Automatic deploys (off until enabled)

`.github/workflows/deploy-preview.yml` runs the steps above after CI passes
Expand Down
22 changes: 14 additions & 8 deletions src/app/api/polymarket/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import { GET } from './route';

const BODY_MARKER = 'UPSTREAM-BODY-MUST-NOT-BE-LOGGED';

let warn: ReturnType<typeof vi.spyOn>;
let log: ReturnType<typeof vi.spyOn>;

beforeEach(() => {
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
log = vi.spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});

/** The whole failure body, so a change to what the route answers shows up here. */
async function failureOf(response: Response) {
expect(response.status).toBe(502);
const body = await response.json();
expect(Object.keys(body).sort()).toEqual(['error', 'success']);
expect(body.success).toBe(false);
return body.error as string;
}
Expand All @@ -37,29 +39,32 @@ describe('GET /api/polymarket', () => {
expect(body.markets).toHaveLength(1);
expect(body.markets[0].outcomes[0]).toEqual({ id: 'mkt-1_0', name: 'Yes', probability: 0.65 });
expect(body.markets[0].volume).toBe(12345);
expect(log).not.toHaveBeenCalled();
});

it('reports an upstream 500 as a failure, not an empty success', async () => {
vi.stubGlobal('fetch', vi.fn(async () => new Response(BODY_MARKER, { status: 500 })));
expect(await failureOf(await GET())).toBe('upstream_status');
expect(warn).toHaveBeenCalledWith('[Polymarket API] upstream returned', 500);
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"polymarket","reason":"status","status":500}');
});

it('reports an unexpected shape as a failure and logs keys, never the body', async () => {
it('reports an unexpected shape as a failure and logs neither the body nor its keys', async () => {
vi.stubGlobal('fetch', vi.fn(async () => Response.json({ surprise: BODY_MARKER, nested: { secret: BODY_MARKER } })));
expect(await failureOf(await GET())).toBe('upstream_shape');
expect(warn).toHaveBeenCalledTimes(1);
const logged = JSON.stringify(warn.mock.calls);
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"polymarket","reason":"shape","status":200}');
const logged = JSON.stringify(log.mock.calls);
expect(logged).not.toContain(BODY_MARKER);
expect(logged).toContain('surprise');
expect(logged).not.toContain('surprise');
});

it('keeps the body out of the log when upstream returns non-JSON', async () => {
vi.stubGlobal('fetch', vi.fn(async () => new Response(`<html>${BODY_MARKER}</html>`, {
status: 200, headers: { 'content-type': 'text/html' }
})));
expect(await failureOf(await GET())).toBe('upstream_unreachable');
expect(JSON.stringify(warn.mock.calls)).not.toContain(BODY_MARKER);
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"polymarket","reason":"network","kind":"SyntaxError"}');
expect(JSON.stringify(log.mock.calls)).not.toContain(BODY_MARKER);
});

it('bounds the upstream fetch and reports a timeout as a failure', async () => {
Expand All @@ -71,5 +76,6 @@ describe('GET /api/polymarket', () => {
}));
expect(await failureOf(await GET())).toBe('upstream_timeout');
expect(timeoutSpy).toHaveBeenCalledWith(8000);
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"polymarket","reason":"timeout","kind":"TimeoutError"}');
});
});
18 changes: 10 additions & 8 deletions src/app/api/polymarket/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// ============================================

import { NextResponse } from 'next/server';
import { errorKind, logEvent } from '@/core/services/server/telemetry';

const UPSTREAM_URL = 'https://gamma-api.polymarket.com/markets?limit=50&closed=false&order=volumeNum&ascending=false';
const UPSTREAM_DEADLINE_MS = 8000;
Expand Down Expand Up @@ -62,8 +63,8 @@ function asString(value: unknown, fallback: string): string {
* GET /api/polymarket
*
* Upstream failure is reported as 502 `{ success: false, error }` so the
* settings probe and the block see the outage. Logs carry the upstream
* status and top-level keys, never the body.
* settings probe and the block see the outage. Logs carry the failure, the
* upstream status and the error class, never the body or its keys.
*/
export async function GET() {
try {
Expand All @@ -77,18 +78,15 @@ export async function GET() {
});

if (!response.ok) {
console.warn('[Polymarket API] upstream returned', response.status);
logEvent('flyspace.source.failed', { provider: 'polymarket', reason: 'status', status: response.status });
return failure('upstream_status');
}

const rawData: unknown = await response.json();
const marketsArray = marketsFromRaw(rawData);

if (marketsArray === null) {
console.warn('[Polymarket API] unexpected upstream shape', {
status: response.status,
keys: isRecord(rawData) ? Object.keys(rawData) : typeof rawData
});
logEvent('flyspace.source.failed', { provider: 'polymarket', reason: 'shape', status: response.status });
return failure('upstream_shape');
}

Expand Down Expand Up @@ -148,7 +146,11 @@ export async function GET() {
const name = error instanceof Error ? error.name : 'Unknown';
const timedOut = name === 'TimeoutError' || name === 'AbortError';
// Only the error name: a JSON SyntaxError message quotes the body.
console.warn('[Polymarket API] fetch failed:', name);
logEvent('flyspace.source.failed', {
provider: 'polymarket',
reason: timedOut ? 'timeout' : 'network',
kind: errorKind(error)
});
return failure(timedOut ? 'upstream_timeout' : 'upstream_unreachable');
}
}
23 changes: 13 additions & 10 deletions src/app/api/public/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { GET } from './route';

const BODY_MARKER = 'UPSTREAM-BODY-MUST-NOT-BE-LOGGED';

let warn: ReturnType<typeof vi.spyOn>;
let log: ReturnType<typeof vi.spyOn>;

beforeEach(() => {
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
log = vi.spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('GET /api/public', () => {

expect(await failureOf(await GET(request('provider=wikipedia')))).toBe('upstream_timeout');
expect(timeoutSpy).toHaveBeenCalledWith(8000);
expect(JSON.stringify(warn.mock.calls)).toContain('upstream_timeout');
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"wikipedia","reason":"timeout","kind":"TimeoutError"}');
});

it('does not follow a redirect and reports it as 502 upstream_redirect', async () => {
Expand All @@ -74,8 +74,9 @@ describe('GET /api/public', () => {

expect(await failureOf(await GET(request('provider=frankfurter')))).toBe('upstream_redirect');
expect(fetchMock).toHaveBeenCalledTimes(1);
const logged = JSON.stringify(warn.mock.calls);
expect(logged).toContain('evil.example');
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"frankfurter","reason":"redirect","status":302}');
const logged = JSON.stringify(log.mock.calls);
expect(logged).not.toContain('evil.example');
expect(logged).not.toContain('SECRET-IN-LOCATION');
});

Expand All @@ -86,6 +87,7 @@ describe('GET /api/public', () => {
})));

expect(await failureOf(await GET(request('provider=crossref')))).toBe('upstream_too_large');
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"crossref","reason":"too_large"}');
});

it('reports an upstream non-2xx as 502 upstream_status and logs the status, never the body', async () => {
Expand All @@ -94,9 +96,8 @@ describe('GET /api/public', () => {
})));

expect(await failureOf(await GET(request('provider=github')))).toBe('upstream_status');
const logged = JSON.stringify(warn.mock.calls);
expect(logged).toContain('403');
expect(logged).not.toContain(BODY_MARKER);
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"github","reason":"status","status":403}');
expect(JSON.stringify(log.mock.calls)).not.toContain(BODY_MARKER);
});

it('reports non-JSON upstream as 502 upstream_shape and keeps the body out of the log', async () => {
Expand All @@ -105,7 +106,8 @@ describe('GET /api/public', () => {
})));

expect(await failureOf(await GET(request('provider=openlibrary')))).toBe('upstream_shape');
expect(JSON.stringify(warn.mock.calls)).not.toContain(BODY_MARKER);
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"openlibrary","reason":"shape","kind":"SyntaxError"}');
expect(JSON.stringify(log.mock.calls)).not.toContain(BODY_MARKER);
});

it('reports a network failure as 502 upstream_unreachable without echoing the error text', async () => {
Expand All @@ -116,6 +118,7 @@ describe('GET /api/public', () => {
const body = await response.json();
expect(body.error.code).toBe('upstream_unreachable');
expect(JSON.stringify(body)).not.toContain('secret-in-url');
expect(JSON.stringify(warn.mock.calls)).not.toContain('secret-in-url');
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"openmeteo","reason":"network","kind":"Error"}');
expect(JSON.stringify(log.mock.calls)).not.toContain('secret-in-url');
});
});
4 changes: 3 additions & 1 deletion src/core/services/server/public.providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ describe('fetchPublicProvider', () => {
});

it('an upstream throw becomes a 502 without echoing the error text', async () => {
vi.spyOn(console, 'warn').mockImplementation(() => {});
const log = vi.spyOn(console, 'log').mockImplementation(() => {});
vi.stubGlobal('fetch', vi.fn(async () => { throw new Error('secret-in-url'); }));
const { status, body } = await fetchPublicProvider('wikipedia', {});
expect(status).toBe(502);
expect(JSON.stringify(body)).not.toContain('secret-in-url');
expect(log).toHaveBeenCalledWith('{"event":"flyspace.source.failed","provider":"wikipedia","reason":"network","kind":"Error"}');
expect(JSON.stringify(log.mock.calls)).not.toContain('secret-in-url');
});
});
37 changes: 20 additions & 17 deletions src/core/services/server/public.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import 'server-only';
import { getApiProvider } from '@/core/schemas/api.schema';
import { readBoundedJson, RequestBodyTooLarge } from './boundedJson';
import { errorKind, logEvent } from './telemetry';

export const PUBLIC_PROXY_IDS = [
'usgs',
Expand Down Expand Up @@ -109,21 +110,24 @@ interface PublicFailure {

type Outcome = { status: number; body: unknown };

function failure(id: string, code: PublicFailureCode, message: string, detail: Record<string, unknown> = {}): Outcome {
// Status, code and keys only. The upstream body never reaches the log.
console.warn('[public proxy] upstream failure', { provider: id, code, ...detail });
/** A failure code, in the words telemetry/events.json allows for `reason`. */
const REASONS: Record<PublicFailureCode, string> = {
upstream_timeout: 'timeout',
upstream_redirect: 'redirect',
upstream_status: 'status',
upstream_too_large: 'too_large',
upstream_shape: 'shape',
upstream_unreachable: 'network'
};

function failure(id: string, code: PublicFailureCode, message: string, detail: { status?: number; kind?: string } = {}): Outcome {
// Provider, reason, status and error class only. Neither the upstream
// body nor where a redirect pointed reaches the log.
logEvent('flyspace.source.failed', { provider: id, reason: REASONS[code], ...detail });
const body: PublicFailure = { error: { code, message: `${id}: ${message}` } };
return { status: 502, body };
}

function redirectHost(res: Response): string {
try {
return new URL(res.headers.get('location') || '').host || '(none)';
} catch {
return '(unparseable)';
}
}

export async function fetchPublicProvider(
id: string,
params: Params,
Expand Down Expand Up @@ -152,15 +156,14 @@ export async function fetchPublicProvider(
} catch (error) {
const name = error instanceof Error ? error.name : 'Unknown';
if (name === 'TimeoutError' || name === 'AbortError') {
return failure(id, 'upstream_timeout', `no response within ${PUBLIC_UPSTREAM_DEADLINE_MS / 1000} s`);
return failure(id, 'upstream_timeout', `no response within ${PUBLIC_UPSTREAM_DEADLINE_MS / 1000} s`, { kind: errorKind(error) });
}
return failure(id, 'upstream_unreachable', 'request failed', { name });
return failure(id, 'upstream_unreachable', 'request failed', { kind: errorKind(error) });
}

if (res.type === 'opaqueredirect' || (res.status >= 300 && res.status < 400)) {
return failure(id, 'upstream_redirect', 'upstream redirected; redirects are not followed', {
status: res.status,
location: redirectHost(res)
status: res.status
});
}
if (!res.ok) {
Expand All @@ -176,9 +179,9 @@ export async function fetchPublicProvider(
}
const name = error instanceof Error ? error.name : 'Unknown';
if (name === 'TimeoutError' || name === 'AbortError') {
return failure(id, 'upstream_timeout', `body not complete within ${PUBLIC_UPSTREAM_DEADLINE_MS / 1000} s`);
return failure(id, 'upstream_timeout', `body not complete within ${PUBLIC_UPSTREAM_DEADLINE_MS / 1000} s`, { kind: errorKind(error) });
}
// SyntaxError (and only its name): the message would quote the body.
return failure(id, 'upstream_shape', 'non-JSON response', { name });
return failure(id, 'upstream_shape', 'non-JSON response', { kind: errorKind(error) });
}
}
Loading
Loading