feat(rate-limit): per-user mutation-rate-limiting (v1-readiness #3)
lib/rate-limit.ts: 11 nieuwe scope-configs + enforceUserRateLimit(scope, userId)
helper. Returnt { error, code: 429 } shape voor consistent foutbeleid.
Toegepast op de high-value mutation-paths:
- actions/pbis.ts createPbiAction
- actions/stories.ts createStoryAction
- actions/tasks.ts saveTask (alleen create-path) + createTaskAction
- actions/todos.ts createTodoAction
- actions/sprints.ts createSprintAction
- actions/products.ts createProductAction + createProductFormAction
- actions/api-tokens.ts createApiTokenAction
- actions/questions.ts answerQuestion
- actions/claude-jobs.ts enqueueClaudeJobAction + enqueueClaudeJobsBatchAction
- app/api/profile/avatar/route.ts POST
- app/api/stories/[id]/log/route.ts POST
Limits zijn ruim genoeg voor normaal gebruik, eng genoeg voor abuse-loops:
create-task 100/min, create-todo 60/min, create-pbi 30/min, create-product
5/min, create-token 10/uur, etc. Per-user scope (geen globale block).
Niet aangeraakt: reorder/status-toggle (intra-session frequent, lage abuse),
update/delete (laag-volume), cron-routes (CRON_SECRET-gated).
Consumer-tweaks: 'success' in result narrowing waar TS de bredere union niet
meer accepteerde. Tests: 9 nieuwe op rate-limit-helper.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
43778e3bcb
commit
a0a10001d5
16 changed files with 175 additions and 13 deletions
|
|
@ -11,6 +11,21 @@ const CONFIGS: Record<string, RateLimitConfig> = {
|
|||
login: { windowMs: 60_000, max: 10 }, // 10 attempts per minute
|
||||
register: { windowMs: 3_600_000, max: 5 }, // 5 attempts per hour
|
||||
'pair-start': { windowMs: 60_000, max: 10 }, // 10 QR-pairings per minute (M10)
|
||||
|
||||
// v1-readiness item 3 — per-user mutation-rate-limits.
|
||||
// Limits zijn ruim genoeg voor normaal gebruik, eng genoeg om abuse-loops
|
||||
// (bv. een runaway-script dat duizenden tasks aanmaakt) te stoppen.
|
||||
'create-pbi': { windowMs: 60_000, max: 30 },
|
||||
'create-story': { windowMs: 60_000, max: 50 },
|
||||
'create-task': { windowMs: 60_000, max: 100 },
|
||||
'create-todo': { windowMs: 60_000, max: 60 },
|
||||
'create-sprint': { windowMs: 60_000, max: 5 },
|
||||
'create-product': { windowMs: 60_000, max: 5 },
|
||||
'create-token': { windowMs: 3_600_000, max: 10 }, // 10 API-tokens/uur/user
|
||||
'enqueue-job': { windowMs: 60_000, max: 30 },
|
||||
'log-story': { windowMs: 60_000, max: 60 },
|
||||
'upload-avatar': { windowMs: 3_600_000, max: 20 },
|
||||
'answer-question': { windowMs: 60_000, max: 30 },
|
||||
}
|
||||
|
||||
const DEFAULT_CONFIG: RateLimitConfig = { windowMs: 60_000, max: 10 }
|
||||
|
|
@ -35,3 +50,32 @@ export function checkRateLimit(key: string): boolean {
|
|||
entry.count++
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper voor server-actions: scope op (action, userId), retourneert het
|
||||
* standaard `{ error, code: 429 }` shape als de gebruiker over de limiet is.
|
||||
*
|
||||
* Gebruik in een action:
|
||||
*
|
||||
* const limited = enforceUserRateLimit('create-pbi', session.userId)
|
||||
* if (limited) return limited
|
||||
*/
|
||||
export function enforceUserRateLimit(
|
||||
scope: keyof typeof CONFIGS,
|
||||
userId: string,
|
||||
): { error: string; code: 429 } | null {
|
||||
if (!checkRateLimit(`${scope}:${userId}`)) {
|
||||
return {
|
||||
error: 'Te veel acties achter elkaar. Probeer het over een minuut opnieuw.',
|
||||
code: 429,
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Voor test-isolatie: leegt de in-memory store. Niet exporteren in productie-paden.
|
||||
*/
|
||||
export function _resetRateLimit() {
|
||||
store.clear()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue