CCTV Street View budget: key the client before the camera wait, share the default

- The /frame handler reads the client's network key (IPv6 /64) before it
  awaits the camera, so a client that drops the connection is still
  charged to its own window instead of the shared fallback key.
- The budget is charged only after the Google key and finite catalog
  coordinates are confirmed, and the key is read once.
- Places and the CCTV window share one default (GOOGLE_DEFAULT_PER_MIN).
- Tests: the catalog-only test checks heading/fov/pitch from the query at
  the catalog location; the budget test covers unknown ids, forwarded
  requests and /64 grouping; a real-socket test covers a dropped
  connection. The duplicate pose test is removed.
- .env.example, SECURITY.md, CURRENT-STATE and the changelog fragment
  describe the separate per-IP window, the catalog position and the
  placeholder for seed cameras.
This commit is contained in:
German Khokhlov committed 2026-11-20 11:31:14 +04:00
1 parent 01368b95bf
commit 01d4a3f454
10 files changed
+175 -201

No files matched your search

+35 -5
View File
@@ -553,12 +553,14 @@ test('the Street View fallback frames catalog cameras only, from the catalog pos
assert.deepEqual(streetView(), [], 'no Google call for an unknown id');
const steered = await app.call(
`/frame/${STILL.id}?lat=48.85&lon=2.29&heading=90`,
`/frame/${STILL.id}?lat=48.85&lon=2.29&heading=90&fov=74&pitch=-17`,
);
assert.equal(steered.headers['X-CCTV-Source'], 'streetview');
const location = new URL(streetView()[0].url).searchParams.get('location');
assert.equal(location, `${STILL.lat},${STILL.lon}`);
assert.equal(new URL(streetView()[0].url).searchParams.get('heading'), '90');
const sv = new URL(streetView()[0].url).searchParams;
assert.equal(sv.get('location'), `${STILL.lat},${STILL.lon}`);
assert.equal(sv.get('heading'), '90');
assert.equal(sv.get('fov'), '74');
assert.equal(sv.get('pitch'), '-17');
});
test('the Street View fallback spends a per-IP Google budget, and only with a key', async (t) => {
@@ -586,8 +588,27 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
assert.equal(streetViewCalls(), 0);
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
// Neither an unknown id nor a forwarded request spends the window.
assert.equal(
(await app.call('/frame/not-a-camera', {}, first)).headers['X-CCTV-Source'],
'synthetic',
);
assert.equal(
(
await app.call(
`/frame/${STILL.id}`,
{ 'x-forwarded-for': '203.0.113.9' },
first,
)
).headers['X-CCTV-Source'],
'synthetic',
);
assert.deepEqual(
[await frameSource(first), await frameSource(first), await frameSource(first)],
[
await frameSource(first),
await frameSource(first),
await frameSource(first),
],
['streetview', 'streetview', 'synthetic'],
);
assert.equal(streetViewCalls(), 2, 'no Google call over the budget');
@@ -598,4 +619,13 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
'streetview',
);
assert.equal(streetViewCalls(), 3);
// Addresses within one IPv6 /64 share a budget.
const v6 = (remoteAddress) => frameSource({ remoteAddress });
assert.deepEqual(
[await v6('2001:db8::1'), await v6('2001:db8::2'), await v6('2001:db8::3')],
['streetview', 'streetview', 'synthetic'],
);
assert.equal(await v6('2001:db8:0:1::1'), 'streetview', 'another /64');
assert.equal(streetViewCalls(), 6);
});
@@ -0,0 +1,87 @@
// Real sockets: a client that closes its connection while the camera upstream
// is still pending must not get a Street View call outside its own budget.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { cctvProxy } from '../../server/providers/cctv.js';
const CAM = {
id: 'still-fixture',
name: 'Still fixture',
lat: 30.27,
lon: -97.74,
feedType: 'image',
url: 'https://cams.upstream.invalid/still.jpg',
};
test('a client that disconnects mid-fetch stays inside its own Street View budget', async (t) => {
const saved = { ...process.env };
process.env.CCTV_SOURCES_JSON = JSON.stringify([CAM]);
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
process.env.CCTV_FORCE_AUSTIN = '0';
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
delete process.env.GOOGLE_MAPS_API_KEY;
process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = '1';
const nativeFetch = globalThis.fetch;
let streetViewCalls = 0;
globalThis.fetch = async (url) => {
if (String(url).startsWith('https://maps.googleapis.com/')) {
streetViewCalls += 1;
return new Response('street-view', {
headers: { 'content-type': 'image/jpeg' },
});
}
await new Promise((r) => setTimeout(r, 400));
return new Response(null, { status: 503 });
};
let handler;
cctvProxy().configureServer({
middlewares: { use: (_r, fn) => (handler = fn) },
});
const server = http.createServer((req, res) => {
req.url = req.url.replace(/^\/api\/cctv/, '');
handler(req, res, () => res.end());
});
await new Promise((r) => server.listen(0, '127.0.0.1', r));
t.after(() => {
server.close();
globalThis.fetch = nativeFetch;
for (const k of Object.keys(process.env))
if (!(k in saved)) delete process.env[k];
Object.assign(process.env, saved);
});
const { port } = server.address();
const get = (abortAfterMs) =>
new Promise((resolve) => {
const req = http.get(
{
host: '127.0.0.1',
port,
path: `/api/cctv/frame/${CAM.id}`,
headers: { host: `localhost:${port}` },
agent: false,
},
(res) => {
res.resume();
res.on('end', () => resolve(res.headers['x-cctv-source']));
},
);
req.on('error', () => resolve('aborted'));
if (abortAfterMs)
setTimeout(() => {
req.destroy();
resolve('aborted');
}, abortAfterMs);
});
assert.equal(await get(), 'streetview');
assert.equal(await get(), 'synthetic', 'the cap of one is spent');
assert.equal(streetViewCalls, 1);
await get(50);
await new Promise((r) => setTimeout(r, 800));
assert.equal(
streetViewCalls,
1,
'an aborted request spent the key outside its own budget',
);
});
-141
View File
@@ -1,141 +0,0 @@
// When a catalog camera has no pose, the browser sends /frame a heading derived
// from the camera ID, a pitch of -17°, and a field of view of 74°. Street View
// uses that pose at the camera's catalog coordinates.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { cctvProxy } from '../../server/providers/cctv.js';
// Same shape as a world-cameras row: an image URL, position, and no
// headingDeg / pitchDeg / fovDeg.
const POSELESS = {
id: 'world-or-fixture',
name: 'Pose-less fixture',
lat: 44.05,
lon: -123.09,
feedType: 'image',
url: 'https://cams.upstream.invalid/still.jpg',
};
/** The browser's id-hash heading (src/layers/cctv/model.js headingFromId). */
function browserHeadingFromId(id) {
let acc = 0;
for (let i = 0; i < id.length; i++) acc = (acc * 33 + id.charCodeAt(i)) >>> 0;
return ((((acc % 16) * 22.5) % 360) + 360) % 360;
}
function response({ status = 200, headers = {}, body = '' }) {
return new Response(status === 204 ? null : String(body), {
status,
headers,
});
}
function recording() {
const chunks = [];
let finished;
const done = new Promise((resolve) => (finished = resolve));
const res = {
statusCode: 0,
headers: {},
writableEnded: false,
writeHead(status, headers) {
res.statusCode = status;
res.headers = { ...res.headers, ...headers };
},
setHeader(name, value) {
res.headers[name] = value;
},
write(chunk) {
chunks.push(Buffer.from(chunk));
return true;
},
end(chunk) {
if (chunk) chunks.push(Buffer.from(chunk));
res.writableEnded = true;
res.body = Buffer.concat(chunks).toString('utf8');
finished();
},
on() {},
once() {},
emit() {},
destroy() {},
};
return { res, done };
}
test('the Street View fallback frames a pose-less catalog camera the way the browser draws it', async (t) => {
const saved = Object.fromEntries(
[
'CCTV_SOURCES_JSON',
'CCTV_SOURCES_FILE',
'CCTV_FORCE_AUSTIN',
'GOOGLE_MAPS_SERVER_API_KEY',
'GOOGLE_MAPS_API_KEY',
'GEV_RATELIMIT_GOOGLE_PER_MIN',
].map((name) => [name, process.env[name]]),
);
process.env.CCTV_SOURCES_JSON = JSON.stringify([POSELESS]);
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
process.env.CCTV_FORCE_AUSTIN = '0';
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
delete process.env.GOOGLE_MAPS_API_KEY;
delete process.env.GEV_RATELIMIT_GOOGLE_PER_MIN;
const nativeFetch = globalThis.fetch;
const streetView = [];
globalThis.fetch = async (url) => {
const href = String(url);
if (href.startsWith('https://maps.googleapis.com/')) {
streetView.push(new URL(href));
return response({
headers: { 'content-type': 'image/jpeg' },
body: 'street-view',
});
}
return response({ status: 503 }); // the camera's upstream is down
};
t.after(() => {
globalThis.fetch = nativeFetch;
for (const [name, value] of Object.entries(saved)) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
});
let handler;
cctvProxy().configureServer({
middlewares: { use: (_route, fn) => (handler = fn) },
});
// What src/layers/cctv/source.js sends for this camera with no calibration.
const heading = Math.round(browserHeadingFromId(POSELESS.id));
const params = new URLSearchParams({
label: POSELESS.name,
city: '',
lat: POSELESS.lat.toFixed(6),
lon: POSELESS.lon.toFixed(6),
heading: String(heading),
fov: '74',
pitch: '-17',
ts: '1',
});
const { res, done } = recording();
await handler(
{
url: `/frame/${POSELESS.id}?${params}`,
headers: {},
method: 'GET',
on() {},
},
res,
);
await done;
assert.equal(res.headers['X-CCTV-Source'], 'streetview');
assert.equal(streetView.length, 1);
const sv = streetView[0].searchParams;
assert.equal(sv.get('location'), `${POSELESS.lat},${POSELESS.lon}`);
assert.equal(Number(sv.get('heading')), heading, 'heading the browser draws');
assert.equal(Number(sv.get('fov')), 74, 'FOV the browser draws');
assert.equal(Number(sv.get('pitch')), -17, 'pitch the browser draws');
});