diff --git a/packages/upload-media/CHANGELOG.md b/packages/upload-media/CHANGELOG.md index 6d9e1967e13be3..c9aedd43c38dcf 100644 --- a/packages/upload-media/CHANGELOG.md +++ b/packages/upload-media/CHANGELOG.md @@ -6,6 +6,10 @@ - Automatically retry failed uploads with exponential backoff for transient (network/server) errors. Retry behavior is configurable via the `retry` store setting; non-transient failures and child sideloads are not retried. The upload queue can also be paused and resumed, allowing uploads to halt while the browser is offline and continue on reconnect ([#76765](https://github.com/WordPress/gutenberg/pull/76765)). +### Bug Fix + +- Route very large images, especially interlaced/progressive JPEGs, to server-side processing instead of attempting client-side processing that would exceed the 1 GiB wasm-vips memory cap and fail. [#78949](https://github.com/WordPress/gutenberg/pull/78949). + ### Code Quality - Add missing `@types/react` dependency. [#78882](https://github.com/WordPress/gutenberg/pull/78882). diff --git a/packages/upload-media/src/feature-detection.ts b/packages/upload-media/src/feature-detection.ts index 1b05187594effd..a9b10cc0d1a95b 100644 --- a/packages/upload-media/src/feature-detection.ts +++ b/packages/upload-media/src/feature-detection.ts @@ -1,3 +1,8 @@ +/** + * Internal dependencies + */ +import type { ImageDimensions } from './get-image-dimensions'; + interface NavigatorNetworkInformation { saveData: boolean; effectiveType: 'slow-2g' | '2g' | '3g' | '4g'; @@ -190,3 +195,52 @@ export function isHeicCanvasSupported(): boolean { export function clearFeatureDetectionCache(): void { cachedResult = null; } + +/** + * Estimated bytes of WASM memory required per decoded pixel. + * + * A decoded image needs roughly width * height * 4 bytes (RGBA) in memory, + * plus additional working buffers while vips processes and re-encodes it. + * Four bytes per pixel is a deliberately conservative lower bound. + */ +const BYTES_PER_PIXEL = 4; + +/** + * Memory budget (in bytes) for processing interlaced images client-side. + * + * wasm-vips is hard-capped at 1 GiB of WASM memory. Progressive JPEGs and + * Adam7-interlaced PNGs cannot be decoded with shrink-on-load, so the entire + * image must be buffered at once. A tighter ~0.5 GiB budget leaves headroom + * for the encode step and avoids the out-of-memory failures these images hit. + */ +const INTERLACED_MEMORY_BUDGET = 0.5 * 1024 * 1024 * 1024; + +/** + * Memory budget (in bytes) for processing non-interlaced images client-side. + * + * Baseline images can be shrunk during decode, so vips needs far less peak + * memory. A generous ~0.9 GiB budget acts as a backstop against extreme sizes + * while leaving the vast majority of real-world uploads on the client. + */ +const BASELINE_MEMORY_BUDGET = 0.9 * 1024 * 1024 * 1024; + +/** + * Determines whether an image is too large to process client-side. + * + * Very large images, especially interlaced/progressive ones, can exceed the + * 1 GiB wasm-vips memory cap and fail to process. Such images are better + * handled by the server, which has no comparable per-image memory ceiling. + * + * @param dimensions The image's parsed dimensions and interlacing. + * @return Whether the image's estimated memory use exceeds the client budget. + */ +export function exceedsClientProcessingMemory( + dimensions: ImageDimensions +): boolean { + const { width, height, interlaced } = dimensions; + const estimatedBytes = width * height * BYTES_PER_PIXEL; + const budget = interlaced + ? INTERLACED_MEMORY_BUDGET + : BASELINE_MEMORY_BUDGET; + return estimatedBytes > budget; +} diff --git a/packages/upload-media/src/get-image-dimensions.ts b/packages/upload-media/src/get-image-dimensions.ts new file mode 100644 index 00000000000000..2901d3f46e717b --- /dev/null +++ b/packages/upload-media/src/get-image-dimensions.ts @@ -0,0 +1,183 @@ +/** + * Image dimensions and encoding details parsed from a file header. + */ +export interface ImageDimensions { + /** + * Image width in pixels. + */ + width: number; + /** + * Image height in pixels. + */ + height: number; + /** + * Whether the image is interlaced (progressive JPEG or Adam7 PNG). + * + * Interlaced images cannot be decoded with shrink-on-load, so the full + * image must be buffered in memory at once. This matters for deciding + * whether an image is safe to process client-side within the wasm-vips + * memory cap. + */ + interlaced: boolean; +} + +/** + * Maximum number of bytes to read from the start of a file when probing for + * dimensions. Generous enough to skip past large EXIF/ICC segments that can + * precede a JPEG's frame header, while avoiding reading huge files in full. + */ +const MAX_HEADER_BYTES = 512 * 1024; + +/** + * Parses dimensions and interlacing from a JPEG file header. + * + * Walks the JPEG marker segments until the Start Of Frame (SOFn) marker, which + * carries the image dimensions. Progressive DCT frames (SOF2/6/10/14) are + * reported as interlaced. + * + * @param view DataView over the file header bytes. + * @return Parsed dimensions, or null if they could not be determined. + */ +function parseJpeg( view: DataView ): ImageDimensions | null { + let offset = 2; // Skip the SOI marker (0xFFD8). + + while ( offset < view.byteLength ) { + // Markers begin with 0xFF; bail if we have lost alignment. + if ( view.getUint8( offset ) !== 0xff ) { + return null; + } + + // Skip any 0xFF padding bytes preceding the marker code. + while ( offset < view.byteLength && view.getUint8( offset ) === 0xff ) { + offset++; + } + + if ( offset >= view.byteLength ) { + return null; + } + + const marker = view.getUint8( offset ); + offset++; + + // Standalone markers without a length payload: TEM, RSTn, SOI, EOI. + if ( marker === 0x01 || ( marker >= 0xd0 && marker <= 0xd9 ) ) { + continue; + } + + // Start Of Scan: pixel data begins, so no frame header was found. + if ( marker === 0xda ) { + return null; + } + + if ( offset + 2 > view.byteLength ) { + return null; + } + + const segmentLength = view.getUint16( offset ); + if ( segmentLength < 2 ) { + return null; + } + + // Start Of Frame markers (0xC0-0xCF) carry the dimensions, except + // DHT (0xC4), JPG extension (0xC8), and DAC/arithmetic (0xCC). + const isStartOfFrame = + marker >= 0xc0 && + marker <= 0xcf && + marker !== 0xc4 && + marker !== 0xc8 && + marker !== 0xcc; + + if ( isStartOfFrame ) { + if ( offset + 7 > view.byteLength ) { + return null; + } + // Segment layout: length(2) precision(1) height(2) width(2). + const height = view.getUint16( offset + 3 ); + const width = view.getUint16( offset + 5 ); + // Progressive DCT frames: SOF2, SOF6, SOF10, SOF14. + const interlaced = + marker === 0xc2 || + marker === 0xc6 || + marker === 0xca || + marker === 0xce; + return { width, height, interlaced }; + } + + offset += segmentLength; + } + + return null; +} + +/** + * Parses dimensions and interlacing from a PNG file header. + * + * Reads the IHDR chunk, which is always the first chunk and contains the + * dimensions and the interlace method (0 = none, 1 = Adam7). + * + * @param view DataView over the file header bytes. + * @return Parsed dimensions, or null if they could not be determined. + */ +function parsePng( view: DataView ): ImageDimensions | null { + // Signature (8 bytes) + IHDR length (4) + "IHDR" (4) + 13 bytes of data. + if ( view.byteLength < 29 ) { + return null; + } + + // The IHDR chunk type must immediately follow the 8-byte signature and + // 4-byte chunk length. + const isIhdr = + view.getUint8( 12 ) === 0x49 && // I + view.getUint8( 13 ) === 0x48 && // H + view.getUint8( 14 ) === 0x44 && // D + view.getUint8( 15 ) === 0x52; // R + + if ( ! isIhdr ) { + return null; + } + + const width = view.getUint32( 16 ); + const height = view.getUint32( 20 ); + const interlaceMethod = view.getUint8( 28 ); + + return { width, height, interlaced: interlaceMethod !== 0 }; +} + +/** + * Reads an image's dimensions and interlacing from its header bytes. + * + * Only the leading bytes of the file are read, so this is cheap even for very + * large images and never fully decodes the pixel data. Currently supports JPEG + * and PNG, the formats most commonly affected by client-side memory limits; + * other formats return null (treated as unknown). + * + * @param file The image file to inspect. + * @return The parsed dimensions, or null if they could not be determined. + */ +export async function getImageDimensions( + file: File +): Promise< ImageDimensions | null > { + try { + const headerBytes = Math.min( file.size, MAX_HEADER_BYTES ); + const buffer = await file.slice( 0, headerBytes ).arrayBuffer(); + const view = new DataView( buffer ); + + // JPEG: starts with the SOI marker 0xFFD8. + if ( view.byteLength >= 3 && view.getUint16( 0 ) === 0xffd8 ) { + return parseJpeg( view ); + } + + // PNG: 8-byte signature 89 50 4E 47 0D 0A 1A 0A. + if ( + view.byteLength >= 8 && + view.getUint32( 0 ) === 0x89504e47 && + view.getUint32( 4 ) === 0x0d0a1a0a + ) { + return parsePng( view ); + } + + return null; + } catch { + return null; + } +} diff --git a/packages/upload-media/src/store/private-actions.ts b/packages/upload-media/src/store/private-actions.ts index 0ceb66fb918ee3..32ed0b35ddbf9e 100644 --- a/packages/upload-media/src/store/private-actions.ts +++ b/packages/upload-media/src/store/private-actions.ts @@ -16,7 +16,11 @@ type WPDataRegistry = ReturnType< typeof createRegistry >; */ import { cloneFile, convertBlobToFile, renameFile } from '../utils'; import { canvasConvertToJpeg } from '../canvas-utils'; -import { isClientSideMediaSupported } from '../feature-detection'; +import { + isClientSideMediaSupported, + exceedsClientProcessingMemory, +} from '../feature-detection'; +import { getImageDimensions } from '../get-image-dimensions'; import { CLIENT_SIDE_SUPPORTED_MIME_TYPES, HEIC_MIME_TYPES } from './constants'; import { StubFile } from '../stub-file'; import { UploadError } from '../upload-error'; @@ -690,6 +694,20 @@ export function prepareItem( id: QueueItemId ) { ); const isHeic = HEIC_MIME_TYPES.includes( file.type ); + // Gate very large images out of client-side processing. wasm-vips is + // capped at 1 GiB of memory, so high-megapixel images, especially + // interlaced/progressive ones, which can't be decoded with + // shrink-on-load, can exhaust it and fail. These are routed to the + // server, which has no comparable per-image ceiling. If dimensions + // can't be determined, the image stays on the client-side path. + let tooLargeForClient = false; + if ( isImage && isVipsSupported ) { + const dimensions = await getImageDimensions( file ); + if ( dimensions && exceedsClientProcessingMemory( dimensions ) ) { + tooLargeForClient = true; + } + } + // For images that can be processed by vips, upload the original and // let generateThumbnails() handle threshold scaling as a sideload. // @@ -704,7 +722,7 @@ export function prepareItem( id: QueueItemId ) { // image_editor_output_format filter during create_item. // The response carries image_output_format so generateThumbnails // can transcode sub-sizes to the same target format. - if ( isImage && isVipsSupported ) { + if ( isImage && isVipsSupported && ! tooLargeForClient ) { operations.push( OperationType.Upload, OperationType.ThumbnailGeneration, @@ -773,7 +791,10 @@ export function prepareItem( id: QueueItemId ) { convert_format: true, }, }; - } else if ( ! isVipsSupported || ! isImage ) { + } else if ( ! isVipsSupported || ! isImage || tooLargeForClient ) { + // Either the format isn't vips-processable, it isn't an image, or + // it's too large for client-side processing. Let the server + // generate sub-sizes and handle format conversion. updates = { additionalData: { ...item.additionalData, diff --git a/packages/upload-media/src/store/test/actions.ts b/packages/upload-media/src/store/test/actions.ts index 997278bbbc8f29..17b39e035a4fe3 100644 --- a/packages/upload-media/src/store/test/actions.ts +++ b/packages/upload-media/src/store/test/actions.ts @@ -334,6 +334,61 @@ describe( 'actions', () => { ); expect( updatedItem.additionalData.convert_format ).toBe( true ); } ); + + it( 'routes very large interlaced images to the server', async () => { + // A progressive JPEG (SOF2) header reporting 20000x11857, which + // exceeds the client-side memory budget for interlaced images. + const dimensions = new Uint8Array( 4 ); + const dimView = new DataView( dimensions.buffer ); + dimView.setUint16( 0, 11857 ); // height + dimView.setUint16( 2, 20000 ); // width + const bytes = new Uint8Array( [ + 0xff, + 0xd8, // SOI + 0xff, + 0xc2, // SOF2 (progressive) + 0x00, + 0x11, // segment length + 0x08, // precision + ...dimensions, // height (2) + width (2) + 0x03, // components + 0x00, + 0x00, + 0x00, + ] ); + const largeJpeg = new File( [ bytes ], 'huge.jpg', { + type: 'image/jpeg', + } ); + + unlock( registry.dispatch( uploadStore ) ).addItem( { + file: largeJpeg, + } ); + + const item = unlock( + registry.select( uploadStore ) + ).getAllItems()[ 0 ]; + + await unlock( registry.dispatch( uploadStore ) ).prepareItem( + item.id + ); + + const updatedItem = unlock( + registry.select( uploadStore ) + ).getAllItems()[ 0 ]; + + // Should fall back to server-side processing: only Upload, no + // client-side thumbnail generation. + expect( updatedItem.operations ).toEqual( + expect.arrayContaining( [ OperationType.Upload ] ) + ); + expect( updatedItem.operations ).not.toEqual( + expect.arrayContaining( [ OperationType.ThumbnailGeneration ] ) + ); + expect( updatedItem.additionalData.generate_sub_sizes ).toBe( + true + ); + expect( updatedItem.additionalData.convert_format ).toBe( true ); + } ); } ); describe( 'concurrent sideloads', () => { diff --git a/packages/upload-media/src/test/feature-detection.ts b/packages/upload-media/src/test/feature-detection.ts index 3c4572892747df..70dcf9eec42bfb 100644 --- a/packages/upload-media/src/test/feature-detection.ts +++ b/packages/upload-media/src/test/feature-detection.ts @@ -6,6 +6,7 @@ import { isClientSideMediaSupported, isHeicCanvasSupported, clearFeatureDetectionCache, + exceedsClientProcessingMemory, } from '../feature-detection'; describe( 'feature-detection', () => { @@ -365,4 +366,63 @@ describe( 'feature-detection', () => { expect( result2.supported ).toBe( false ); } ); } ); + + describe( 'exceedsClientProcessingMemory', () => { + it( 'allows typical images', () => { + expect( + exceedsClientProcessingMemory( { + width: 4000, + height: 3000, + interlaced: false, + } ) + ).toBe( false ); + expect( + exceedsClientProcessingMemory( { + width: 4000, + height: 3000, + interlaced: true, + } ) + ).toBe( false ); + } ); + + it( 'gates the reported interlaced flower.jpg (20000x11857)', () => { + expect( + exceedsClientProcessingMemory( { + width: 20000, + height: 11857, + interlaced: true, + } ) + ).toBe( true ); + } ); + + it( 'applies a tighter budget to interlaced images', () => { + // ~150 MP: over the ~0.5 GiB interlaced budget but under the + // ~0.9 GiB baseline budget. + const dimensions = { width: 15000, height: 10000 }; + + expect( + exceedsClientProcessingMemory( { + ...dimensions, + interlaced: true, + } ) + ).toBe( true ); + expect( + exceedsClientProcessingMemory( { + ...dimensions, + interlaced: false, + } ) + ).toBe( false ); + } ); + + it( 'gates extremely large baseline images', () => { + // ~300 MP exceeds even the generous baseline budget. + expect( + exceedsClientProcessingMemory( { + width: 20000, + height: 15000, + interlaced: false, + } ) + ).toBe( true ); + } ); + } ); } ); diff --git a/packages/upload-media/src/test/get-image-dimensions.ts b/packages/upload-media/src/test/get-image-dimensions.ts new file mode 100644 index 00000000000000..2ef742ed8d89ab --- /dev/null +++ b/packages/upload-media/src/test/get-image-dimensions.ts @@ -0,0 +1,172 @@ +/** + * Internal dependencies + */ +import { getImageDimensions } from '../get-image-dimensions'; + +/** + * Builds a File from a byte array for testing. + * + * @param bytes The file contents. + * @param type The MIME type. + * @return A File wrapping the bytes. + */ +function fileFromBytes( bytes: number[], type: string ): File { + return new File( [ new Uint8Array( bytes ) ], 'test', { type } ); +} + +/** + * Builds a minimal JPEG header with a single Start Of Frame segment. + * + * @param sofMarker The SOFn marker byte (e.g. 0xc0 baseline, 0xc2 progressive). + * @param width Image width. + * @param height Image height. + * @param prefix Optional bytes inserted between SOI and the frame header. + * @return The JPEG header bytes. + */ +function buildJpeg( + sofMarker: number, + width: number, + height: number, + prefix: number[] = [] +): number[] { + // Encode height and width as big-endian 16-bit values. + const dimensions = new Uint8Array( 4 ); + const view = new DataView( dimensions.buffer ); + view.setUint16( 0, height ); + view.setUint16( 2, width ); + + return [ + 0xff, + 0xd8, // SOI + ...prefix, + 0xff, + sofMarker, // SOFn + 0x00, + 0x11, // segment length (17) + 0x08, // precision + ...dimensions, // height (2) + width (2) + 0x03, // number of components + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, // component data (padding) + ]; +} + +/** + * Builds a minimal PNG header (signature + IHDR chunk). + * + * @param width Image width. + * @param height Image height. + * @param interlace Interlace method (0 = none, 1 = Adam7). + * @return The PNG header bytes. + */ +function buildPng( + width: number, + height: number, + interlace: number +): number[] { + // Encode width and height as big-endian 32-bit values. + const dimensions = new Uint8Array( 8 ); + const view = new DataView( dimensions.buffer ); + view.setUint32( 0, width ); + view.setUint32( 4, height ); + + return [ + 0x89, + 0x50, + 0x4e, + 0x47, + 0x0d, + 0x0a, + 0x1a, + 0x0a, // signature + 0x00, + 0x00, + 0x00, + 0x0d, // IHDR length (13) + 0x49, + 0x48, + 0x44, + 0x52, // "IHDR" + ...dimensions, // width (4) + height (4) + 0x08, // bit depth + 0x06, // color type + 0x00, // compression + 0x00, // filter + interlace, + ]; +} + +describe( 'getImageDimensions', () => { + it( 'parses a baseline JPEG', async () => { + const file = fileFromBytes( + buildJpeg( 0xc0, 1920, 1080 ), + 'image/jpeg' + ); + await expect( getImageDimensions( file ) ).resolves.toEqual( { + width: 1920, + height: 1080, + interlaced: false, + } ); + } ); + + it( 'parses a progressive JPEG as interlaced', async () => { + const file = fileFromBytes( + buildJpeg( 0xc2, 4000, 3000 ), + 'image/jpeg' + ); + await expect( getImageDimensions( file ) ).resolves.toEqual( { + width: 4000, + height: 3000, + interlaced: true, + } ); + } ); + + it( 'skips preceding marker segments (e.g. APP0/EXIF) to find the frame', async () => { + // APP0 segment: FF E0, length 0x0006, then 4 bytes of payload. + const app0 = [ 0xff, 0xe0, 0x00, 0x06, 0x4a, 0x46, 0x49, 0x46 ]; + const file = fileFromBytes( + buildJpeg( 0xc0, 800, 600, app0 ), + 'image/jpeg' + ); + await expect( getImageDimensions( file ) ).resolves.toEqual( { + width: 800, + height: 600, + interlaced: false, + } ); + } ); + + it( 'parses a non-interlaced PNG', async () => { + const file = fileFromBytes( buildPng( 1024, 768, 0 ), 'image/png' ); + await expect( getImageDimensions( file ) ).resolves.toEqual( { + width: 1024, + height: 768, + interlaced: false, + } ); + } ); + + it( 'parses an Adam7-interlaced PNG', async () => { + const file = fileFromBytes( buildPng( 1024, 768, 1 ), 'image/png' ); + await expect( getImageDimensions( file ) ).resolves.toEqual( { + width: 1024, + height: 768, + interlaced: true, + } ); + } ); + + it( 'returns null for unsupported formats', async () => { + // A WEBP-ish RIFF header, which the parser does not handle. + const file = fileFromBytes( + [ 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00 ], + 'image/webp' + ); + await expect( getImageDimensions( file ) ).resolves.toBeNull(); + } ); + + it( 'returns null for truncated/garbage data', async () => { + const file = fileFromBytes( [ 0xff, 0xd8, 0x00 ], 'image/jpeg' ); + await expect( getImageDimensions( file ) ).resolves.toBeNull(); + } ); +} );