Skip to content

A spy satellite simulator in your browser, except the data is real. Live open source spatial intelligence on a photorealistic 3D globe.

Merge upstream main into fix/tarktee-datex-catalog-hardening

Said Turksever59646e52f9cc
diff --cc changelog.d/1233-tarktee-datex-catalog-hardening.md
index 11b9641e2,000000000..39f184526
mode 100644,000000..100644
--- a/changelog.d/1233-tarktee-datex-catalog-hardening.md
+++ b/changelog.d/1233-tarktee-datex-catalog-hardening.md
@@@ -1,4 -1,0 +1,4 @@@
+- Harden the Tarktee (Estonia) DATEX2 camera catalog fetches: refuse redirects,
- cancel both response streams when either request fails or overflows the 4 MB
- byte cap, extend the dual-XML fetch deadline to 15 s, and deduplicate camera
- IDs before per-pack prioritization (yaka0007, #1233).
++ cancel both response streams when either response fails or overflows the 4 MB
++ byte cap, and deduplicate camera IDs before per-pack prioritization
++ (yaka0007, #1233).
diff --cc server/providers/cctv/constants.js
index 9bdb5e511,49eb08253..af1397ffe
--- a/server/providers/cctv/constants.js
+++ b/server/providers/cctv/constants.js
@@@ -188,11 -209,6 +209,8 @@@ export const TARKTEE_IMAGES_URL
'https://tarktee.transpordiamet.ee/api/v1/datex/roadCameraImages';
export const TARKTEE_IMAGE_ORIGIN = 'https://tarktee.transpordiamet.ee/images/';
export const DEFAULT_TARKTEE_MAX_SOURCES = 179;
+/** Hard ceiling per Tarktee DATEX XML document (~120–165 KB live). */
+export const TARKTEE_MAX_CATALOG_BYTES = 4 * 1024 * 1024;
- /** Two DATEX XML documents download concurrently from the same origin (~5.3 s
- * baseline) alongside the other packs, so the 8 s default can abort mid-stream. */
- export const TARKTEE_FETCH_TIMEOUT_MS = 15_000;
export const TARKTEE_ANCHORS = [
{ lat: 59.437, lon: 24.753 }, // Tallinn
{ lat: 58.378, lon: 26.729 }, // Tartu
diff --cc server/providers/cctv/sources.js
index cba857565,72e763135..7ccbf3aad
--- a/server/providers/cctv/sources.js
+++ b/server/providers/cctv/sources.js
@@@ -42,8 -42,6 +42,7 @@@ import
TARKTEE_IMAGES_URL,
TARKTEE_IMAGE_ORIGIN,
DEFAULT_TARKTEE_MAX_SOURCES,
+ TARKTEE_MAX_CATALOG_BYTES,
- TARKTEE_FETCH_TIMEOUT_MS,
TARKTEE_ANCHORS,
DEFAULT_WARENDORF_SOURCE_FILE,
WARENDORF_IMAGE_ORIGINS,
diff --cc src/data/cctvEstonia.test.mjs
index a6385854a,b46f9eb06..4c2a0f686
--- a/src/data/cctvEstonia.test.mjs
+++ b/src/data/cctvEstonia.test.mjs
@@@ -97,129 -86,25 +97,152 @@@ test('Tarktee DATEX image parser reject
assert.equal(images.size, 0);
});
+test('Tarktee loader deduplicates cameras sharing an image number before the cap', async (t) => {
+ quiet(t);
+ const dupLocationsXml = `<?xml version="1.0" encoding="UTF-8"?>
+<d2LogicalModel xmlns="http://datex2.eu/schema/2/2_0">
+ <payloadPublication>
+ <predefinedLocationContainer id="TRAFFIC_CAMERAS" version="0">
+ <predefinedLocation id="loc-1" version="0">
+ <predefinedLocationName><values><value lang="et">Tallinn Ringtee</value></values></predefinedLocationName>
+ <location><pointByCoordinates><pointCoordinates><latitude>59.4370</latitude><longitude>24.7530</longitude></pointCoordinates></pointByCoordinates></location>
+ </predefinedLocation>
+ <predefinedLocation id="loc-2" version="0">
+ <predefinedLocationName><values><value lang="et">Tallinn Ringtee duplicate</value></values></predefinedLocationName>
+ <location><pointByCoordinates><pointCoordinates><latitude>59.4371</latitude><longitude>24.7531</longitude></pointCoordinates></pointByCoordinates></location>
+ </predefinedLocation>
+ </predefinedLocationContainer>
+ </payloadPublication>
+</d2LogicalModel>`;
+ const dupImagesXml = `<?xml version="1.0" encoding="UTF-8"?>
+<d2LogicalModel xmlns="http://datex2.eu/schema/2/2_0">
+ <payloadPublication>
+ <trafficView id="1"><linearTrafficView id="1"><linearPredefinedLocationReference id="loc-1"/><trafficViewRecord id="1-1"><urlLink><urlLinkAddress>https://tarktee.transpordiamet.ee/images/42/42_1.jpg</urlLinkAddress></urlLink></trafficViewRecord></linearTrafficView></trafficView>
+ <trafficView id="2"><linearTrafficView id="2"><linearPredefinedLocationReference id="loc-2"/><trafficViewRecord id="2-1"><urlLink><urlLinkAddress>https://tarktee.transpordiamet.ee/images/42/42_2.jpg</urlLinkAddress></urlLink></trafficViewRecord></linearTrafficView></trafficView>
+ </payloadPublication>
+</d2LogicalModel>`;
+
+ const seen = [];
+ t.mock.method(globalThis, 'fetch', async (url, init) => {
+ seen.push([String(url), init?.redirect]);
+ const body =
+ String(url) === TARKTEE_LOCATIONS_URL ? dupLocationsXml : dupImagesXml;
+ return new Response(body, {
+ status: 200,
+ headers: { 'Content-Type': 'application/xml' },
+ });
+ });
+
+ const cameras = await loadTarkteeSourcesFromDatex();
+ assert.deepEqual(seen, [
+ [TARKTEE_LOCATIONS_URL, 'manual'],
+ [TARKTEE_IMAGES_URL, 'manual'],
+ ]);
+ assert.equal(cameras.length, 1);
+ assert.equal(cameras[0].id, 'ee-tarktee-42');
+ assert.equal(cameras[0].name, 'Tallinn Ringtee');
+});
+
+test('Tarktee loader refuses a redirect and cancels both response bodies', async (t) => {
+ quiet(t);
+ const cancelled = { locations: false, images: false };
+ t.mock.method(globalThis, 'fetch', async (url) => {
+ const isLocations = String(url) === TARKTEE_LOCATIONS_URL;
+ const body = new ReadableStream({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode('<xml/>'));
+ },
+ cancel() {
+ if (isLocations) cancelled.locations = true;
+ else cancelled.images = true;
+ },
+ });
+ return new Response(body, {
+ status: isLocations ? 302 : 200,
+ headers: isLocations ? { location: 'https://evil.example/loc.xml' } : {},
+ });
+ });
+
+ assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
+ assert.equal(cancelled.locations, true, 'redirected body is cancelled');
+ assert.equal(cancelled.images, true, 'sibling 200 body is also cancelled');
+});
+
+test('Tarktee loader cancels the sibling body when one feed fails or overflows the byte cap', async (t) => {
+ quiet(t);
+ let siblingCancelled = false;
+ t.mock.method(globalThis, 'fetch', async (url) => {
+ const isLocations = String(url) === TARKTEE_LOCATIONS_URL;
+ if (isLocations) {
+ return new Response('', { status: 503 });
+ }
+ const body = new ReadableStream({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode(SAMPLE_IMAGES_XML));
+ },
+ cancel() {
+ siblingCancelled = true;
+ },
+ });
+ return new Response(body, { status: 200 });
+ });
+
+ assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
+ assert.equal(
+ siblingCancelled,
+ true,
+ 'sibling 200 stream is cancelled on 503',
+ );
+
+ let overflowSiblingCancelled = false;
+ t.mock.method(globalThis, 'fetch', async (url) => {
+ const isLocations = String(url) === TARKTEE_LOCATIONS_URL;
+ if (isLocations) {
+ const padded =
+ SAMPLE_LOCATIONS_XML + ' '.repeat(TARKTEE_MAX_CATALOG_BYTES);
+ return new Response(padded, {
+ status: 200,
+ headers: { 'Content-Length': String(padded.length) },
+ });
+ }
+ const body = new ReadableStream({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode(SAMPLE_IMAGES_XML));
+ },
+ cancel() {
+ overflowSiblingCancelled = true;
+ },
+ });
+ return new Response(body, { status: 200 });
+ });
+
+ assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
+ assert.equal(
+ overflowSiblingCancelled,
+ true,
+ 'sibling stream is cancelled when the other feed exceeds the byte cap',
+ );
+});
++
+ test('Tarktee DATEX requests wait 30 s, past the slow answers measured live', async (t) => {
+ const timeouts = [];
+ const timeout = AbortSignal.timeout;
+ t.mock.method(AbortSignal, 'timeout', (ms) => {
+ timeouts.push(ms);
+ return timeout.call(AbortSignal, ms);
+ });
+ t.mock.method(console, 'log', () => {});
+ t.mock.method(
+ globalThis,
+ 'fetch',
+ async (url) =>
+ new Response(
+ String(url).includes('roadCameraLocations')
+ ? SAMPLE_LOCATIONS_XML
+ : SAMPLE_IMAGES_XML,
+ ),
+ );
+ const cameras = await loadTarkteeSourcesFromDatex();
+ assert.deepEqual(timeouts, [30_000, 30_000]);
+ assert.equal(cameras.length, 1);
+ });