Skip to content

A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.

JavaScript

fix(perf): optimize check-user rate limiting to O(1) sliding window and whitelist test key in gitleaks

ArunGupta1526675e2ad509b9
diff --git a/.gitleaksignore b/.gitleaksignore
new file mode 100644
index 0000000..5a0f1c1
--- /dev/null
+++ b/.gitleaksignore
@@ -0,0 +1,2 @@
+# Gitleaks ignore file for false positives in automated unit test fixtures
+e6a819556326f74bbfbecae33a0144bc65f129b1:apps/payment-api/src/__tests__/gateway-hardening-invariants.test.js:generic-api-key:369
diff --git a/apps/payment-api/src/routes/customer.routes.js b/apps/payment-api/src/routes/customer.routes.js
index c46b971..a4f0d9a 100644
--- a/apps/payment-api/src/routes/customer.routes.js
+++ b/apps/payment-api/src/routes/customer.routes.js
@@ -46,6 +46,18 @@ export function registerCustomerRoutes(server, ctx) {
// Throttles check-user inquiries to prevent bulk mobile number scanning / user enumeration.
const checkUserRateMap = new Map();
+ const checkUserRateCleaner = setInterval(() => {
+ const now = Date.now();
+ for (const [ip, timestamps] of checkUserRateMap) {
+ const active = Array.isArray(timestamps) ? timestamps.filter(t => now - t < 60000) : [];
+ if (active.length === 0) {
+ checkUserRateMap.delete(ip);
+ } else {
+ checkUserRateMap.set(ip, active);
+ }
+ }
+ }, 60000);
+ if (checkUserRateCleaner.unref) checkUserRateCleaner.unref();
/**
* Resolves the configured cooldown between consecutive OTP dispatches.
@@ -118,15 +130,14 @@ export function registerCustomerRoutes(server, ctx) {
const ip = req.socket?.remoteAddress || '127.0.0.1';
const now = Date.now();
- for (const [key, at] of checkUserRateMap) {
- if (now - at > 60000) checkUserRateMap.delete(key);
- }
- const ipCount = Array.from(checkUserRateMap.entries()).filter(([k]) => k.startsWith(ip)).length;
- if (ipCount >= 60) {
+ const timestamps = checkUserRateMap.get(ip) || [];
+ const recent = timestamps.filter(t => now - t < 60000);
+ if (recent.length >= 60) {
HttpServer.json(res, 429, { error: 'Too many requests. Please try again in a minute.' });
return;
}
- checkUserRateMap.set(`${ip}_${cleanMobile}_${now}`, now);
+ recent.push(now);
+ checkUserRateMap.set(ip, recent);
const existingUser = ctx.customerRepo.findByMobile(cleanMobile);