Skip to content

Motorola Portrait JPEGs won't open: "malformed Jpeg data: too many APP1 markers" (the depth map in Extended XMP spans more than 128 APP1 segments) #4815

Backlog
acebear17/lightcraft
#12

Description

@fuse314

What happens

A Portrait-mode photo from a Motorola razr 60 ultra imports, but it doesn't render. LightCraft shows:

malformed Jpeg data: too many APP1 markers

The image data is fine. macOS Preview/sips, browsers and other viewers decode the file at 4096×3072.

Why

The phone embeds a full-resolution depth map (GDepth:Data, a base64-encoded 4096×3072 JPEG) as Extended XMP, the standard XMP way to split a large packet across APP1 segments. The file's segments before SOS are:

Segment Count
APP1 Exif 1
APP1 http://ns.adobe.com/xap/1.0/ (xmpNote:HasExtendedXMP, GDepth:*, MotDepthFocus:*, GCamera:SpecialTypeID=…Portrait) 1
APP1 http://ns.adobe.com/xmp/extension/ (65,077 bytes each, full length 16,480,000) 254
APP0 JFIF, APP2 ICC_PROFILE, APP6 MMIMETA, DQT/SOF0/DHT 1 each

That makes 256 APP1 segments in total. crates/codecs/src/jpeg.rs refuses anything over MAX_APP1 = 128 before decoding:

const MAX_APP1: u32 = 128;
...
if m.app1 > MAX_APP1 {
    return Err(Error::Malformed(F, "too many APP1 markers".into()));
}

This cap was added in #2769 (security: bound codec allocation) to keep the allocating decoders' APP1 handling linear. 128 × 64 KiB is only about 8 MiB of metadata, though, and craft-metadata already accepts Extended XMP up to 16 MiB / 4096 chunks (MAX_EXTENDED_XMP_BYTES, MAX_EXTENDED_XMP_CHUNKS). This file's 15.7 MiB Extended XMP is inside the metadata limits but is still rejected by the decode gate. Google/Android "GDepth" portraits from other phones can carry similar depth maps, so they may hit the same limit.

Expected

The photo opens and develops like any other JPEG. The depth map can be ignored for now. It could later feed the depth work in #4328/#4334.

Suggested fix

The pixel decoders never need the Extended XMP. craft_metadata::inspect_jpeg has already read Exif/XMP/ICC into Markers, and Extended XMP is bounded separately by craft-metadata. In decode_with_fallback, the decoder could get a view of the stream without the Extended XMP APP1 segments, and the cap would apply only to the APP1 segments that remain:

const XMP_EXT_SIG: &[u8] = b"http://ns.adobe.com/xmp/extension/\0";

/// `b` without its Extended XMP APP1 segments, and the number of APP1 segments left. The
/// metadata layer reads (and bounds) Extended XMP itself; the pixel decoders never need it.
/// `None` when nothing is dropped, so the common case copies nothing.
fn without_extended_xmp(b: &[u8]) -> Option<(Vec<u8>, u32)> {
    let mut out = Vec::with_capacity(b.len());
    out.extend_from_slice(b.get(..2)?);
    let (mut i, mut app1, mut dropped) = (2usize, 0u32, false);
    loop {
        if *b.get(i)? != 0xFF { return None; }
        let marker = *b.get(i + 1)?;
        if marker == 0xFF { out.push(0xFF); i += 1; continue; } // fill byte
        if matches!(marker, 0xDA | 0xD9) {                      // SOS / EOI: copy the rest
            out.extend_from_slice(&b[i..]);
            return dropped.then_some((out, app1));
        }
        let len = usize::from(u16::from_be_bytes([*b.get(i + 2)?, *b.get(i + 3)?]));
        let seg = b.get(i..i.checked_add(2 + len)?)?;
        if marker == 0xE1 && seg.get(4..).is_some_and(|p| p.starts_with(XMP_EXT_SIG)) {
            dropped = true;
        } else {
            app1 += u32::from(marker == 0xE1);
            out.extend_from_slice(seg);
        }
        i += seg.len();
    }
}
// in decode_with_fallback, replacing the current gate:
let stripped = (m.app1 > MAX_APP1).then(|| without_extended_xmp(bytes)).flatten();
let app1 = stripped.as_ref().map_or(m.app1, |(_, n)| *n);
if app1 > MAX_APP1 {
    return Err(Error::Malformed(F, "too many APP1 markers".into()));
}
let decode_bytes = stripped.as_ref().map_or(bytes, |(b, _)| b.as_slice());
let raw = decode_via_craft_codecs(decode_bytes, opts)?;

This change:

  • keeps the security: bound codec allocation to what a hostile file declares #2769 protection: a flood of Exif or plain APP1 segments is still refused;
  • copies nothing for normal files, because it only runs above the cap;
  • removes only what craft-metadata already bounds (≤ 4096 chunks, ≤ 16 MiB);
  • leaves Exif, XMP and ICC untouched, because Meta comes from Markers, not from the decoder.

An alternative is a byte budget for APP1 instead of a count (for example, total APP1 payload ≤ MAX_EXTENDED_XMP_BYTES + 1 MiB). That matches the "linear in file size" goal more directly.

Regression test idea

  • A small JPEG with 254 valid Extended XMP chunks (one GUID, consistent full length) decodes, and xmp still holds the main packet.
  • The same JPEG with 200 plain Exif\0\0 APP1 segments is still refused with "too many APP1 markers".

Environment

  • LightCraft: 0.21.1, macOS. MAX_APP1 is checked in three places in crates/codecs/src/jpeg.rs in v0.21.1.
  • Camera: motorola razr 60 ultra, Portrait mode (17.5 MB; main image is about 1 MB, depth map about 16 MB)
  • Sample file: sample_motorola_portrait_anonymized.jpg (attached). Capture dates, sub-second times, the maker note and the Motorola MMIMETA blob are zeroed, and the depth-map base64 is replaced with filler. Every segment keeps its original size, so it still has the same 256 APP1 segments. The image data is byte-identical to the original.

Reproduction (0.21.1, --memory --control, ui.render)

File Result
Original photo malformed Jpeg data: too many APP1 markers
Anonymized sample (256 APP1 segments) malformed Jpeg data: too many APP1 markers
Same photo with the 254 Extended XMP segments removed renders (768×1024)

Sample file

I cannot upload the sample file to github. Contact me for a link to the sample file.

Activity

  1. wanikhawar commented on Dec 1, 2026

    @wanikhawar
    Contributor

    Hi @acebear17, thanks for the fix in acebear17#12. Merging the three checks into one check_app1, and not counting Extended XMP segments once a file is over the cap, looks like the right approach. It also reuses walk_jpeg_segments from craft-image-codecs instead of adding a copy of it to the app.

    #4815 is now listed in the v0.22.1 hotfix tracker (#4964). Could you open the PR against storytold/lightcraft, so it can get CI and review in time for the release? If you'd rather not, please say so here and someone else can pick it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions