mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
fix(webhooks): rate-limit every verb that checks the shared secret
The Withings entrypoints, the Telegram bot webhook and the Coolify deploy hook limited POST and left the verification verbs unlimited. GET and HEAD compare the same secret and answer 200 against 401, which is an oracle for guessing it and a free load channel besides; timingSafeEqual protects the timing channel, not the status code. The project's rule is that the per-source limit runs before secret verification, and it was being applied per handler rather than per file. Each verification verb now runs the same limit its POST sibling used, on the same bucket. Telegram and the deploy hook grow the small local helper the Withings routes already share. HEAD re-shapes the limiter's JSON envelope into a bodyless 429 and keeps the rate-limit headers. The new guard walks every webhook route from the filesystem, resolves which local helpers reach a comparison and which reach the limiter, and fails when an exported verb reaches a comparison without the limiter in front of it. It asserts a non-zero match count so an empty match set cannot read as green. WHOOP already passed: it exports POST only.
This commit is contained in:
1 parent
214b268b41
commit
20812b54f5
10 files changed
+556
-22
No files matched your search
@@ -1155,3 +1155,29 @@ describe("Telegram webhook — interactive measurement reminder (v1.19.0)", () =
|
||||
expect(deleteMessage).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Telegram webhook — GET verification is rate-limited first", () => {
|
||||
// The reachability GET compares the same bot secret the POST does and
|
||||
// answers 200 against 401. Without the limit in front it is an unthrottled
|
||||
// oracle for guessing that secret, so a VALID secret over the limit must
|
||||
// still be refused — that ordering is the whole point.
|
||||
it("returns 429 for a valid secret when over the limit", async () => {
|
||||
vi.mocked(checkRateLimit).mockResolvedValue({
|
||||
allowed: false,
|
||||
remaining: 0,
|
||||
resetAt: Date.now() + 60_000,
|
||||
} as never);
|
||||
|
||||
const res = await GET(
|
||||
new NextRequest("http://localhost/api/telegram/webhook", {
|
||||
headers: { "x-telegram-bot-api-secret-token": "test-secret" },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(res.status).toBe(429);
|
||||
expect(checkRateLimit).toHaveBeenCalledTimes(1);
|
||||
expect(String(vi.mocked(checkRateLimit).mock.calls[0][0])).toMatch(
|
||||
/^telegram-webhook:/,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -32,17 +32,29 @@ function hasValidSecret(request: NextRequest): boolean {
|
||||
return timingSafeEqual(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* The per-source limit both verbs share, on one bucket. It runs before the
|
||||
* secret comparison on every verb that performs one: the comparison answers
|
||||
* 200 against 401, and a constant-time compare hides the timing channel but
|
||||
* not the status code, so the limiter is what makes guessing expensive.
|
||||
*/
|
||||
async function applyWebhookRateLimit(
|
||||
request: NextRequest,
|
||||
): Promise<NextResponse | null> {
|
||||
const ip = getClientIp(request);
|
||||
const rl = await checkRateLimit(`telegram-webhook:${ip}`, 120, 60 * 1000);
|
||||
if (rl.allowed) return null;
|
||||
return NextResponse.json(
|
||||
{ status: "rate_limited" },
|
||||
{ status: 429, headers: rateLimitHeaders(rl) },
|
||||
);
|
||||
}
|
||||
|
||||
export const POST = apiHandler(async (request: NextRequest) => {
|
||||
annotate({ action: { name: "telegram.webhook" } });
|
||||
|
||||
const ip = getClientIp(request);
|
||||
const rl = await checkRateLimit(`telegram-webhook:${ip}`, 120, 60 * 1000);
|
||||
if (!rl.allowed) {
|
||||
return NextResponse.json(
|
||||
{ status: "rate_limited" },
|
||||
{ status: 429, headers: rateLimitHeaders(rl) },
|
||||
);
|
||||
}
|
||||
const limited = await applyWebhookRateLimit(request);
|
||||
if (limited) return limited;
|
||||
|
||||
if (!hasValidSecret(request)) {
|
||||
return NextResponse.json({ status: "unauthorized" }, { status: 401 });
|
||||
@@ -84,6 +96,9 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
export const GET = apiHandler(async (request: NextRequest) => {
|
||||
annotate({ action: { name: "telegram.webhook.verify" } });
|
||||
|
||||
const limited = await applyWebhookRateLimit(request);
|
||||
if (limited) return limited;
|
||||
|
||||
if (!hasValidSecret(request)) {
|
||||
return NextResponse.json({ status: "unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user