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
|
|
@ -10,6 +10,7 @@ import { isValidCode, normalizeCode } from '@/lib/code'
|
|||
import { createWithCodeRetry, generateNextPbiCode } from '@/lib/code-server'
|
||||
import { pbiStatusFromApi } from '@/lib/task-status'
|
||||
import { createPbiSchema, updatePbiSchema } from '@/lib/schemas/pbi'
|
||||
import { enforceUserRateLimit } from '@/lib/rate-limit'
|
||||
|
||||
async function getSession() {
|
||||
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
||||
|
|
@ -22,6 +23,9 @@ export async function createPbiAction(_prevState: unknown, formData: FormData) {
|
|||
if (!session.userId) return { error: 'Niet ingelogd', code: 403 }
|
||||
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus', code: 403 }
|
||||
|
||||
const limited = enforceUserRateLimit('create-pbi', session.userId)
|
||||
if (limited) return limited
|
||||
|
||||
const parsed = createPbiSchema.safeParse({
|
||||
productId: formData.get('productId'),
|
||||
code: (formData.get('code') as string) || undefined,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue