fix(rate-limit): fail the shared write ceiling open and skip reads over POST

The shared write bucket threw on any SQL error, so a lock on rate_limits
answered every authenticated write with 500. It is a loop cutter, not an
access check: when the bucket cannot be read the write now goes ahead with
a warn signal. The login and other auth-surface limits call the limiter
directly and still fail closed. requireAuth now passes readOverPost to the
meter, the draft-only document assist declares it, and the three chat
routes, which meter their turns with buckets of their own, declare that
instead of being counted twice.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 16:09:28 +02:00
1 parent 408ae35b7c
commit 5185edca74
12 files changed
+148 -19

No files matched your search

+11 -3
View File
@@ -2,8 +2,10 @@
* `readOverPost` lets a delegable route answer a POST at the grant's read
* level. That widens what a read-only grant reaches, so the set of routes
* declaring it is frozen here: adding one is a reviewed diff, not an
* accident. Today it is the record search alone, whose query is health data
* that must stay out of URLs.
* accident. Today it is the record search, whose query is health data that
* must stay out of URLs, and the document assist, whose POST writes nothing
* (under `requireAuth` the declaration only keeps it off the shared write
* ceiling).
*
* The matcher tolerates whitespace and line breaks, and the guard asserts it
* matched something, so a rewrite that hides the declaration from the regex
@@ -16,7 +18,13 @@ import { join, relative, sep } from "node:path";
const ROOT = process.cwd();
const FROZEN = ["src/app/api/search/route.ts"];
const FROZEN = [
// The draft-only document assist: a POST that writes nothing. Under
// `requireAuth` the declaration only keeps it out of the shared write
// ceiling; there is no grant for it to widen.
"src/app/api/documents/inbound/[id]/suggest/route.ts",
"src/app/api/search/route.ts",
];
describe("readOverPost declarations", () => {
it("are exactly the frozen set", () => {
@@ -75,6 +75,12 @@ const OWN_BUCKET: Record<string, string> = {
"The cycle HealthKit sweep sends 500-day slices in sequence; the route's own bulk bucket meters it.",
"app/api/nutrients/batch/route.ts":
"The nutrient sync sends 500-row batches in sequence; the route's own batch bucket meters it.",
"app/api/insights/chat/route.ts":
"A Coach turn is metered by the Coach's own buckets: 20 turns a minute (`coach-chat`) and one turn slot at a time.",
"app/api/insights/chat/fenced/route.ts":
"A fenced document chat turn is metered by its own per-account bucket, sized for model turns.",
"app/api/documents/inbound/[id]/chat/route.ts":
"A document chat turn is metered by its own per-account bucket, sized for model turns.",
"app/api/insights/ecg/route.ts":
"The ECG sweep sends one recording per request with no client-side delay, newest first, over the whole HealthKit history; the route's own 600-a-minute ingest bucket meters it.",
};