Skip to content

Strip location data from sent images and drop unreadable pasted data - #2164

Open
YeungKC wants to merge 7 commits into
mainfrom
fix/paste-image-metadata
Open

YeungKC wants to merge 7 commits into
mainfrom
fix/paste-image-metadata

Conversation

@YeungKC

@YeungKC YeungKC commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #2159.

 send image message
   compressed
     compressImage
-      re-encode, source EXIF copied (GPS included)
+      re-encode, keep only EXIF orientation
   uncompressed
-    upload original bytes (GPS included)
+    stripImageMetadata (off the UI isolate), pixels unchanged:
+      JPEG: keep JFIF, ICC, Adobe and image segments; EXIF -> orientation only; drop XMP, IPTC, other APPn
+      PNG:  EXIF -> orientation only; drop XMP and raw-profile text chunks; keep iCCP
+      WebP: EXIF -> orientation only; drop XMP; fix VP8X flags
+      unreadable EXIF -> dropped; malformed file with metadata -> re-encoded
+    a forward whose bytes change uploads again

 paste image data (pastedImageData)
   JPEG / PNG / GIF / WebP   -> keep bytes and extension (unchanged)
   BMP                       -> unchanged
-  anything else             -> saved as .png
+  anything else             -> decode to PNG, or paste nothing if decoding fails

Pasting, picking, dropping and URL images all go through compressImage or sendImageMessage, so one change covers each source. The Android and iOS clients re-encode images before upload, so their output also has no source GPS.

Evidence

  • test/utils/image_test.dart: a generated JPEG with GPS EXIF and orientation.
    • Before: the compressed output still has GPS tags 1–4.
    • After: no GPS IFD; the rotated size and pixel positions are correct.
  • test/account/send_message_helper_test.dart and test/utils/image_test.dart: uncompressed JPEG, PNG and WebP with GPS EXIF, XMP and IPTC.
    • Before: GPS EXIF, XMP and IPTC stay in the uploaded bytes; a corrupt EXIF throws RangeError and the send fails.
    • After: only orientation EXIF is left, ICC profiles are byte-for-byte unchanged, decoded pixels are identical, and a corrupt EXIF is dropped without an error.
  • test/utils/clipboard_test.dart:
    • Before: PDF bytes are returned as .png; a decodable TIFF and a throwing decoder are not handled.
    • After: PDF -> nothing, TIFF -> PNG, throwing decoder -> nothing.
  • flutter test test/utils/ test/account/: 240 passed, 4 skipped. dart analyze --fatal-infos lib test: no issues.

Merge Danger

Door: two-way

Blast Radius: sent images.

  • An uncompressed JPEG, PNG or WebP is no longer byte-identical to the source file: its metadata keeps only orientation and the color profile.
  • GIF and BMP are sent unchanged, as on Android and iOS.
  • Pasted data that is not a readable image now pastes nothing instead of a broken .png.
  • Not tested on Windows; the Windows paste path is covered by unit tests only.

When pastedImageData receives bytes that do not match BMP, JPEG, PNG, GIF, or WebP, attempt to decode them to PNG with convertToPng instead of saving the raw bytes with a .png suffix. Drop the pasted item if conversion returns null or throws.
@gaotic

gaotic Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

AI review done for 56254e5 in 9m 01s · 4 comments

Quality: Needs fixes before merge. Four send-path comparisons confirmed that images accepted by the previous revision now fail before upload.

Role Model Status
Reviewer Codex codex/gpt-6.2-sol · effort high ok

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · Codex · f7cb645

Flutter analysis passed on all six changed files. The test/utils/ and test/account/ suites finished with 240 passed and 4 skipped.

Comment thread lib/utils/image.dart Resolved
Comment thread lib/utils/image.dart Resolved
Comment thread lib/utils/image.dart Resolved

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · Codex · 4a567ac

Needs fixes before merge. Flutter analysis found no issues in the seven touched Dart files, and the test/utils/ and test/account/ suites finished with 259 passed and 4 skipped.

Comment thread lib/utils/image.dart Resolved
Comment thread lib/utils/image.dart Resolved
Comment thread lib/utils/image.dart Resolved
Comment thread lib/utils/image.dart Resolved
Comment thread lib/utils/image.dart Resolved

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · Codex · 56254e5

Flutter analysis found no issues in the seven touched files. The utility and account suites finished with 265 passed and 4 skipped, and git diff --check passed.

Comment thread lib/utils/image.dart
if (max(width, height) > _kMaxDimension) {
image = switch (width / height) {
< 1 / 3 => bakeOrientation(image),
> 1 => copyResize(image, width: _kMaxDimension),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A 1×5000 PNG with EXIF orientation 6 used to upload with compression enabled. The new width-only resize treats it as 5000×1 and rounds the target height to zero, so the encoder throws Cannot encode an empty image. Compute both target dimensions from the oriented size and clamp each to at least one pixel.

Comment thread lib/utils/image.dart
if (type == 'IEND' && end != start + 12) throw Exception('compress failed');
if (end > bytes.length) {
if (type == 'IEND') break;
throw Exception('compress failed');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This aborts uncompressed sends when only trailing metadata is truncated. A PNG with complete IDAT data and a truncated final tEXt chunk decoded in Flutter and uploaded after sanitization on 4a567ac. Recover this case by dropping the damaged tail and writing a fresh IEND after verifying the retained pixels decode.

Comment thread lib/utils/image.dart
if (copiedUntil == 0) return bytes;
clean.add(Uint8List.sublistView(bytes, copiedUntil));
if (orientation != null) {
if (flagsOffset == null) throw Exception('compress failed');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A WebP with a valid image bitstream and an appended EXIF chunk now fails to send if it lacks VP8X. Flutter decoded the fixture, and 4a567ac uploaded it with orientation-only EXIF. Rebuild VP8X from the bitstream when needed, or restore the sanitized re-encode fallback for this case.

Comment thread lib/utils/image.dart

// A readable WebP header does not prove that the frame bitstreams decode.
try {
final info = decoder.startDecode(output);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Dart decode check rejects some files Flutter can display. A 16×8 single-frame animated WebP with XMP and a nonzero reserved VP8X byte decoded in Flutter and uploaded on 4a567ac. The rewritten file now fails startDecode and aborts before upload. Use Flutter's decoder to validate the sanitized output so sending and display accept the same files.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant