mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
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:
1 parent
408ae35b7c
commit
5185edca74
12 files changed
+148
-19
No files matched your search
@@ -6,7 +6,9 @@
|
||||
* before. What has to hold: a write is counted, a read is not, a spent bucket
|
||||
* refuses with the published code and the headers that describe it and
|
||||
* writes nothing, and a bulk route on its own bucket keeps working while the
|
||||
* shared one is spent.
|
||||
* shared one is spent. A POST that reads (`readOverPost`) is not counted on
|
||||
* either resolver, and a bucket that cannot be read lets the write through
|
||||
* while the limiter itself still fails for its other callers.
|
||||
*/
|
||||
import { NextRequest } from "next/server";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
@@ -183,3 +185,58 @@ describe("the shared write bucket on routes without a limiter of their own", ()
|
||||
).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what the shared bucket does not count", () => {
|
||||
async function postThrough(options?: { readOverPost: true }) {
|
||||
const { apiHandler, requireAuth } = await import("@/lib/api-handler");
|
||||
const { apiSuccess } = await import("@/lib/api-response");
|
||||
const POST = apiHandler(async (_request: NextRequest) => {
|
||||
const { user } = await requireAuth(undefined, options);
|
||||
return apiSuccess({ id: user.id });
|
||||
});
|
||||
return POST(
|
||||
new NextRequest("http://localhost/api/probe", {
|
||||
method: "POST",
|
||||
body: "{}",
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
it("leaves a read-over-POST through requireAuth uncounted", async () => {
|
||||
expect((await postThrough({ readOverPost: true })).status).toBe(200);
|
||||
expect(await bucketCount()).toBe(0);
|
||||
});
|
||||
|
||||
it("counter-check: the same POST without the declaration is counted", async () => {
|
||||
expect((await postThrough()).status).toBe(200);
|
||||
expect(await bucketCount()).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("a bucket that cannot be read", () => {
|
||||
it("lets the write through instead of answering 500, and the limiter still fails for its own callers", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const { checkRateLimit } = await import("@/lib/rate-limit");
|
||||
await prisma.$executeRawUnsafe(
|
||||
`ALTER TABLE "rate_limits" RENAME TO "rate_limits_away"`,
|
||||
);
|
||||
try {
|
||||
const res = await patchAllergy({ substance: "Birch pollen" });
|
||||
expect(res.status, await res.clone().text()).toBe(200);
|
||||
const row = await prisma.allergy.findUniqueOrThrow({
|
||||
where: { id: allergyId },
|
||||
});
|
||||
expect(row.substance).toBe("Birch pollen");
|
||||
|
||||
// A login or sign-up limit calls the limiter directly: no fail-open.
|
||||
await expect(
|
||||
checkRateLimit("auth:login:probe", 5, 60_000),
|
||||
).rejects.toThrow();
|
||||
} finally {
|
||||
await prisma.$executeRawUnsafe(
|
||||
`ALTER TABLE "rate_limits_away" RENAME TO "rate_limits"`,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user