mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
fix(api): meter every authenticated write where the caller is resolved
169 of 364 mutating handlers met no rate limit, every PATCH and DELETE on an [id] among them. The shared per-account write bucket (300 a minute, keyed on the acting account) was called by hand in a dozen creates; it is now charged by the identity resolvers for any POST, PUT, PATCH or DELETE, once per request, so a route is metered by resolving its caller. The bulk ingest routes the native client syncs through in bursts (measurements, workouts, intake, mood and cycle bulk, nutrients, the ECG sweep) declare writeBucket: "own" and keep their dedicated ceilings, so a large HealthKit backfill cannot lock the person out of logging a dose. A read-over-POST stays free. write-rate-limit-guard fails on any mutating route that neither resolves its caller through a metering resolver nor calls a limiter, unless it is on a short reasoned exception list; an opt-out must be on its own reviewed list and call its own limiter.
This commit is contained in:
1 parent
3e9594b3b8
commit
39d23dbb7d
33 files changed
+730
-216
No files matched your search
@@ -55,6 +55,7 @@ import {
|
||||
attachRecordContextEcho,
|
||||
} from "./sharing/record-session-fence";
|
||||
import { refuseOnKeyMismatch } from "./boot/key-mismatch-refusal";
|
||||
import { meterAccountWrite, type WriteMeterOptions } from "./auth/write-meter";
|
||||
import { acceptIncomingRequestId } from "./http/request-id";
|
||||
import { withErrorEnvelopeDefaults } from "./api-response";
|
||||
import {
|
||||
@@ -545,12 +546,16 @@ export function isScopedCredential(auth: AuthContext): boolean {
|
||||
*/
|
||||
export async function requireAuth(
|
||||
requiredPermission?: string,
|
||||
options?: Pick<WriteMeterOptions, "writeBucket">,
|
||||
): Promise<AuthContext> {
|
||||
const auth = await authenticateCaller(requiredPermission);
|
||||
const carrier = await readActingCarrier(auth);
|
||||
if (carrier.kind !== "none") {
|
||||
throw refuseUndeclaredMode(auth, carrier);
|
||||
}
|
||||
// The shared write ceiling, for every write this resolver admits — see
|
||||
// `meterAccountWrite`.
|
||||
await meterAccountWrite(auth.user.id, options);
|
||||
return auth;
|
||||
}
|
||||
|
||||
@@ -866,6 +871,7 @@ export async function requireActorAuth(): Promise<AuthContext> {
|
||||
auditRefusal(auth, { kind: "header", accountId: header }, "actor_surface");
|
||||
throw new SharingNotPermittedError();
|
||||
}
|
||||
await meterAccountWrite(auth.user.id);
|
||||
return auth;
|
||||
}
|
||||
|
||||
@@ -913,6 +919,13 @@ export interface RecordAuthOptions {
|
||||
* above `"read"`.
|
||||
*/
|
||||
readonly readOverPost?: true;
|
||||
/**
|
||||
* This route meters its writes with a dedicated bucket instead of the shared
|
||||
* per-account one (`meterAccountWrite`). Frozen per route, with the reason,
|
||||
* by `write-rate-limit-guard.test.ts`, which also requires the route to call
|
||||
* its own limiter.
|
||||
*/
|
||||
readonly writeBucket?: "own";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -966,6 +979,13 @@ export async function requireRecordAuth(
|
||||
// fence reads, so the invariant is "before the carrier", not "before the
|
||||
// first database await".
|
||||
await assertRecordSessionFence(auth);
|
||||
// The shared write ceiling, charged to the ACTING account before any grant
|
||||
// is read, so a refused write counts like an admitted one — see
|
||||
// `meterAccountWrite`.
|
||||
await meterAccountWrite(auth.user.id, {
|
||||
readOverPost: options?.readOverPost,
|
||||
writeBucket: options?.writeBucket,
|
||||
});
|
||||
const carrier = await readActingCarrier(auth);
|
||||
|
||||
// A narrow, single-purpose Bearer never acts on another record.
|
||||
@@ -1063,6 +1083,7 @@ export async function requireGuardianAuth(): Promise<RecordAuthContext> {
|
||||
// a context that has since moved must be refused before the marker is read,
|
||||
// not after.
|
||||
await assertRecordSessionFence(auth);
|
||||
await meterAccountWrite(auth.user.id);
|
||||
const carrier = await readActingCarrier(auth);
|
||||
|
||||
if (carrier.kind === "none") {
|
||||
@@ -1207,6 +1228,7 @@ export async function requireAdmin(): Promise<AuthContext> {
|
||||
if (sessionData.user.role !== "ADMIN") {
|
||||
throw new HttpError(403, "Admin access required", AUTH_ERROR_CODES.admin);
|
||||
}
|
||||
await meterAccountWrite(sessionData.user.id);
|
||||
return { ...sessionData, authMethod: "cookie" };
|
||||
}
|
||||
|
||||
@@ -1233,6 +1255,7 @@ export async function requireCookieAuth(): Promise<AuthContext> {
|
||||
auth_method: "session",
|
||||
});
|
||||
}
|
||||
await meterAccountWrite(sessionData.user.id);
|
||||
return { ...sessionData, authMethod: "cookie" };
|
||||
}
|
||||
|
||||
@@ -1343,6 +1366,8 @@ export async function requireFreshMfa(
|
||||
});
|
||||
}
|
||||
|
||||
await meterAccountWrite(sessionData.user.id);
|
||||
|
||||
// The account must hold a credential that can stamp `mfaVerifiedAt` in the
|
||||
// first place. Without one a step-up-gated action is unreachable by design
|
||||
// rather than merely refused, so the refusal is named differently
|
||||
@@ -1661,6 +1686,7 @@ export async function requireBearerAuth(): Promise<BearerAuthContext> {
|
||||
}
|
||||
const raw = authHeader.slice(7);
|
||||
const auth = await authenticateBearer(raw, undefined);
|
||||
await meterAccountWrite(auth.user.id);
|
||||
return {
|
||||
...auth,
|
||||
apiTokenId: auth.session.id,
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
// The unit setup stubs the meter for every route test; this file is where the
|
||||
// real one is pinned.
|
||||
vi.mock("@/lib/auth/write-meter", async (importOriginal) =>
|
||||
importOriginal<typeof import("@/lib/auth/write-meter")>(),
|
||||
);
|
||||
|
||||
const state = vi.hoisted(() => ({
|
||||
event: null as null | { getHttpMethod: () => string | undefined },
|
||||
allowed: true,
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/logging/context", () => ({
|
||||
getEvent: () => state.event,
|
||||
annotate: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/rate-limit", () => ({
|
||||
checkRecordWriteRateLimit: vi.fn(async () => ({
|
||||
allowed: state.allowed,
|
||||
limit: 300,
|
||||
remaining: state.allowed ? 299 : 0,
|
||||
resetAt: Date.now() + 60_000,
|
||||
})),
|
||||
}));
|
||||
|
||||
import {
|
||||
meterAccountWrite,
|
||||
RECORD_WRITE_RATE_LIMITED_CODE,
|
||||
} from "../write-meter";
|
||||
import { checkRecordWriteRateLimit } from "@/lib/rate-limit";
|
||||
import { HttpError } from "@/lib/api-errors";
|
||||
|
||||
function request(method: string | undefined) {
|
||||
state.event = { getHttpMethod: () => method };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(checkRecordWriteRateLimit).mockClear();
|
||||
state.allowed = true;
|
||||
state.event = null;
|
||||
});
|
||||
|
||||
describe("meterAccountWrite", () => {
|
||||
it.each(["POST", "PUT", "PATCH", "DELETE", "patch"])(
|
||||
"charges the acting account for a %s",
|
||||
async (method) => {
|
||||
request(method);
|
||||
await meterAccountWrite("account-1");
|
||||
expect(checkRecordWriteRateLimit).toHaveBeenCalledWith("account-1");
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["GET", "HEAD", "OPTIONS"])("leaves a %s alone", async (method) => {
|
||||
request(method);
|
||||
await meterAccountWrite("account-1");
|
||||
expect(checkRecordWriteRateLimit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("charges nothing outside a request", async () => {
|
||||
await meterAccountWrite("account-1");
|
||||
expect(checkRecordWriteRateLimit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("charges one request once, however many resolvers it passes", async () => {
|
||||
request("POST");
|
||||
await meterAccountWrite("account-1");
|
||||
await meterAccountWrite("account-1");
|
||||
expect(checkRecordWriteRateLimit).toHaveBeenCalledTimes(1);
|
||||
|
||||
request("POST");
|
||||
await meterAccountWrite("account-1");
|
||||
expect(checkRecordWriteRateLimit).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("leaves a read-over-POST alone, and only the POST", async () => {
|
||||
request("POST");
|
||||
await meterAccountWrite("account-1", { readOverPost: true });
|
||||
expect(checkRecordWriteRateLimit).not.toHaveBeenCalled();
|
||||
request("DELETE");
|
||||
await meterAccountWrite("account-1", { readOverPost: true });
|
||||
expect(checkRecordWriteRateLimit).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("leaves a route with its own bucket alone", async () => {
|
||||
request("POST");
|
||||
await meterAccountWrite("account-1", { writeBucket: "own" });
|
||||
expect(checkRecordWriteRateLimit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("refuses a spent bucket with 429 and the published code", async () => {
|
||||
state.allowed = false;
|
||||
request("PATCH");
|
||||
const refusal = await meterAccountWrite("account-1").catch((e) => e);
|
||||
expect(refusal).toBeInstanceOf(HttpError);
|
||||
expect((refusal as HttpError).statusCode).toBe(429);
|
||||
expect((refusal as HttpError).errorCode).toBe(
|
||||
RECORD_WRITE_RATE_LIMITED_CODE,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* The shared per-account write ceiling, applied where the caller is resolved.
|
||||
*
|
||||
* `checkRecordWriteRateLimit` (300 writes a minute per acting account) used to
|
||||
* be called by hand in a dozen create routes, and the other mutating routes
|
||||
* had no ceiling at all: of 364 mutating handlers, 169 met no limiter, every
|
||||
* PATCH and DELETE on an `[id]` among them. A positive list of routes that
|
||||
* remember to call it can only fall behind the routes that exist, so the
|
||||
* ceiling now sits on the path every authenticated write already takes: the
|
||||
* identity resolvers in `src/lib/api-handler.ts` call {@link meterAccountWrite}
|
||||
* once the caller is known, and it charges the bucket for any request whose
|
||||
* method writes.
|
||||
*
|
||||
* What it does not do:
|
||||
*
|
||||
* - charge a read. GET, HEAD and OPTIONS pass untouched, and so does a POST a
|
||||
* route declared `readOverPost` (a search whose query must not stand in a
|
||||
* URL).
|
||||
* - charge one request twice. A route that resolves the caller through two
|
||||
* helpers (step-up on top of a session) is charged once.
|
||||
* - meter a route that declared its own bucket. The bulk ingest endpoints
|
||||
* the native client syncs through have dedicated ceilings sized for that
|
||||
* volume; charging the shared bucket as well would let a large HealthKit
|
||||
* backfill lock the person out of logging a single dose by hand. Declaring
|
||||
* `writeBucket: "own"` is frozen per route by
|
||||
* `src/__tests__/write-rate-limit-guard.test.ts`, which also requires the
|
||||
* route to call its own limiter.
|
||||
*
|
||||
* The bucket is keyed on the ACTING account, the precedent from the delegable
|
||||
* medication routes: a delegate spends their own allowance rather than the
|
||||
* 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.
|
||||
*/
|
||||
import { HttpError } from "@/lib/api-errors";
|
||||
import { annotate, getEvent } from "@/lib/logging/context";
|
||||
import { checkRecordWriteRateLimit } from "@/lib/rate-limit";
|
||||
|
||||
/** The code a refusal by the shared write bucket carries. */
|
||||
export const RECORD_WRITE_RATE_LIMITED_CODE = "record_write.rate_limited";
|
||||
|
||||
const WRITE_METHODS = new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
||||
|
||||
/** Requests already charged, so a second resolver in one request is free. */
|
||||
const charged = new WeakSet<object>();
|
||||
|
||||
export interface WriteMeterOptions {
|
||||
/** The route answers this POST without writing (see `readOverPost`). */
|
||||
readOverPost?: true;
|
||||
/**
|
||||
* The route meters its writes with a dedicated bucket instead of the shared
|
||||
* one. Reviewed per route by the write rate-limit guard.
|
||||
*/
|
||||
writeBucket?: "own";
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge the acting account's shared write bucket for this request, or refuse
|
||||
* it with 429 when the bucket is spent.
|
||||
*
|
||||
* Outside a request (no wide event, so no method) there is nothing to charge.
|
||||
*/
|
||||
export async function meterAccountWrite(
|
||||
accountId: string,
|
||||
options: WriteMeterOptions = {},
|
||||
): Promise<void> {
|
||||
const evt = getEvent();
|
||||
if (!evt) return;
|
||||
const method = (evt.getHttpMethod() ?? "").toUpperCase();
|
||||
if (!WRITE_METHODS.has(method)) return;
|
||||
if (options.readOverPost === true && method === "POST") return;
|
||||
if (options.writeBucket === "own") {
|
||||
annotate({ meta: { write_bucket: "own" } });
|
||||
return;
|
||||
}
|
||||
if (charged.has(evt)) return;
|
||||
charged.add(evt);
|
||||
|
||||
const rl = await checkRecordWriteRateLimit(accountId);
|
||||
if (!rl.allowed) {
|
||||
annotate({ meta: { record_write_rate_limited: true } });
|
||||
throw new HttpError(
|
||||
429,
|
||||
"Too many writes, try again later",
|
||||
RECORD_WRITE_RATE_LIMITED_CODE,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -356,10 +356,12 @@ export const stdResponses = {
|
||||
*
|
||||
* The batch endpoints have been capped at 60 calls a minute since they were
|
||||
* written and the per-record siblings were not capped at all, which is exactly
|
||||
* backwards: the batch endpoint is the one a well-behaved client uses. The
|
||||
* eleven single-record creates now share one generous per-account bucket, and
|
||||
* this is the response that names it — a client that meets a ceiling should be
|
||||
* able to read which one it met without a bug report.
|
||||
* backwards: the batch endpoint is the one a well-behaved client uses. Every
|
||||
* authenticated write now shares one generous per-account bucket, charged where
|
||||
* the caller is resolved (`src/lib/auth/write-meter.ts`), and this is the
|
||||
* response that names it on the single-record creates that publish it — a
|
||||
* client that meets a ceiling should be able to read which one it met without
|
||||
* a bug report.
|
||||
*
|
||||
* Spread AFTER `...stdResponses` so it replaces the generic 429 on those
|
||||
* operations. The headers block is the one the standard 429 already declares.
|
||||
@@ -371,7 +373,7 @@ export const stdResponses = {
|
||||
export const recordWriteRateLimitResponse = {
|
||||
"429": {
|
||||
description:
|
||||
"Rate limit exceeded. This route shares one per-account bucket with the other single-record writes — `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. 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 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.",
|
||||
content: { "application/json": { schema: errorEnvelope } },
|
||||
headers: stdResponses["429"].headers,
|
||||
},
|
||||
|
||||
+9
-11
@@ -136,25 +136,23 @@ export async function checkEnvironmentBackfillRateLimit(
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared per-account ceiling for the single-record writes.
|
||||
* Shared per-account ceiling for every authenticated write.
|
||||
*
|
||||
* The batch endpoints have been capped at 60 calls a minute since they were
|
||||
* written; the per-record siblings a naive or older client loops over were not
|
||||
* capped at all, which is backwards — the batch endpoint is the one a
|
||||
* well-behaved client uses. This is the floor under the other direction:
|
||||
* `POST /api/measurements`, `/api/mood-entries`, both medication-intake writes,
|
||||
* `/api/labs`, a custom-metric entry, a cycle day-log, a vaccination, an
|
||||
* allergy, a biomarker and an encounter.
|
||||
* well-behaved client uses. This was first the floor under a dozen creates,
|
||||
* called by hand; it is now charged where the caller is resolved
|
||||
* (`meterAccountWrite`, `src/lib/auth/write-meter.ts`), so every POST, PUT,
|
||||
* PATCH and DELETE an authenticated caller sends counts against it, except on
|
||||
* the bulk ingest routes that meter themselves.
|
||||
*
|
||||
* One shared bucket, not one per route, for the reason
|
||||
* `checkAnalyticsReadRateLimit` gives: a loop that rotates across ten creates
|
||||
* `checkAnalyticsReadRateLimit` gives: a loop that rotates across ten routes
|
||||
* should still meet a cap. Three hundred a minute is five writes a second
|
||||
* sustained for a full minute — no hand-driven session and no sync burst
|
||||
* reaches it. Ten of the eleven are wrapped in `withIdempotency`, so an honest
|
||||
* retry storm is deduplicated before it is counted; `POST
|
||||
* /api/medications/intake` is not, because it updates a named `intakeId`
|
||||
* rather than creating a row, so a replay writes the status that is already
|
||||
* there. It exists to cut off the pathological loop, not to shape traffic.
|
||||
* reaches it. It exists to cut off the pathological loop, not to shape
|
||||
* traffic.
|
||||
*
|
||||
* A refused request is counted too: the check runs before the body is read,
|
||||
* so a client looping on a 422 spends its allowance without writing a row.
|
||||
|
||||
Reference in new issue
Block a user