chore(errors): incremental tech debt — rate-limit typings, legacy web on Next 15
- api: Align rate-limit plugin docs with actual store (in-memory unless wired in index); type errorResponseBuilder context; augment FastifyRequest with optional ingest_token_id instead of casting to any. - web: Upgrade to Next 15 / React 19; add ESLint 9 flat config, tsconfig, next-env.d.ts; outputFileTracingRoot so file tracing stays within this product tree. Paperclip-Agent: Tiago Neves (urlKey tiago-neves; id 5d084b36-f38f-42a5-a92a-520005afda4f) Paperclip-Issue: BIT-2479 Paperclip-Run: e3292ca4-a7b9-42f1-83ca-bc3d78906532
This commit is contained in:
1 parent
0bd13bac5b
commit
78003b001d
7 files changed
+5594
-15
No files matched your search
@@ -20,13 +20,21 @@
|
||||
* INGEST_MAX_BODY_BYTES max ingest request body (default: 512 KB)
|
||||
* ADMIN_MAX_BODY_BYTES max admin API request body (default: 64 KB)
|
||||
*
|
||||
* All limits use Redis (via ioredis) for distributed counting when multiple
|
||||
* API instances run. Falls back to in-memory (no Redis configured) for dev.
|
||||
* @fastify/rate-limit uses an in-memory store unless a custom store is
|
||||
* configured at registration time (see `src/index.ts`).
|
||||
*/
|
||||
|
||||
import fp from 'fastify-plugin';
|
||||
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
||||
|
||||
/** Shape of the `context` argument to @fastify/rate-limit `errorResponseBuilder`. */
|
||||
interface RateLimitErrorContext {
|
||||
ban: boolean;
|
||||
after: string;
|
||||
max: number;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
const RATE_INGEST_MAX = parseInt(process.env.RATE_INGEST_MAX ?? '1000', 10);
|
||||
const RATE_INGEST_WINDOW_MS = parseInt(process.env.RATE_INGEST_WINDOW_MS ?? '60000', 10);
|
||||
const RATE_LOGIN_MAX = parseInt(process.env.RATE_LOGIN_MAX ?? '10', 10);
|
||||
@@ -41,8 +49,8 @@ export const ADMIN_MAX_BODY_BYTES = parseInt(process.env.ADMIN_MAX_BODY_BYTES
|
||||
|
||||
/** Key generator for ingest rate limiting: prefer token ID, fall back to IP. */
|
||||
function ingest_key(request: FastifyRequest): string {
|
||||
// Ingest routes set request.ingest_token_id after token validation.
|
||||
const token_id = (request as any).ingest_token_id as string | undefined;
|
||||
// Ingest routes may set `ingest_token_id` after token validation (optional augmentation).
|
||||
const token_id = request.ingest_token_id;
|
||||
return token_id ? `ingest:token:${token_id}` : `ingest:ip:${request.ip}`;
|
||||
}
|
||||
|
||||
@@ -59,7 +67,7 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
max: RATE_INGEST_MAX,
|
||||
timeWindow: RATE_INGEST_WINDOW_MS,
|
||||
keyGenerator: ingest_key,
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: any) => ({
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: RateLimitErrorContext) => ({
|
||||
error: 'TooManyRequests',
|
||||
message: 'Ingest rate limit exceeded. Slow down.',
|
||||
retryAfter: Math.ceil(context.ttl / 1000),
|
||||
@@ -69,7 +77,7 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
max: RATE_LOGIN_MAX,
|
||||
timeWindow: RATE_LOGIN_WINDOW_MS,
|
||||
keyGenerator: (request: FastifyRequest) => `login:ip:${request.ip}`,
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: any) => ({
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: RateLimitErrorContext) => ({
|
||||
error: 'TooManyRequests',
|
||||
message: 'Too many login attempts. Try again later.',
|
||||
retryAfter: Math.ceil(context.ttl / 1000),
|
||||
@@ -83,7 +91,7 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
max: RATE_MUTATION_MAX,
|
||||
timeWindow: RATE_MUTATION_WINDOW_MS,
|
||||
keyGenerator: (request: FastifyRequest) => `errmon_mut:ip:${request.ip}`,
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: any) => ({
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: RateLimitErrorContext) => ({
|
||||
success: false,
|
||||
error: 'TooManyRequests',
|
||||
message: 'Error-monitoring mutation rate limit exceeded. Slow down.',
|
||||
@@ -120,6 +128,10 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
}, { name: 'rate-limit-config', dependencies: ['@fastify/rate-limit'] });
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
/** When set by ingest auth, rate limiting keys by token instead of IP. */
|
||||
ingest_token_id?: string;
|
||||
}
|
||||
interface FastifyInstance {
|
||||
rateLimits: {
|
||||
ingest: Record<string, unknown>;
|
||||
|
||||
Reference in new issue
Block a user