mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +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
@@ -546,7 +546,7 @@ export function isScopedCredential(auth: AuthContext): boolean {
|
||||
*/
|
||||
export async function requireAuth(
|
||||
requiredPermission?: string,
|
||||
options?: Pick<WriteMeterOptions, "writeBucket">,
|
||||
options?: WriteMeterOptions,
|
||||
): Promise<AuthContext> {
|
||||
const auth = await authenticateCaller(requiredPermission);
|
||||
const carrier = await readActingCarrier(auth);
|
||||
|
||||
@@ -16,6 +16,12 @@ vi.mock("@/lib/logging/context", () => ({
|
||||
annotate: vi.fn(),
|
||||
}));
|
||||
|
||||
const signals = vi.hoisted(() => ({ emitted: [] as unknown[] }));
|
||||
|
||||
vi.mock("@/lib/logging/signal", () => ({
|
||||
emitSignal: (input: unknown) => signals.emitted.push(input),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/rate-limit", () => ({
|
||||
checkRecordWriteRateLimit: vi.fn(async () => ({
|
||||
allowed: state.allowed,
|
||||
@@ -38,6 +44,7 @@ function request(method: string | undefined) {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(checkRecordWriteRateLimit).mockClear();
|
||||
signals.emitted.length = 0;
|
||||
state.allowed = true;
|
||||
state.event = null;
|
||||
});
|
||||
@@ -99,4 +106,27 @@ describe("meterAccountWrite", () => {
|
||||
RECORD_WRITE_RATE_LIMITED_CODE,
|
||||
);
|
||||
});
|
||||
|
||||
it("lets the write through with a warning when the bucket cannot be read", async () => {
|
||||
vi.mocked(checkRecordWriteRateLimit).mockRejectedValueOnce(
|
||||
new Error("canceling statement due to lock timeout"),
|
||||
);
|
||||
request("POST");
|
||||
await expect(meterAccountWrite("account-1")).resolves.toBeUndefined();
|
||||
expect(signals.emitted).toEqual([
|
||||
expect.objectContaining({
|
||||
action: "rate_limit.record_write.unavailable",
|
||||
level: "warn",
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it("counter-check: a readable, spent bucket still refuses, with no warning", async () => {
|
||||
state.allowed = false;
|
||||
request("POST");
|
||||
await expect(meterAccountWrite("account-1")).rejects.toBeInstanceOf(
|
||||
HttpError,
|
||||
);
|
||||
expect(signals.emitted).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -31,9 +31,18 @@
|
||||
* record owner's, and cannot collect a fresh one by switching records. A
|
||||
* refused request is counted too, since the charge happens before the body is
|
||||
* read.
|
||||
*
|
||||
* The bucket fails open. It cuts off a runaway loop; it is not what decides
|
||||
* who may write, which the resolver that called it has already settled. So
|
||||
* when the bucket itself cannot be read (the upsert fails: a lock on
|
||||
* `rate_limits`, a pool at its limit), the write goes ahead and a `warn`
|
||||
* signal says the ceiling was not applied, instead of every authenticated
|
||||
* write in the app answering 500. The login, sign-up and other auth-surface
|
||||
* limits call the limiter directly and keep failing closed.
|
||||
*/
|
||||
import { HttpError } from "@/lib/api-errors";
|
||||
import { annotate, getEvent } from "@/lib/logging/context";
|
||||
import { emitSignal } from "@/lib/logging/signal";
|
||||
import { checkRecordWriteRateLimit } from "@/lib/rate-limit";
|
||||
|
||||
/** The code a refusal by the shared write bucket carries. */
|
||||
@@ -76,7 +85,18 @@ export async function meterAccountWrite(
|
||||
if (charged.has(evt)) return;
|
||||
charged.add(evt);
|
||||
|
||||
const rl = await checkRecordWriteRateLimit(accountId);
|
||||
let rl: Awaited<ReturnType<typeof checkRecordWriteRateLimit>>;
|
||||
try {
|
||||
rl = await checkRecordWriteRateLimit(accountId);
|
||||
} catch (error) {
|
||||
annotate({ meta: { record_write_rate_limit_unavailable: true } });
|
||||
emitSignal({
|
||||
action: "rate_limit.record_write.unavailable",
|
||||
level: "warn",
|
||||
error,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!rl.allowed) {
|
||||
annotate({ meta: { record_write_rate_limited: true } });
|
||||
throw new HttpError(
|
||||
|
||||
@@ -373,7 +373,7 @@ export const stdResponses = {
|
||||
export const recordWriteRateLimitResponse = {
|
||||
"429": {
|
||||
description:
|
||||
"Rate limit exceeded. This route shares one per-account bucket with every other authenticated write — `record-write:<accountId>`, 300 requests per 60 seconds — keyed on the ACTING account, so a delegate burns their own allowance rather than the record owner's. Only the bulk ingest endpoints, which meter themselves with a dedicated bucket, stay outside it. Nothing was written. `meta.errorCode` is `record_write.rate_limited`, which is what tells this refusal apart from a route's own narrower bucket when both can answer 429. The `Retry-After`, `X-RateLimit-Limit`, `X-RateLimit-Remaining` and `X-RateLimit-Reset` headers describe that bucket; back off on `Retry-After` rather than guessing. Every request counts against the bucket, refused ones included: the check runs before the body is read, so a client looping on a 422 spends the same allowance as one that writes rows. A client with more than a handful of rows to send should use the batch endpoint for its domain instead of looping this one.",
|
||||
"Rate limit exceeded. This route shares one per-account bucket with every other authenticated write — `record-write:<accountId>`, 300 requests per 60 seconds — keyed on the ACTING account, so a delegate burns their own allowance rather than the record owner's. Only the routes that meter themselves with a dedicated bucket (the bulk ingest endpoints and the model-turn chats) and a POST that writes nothing stay outside it. If the bucket cannot be read, the write goes ahead unmetered rather than failing. Nothing was written. `meta.errorCode` is `record_write.rate_limited`, which is what tells this refusal apart from a route's own narrower bucket when both can answer 429. The `Retry-After`, `X-RateLimit-Limit`, `X-RateLimit-Remaining` and `X-RateLimit-Reset` headers describe that bucket; back off on `Retry-After` rather than guessing. Every request counts against the bucket, refused ones included: the check runs before the body is read, so a client looping on a 422 spends the same allowance as one that writes rows. A client with more than a handful of rows to send should use the batch endpoint for its domain instead of looping this one.",
|
||||
content: { "application/json": { schema: errorEnvelope } },
|
||||
headers: stdResponses["429"].headers,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user