fix(places): don't let the browser cache a refused Google answer

The Places and Geocoding proxies sent Cache-Control: private,
max-age=300 whatever Google answered. A 403 from a key without the
Places API, or a geocode REQUEST_DENIED / OVER_QUERY_LIMIT (which
Google sends as HTTP 200), was then replayed from the browser cache for
five minutes after the key was fixed. Refusals are now no-store.
This commit is contained in:
Rudy Celekli committed 2026-11-04 22:13:01 -05:00
1 parent 7d8b80db0a
commit 2e2f756468
3 files changed
+62 -3

No files matched your search

+1
View File
@@ -5996,6 +5996,7 @@ apply to `public/` assets or built-preview serving.
- `AISSTREAM_API_KEY` is server-side only; the browser reads the same-origin `/api/vessels` cache.
- `/api/google/nearby-places` keeps the Google key out of Places requests issued for voice scene context.
- `/api/google/text-search` keeps the Google key server-side for view-biased Places recovery used by annotation resolution.
- The two Places routes and both geocode routes let the browser keep an answer for five minutes (`private, max-age=300`), but a refusal is `no-store`: a Places error status, or a Geocoding status other than `OK` or `ZERO_RESULTS` (Google sends `REQUEST_DENIED` and `OVER_QUERY_LIMIT` as HTTP 200). A denied or exhausted key is not replayed from the browser cache after it is fixed.
- `/api/overpass` is bounded by body/response caps, per-client/global rate limits, concurrency limits, operator-configured upstreams, in-flight dedupe, cache bounds, and static validation that every selector is spatially bounded.
- `/api/military-installations` uses an independent limiter with the same 90-per-client/300-global one-minute bounds, so viewport installation refreshes never consume `/api/overpass` annotation capacity.
- `/api/route` proxies bounded OSRM route requests for annotation routes, with profile allowlisting, distance caps, response caps, caching, and sanitized "no route found" errors.
+23 -3
View File
@@ -71,6 +71,9 @@ const GEOCODE_UPSTREAM_TIMEOUT_MS = 10_000;
const GEOCODE_MAX_RESPONSE_BYTES = 256 * 1024;
/** Geocoding statuses that answer the query; the rest report a failure. */
const GEOCODE_ANSWER_STATUSES = new Set(['OK', 'ZERO_RESULTS']);
/** Validate a free-text geocoder query before consuming request quota. */
export function validateGeocodeAddress(searchParams) {
const address = String(searchParams.get('address') || '').trim();
@@ -199,7 +202,12 @@ export function googlePlacesContextProxy({
res.statusCode = response.ok ? 200 : response.status;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Cache-Control', 'private, max-age=300');
// A refusal (bad key, API not enabled, quota) is not kept: the browser
// would replay it for five minutes after the key is fixed.
res.setHeader(
'Cache-Control',
response.ok ? 'private, max-age=300' : 'no-store',
);
res.end(
JSON.stringify({
places,
@@ -320,7 +328,12 @@ export function googlePlacesContextProxy({
res.statusCode = response.ok ? 200 : response.status;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Cache-Control', 'private, max-age=300');
// A refusal (bad key, API not enabled, quota) is not kept: the browser
// would replay it for five minutes after the key is fixed.
res.setHeader(
'Cache-Control',
response.ok ? 'private, max-age=300' : 'no-store',
);
res.end(
JSON.stringify({
places,
@@ -446,7 +459,14 @@ export function googlePlacesContextProxy({
res.statusCode = response.ok ? 200 : response.status;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Cache-Control', 'private, max-age=300');
// Geocoding reports a denied key or spent quota as HTTP 200 with a
// status of its own; only an answer is worth keeping.
res.setHeader(
'Cache-Control',
response.ok && GEOCODE_ANSWER_STATUSES.has(data.status)
? 'private, max-age=300'
: 'no-store',
);
res.end(JSON.stringify(data));
} catch {
// Fixed public text: a transport, URL or runtime message would
+38
View File
@@ -185,6 +185,44 @@ for (const preview of [false, true]) {
});
}
test('a refused Places or Geocoding answer is not kept by the browser', async (t) => {
const plugin = googlePlacesContextProxy({
resolveApiKey: () => 'fixture-server-key',
});
const request = install((middlewares) =>
plugin.configureServer({ middlewares }),
);
t.mock.method(globalThis, 'fetch', async () =>
Response.json({ error: { message: 'API key not valid' } }, { status: 403 }),
);
for (const url of [
['/api/google/nearby-places', '?lat=30&lon=-97'],
['/api/google/text-search', '?q=museum&lat=30&lon=-97'],
]) {
const refused = await request(...url);
assert.equal(refused.statusCode, 403);
assert.equal(refused.headers['cache-control'], 'no-store');
}
// Geocoding reports a denied key as HTTP 200 with a status of its own.
let status = 'REQUEST_DENIED';
t.mock.method(globalThis, 'fetch', async () =>
Response.json({ status, results: [] }),
);
for (const denied of ['REQUEST_DENIED', 'OVER_QUERY_LIMIT']) {
status = denied;
const answer = await request('/api/google/geocode', '?address=Austin');
assert.equal(answer.statusCode, 200);
assert.equal(answer.body.status, denied);
assert.equal(answer.headers['cache-control'], 'no-store', denied);
}
for (const answered of ['OK', 'ZERO_RESULTS']) {
status = answered;
const answer = await request('/api/google/geocode', '?address=Austin');
assert.equal(answer.headers['cache-control'], 'private, max-age=300');
}
});
test('OSRM routing preserves aliases, cache, span guards and upstream failure behavior', async (t) => {
const request = install(installRouteMiddleware);
let calls = 0;