fix(studio): read a finer country outline only as a plain path

The trip PDF writes an outline's d into an SVG string as it stands. A detail
file that carries anything but moves, lines and closes in numbers (a stale
cache, a proxy in between) is now refused, and the coarse outline is drawn.
This commit is contained in:
Maurice
2026-11-30 14:50:40 +01:00
parent 94b2352cb3
commit ddfb7ad7ad
2 changed files with 10 additions and 1 deletions
@@ -46,6 +46,8 @@ describe('a file', () => {
expect(readCountryDetail(null)).toBeNull();
expect(readCountryDetail('M0 0')).toBeNull();
expect(readCountryDetail({ ...SHAPE, d: '<svg/>' })).toBeNull();
expect(readCountryDetail({ ...SHAPE, d: 'M0 0"/><script>alert(1)</script><path d="M0 0' })).toBeNull();
expect(readCountryDetail({ ...SHAPE, d: 'M0 0C1 1 2 2 3 3Z' })).toBeNull();
expect(readCountryDetail({ ...SHAPE, w: '100' })).toBeNull();
expect(readCountryDetail({ ...SHAPE, b: [1, 2, 3] })).toBeNull();
expect(readCountryDetail({ ...SHAPE, b: [1, 2, 3, Number.NaN] })).toBeNull();
@@ -45,11 +45,18 @@ const codeOf = (code: string): string | null => {
const isNumber = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
/**
* A path of moves, lines and closes in plain numbers, nothing else. The trip
* PDF writes `d` into an SVG string as it is, so a file that says more (a quote,
* a tag) from a stale cache or a proxy in between is refused, not printed.
*/
const PLAIN_PATH = /^M[\d\s.,MLZmlz-]*$/;
/** The file's contents as a shape, or null for anything that is not one. */
export function readCountryDetail(raw: unknown): CountryShape | null {
if (!raw || typeof raw !== 'object') return null;
const { d, w, h, b } = raw as Record<string, unknown>;
if (typeof d !== 'string' || !d.startsWith('M') || !isNumber(w) || !isNumber(h)) return null;
if (typeof d !== 'string' || !PLAIN_PATH.test(d) || !isNumber(w) || !isNumber(h)) return null;
const box: unknown[] = Array.isArray(b) ? b : [];
if (box.length !== 4 || !box.every(isNumber)) return null;
return { d, w, h, b: [box[0], box[1], box[2], box[3]] };