mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
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:
@@ -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]] };
|
||||
|
||||
Reference in New Issue
Block a user