mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-23 03:32:19 +00:00
fix(adsbdb): rate-limit /api/adsbdb and cap its enrichment caches
Both keys this route accepts come from the caller — a callsign is 2-8 of [A-Z0-9], a hex is six of [0-9a-f] — and every distinct key that reached upstream was cached, 404s included, with nothing ever removing one. A caller walking the keyspace could therefore drive unlimited live requests at the free community API and grow `.gev-cache/adsbdb.json` without end. Admit 360 requests/minute per client address, global backstop 3x, refusing the rest with 429 and `Retry-After` before any cache, disk or upstream work. The browser cannot exceed 300/min by construction: ENRICH_DISPATCH_GAP_MS is 200 ms, so the enrichment queue drips at most five lookups a second. Hold each store to 20,000 entries, dropping least-recently-written first, and discard entries past the existing 24-hour TTL when the file is read back. A cache file above 16 MB is skipped rather than parsed. Sized from a real session's own file: 397 aircraft entries (~100 B) and 3 route entries (~231 B), so the ceiling is about fifty times an ordinary working set and bounds the file at roughly 7 MB. The two stores become Maps: `size` is O(1), so the ceiling can be enforced on every write, and iteration is true insertion order — an all-digit callsign like "1234" sorts ahead of everything else as an object key. The on-disk JSON shape is unchanged. Refs #261
This commit is contained in:
1 parent
b210ab0fe4
commit
03814d8c47
7 files changed
+362
-18
No files matched your search
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { readResponseTextCapped, coalesceProxyRequest } from './sources/httpBody.js';
|
||||
import { makeRateLimiter, clientKey } from '../server/providers/common/rate-limit.js';
|
||||
|
||||
const source = ['local.js', 'common/http.js', 'aircraft/enrichment.js', 'terrain.js', 'space/celestrak.js', 'space/launch-library.js', '../../src/data/spaceProviderRequests.js']
|
||||
.map(file => readFileSync(new URL(`../server/providers/${file}`, import.meta.url), 'utf8'))
|
||||
@@ -22,6 +23,9 @@ function fixture(name, overrides = {}, preview = false) {
|
||||
const logs = [];
|
||||
const deps = {
|
||||
readResponseTextCapped, coalesceProxyRequest,
|
||||
// The rate-limited proxies reach for these; give them the real ones so a
|
||||
// fixture request is throttled exactly as production would be.
|
||||
makeRateLimiter, clientKey,
|
||||
path, process: { cwd: () => '/fixture', env: {} },
|
||||
fsp: {
|
||||
readFile: async () => { throw new Error('cache absent'); },
|
||||
|
||||
Reference in new issue
Block a user