mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-05 10:53:59 +00:00
fix(providers): validate before limiting and tighten capped image reads
Apply shared Places validation, owned image chunks, and response-header deadline tests to the provider modules.
This commit is contained in:
1 parent
59a20e2d18
commit
3d956177ab
8 files changed
+440
-60
No files matched your search
+178
-2
@@ -1,9 +1,27 @@
|
||||
import { CCTV_FRAME_FETCH_TIMEOUT_MS, CCTV_FRAME_MAX_BODY_BYTES, CCTV_MEDIA_FETCH_TIMEOUT_MS, CCTV_MEDIA_MAX_BODY_BYTES } from '../../server/providers/cctv/constants.js';
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
CCTV_FRAME_FETCH_TIMEOUT_MS,
|
||||
fetchCctvImageFromUpstream,
|
||||
} from '../../vite.config.js';
|
||||
fetchCctvMediaUpstream,
|
||||
} from '../../server/providers/cctv/media.js';
|
||||
|
||||
/** A body that arrives in chunks and never declares a Content-Length. */
|
||||
function chunkedImageResponse(chunkBytes, chunkCount, { onChunk = () => {}, onCancel = () => {} } = {}) {
|
||||
const stream = new ReadableStream({
|
||||
async pull(controller) {
|
||||
if (chunkCount <= 0) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
chunkCount -= 1;
|
||||
onChunk();
|
||||
controller.enqueue(new Uint8Array(chunkBytes));
|
||||
},
|
||||
cancel: onCancel,
|
||||
});
|
||||
return new Response(stream, { status: 200, headers: { 'Content-Type': 'image/jpeg' } });
|
||||
}
|
||||
|
||||
test('CCTV upstream frame fetch supplies a bounded abort signal', async () => {
|
||||
let observedSignal = null;
|
||||
@@ -36,3 +54,161 @@ test('CCTV upstream frame fetch returns a valid image response', async () => {
|
||||
assert.equal(result?.contentType, 'image/jpeg');
|
||||
assert.deepEqual(result?.body, Buffer.from([1, 2, 3]));
|
||||
});
|
||||
|
||||
test('CCTV upstream frame fetch rejects a declared oversize body without draining it', async () => {
|
||||
const chunkBytes = 64 * 1024;
|
||||
let pulled = 0;
|
||||
let cancelled = false;
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 1000,
|
||||
maxBytes: chunkBytes * 4,
|
||||
fetchImpl: async () => {
|
||||
const response = chunkedImageResponse(chunkBytes, 64, {
|
||||
onChunk: () => { pulled += 1; },
|
||||
onCancel: () => { cancelled = true; },
|
||||
});
|
||||
response.headers.set('Content-Length', String(chunkBytes * 64));
|
||||
return response;
|
||||
},
|
||||
});
|
||||
|
||||
// Assert on the byte count, not the object: a regressed proxy returns a
|
||||
// multi-megabyte Buffer here, and diffing one into the failure report is
|
||||
// slower than the check it is reporting on.
|
||||
assert.equal(result?.body?.length ?? null, null, 'a declared oversize snapshot is a miss, not a buffered body');
|
||||
assert.equal(cancelled, true, 'the declared cap is enforced by cancelling, not reading');
|
||||
// Only the stream's own one-chunk prefetch may have run; the proxy pulls none.
|
||||
assert.ok(pulled <= 1, `declared cap short-circuits the read, pulled ${pulled} chunks`);
|
||||
});
|
||||
|
||||
test('CCTV upstream frame fetch aborts an undeclared body once it crosses the cap', async () => {
|
||||
const chunkBytes = 64 * 1024;
|
||||
let pulled = 0;
|
||||
let cancelled = false;
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 1000,
|
||||
maxBytes: chunkBytes * 4,
|
||||
// Sixty-four chunks are offered with no Content-Length; a proxy that
|
||||
// buffers first would take all of them.
|
||||
fetchImpl: async () => chunkedImageResponse(chunkBytes, 64, {
|
||||
onChunk: () => { pulled += 1; },
|
||||
onCancel: () => { cancelled = true; },
|
||||
}),
|
||||
});
|
||||
|
||||
assert.equal(result?.body?.length ?? null, null, 'a chunked body over the cap is a miss');
|
||||
// Four chunks fit, the fifth crosses the cap, and one more sits in the
|
||||
// stream's prefetch queue — nothing past that is ever pulled.
|
||||
assert.ok(pulled <= 6, `stopped reading at the cap, pulled ${pulled} chunks`);
|
||||
assert.equal(cancelled, true, 'the upstream stream is cancelled, not drained');
|
||||
});
|
||||
|
||||
test('CCTV upstream frame fetch still returns a chunked body under the cap', async () => {
|
||||
const chunkBytes = 1024;
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 1000,
|
||||
maxBytes: chunkBytes * 8,
|
||||
fetchImpl: async () => chunkedImageResponse(chunkBytes, 3),
|
||||
});
|
||||
|
||||
assert.equal(result?.ok, true);
|
||||
assert.equal(result?.body?.length, chunkBytes * 3, 'every chunk is reassembled in order');
|
||||
assert.ok(CCTV_FRAME_MAX_BODY_BYTES > 0 && CCTV_FRAME_MAX_BODY_BYTES <= CCTV_MEDIA_MAX_BODY_BYTES,
|
||||
'the frame cap must be at or under the media route cap — pinned against the real constant');
|
||||
});
|
||||
|
||||
test('CCTV media upstream fetch aborts when response headers never arrive', async () => {
|
||||
let observedSignal = null;
|
||||
const startedAt = Date.now();
|
||||
await assert.rejects(
|
||||
fetchCctvMediaUpstream('https://example.com/stream.m3u8', {
|
||||
timeoutMs: 25,
|
||||
fetchImpl: (url, { signal }) => new Promise((resolve, reject) => {
|
||||
observedSignal = signal;
|
||||
signal.addEventListener('abort', () => reject(Object.assign(new Error('aborted'), { name: 'AbortError' })));
|
||||
}),
|
||||
}),
|
||||
(error) => error.name === 'AbortError',
|
||||
);
|
||||
assert.equal(observedSignal?.aborted, true, 'the header deadline aborts the attempt');
|
||||
assert.ok(Date.now() - startedAt < 2_000, 'the deadline is the injected one, not the production one');
|
||||
assert.ok(CCTV_MEDIA_FETCH_TIMEOUT_MS >= 10_000, 'production keeps a generous header deadline for slow cameras');
|
||||
});
|
||||
|
||||
test('CCTV media upstream fetch never cuts a stream whose headers arrived in time', async () => {
|
||||
let observedSignal = null;
|
||||
const upstream = await fetchCctvMediaUpstream('https://example.com/stream.m3u8', {
|
||||
timeoutMs: 20,
|
||||
fetchImpl: async (url, { signal }) => {
|
||||
observedSignal = signal;
|
||||
return { ok: true, status: 200, headers: new Map([['content-type', 'video/mp2t']]), body: null };
|
||||
},
|
||||
});
|
||||
assert.equal(upstream.ok, true);
|
||||
// Well past the header deadline: the timer was cleared at header arrival,
|
||||
// so the live body is still allowed to flow.
|
||||
await new Promise((resolve) => setTimeout(resolve, 60));
|
||||
assert.equal(observedSignal?.aborted, false, 'a slow body after timely headers is never aborted here');
|
||||
});
|
||||
|
||||
test('CCTV upstream frame fetch keeps a body that lands exactly on the cap', async () => {
|
||||
const chunkBytes = 512;
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 1000,
|
||||
maxBytes: chunkBytes * 4,
|
||||
fetchImpl: async () => chunkedImageResponse(chunkBytes, 4),
|
||||
});
|
||||
assert.equal(result?.ok, true, 'exactly-at-cap is under the ceiling, not over it');
|
||||
assert.equal(result?.body?.length, chunkBytes * 4);
|
||||
});
|
||||
|
||||
test('CCTV upstream frame fetch ignores a garbage Content-Length and still caps the stream', async () => {
|
||||
const chunkBytes = 1024;
|
||||
let cancelled = false;
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 1000,
|
||||
maxBytes: chunkBytes * 2,
|
||||
fetchImpl: async () => {
|
||||
const response = chunkedImageResponse(chunkBytes, 8, { onCancel: () => { cancelled = true; } });
|
||||
response.headers.set('Content-Length', 'not-a-number');
|
||||
return response;
|
||||
},
|
||||
});
|
||||
assert.equal(result?.body?.length ?? null, null, 'an unparseable length falls back to counting bytes');
|
||||
assert.equal(cancelled, true);
|
||||
});
|
||||
|
||||
test('CCTV upstream frame fetch retains owned bytes, not the chunk\'s backing allocation', async () => {
|
||||
const backing = new ArrayBuffer(4 * 1024 * 1024);
|
||||
const view = new Uint8Array(backing, 8, 3);
|
||||
view.set([7, 8, 9]);
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 1000,
|
||||
maxBytes: 1024,
|
||||
fetchImpl: async () => new Response(new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(view);
|
||||
controller.close();
|
||||
},
|
||||
}), { status: 200, headers: { 'Content-Type': 'image/jpeg' } }),
|
||||
});
|
||||
assert.equal(result?.ok, true);
|
||||
assert.deepEqual(Array.from(result.body), [7, 8, 9]);
|
||||
assert.ok(result.body.buffer.byteLength < backing.byteLength,
|
||||
'the 4 MB backing allocation is not retained behind a 3-byte frame');
|
||||
});
|
||||
|
||||
test('CCTV upstream frame fetch refuses a body it cannot stream instead of buffering it uncapped', async () => {
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 1000,
|
||||
maxBytes: 1024,
|
||||
fetchImpl: async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
headers: new Map([['content-type', 'image/jpeg']]),
|
||||
body: { /* neither async-iterable nor reader-backed */ },
|
||||
arrayBuffer: async () => { throw new Error('arrayBuffer must never be used as an uncapped fallback'); },
|
||||
}),
|
||||
});
|
||||
assert.equal(result, null);
|
||||
});
|
||||
@@ -0,0 +1,91 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { googlePlacesContextProxy, validatePlacesCoordinates } from '../server/providers/places/google.js';
|
||||
|
||||
function installGooglePlacesRoutes() {
|
||||
const routes = new Map();
|
||||
googlePlacesContextProxy({ resolveApiKey: () => process.env.GOOGLE_MAPS_API_KEY }).configureServer({
|
||||
middlewares: {
|
||||
use(path, handler) {
|
||||
routes.set(path, handler);
|
||||
},
|
||||
},
|
||||
});
|
||||
return routes;
|
||||
}
|
||||
|
||||
function invokeRoute(handler, { method = 'GET', url = '/', remoteAddress = '127.0.0.1' } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headers = new Map();
|
||||
const req = { method, url, headers: {}, socket: { remoteAddress } };
|
||||
const res = {
|
||||
statusCode: 200,
|
||||
setHeader(name, value) { headers.set(String(name).toLowerCase(), String(value)); },
|
||||
end(body = '') {
|
||||
resolve({ statusCode: this.statusCode, headers: Object.fromEntries(headers), body: body ? JSON.parse(String(body)) : null });
|
||||
},
|
||||
};
|
||||
Promise.resolve(handler(req, res)).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
test('validatePlacesCoordinates refuses missing, blank, non-numeric, and out-of-range values', () => {
|
||||
const check = (query) => validatePlacesCoordinates(new URLSearchParams(query));
|
||||
for (const bad of ['', 'lat=30.27', 'lon=-97.74', 'lat=&lon=', 'lat=%20&lon=-97.74', 'lat=abc&lon=-97.74',
|
||||
'lat=Infinity&lon=-97.74', 'lat=NaN&lon=0', 'lat=90.001&lon=0', 'lat=-90.001&lon=0', 'lat=0&lon=180.5', 'lat=0&lon=-180.5']) {
|
||||
assert.equal(check(bad).ok, false, `${bad || '(empty)'} must be refused`);
|
||||
}
|
||||
// Inclusive boundaries are real places (the poles, the antimeridian).
|
||||
assert.deepEqual(check('lat=90&lon=-180'), { ok: true, latitude: 90, longitude: -180 });
|
||||
assert.deepEqual(check('lat=-90&lon=180'), { ok: true, latitude: -90, longitude: 180 });
|
||||
assert.deepEqual(check('lat=30.27&lon=-97.74'), { ok: true, latitude: 30.27, longitude: -97.74 });
|
||||
});
|
||||
|
||||
test('both Places routes answer bad coordinates with a 400 before the limiter and before Google', async () => {
|
||||
const previousKey = process.env.GOOGLE_MAPS_API_KEY;
|
||||
const previousLimit = process.env.GEV_RATELIMIT_GOOGLE_PER_MIN;
|
||||
const originalFetch = globalThis.fetch;
|
||||
process.env.GOOGLE_MAPS_API_KEY = 'test-key';
|
||||
process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = '1';
|
||||
let upstreamCalls = 0;
|
||||
globalThis.fetch = async () => {
|
||||
upstreamCalls += 1;
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
headers: new Map(),
|
||||
json: async () => ({ places: [] }),
|
||||
text: async () => '{"places":[]}',
|
||||
};
|
||||
};
|
||||
try {
|
||||
const routes = installGooglePlacesRoutes();
|
||||
const nearby = routes.get('/api/google/nearby-places');
|
||||
const textSearch = routes.get('/api/google/text-search');
|
||||
const badQueries = ['', 'lat=30.27', 'lat=&lon=', 'lat=abc&lon=-97.74', 'lat=999&lon=-200'];
|
||||
for (const query of badQueries) {
|
||||
const nearbyResponse = await invokeRoute(nearby, { url: `/?${query}` });
|
||||
assert.equal(nearbyResponse.statusCode, 400, `nearby-places must refuse ${query || '(empty)'}`);
|
||||
assert.deepEqual(nearbyResponse.body.places, []);
|
||||
const textResponse = await invokeRoute(textSearch, { url: `/?q=capitol&${query}` });
|
||||
assert.equal(textResponse.statusCode, 400, `text-search must refuse ${query || '(empty)'}`);
|
||||
assert.deepEqual(textResponse.body.places, []);
|
||||
}
|
||||
const missingQuery = await invokeRoute(textSearch, { url: '/?lat=30.27&lon=-97.74' });
|
||||
assert.equal(missingQuery.statusCode, 400, 'text-search still requires q');
|
||||
assert.equal(upstreamCalls, 0, 'invalid input never reaches Google');
|
||||
|
||||
// The limiter allows ONE request per minute per client. None of the
|
||||
// rejected requests may have consumed it, so the first valid request
|
||||
// still goes upstream instead of meeting a 429.
|
||||
const valid = await invokeRoute(nearby, { url: '/?lat=30.27&lon=-97.74' });
|
||||
assert.notEqual(valid.statusCode, 429, 'malformed requests must not consume limiter capacity');
|
||||
assert.equal(upstreamCalls, 1, 'a valid request reaches Google exactly once');
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
if (previousKey === undefined) delete process.env.GOOGLE_MAPS_API_KEY;
|
||||
else process.env.GOOGLE_MAPS_API_KEY = previousKey;
|
||||
if (previousLimit === undefined) delete process.env.GEV_RATELIMIT_GOOGLE_PER_MIN;
|
||||
else process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = previousLimit;
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user