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.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(server): read /track's body only with its own 64 KB parser, after its limiter (#1533)
Gökhan Geyikced91f849d6c
diff --git a/server/src/routes/traffic-track.test.js b/server/src/routes/traffic-track.test.jsindex 900ae371e..42253dd34 100644--- a/server/src/routes/traffic-track.test.js+++ b/server/src/routes/traffic-track.test.js@@ -306,4 +306,35 @@ describe('POST /track/:trackingCode', () => {expect(updated).toHaveLength(1);expect(inserted).toEqual([]);});++ it('reads an application/json beacon itself, as text, without the server-wide JSON parser', async () => {+ findVisitOf.mockResolvedValue(null);+ const res = await fetch(baseUrl, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json', 'X-Forwarded-For': '203.0.113.7' },+ body: JSON.stringify({+ u: 'https://acme.example/menu',+ s: 'gemini.google.com',+ a: 'Mozilla/5.0',+ }),+ });+ expect(res.status).toBe(204);+ expect(inserted[0].row).toMatchObject({ url: 'https://acme.example/menu' });+ });++ it('refuses a body over 64 KB, whatever its type, before reading it all', async () => {+ for (const type of ['text/plain', 'application/json']) {+ const res = await fetch(baseUrl, {+ method: 'POST',+ headers: { 'Content-Type': type, 'X-Forwarded-For': '203.0.113.7' },+ body: JSON.stringify({+ u: 'https://acme.example/',+ s: 'gemini.google.com',+ pad: 'x'.repeat(70_000),+ }),+ });+ expect(res.status, type).toBe(413);+ }+ expect(inserted).toEqual([]);+ });});diff --git a/server/src/routes/traffic.js b/server/src/routes/traffic.jsindex 5b49bfc7f..0cb8798eb 100644--- a/server/src/routes/traffic.js+++ b/server/src/routes/traffic.js@@ -18,8 +18,8 @@ const router = Router();// request — application/json would require a preflight, which beacons can't do,// so the browser silently blocks them. Read the body as text here; the handler// JSON-parses it. application/json is still accepted (older cached t.js, direct-// API callers): the global json parser handles those and this middleware no-ops-// once a body has already been parsed.+// API callers) and read the same way: these routes are mounted before the+// server's JSON parser, so a body is read here, up to 64 KB, whatever its type.const beaconBody = express.text({ type: ['text/plain', 'application/json'], limit: '64kb' });let cachedScript = null;@@ -91,8 +91,8 @@ router.post('/track/:trackingCode', trafficLimiter, beaconBody, async (req, res)return res.status(404).json({ ok: false });}- // Body arrives as a JSON string (text/plain beacon) or an already-parsed- // object (application/json via the global json parser). Normalize both.+ // Body arrives as a JSON string (beaconBody reads every type as text); an+ // object only if something upstream parsed it first. Normalize both.let body = req.body;if (typeof body === 'string') {try {diff --git a/server/src/server.js b/server/src/server.jsindex 59e74ce30..d124a4b86 100644--- a/server/src/server.js+++ b/server/src/server.js@@ -69,12 +69,16 @@ app.use('/cloro/callback', express.raw({ type: 'application/json', limit: '50mb'// parsers: the route checks the secret first, then reads the Worker's JSON,// the gzipped Logpush file or the Vercel drain's logs raw itself.app.use('/', botIngestRoutes);-app.use(bodyParser.json({ limit: '50mb' }));-app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));// --- Public traffic tracking (before helmet/cors — needs its own CORS for any origin) ---+// Mounted before the JSON and form parsers too: anyone can post to /track, so+// its own limiter runs first and its body is read only up to its own 64 KB,+// whatever its content type, never by the 50 MB parsers below.app.use('/', trafficRoutes);+app.use(bodyParser.json({ limit: '50mb' }));+app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));+// --- Request logger (before everything to catch all requests) ---app.use(requestIdMiddleware);app.use((req, res, next) => {