Skip to content

Commit c1a2c4a

Browse files
fix(relay): rate-limit webhooks per hook, not per environment
Environment ids appear in every URL given to senders, so a per-environment budget let one junk sender block every hook on that environment. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 06c9db7 commit c1a2c4a

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

‎docs/internals/t3-connect.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ bootstrap, clients send application traffic through the environment's tunnel
66
hostname; the relay Worker does not proxy their HTTP or WebSocket sessions.
77
The one exception is automation webhooks: the relay forwards
88
`/v1/hooks/:environmentId/:hookId/:token` statelessly to the environment's
9-
tunnel so senders get a stable URL. It never stores or logs bodies or tokens,
10-
and the environment owns token and signature verification
9+
tunnel so senders get a stable URL. It stores nothing, keeps bodies and tokens
10+
out of its traces, and leaves token and signature verification to the
11+
environment
1112
([forwarder](../../infra/relay/src/hooks/HookForwarder.ts)).
1213

1314
Clerk, deployment, and native authentication setup live in the

‎infra/relay/src/hooks/HookForwarder.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe("HookForwarder", () => {
194194
}
195195
// Recomputed from the forwarded bytes, not copied from the sender.
196196
expect(sent.headers["content-length"]).toBe(String(body.length));
197-
expect(harness.rateLimitKeys).toEqual([environmentId]);
197+
expect(harness.rateLimitKeys).toEqual([`${environmentId}:hook-1`]);
198198
}),
199199
);
200200

@@ -335,7 +335,7 @@ describe("HookForwarder", () => {
335335
}),
336336
);
337337

338-
it.effect("returns 429 when the environment's budget is spent", () =>
338+
it.effect("returns 429 when the hook's budget is spent", () =>
339339
Effect.gen(function* () {
340340
const harness = makeHarness({ allow: () => false });
341341
const response = yield* harness.send(new Request(hookUrl(), { method: "POST" }));

‎infra/relay/src/hooks/HookForwarder.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ const make = Effect.gen(function* () {
189189
yield* outcome("method_not_allowed");
190190
return errorResponse(405, "method_not_allowed", { allow: "GET, POST, PUT, PATCH" });
191191
}
192-
if (!(yield* rateLimiter.allow(parsed.environmentId))) {
192+
// Keyed per hook: environment ids appear in every URL handed to senders,
193+
// so a per-environment budget would let one junk sender block every hook.
194+
if (!(yield* rateLimiter.allow(`${parsed.environmentId}:${parsed.hookId}`))) {
193195
yield* outcome("rate_limited");
194196
return errorResponse(429, "rate_limited", {
195197
"retry-after": String(RELAY_HOOK_RATE_LIMIT.periodSeconds),

0 commit comments

Comments
 (0)