Skip to content
Open
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
16 changes: 16 additions & 0 deletions telemetry-dashboard/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ async function loginRateLimitOk(env: Env, request: Request): Promise<boolean> {
}
}

/** Best-effort abuse cap on the authenticated API surface, keyed by client IP. */
async function apiRateLimitOk(env: Env, request: Request): Promise<boolean> {
const key = request.headers.get('cf-connecting-ip') ?? 'unknown';
try {
const { success } = await env.API_RATE_LIMITER.limit({ key });
return success;
} catch (err) {
// Fail open: a rate-limiter outage must not take the dashboard down.
console.error(JSON.stringify({ msg: 'api rate limiter unavailable', err: String(err) }));
return true;
}
}

async function handleLoginPage(env: Env, request: Request, url: URL): Promise<Response> {
const next = safeNextPath(url.searchParams.get('next'));
if (await hasValidSession(env, request)) return redirect(next);
Expand Down Expand Up @@ -260,6 +273,9 @@ export default {
if (!isRead) {
return json({ error: 'method not allowed' }, { status: 405, headers: { allow: 'GET' } });
}
if (!(await apiRateLimitOk(env, request))) {
return json({ error: 'too many requests' }, { status: 429 });
}
return await apiResponse(env, url);
}

Expand Down
5 changes: 5 additions & 0 deletions telemetry-dashboard/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
"name": "LOGIN_RATE_LIMITER",
"namespace_id": "2001",
"simple": { "limit": 5, "period": 60 }
},
{
"name": "API_RATE_LIMITER",
"namespace_id": "2002",
"simple": { "limit": 120, "period": 60 }
}
]
}