Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions web/services/relay/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export function enforceRelayRateLimit(input: {
}
const ruleId = input.ruleId?.trim();
if (!ruleId) {
return Effect.fail(
new RelayConfigurationError({ code: "rate_limit_not_configured" }),
);
// No configured rule means the operator wants no rate limiting. Failing
// here would turn a deliberately-unset env var into a relay outage.
return Effect.void;
}
return Effect.tryPromise({
try: () => input.check(ruleId, {
Expand All @@ -64,6 +64,14 @@ export function enforceRelayRateLimit(input: {
: {}),
}));
}
if (error === "not-found") {
// The configured rule no longer exists (Vercel returns 404). That
// means the operator deleted the limit, so treat it as "no limit" and
// fail open rather than 503-ing every request. Genuine unavailability
// (a thrown check or an unexpected status) still fails closed below.
console.warn("relay rate-limit rule not found; failing open");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The new warning uses a bare string, while every other operational event in this file uses a structured key + payload (e.g. console.error("relay.policy.unavailable", tag)). Using a consistent key makes the event filterable in log aggregation.

Suggested change
console.warn("relay rate-limit rule not found; failing open");
console.warn("relay.rate_limit.rule_not_found", { ruleId });

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

return Effect.void;
}
if (error) {
return Effect.fail(
new RelayRateLimitError({ code: "rate_limit_unavailable" }),
Expand Down
42 changes: 41 additions & 1 deletion web/tests/relay-token-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,50 @@ describe("POST /api/relay/token", () => {
expect(invalid.status).toBe(400);
expect(checks).toBe(1);

const blocked = await handleRelayTokenRequest(
request({ endpointId: ENDPOINT_ID }),
deps({
isVercel: () => true,
rateLimitRuleId: () => "relay-token",
checkRateLimit: async () => ({ rateLimited: false, error: "blocked" }),
}),
);
expect(blocked.status).toBe(429);

const unavailable = await handleRelayTokenRequest(
request({ endpointId: ENDPOINT_ID }),
deps({ isVercel: () => true, rateLimitRuleId: () => undefined }),
deps({
isVercel: () => true,
rateLimitRuleId: () => "relay-token",
checkRateLimit: async () => {
throw new Error("firewall unreachable");
},
}),
);
expect(unavailable.status).toBe(503);
});

test("skips rate limiting when no rule is configured", async () => {
// An unset rule id env var means the operator wants no rate limiting.
// This must mint credentials, not 503 every device off the relay network.
const response = await handleRelayTokenRequest(
request({ endpointId: ENDPOINT_ID }),
deps({ isVercel: () => true, rateLimitRuleId: () => undefined }),
);
expect(response.status).toBe(200);
});

test("fails open when the configured rate-limit rule no longer exists", async () => {
// Vercel reports a deleted firewall rule as not-found. That is an operator
// action, not an outage, so the mint must proceed as if unlimited.
const response = await handleRelayTokenRequest(
request({ endpointId: ENDPOINT_ID }),
deps({
isVercel: () => true,
rateLimitRuleId: () => "relay-token",
checkRateLimit: async () => ({ rateLimited: false, error: "not-found" }),
}),
);
expect(response.status).toBe(200);
});
});