From 302eeab47e4eba40b160090899e40012d9593d73 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 1 Nov 2022 17:02:49 +0800 Subject: [PATCH] js client: show error when future spec version is read --- js/index.ts | 9 ++++++++- js/test/data/invalid_v4.pmtiles | Bin 0 -> 468 bytes js/test/v3.test.ts | 12 +++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 js/test/data/invalid_v4.pmtiles diff --git a/js/index.ts b/js/index.ts index 4d8bc03..7f8ffa9 100644 --- a/js/index.ts +++ b/js/index.ts @@ -309,8 +309,15 @@ export class FetchSource implements Source { export function bytesToHeader(bytes: ArrayBuffer, etag?: string): Header { const v = new DataView(bytes); + const spec_version = v.getUint8(7); + if (spec_version > 3) { + throw Error( + `Archive is spec version ${spec_version} but this library supports up to spec version 3` + ); + } + return { - specVersion: 3, + specVersion: spec_version, rootDirectoryOffset: Number(v.getBigUint64(8, true)), rootDirectoryLength: Number(v.getBigUint64(16, true)), jsonMetadataOffset: Number(v.getBigUint64(24, true)), diff --git a/js/test/data/invalid_v4.pmtiles b/js/test/data/invalid_v4.pmtiles new file mode 100644 index 0000000000000000000000000000000000000000..1871cb2759b535b65a3581974b061ac05c58ab58 GIT binary patch literal 468 zcmWIW4av+&EoP}_fB;D-Jp)RAhtlX&KO;oM6)FMcGN988OiVB#pvwAbGZ-3x7!KsS zIlyct;pCKrl2jH3ANx)QR-h0r{uR(?Y;E0N${r@wV$Rqz?{?h$%CZ~pR-fD;chPxcGvD5#swCfUJ3q9X zyYJfGQaN*z>CwX)(_MHoE_$j>pZmpR;iPE+bNH@ihyD-PGDW#IC`?~Ew^Gu@^U;5~ zMLN|*T=}&jpE@sYjM(y1WHrOxzp@|qI{uNHt+DuR>j{?#b(QndSF&YxKiSJB=o0K; zmUMDvv&>(=%}+VLv)Zh>lkdN)q3RE>2ir|u&wq}OjgFMDdZaDyH+~%MwD)0=m+&ls zb+dE0Za)PE6vEq+FRf2_B&6%9tJmp&dX0vru2-+Q&I#R { await cache.getHeader(source); assertion.fail("Should have thrown"); } catch (e) { - console.log(e); + assertion.ok(e instanceof Error); + } +}); + +test("cache check future spec version", async (assertion) => { + const source = new TestNodeFileSource("test/data/invalid_v4.pmtiles", "1"); + const cache = new SharedPromiseCache(); + try { + await cache.getHeader(source); + assertion.fail("Should have thrown"); + } catch (e) { assertion.ok(e instanceof Error); } });