Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(server): key the API rate limiter by auth token instead of IP (#537)

Gökhan Geyika8dafce4c9e0
diff --git a/server/src/middleware/index.js b/server/src/middleware/index.js
index 016c1c7d7..365c0c404 100644
--- a/server/src/middleware/index.js
+++ b/server/src/middleware/index.js
@@ -1,5 +1,6 @@
import supabaseAdmin from '../config/supabase.js';
import logger from '../lib/logger.js';
+import { markTokenVerified } from './verified-tokens.js';
class Middleware {
async decodeTokenForSocket(socket, next) {
@@ -45,6 +46,9 @@ class Middleware {
return res.status(401).json({ message: 'Unauthorized API Request' });
}
+ // Promote this token to a per-user rate-limit bucket (see rate-limiter.js)
+ markTokenVerified(req.headers.authorization, user.id);
+
req.user = user;
return next();
} catch {
diff --git a/server/src/middleware/rate-limiter.js b/server/src/middleware/rate-limiter.js
index 0f0dc2982..6afc5bfea 100644
--- a/server/src/middleware/rate-limiter.js
+++ b/server/src/middleware/rate-limiter.js
@@ -1,9 +1,52 @@
import rateLimit from 'express-rate-limit';
+import { verifiedUserId } from './verified-tokens.js';
-// General API rate limit: 100 requests per 15 minutes
+/**
+ * Anonymous bucket key. IPv4 (including v4-mapped IPv6) keys on the exact
+ * address; plain IPv6 collapses to the /64 prefix so one caller can't dodge
+ * the limit by rotating through their subnet's practically unlimited
+ * addresses.
+ */
+export function anonymousIpKey(ip) {
+ if (!ip) return 'ip:unknown';
+ if (!ip.includes(':') || ip.includes('.')) return 'ip:' + ip;
+ const [head, tail = ''] = ip.split('::');
+ const headGroups = head ? head.split(':') : [];
+ const tailGroups = tail ? tail.split(':') : [];
+ const zeros = Array(Math.max(0, 8 - headGroups.length - tailGroups.length)).fill('0');
+ const prefix = [...headGroups, ...zeros, ...tailGroups].slice(0, 4).join(':');
+ return 'ip:' + prefix + '::/64';
+}
+
+/**
+ * Key dashboard traffic by VERIFIED user, falling back to IP. The web app's
+ * server actions all egress through Vercel's small shared IP pool, so a purely
+ * IP-keyed limiter lumps every user's server-side traffic (volume analyses,
+ * tracking-status polls, page loads…) into a handful of buckets and 429s
+ * legitimate users while browser-direct calls sail through.
+ *
+ * The per-user bucket is granted only to tokens decodeToken has already
+ * verified (see verified-tokens.js): merely ATTACHING an Authorization header
+ * earns nothing, so an attacker spamming forged tokens stays pinned to their
+ * IP bucket. A legit token's first-ever request counts against the IP bucket
+ * too (it isn't verified yet) — one request per token per half hour, noise.
+ * Rotating IPs to evade the anonymous limit remains an infra-level concern
+ * (WAF/CDN), same as before this limiter existed.
+ */
+export function apiLimiterKey(req) {
+ const userId = verifiedUserId(req.headers.authorization);
+ if (userId) return 'user:' + userId;
+ return anonymousIpKey(req.ip);
+}
+
+// General API rate limit, per verified user (per IP otherwise)
export const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
- max: 250,
+ // A verified user's bucket only needs to absorb their own burst (several
+ // tabs polling tracking status every 15s plus normal navigation).
+ // Unverified traffic keeps the tighter IP limit.
+ max: (req) => (verifiedUserId(req.headers.authorization) ? 1000 : 250),
+ keyGenerator: apiLimiterKey,
standardHeaders: true,
legacyHeaders: false,
message: {
diff --git a/server/src/middleware/rate-limiter.test.js b/server/src/middleware/rate-limiter.test.js
new file mode 100644
index 000000000..7b068d054
--- /dev/null
+++ b/server/src/middleware/rate-limiter.test.js
@@ -0,0 +1,62 @@
+import { describe, it, expect, beforeEach } from 'vitest';
+import { apiLimiterKey } from './rate-limiter.js';
+import { markTokenVerified, clearVerifiedTokens } from './verified-tokens.js';
+
+function reqWith({ authorization, ip = '203.0.113.7' } = {}) {
+ return { headers: authorization ? { authorization } : {}, ip };
+}
+
+describe('rate-limiter – apiLimiterKey', () => {
+ beforeEach(() => clearVerifiedTokens());
+
+ it('keeps unverified Authorization headers in the caller IP bucket', () => {
+ const forged = apiLimiterKey(reqWith({ authorization: 'Bearer forged-1', ip: '1.1.1.1' }));
+ const forged2 = apiLimiterKey(reqWith({ authorization: 'Bearer forged-2', ip: '1.1.1.1' }));
+ const anonymous = apiLimiterKey(reqWith({ ip: '1.1.1.1' }));
+ expect(forged).toBe(anonymous);
+ expect(forged2).toBe(anonymous);
+ });
+
+ it('buckets verified tokens by user across IPs', () => {
+ markTokenVerified('Bearer tok-1', 'user-a');
+ const a = apiLimiterKey(reqWith({ authorization: 'Bearer tok-1', ip: '1.1.1.1' }));
+ const b = apiLimiterKey(reqWith({ authorization: 'Bearer tok-1', ip: '2.2.2.2' }));
+ expect(a).toBe('user:user-a');
+ expect(b).toBe('user:user-a');
+ });
+
+ it('maps a refreshed token for the same user to the same bucket', () => {
+ markTokenVerified('Bearer tok-old', 'user-a');
+ markTokenVerified('Bearer tok-new', 'user-a');
+ const oldKey = apiLimiterKey(reqWith({ authorization: 'Bearer tok-old' }));
+ const newKey = apiLimiterKey(reqWith({ authorization: 'Bearer tok-new' }));
+ expect(oldKey).toBe(newKey);
+ });
+
+ it('gives different verified users different buckets', () => {
+ markTokenVerified('Bearer tok-1', 'user-a');
+ markTokenVerified('Bearer tok-2', 'user-b');
+ const a = apiLimiterKey(reqWith({ authorization: 'Bearer tok-1' }));
+ const b = apiLimiterKey(reqWith({ authorization: 'Bearer tok-2' }));
+ expect(a).not.toBe(b);
+ });
+
+ it('falls back to the caller IP for anonymous requests', () => {
+ const a = apiLimiterKey(reqWith({ ip: '1.1.1.1' }));
+ const b = apiLimiterKey(reqWith({ ip: '2.2.2.2' }));
+ expect(a).not.toBe(b);
+ expect(a).toContain('1.1.1.1');
+ });
+
+ it('collapses an IPv6 subnet into one anonymous bucket', () => {
+ const a = apiLimiterKey(reqWith({ ip: '2001:db8:abcd:12::1' }));
+ const b = apiLimiterKey(reqWith({ ip: '2001:db8:abcd:12::2' }));
+ expect(a).toBe(b);
+ });
+
+ it('keeps v4-mapped IPv6 addresses distinct per IPv4 address', () => {
+ const a = apiLimiterKey(reqWith({ ip: '::ffff:1.1.1.1' }));
+ const b = apiLimiterKey(reqWith({ ip: '::ffff:2.2.2.2' }));
+ expect(a).not.toBe(b);
+ });
+});
diff --git a/server/src/middleware/verified-tokens.js b/server/src/middleware/verified-tokens.js
new file mode 100644
index 000000000..c2af17637
--- /dev/null
+++ b/server/src/middleware/verified-tokens.js
@@ -0,0 +1,48 @@
+import { createHash } from 'node:crypto';
+
+/**
+ * Short-lived cache of Authorization headers that decodeToken has already
+ * verified against Supabase Auth, mapped to their user id. The rate limiter
+ * consults it to give real users per-user buckets WITHOUT trusting the header
+ * blindly: a forged or garbage token never gets verified, so it never earns a
+ * bucket of its own and keeps counting against the caller's IP limit.
+ *
+ * Only the SHA-256 of the header is stored, never the raw JWT. Entries expire
+ * after 30 minutes (well under the token's own lifetime) and the map is
+ * capped, evicting oldest-inserted first, so it can't grow unbounded.
+ */
+const TTL_MS = 30 * 60 * 1000;
+const MAX_ENTRIES = 5000;
+const cache = new Map();
+
+function hashAuthHeader(authorizationHeader) {
+ return createHash('sha256').update(authorizationHeader).digest('hex').slice(0, 32);
+}
+
+export function markTokenVerified(authorizationHeader, userId) {
+ if (!authorizationHeader || !userId) return;
+ if (cache.size >= MAX_ENTRIES) {
+ cache.delete(cache.keys().next().value);
+ }
+ cache.set(hashAuthHeader(authorizationHeader), {
+ userId,
+ expiresAt: Date.now() + TTL_MS,
+ });
+}
+
+export function verifiedUserId(authorizationHeader) {
+ if (!authorizationHeader) return null;
+ const key = hashAuthHeader(authorizationHeader);
+ const hit = cache.get(key);
+ if (!hit) return null;
+ if (hit.expiresAt < Date.now()) {
+ cache.delete(key);
+ return null;
+ }
+ return hit.userId;
+}
+
+// Test hook — the limiter's behavior depends on cache state.
+export function clearVerifiedTokens() {
+ cache.clear();
+}