Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Refuse a thumbnail that is cut short
Claudeb8ed5934d9a8
diff --git a/src/lib/services/thumbnails.test.ts b/src/lib/services/thumbnails.test.tsindex 6c84917..ae36f64 100644--- a/src/lib/services/thumbnails.test.ts+++ b/src/lib/services/thumbnails.test.ts@@ -146,6 +146,22 @@ test("refuses a thumbnail that is empty, too heavy, or not a JPEG", () => {);});+test("refuses a thumbnail that was cut short", () => {+ // A frame header that says 16 by 16, and nothing after it.+ const headerOnly = Uint8Array.from([+ ...START_OF_IMAGE,+ ...[0xff, 0xc0, 0x00, 0x07, 0x08, 0x00, 0x10, 0x00, 0x10],+ ]);+ const cutShort = buildJpeg().slice(0, -1);++ assert.deepEqual(readJpegDimensions(headerOnly), { height: 16, width: 16 });+ assert.throws(+ () => assertThumbnailImage(headerOnly),+ ThumbnailValidationError,+ );+ assert.throws(() => assertThumbnailImage(cutShort), ThumbnailValidationError);+});+test("refuses a thumbnail whose picture is too large or too small", () => {assert.throws(() =>diff --git a/src/lib/services/thumbnails.ts b/src/lib/services/thumbnails.tsindex 2ac432d..4e62406 100644--- a/src/lib/services/thumbnails.ts+++ b/src/lib/services/thumbnails.ts@@ -105,7 +105,12 @@ export function assertThumbnailImage(bytes: Uint8Array): JpegDimensions {const dimensions = readJpegDimensions(bytes);- if (!dimensions) {+ // The frame header alone says nothing about the rest. A whole JPEG ends+ // with its end-of-image marker, and one that was cut short does not.+ const isWhole =+ bytes[bytes.length - 2] === 0xff && bytes[bytes.length - 1] === 0xd9;++ if (!dimensions || !isWhole) {throw new ThumbnailValidationError("The thumbnail must be a JPEG image.");}