Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 4 min read

Why Browser Metadata Strippers Break Portrait Photos and PDFs (and How We Built a Lossless Binary Engine in Pure JS)

Most online metadata removers make one of two compromises: Upload to a server, which creates an obvious privacy paradox when the whole point of stripping metadata is to avoid exposing private GPS coordinates, unpublis

Most online metadata removers make one of two compromises:

  1. Upload to a server, which creates an obvious privacy paradox when the whole point of stripping metadata is to avoid exposing private GPS coordinates, unpublished client artwork, or confidential PDF contracts.
  2. Draw the image onto an HTML <canvas> and call toBlob(), which avoids the server but silently re-compresses JPEGs, bloats PNGs, and discards embedded Display P3 / Adobe RGB ICC color profiles.

While building MetaOut β€” a zero-upload, browser-local metadata inspector and remover for JPEG, PNG, WebP, and PDF β€” we decided to edit file containers directly at the byte level in pure zero-dependency JavaScript.

Synthetic unit tests passed on day one. Then we threw real camera photos, OpenAI C2PA PNGs, macOS Pages exports, and arXiv LaTeX PDFs at the engine and ran independent CLI tools (exiftool, sips, pdfinfo) against the output.

Four real-world container bugs jumped out immediately. Here is what broke and how we fixed each one.

1. The Portrait JPEG Sideways Bug (and 1-Tag Orientation-Only Preservation)

Smartphone cameras typically store vertical photos sideways in the raw JPEG pixel grid and rely on a single EXIF TIFF tag (0x0112 Orientation = 6 or 8) to instruct browsers and photo viewers to rotate the image upright.

If a lossless JPEG stripper blindly deletes every APP1 Exif segment (0xFF 0xE1), all privacy tags disappearβ€”and every vertical smartphone photo flips 90Β° sideways.

Our fix (Orientation-Only Preservation):

  • Copy the compressed DCT scan stream from Start-of-Scan (0xFF 0xDA) through End-of-Image (0xFF 0xD9) bit for bit (zero pixel re-encoding).
  • Keep APP2 ICC_PROFILE and APP14 Adobe color markers intact.
  • When Orientation !== 1, drop the entire original EXIF block (camera Make/Model, GPS, serial numbers, timestamps) and splice in a minimal 26-byte big-endian TIFF APP1 containing strictly one non-identifying tag (0x0112 = orientation).
  • Expose a live toggle in the UI so users who require literal 0 EXIF segments can uncheck it and immediately see an honest sideways-rotation notice.

In headless Chromium verification with a real Orientation=6 test photo (portrait_6.jpg), the cleaned output renders upright at 450 Γ— 600 with zero identifying EXIF tags and the original Generic RGB Profile ICC intact.

2. X.509 DER Tag Bleed in C2PA Signer Parsing

Modern AI image generators (OpenAI, Adobe Firefly, Photoshop) embed C2PA Content Credentials inside ISO/IEC 19566-5 JUMBF boxes (APP11 in JPEG, caBX in PNG, C2PA in WebP).

When building our read-only C2PA Viewer, a naive ASCII regex scan over the JUMBF bytes extracted certificate names like:

SSL.com C2PA ICA R1 20251
SSL.com C2PA ICA R1 OCSP Responder0

Where did the trailing 1 and 0 come from? In X.509 DER encoding, the string "SSL.com C2PA ICA R1 2025" is a UTF8String (0x0C, length 0x18 = 24 bytes), immediately followed by the next ASN.1 tag: 0x31 (ASCII '1', SET) or 0x30 (ASCII '0', SEQUENCE). The regex greedily ate the next ASN.1 tag byte!

The fix took 6 lines: whenever a signer match starts at offset idx >= 2, inspect bytes[idx - 2] for DER string tags (0x0C, 0x13, 0x16) and honor the exact DER short-form length bytes[idx - 1]. Every certificate CN/O string immediately snapped to exact boundaries.

3. Three PDF Bugs You Only Hit on Real Exports

Unlike images, PDFs maintain a cross-reference table (xref) storing exact byte offsets for every internal object. Deleting strings changes byte offsets and breaks strict readers. So our PDF Metadata Remover uses in-place equal-length scrubbing: overwriting sensitive string payloads and uncompressed XMP packets with identical-length ASCII spaces (0x20), keeping exact file size and xref offsets valid.

Testing against real macOS Pages and arXiv LaTeX PDFs surfaced three nasty traps:

  1. UTF-16BE + octal escape mojibake: macOS Quartz PDFContext encodes non-ASCII fields as \376\377\000P\000a\000g\000e\000s.... Decoding without unescaping PDF octal sequences first produced gibberish. Unescaping octal/literal escapes before checking the 0xFE 0xFF UTF-16BE BOM fixed Chinese/Unicode inspection cleanly.
  2. Custom /Info keys & protecting /Outlines bookmarks: LaTeX (pdftex/hyperref) writes custom keys like /PTEX.Fullbanner, while enterprise Word templates write /Company. Conversely, a naive global /Title (...) regex corrupts /Outlines table-of-contents bookmarks! Resolving the trailer's indirect /Info N G R object lets us scrub all standard and custom keys inside /Info without touching /Outlines.
  3. Embedded JPEG EXIF & compressed streams: Real PDFs often embed raw /DCTDecode JPEG images that still contain APP1 Exif artist names and timestampsβ€”we neutralize those in-place by flipping FF E1 to a neutral FF FE comment of identical length filled with spaces.

4. The Sneakiest Privacy Trap: Compressed /Metadata XMP Streams

During adversarial QA testing, we generated a valid PDF whose /Type /Metadata /Subtype /XML stream used /Filter /FlateDecode.

Our early detector checked for /ObjStm (compressed object streams) and /Encrypt, but forgot to check for /Filter on /Metadata streams. Plaintext scanning skipped the compressed XMP bytes, and the UI showed a green "Clean" badgeβ€”while pdfinfo -meta still extracted the full XMP author intact.

That taught us the most important rule of privacy utility engineering: never fake a verified clean badge. We added explicit /Metadata /Filter detection so that whenever a PDF contains /ObjStm, compressed /Metadata, or /Encrypt, the UI surfaces an unmistakable Partial Scrub Warning explaining what was scrubbed and what cannot be verified in-place.

Try It Locally (Even Offline)

Every test case aboveβ€”including real-sample lossless invariants and independent sips/pdfinfo cross-checksβ€”powers metaout.com. You can load the page, disconnect your Wi-Fi, drop a photo or PDF, inspect the detected fields before and after, and verify the downloaded output yourself with exiftool or pdfinfo.

If you have encountered any weird JPEG, PNG, WebP, C2PA, or PDF container edge cases in the wild, I would love to hear about them in the comments!

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.