Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Check recovery image dimensions before decode

Neeraj Guptae7fd2787f84e
diff --git a/web/apps/legacy/src/features/legacy-kit/scan.ts b/web/apps/legacy/src/features/legacy-kit/scan.ts
index 7d01d975e2..6acc7e0466 100644
--- a/web/apps/legacy/src/features/legacy-kit/scan.ts
+++ b/web/apps/legacy/src/features/legacy-kit/scan.ts
@@ -25,6 +25,11 @@ interface DecodableImage {
width: number;
}
+interface ImageDimensions {
+ height: number;
+ width: number;
+}
+
interface DrawableImage {
dispose: () => void;
height: number;
@@ -75,6 +80,255 @@ const assertFileSize = (file: File, maxBytes: number) => {
}
};
+const assertImageDimensions = ({ height, width }: ImageDimensions) => {
+ if (
+ width <= 0 ||
+ height <= 0 ||
+ width > maxInputImageSide ||
+ height > maxInputImageSide ||
+ width * height > maxInputImagePixels
+ ) {
+ throw new Error(tooLargeMessage);
+ }
+};
+
+const imageOutputDimensions = ({ height, width }: ImageDimensions) => {
+ const scale = Math.min(
+ 1,
+ maxCanvasSide / Math.max(width, height),
+ Math.sqrt(maxCanvasPixels / (width * height)),
+ );
+ return {
+ height: Math.max(1, Math.floor(height * scale)),
+ width: Math.max(1, Math.floor(width * scale)),
+ };
+};
+
+const imageBitmapOptions = (
+ dimensions: ImageDimensions,
+): ImageBitmapOptions => {
+ const { height, width } = imageOutputDimensions(dimensions);
+ return { resizeHeight: height, resizeQuality: "high", resizeWidth: width };
+};
+
+const byteAt = (bytes: Uint8Array, offset: number) => bytes[offset] ?? 0;
+
+const hasBytes = (bytes: Uint8Array, offset: number, values: number[]) =>
+ values.every((value, index) => byteAt(bytes, offset + index) === value);
+
+const hasASCII = (bytes: Uint8Array, offset: number, value: string) => {
+ for (let index = 0; index < value.length; index++) {
+ if (byteAt(bytes, offset + index) !== value.charCodeAt(index)) {
+ return false;
+ }
+ }
+ return true;
+};
+
+const readUInt24LE = (bytes: Uint8Array, offset: number) =>
+ byteAt(bytes, offset) |
+ (byteAt(bytes, offset + 1) << 8) |
+ (byteAt(bytes, offset + 2) << 16);
+
+const jpegStartOfFrameMarkers = new Set([
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce,
+ 0xcf,
+]);
+
+const jpegDimensions = (
+ bytes: Uint8Array,
+ view: DataView,
+): ImageDimensions | undefined => {
+ if (!hasBytes(bytes, 0, [0xff, 0xd8])) return undefined;
+
+ let offset = 2;
+ while (offset + 3 < bytes.length) {
+ while (byteAt(bytes, offset) === 0xff) offset += 1;
+ const marker = byteAt(bytes, offset);
+ offset += 1;
+
+ if (marker === 0xda || marker === 0xd9) break;
+ if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) continue;
+ if (offset + 2 > bytes.length) break;
+
+ const segmentLength = view.getUint16(offset, false);
+ if (segmentLength < 2) break;
+ if (jpegStartOfFrameMarkers.has(marker)) {
+ if (offset + 7 > bytes.length) break;
+ return {
+ height: view.getUint16(offset + 3, false),
+ width: view.getUint16(offset + 5, false),
+ };
+ }
+ offset += segmentLength;
+ }
+
+ throw new Error("Could not read that image.");
+};
+
+const webPDimensions = (
+ bytes: Uint8Array,
+ view: DataView,
+): ImageDimensions | undefined => {
+ if (
+ bytes.length < 20 ||
+ !hasASCII(bytes, 0, "RIFF") ||
+ !hasASCII(bytes, 8, "WEBP")
+ ) {
+ return undefined;
+ }
+
+ if (bytes.length >= 30 && hasASCII(bytes, 12, "VP8X")) {
+ return {
+ height: 1 + readUInt24LE(bytes, 27),
+ width: 1 + readUInt24LE(bytes, 24),
+ };
+ }
+ if (
+ bytes.length >= 30 &&
+ hasASCII(bytes, 12, "VP8 ") &&
+ hasBytes(bytes, 23, [0x9d, 0x01, 0x2a])
+ ) {
+ return {
+ height: view.getUint16(28, true) & 0x3fff,
+ width: view.getUint16(26, true) & 0x3fff,
+ };
+ }
+ if (
+ bytes.length >= 25 &&
+ hasASCII(bytes, 12, "VP8L") &&
+ byteAt(bytes, 20) === 0x2f
+ ) {
+ return {
+ height:
+ 1 +
+ (((byteAt(bytes, 24) & 0x0f) << 10) |
+ (byteAt(bytes, 23) << 2) |
+ ((byteAt(bytes, 22) & 0xc0) >> 6)),
+ width: 1 + (((byteAt(bytes, 22) & 0x3f) << 8) | byteAt(bytes, 21)),
+ };
+ }
+
+ throw new Error("Could not read that image.");
+};
+
+const isoBMFFContainerBoxes = new Set([
+ "meta",
+ "iprp",
+ "ipco",
+ "moov",
+ "trak",
+ "mdia",
+ "minf",
+ "stbl",
+]);
+
+const isoBMFFDimensions = (
+ bytes: Uint8Array,
+ view: DataView,
+): ImageDimensions | undefined => {
+ if (bytes.length < 16 || !hasASCII(bytes, 4, "ftyp")) return undefined;
+
+ const scanBoxes = (
+ start: number,
+ end: number,
+ depth: number,
+ ): ImageDimensions | undefined => {
+ if (depth > 8) return undefined;
+ let offset = start;
+ while (offset + 8 <= end) {
+ let boxSize = view.getUint32(offset, false);
+ const boxTypeOffset = offset + 4;
+ let contentOffset = offset + 8;
+ if (boxSize === 1) {
+ if (
+ offset + 16 > end ||
+ view.getUint32(offset + 8, false) > 0
+ ) {
+ return undefined;
+ }
+ boxSize = view.getUint32(offset + 12, false);
+ contentOffset = offset + 16;
+ } else if (boxSize === 0) {
+ boxSize = end - offset;
+ }
+
+ if (boxSize < contentOffset - offset || offset + boxSize > end) {
+ return undefined;
+ }
+
+ const boxType = String.fromCharCode(
+ byteAt(bytes, boxTypeOffset),
+ byteAt(bytes, boxTypeOffset + 1),
+ byteAt(bytes, boxTypeOffset + 2),
+ byteAt(bytes, boxTypeOffset + 3),
+ );
+ const boxEnd = offset + boxSize;
+ if (boxType === "ispe" && contentOffset + 12 <= boxEnd) {
+ return {
+ height: view.getUint32(contentOffset + 8, false),
+ width: view.getUint32(contentOffset + 4, false),
+ };
+ }
+
+ const childOffset =
+ boxType === "meta" ? contentOffset + 4 : contentOffset;
+ if (isoBMFFContainerBoxes.has(boxType) && childOffset < boxEnd) {
+ const dimensions = scanBoxes(childOffset, boxEnd, depth + 1);
+ if (dimensions) return dimensions;
+ }
+ offset = boxEnd;
+ }
+ return undefined;
+ };
+
+ return scanBoxes(0, bytes.length, 0);
+};
+
+const imageDimensionsFromBytes = (bytes: Uint8Array) => {
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
+
+ if (
+ bytes.length >= 24 &&
+ hasBytes(bytes, 0, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+ ) {
+ return {
+ height: view.getUint32(20, false),
+ width: view.getUint32(16, false),
+ };
+ }
+ if (
+ bytes.length >= 10 &&
+ (hasASCII(bytes, 0, "GIF87a") || hasASCII(bytes, 0, "GIF89a"))
+ ) {
+ return {
+ height: view.getUint16(8, true),
+ width: view.getUint16(6, true),
+ };
+ }
+ if (bytes.length >= 26 && hasASCII(bytes, 0, "BM")) {
+ return {
+ height: Math.abs(view.getInt32(22, true)),
+ width: view.getInt32(18, true),
+ };
+ }
+
+ return (
+ jpegDimensions(bytes, view) ??
+ webPDimensions(bytes, view) ??
+ isoBMFFDimensions(bytes, view)
+ );
+};
+
+// Keep this before browser image decode; compressed images can expand to huge bitmaps.
+const imageDimensionsFromFile = async (file: File) => {
+ const bytes = new Uint8Array(await file.arrayBuffer());
+ const dimensions = imageDimensionsFromBytes(bytes);
+ if (!dimensions) throw new Error("Could not read that image.");
+ assertImageDimensions(dimensions);
+ return dimensions;
+};
+
const findJsonObject = (value: string) => {
const start = value.indexOf('{"pv"');
if (start < 0) return undefined;
@@ -174,10 +428,16 @@ const loadImageFromFile = (file: File) =>
image.src = objectURL;
});
-const loadDrawableFromFile = async (file: File): Promise<DrawableImage> => {
+const loadDrawableFromFile = async (
+ file: File,
+ dimensions: ImageDimensions,
+): Promise<DrawableImage> => {
if (typeof createImageBitmap === "function") {
try {
- const bitmap = await createImageBitmap(file);
+ const bitmap = await createImageBitmap(
+ file,
+ imageBitmapOptions(dimensions),
+ );
return {
dispose: () => bitmap.close(),
height: bitmap.height,
@@ -193,23 +453,9 @@ const loadDrawableFromFile = async (file: File): Promise<DrawableImage> => {
};
const imageDataFromDrawable = (drawable: DrawableImage) => {
- if (
- drawable.width <= 0 ||
- drawable.height <= 0 ||
- drawable.width > maxInputImageSide ||
- drawable.height > maxInputImageSide ||
- drawable.width * drawable.height > maxInputImagePixels
- ) {
- throw new Error(tooLargeMessage);
- }
+ assertImageDimensions(drawable);
- const scale = Math.min(
- 1,
- maxCanvasSide / Math.max(drawable.width, drawable.height),
- Math.sqrt(maxCanvasPixels / (drawable.width * drawable.height)),
- );
- const width = Math.max(1, Math.floor(drawable.width * scale));
- const height = Math.max(1, Math.floor(drawable.height * scale));
+ const { height, width } = imageOutputDimensions(drawable);
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
@@ -222,7 +468,8 @@ const imageDataFromDrawable = (drawable: DrawableImage) => {
};
const imageDataFromFile = async (file: File) => {
- const drawable = await loadDrawableFromFile(file);
+ const dimensions = await imageDimensionsFromFile(file);
+ const drawable = await loadDrawableFromFile(file, dimensions);
try {
return imageDataFromDrawable(drawable);
} finally {