Overview
ImageDecoder provides secure image decoding for web content by:- Decoding images in isolated processes
- Supporting multiple image formats
- Handling animated images
- Preventing malicious image exploits from affecting the browser
Supported formats
ImageDecoder supports a wide range of image formats:- PNG - Portable Network Graphics
- JPEG - Joint Photographic Experts Group
- GIF - Graphics Interchange Format (including animations)
- BMP - Bitmap Image File
- ICO - Windows Icon
- PBM - Portable Bitmap
- PGM - Portable Graymap
- PPM - Portable Pixmap
- WebP - Modern web image format
- QOI - Quite OK Image format
Architecture
ImageDecoder uses a minimal architecture focused on security and simplicity.Key components
ConnectionFromClient
Manages IPC connections and decoding requests:Services/ImageDecoder/ConnectionFromClient.h:22
DecodeResult
Contains the result of image decoding:Services/ImageDecoder/ConnectionFromClient.h:31
AnimationSession
Manages decoding of animated images:Services/ImageDecoder/ConnectionFromClient.h:45
Process lifecycle
ImageDecoder processes have a short lifecycle:- Spawn: Created when WebContent needs to decode an image
- Decode: Receives image data and returns bitmap
- Terminate: Process exits after decoding completes
Each image gets a fresh ImageDecoder process for maximum security isolation.
Decoding pipeline
Static images
For non-animated images:- WebContent sends encoded image data via IPC
- ImageDecoder receives data in
AnonymousBuffer - Format detection determines decoder to use
- Image is decoded to bitmap
- Bitmap is returned to WebContent
- Process terminates
Animated images
For animations (GIF, APNG, WebP):- Create animation session
- Decode frames on-demand
- Stream frames back to WebContent
- Maintain decoder state across frames
Services/ImageDecoder/ConnectionFromClient.h:60
IPC protocol
ImageDecoder uses a simple request-response protocol:Decode image
data- Encoded image bytes in shared memoryideal_size- Target size for decoding (optional)mime_type- Image MIME type hint (optional)request_id- Unique identifier for the request
Cancel decoding
Stop animation
Background decoding
Decoding runs on background threads to avoid blocking:Services/ImageDecoder/ConnectionFromClient.h:52
Security model
ImageDecoder implements defense-in-depth security:Process isolation
- Fresh process per image
- No shared state between images
- Process terminates after decoding
Sandboxing
- Minimal system call access via
pledge() - No filesystem access via
unveil() - Cannot spawn processes
- Cannot access network
- Runs as unprivileged user
Memory safety
- Uses safe Ladybird APIs
- Bounds checking on buffer access
- Protected against buffer overflows
- Automatic resource cleanup
Color management
ImageDecoder handles color profiles:- Extracts embedded ICC profiles
- Reports color space information
- Supports color conversion
- Preserves high color depth when possible
Size optimization
Images can be decoded at optimal sizes:- Reduced memory usage for thumbnails
- Faster decoding for downscaled images
- Progressive decoding for large images
Error handling
ImageDecoder reports various error conditions:- Invalid format: Image data is corrupted or unsupported
- Out of memory: Image is too large to decode
- Decode failure: Error during format-specific decoding
- Truncated data: Incomplete image data
Multi-client support
ImageDecoder can handle multiple clients:Configuration options
| Option | Description |
|---|---|
--wait-for-debugger | Pause on startup for debugger attachment |
--mach-server-name | Mach server name (macOS only) |
Performance considerations
- Process creation overhead: Spawning processes has cost, but security benefits outweigh it
- IPC overhead: Shared memory buffers minimize data copying
- Background threads: Prevents UI blocking during decoding
- Frame caching: Decoded frames cached for animations
Integration with WebContent
WebContent uses ImageDecoder through a plugin:Services/WebContent/main.cpp:288
Animation support
Animated images are handled specially:- Loop count: Number of times to repeat animation
- Frame durations: Timing for each frame
- Frame count: Total frames in animation
- Progressive loading: Frames decoded as needed
Related services
- WebContent: Primary client that requests image decoding
- RequestServer: Fetches image data from network
- Browser: Initiates the process hierarchy