Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Refuse a lossy WebP whose first partition runs past its data

Claude0cbd99747961
diff --git a/src/lib/brand-logo.test.ts b/src/lib/brand-logo.test.ts
index 4ef6131..80a6cd2 100644
--- a/src/lib/brand-logo.test.ts
+++ b/src/lib/brand-logo.test.ts
@@ -392,3 +392,16 @@ test("a WebP whose last chunk's padding runs past the file is refused", () => {
assert.throws(() => checkLogoUpload(bytes), BrandLogoError);
});
+
+test("a lossy WebP whose first partition runs past its data is refused", () => {
+ // A key frame tag declaring a 64-byte first partition, a 1x1 frame, and
+ // only 2 bytes of data after the 10-byte header.
+ const frame = Buffer.from([
+ 0x10, 0x08, 0x00, 0x9d, 0x01, 0x2a, 1, 0, 1, 0, 0, 0,
+ ]);
+
+ assert.throws(
+ () => checkLogoUpload(buildWebp(webpChunk("VP8 ", frame))),
+ BrandLogoError,
+ );
+});
diff --git a/src/lib/brand-logo.ts b/src/lib/brand-logo.ts
index 6318a3b..7bf136d 100644
--- a/src/lib/brand-logo.ts
+++ b/src/lib/brand-logo.ts
@@ -385,9 +385,20 @@ function hasWebpImage(
}
if (type === "VP8 ") {
- // A key frame header with its size, and some data after it.
+ // A key frame header with its size, and a first partition that fits
+ // in the data after it. The frame tag's top 19 bits give that size.
+ const firstPartitionSize =
+ length > 10
+ ? (bytes[dataStart] |
+ (bytes[dataStart + 1] << 8) |
+ (bytes[dataStart + 2] << 16)) >>>
+ 5
+ : 0;
+
if (
length <= 10 ||
+ firstPartitionSize === 0 ||
+ firstPartitionSize > length - 10 ||
!startsWith(bytes, [0x9d, 0x01, 0x2a], dataStart + 3) ||
(readUint16LE(bytes, dataStart + 6) & 0x3fff) === 0 ||
(readUint16LE(bytes, dataStart + 8) & 0x3fff) === 0