The Worker now runs first for every request except the content-hashed bundles under /assets/ (run_worker_first: ["/*", "!/assets/*"]) and counts it against a per-client limit before serving it: - SITE_LIMITER: pages and public files, 120 requests / 60 s - API_LIMITER: every /api/* request, 60 / 60 s - VERIFY_LIMITER: /api/turnstile/verify on top, 30 / 60 s (unchanged) Over the limit, pages get a plain-text 429 in the four UI languages and the API a JSON 429 (rate_limited); both carry Retry-After: 60. Clients are keyed on their IPv4 address or the /64 of their IPv6 address, never logged. Pages still go through env.ASSETS.fetch(), so _headers, _redirects and the SPA fallback apply as before. A failing limiter lets requests through. Previews get their own counters (namespace_id 4204/4206). Docs: DEPLOYMENT, PRIVACY (wording only, nothing new collected, no re-consent), SECURITY, CONTRIBUTING, READMEs, CLAUDE.md, ROADMAP, CHANGELOG. Version 2.0.2 (versionCode 20002). Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
/** Responses the Worker builds itself, with defensive headers (assets get theirs from public/_headers). */
|
|
|
|
const BASE_HEADERS: Record<string, string> = {
|
|
'Cache-Control': 'no-store',
|
|
'X-Content-Type-Options': 'nosniff',
|
|
'Referrer-Policy': 'no-referrer',
|
|
'Cross-Origin-Resource-Policy': 'same-origin',
|
|
'Content-Security-Policy': "default-src 'none'; frame-ancestors 'none'",
|
|
};
|
|
|
|
export function json(
|
|
body: unknown,
|
|
status = 200,
|
|
extraHeaders: Record<string, string> = {},
|
|
): Response {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: {
|
|
...BASE_HEADERS,
|
|
'Content-Type': 'application/json; charset=utf-8',
|
|
...extraHeaders,
|
|
},
|
|
});
|
|
}
|
|
|
|
export function text(
|
|
body: string,
|
|
status: number,
|
|
extraHeaders: Record<string, string> = {},
|
|
): Response {
|
|
return new Response(body, {
|
|
status,
|
|
headers: { ...BASE_HEADERS, 'Content-Type': 'text/plain; charset=utf-8', ...extraHeaders },
|
|
});
|
|
}
|
|
|
|
/** Structured log line for Workers Logs. Never pass tokens, IPs or user content. */
|
|
export function logEvent(event: string, fields: Record<string, unknown>): void {
|
|
console.log(JSON.stringify({ event, ...fields }));
|
|
}
|