fix(api): rate-limit feedback endpoint + drop overly broad cache invalidation (H5/H6 code-review)

H5 — `POST /api/insights/feedback` had no rate limit. The unique
index `(userId, recommendationId, recommendationText)` blocks exact
replays but a malicious client could distort the daily aggregator's
bucket slice by varying recommendationId/text per request. Added
`checkRateLimit("insights-feedback:<user.id>", 60, 60 * 60 * 1000)`.
60/h is generous given a comprehensive insight rarely has >10 recs.

H6 — `<RecommendationFeedback>` invalidated the entire `["insights"]`
prefix on every thumbs click, which matches all 8 insights queries
(comprehensive + 7 per-status). None of them depend on the feedback
row. The 24h server cache absorbs the actual LLM round-trip but the
network thrash + stale-cache refetch was waste. Dropped the
invalidation; user-visible verdict state lives in localStorage +
local React state. v1.4.17's prompt-version ratchet will scope its
own invalidation when feedback feeds the next generation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-10 03:11:34 +02:00
1 parent f3025a8dd5
commit 6863ecb87d
2 files changed
+28 -3

No files matched your search

+20
View File
@@ -8,6 +8,7 @@ import { Prisma } from "@/generated/prisma/client";
import { resolveFeedbackAttribution } from "@/lib/ai/feedback-attribution";
import { withIdempotency } from "@/lib/idempotency";
import { annotate } from "@/lib/logging/context";
import { checkRateLimit } from "@/lib/rate-limit";
import { recommendationFeedbackRequestSchema } from "@/lib/validations/recommendation-feedback";
/**
@@ -30,6 +31,25 @@ import { recommendationFeedbackRequestSchema } from "@/lib/validations/recommend
async function handlePost(request: NextRequest) {
const { user } = await requireAuth();
// v1.4.16 phase D reconcile (code-review H5 / security M1) — bound
// the per-user write rate. Without this, a single client can
// distort the daily aggregator's bucket slice by varying
// recommendationId/text per request (the unique index only catches
// exact replays). 60/h is generous: a comprehensive insight rarely
// has >10 recs, so ≥6 regenerations/h before throttle hits.
const rl = await checkRateLimit(
`insights-feedback:${user.id}`,
60,
60 * 60 * 1000,
);
if (!rl.allowed) {
annotate({
action: { name: "insights.recommendation.feedback" },
meta: { outcome: "rate_limited" },
});
return apiError("Too many feedback submissions, try again later", 429);
}
const { data: rawBody, error } = await safeJson<unknown>(request);
if (error) return error;