Skip to content

Commit 161b377

Browse files
brownpub1ft62tztwwm2x9xamk25smmuaj4sfckdkldksruf2x2jwqalffkrq0g7arr
andauthored
fix(mobile): image upload fails due to unstripped metadata (#2185)
Signed-off-by: npub1ft62tztwwm2x9xamk25smmuaj4sfckdkldksruf2x2jwqalffkrq0g7arr <4af4a5896e76d4629bbbb2a90def9d95609c59b6fb6d01f12a32a4e077e94d86@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1ft62tztwwm2x9xamk25smmuaj4sfckdkldksruf2x2jwqalffkrq0g7arr <4af4a5896e76d4629bbbb2a90def9d95609c59b6fb6d01f12a32a4e077e94d86@sprout-oss.stage.blox.sqprod.co>
1 parent 732a8f9 commit 161b377

16 files changed

Lines changed: 811 additions & 20 deletions

File tree

‎crates/buzz-media/src/validation.rs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,51 @@ mod tests {
10871087
));
10881088
}
10891089

1090+
#[test]
1091+
fn test_ios_uikit_sanitizer_outputs_match_relay_contract() {
1092+
let config = test_config();
1093+
for (name, bytes, expected_mime) in [
1094+
(
1095+
"PNG",
1096+
include_bytes!("../tests/fixtures/ios/uikit-sanitized.png").as_slice(),
1097+
"image/png",
1098+
),
1099+
(
1100+
"JPEG",
1101+
include_bytes!("../tests/fixtures/ios/uikit-sanitized.jpg").as_slice(),
1102+
"image/jpeg",
1103+
),
1104+
] {
1105+
let actual = validate_content(bytes, &config).unwrap_or_else(|error| {
1106+
panic!("rejected iOS UIKit-sanitized {name} fixture: {error}")
1107+
});
1108+
assert_eq!(actual, expected_mime);
1109+
}
1110+
}
1111+
1112+
#[test]
1113+
fn test_ios_uikit_encoder_outputs_require_sanitization() {
1114+
let config = test_config();
1115+
for (name, bytes) in [
1116+
(
1117+
"PNG",
1118+
include_bytes!("../tests/fixtures/ios/uikit-encoded.png").as_slice(),
1119+
),
1120+
(
1121+
"JPEG",
1122+
include_bytes!("../tests/fixtures/ios/uikit-encoded.jpg").as_slice(),
1123+
),
1124+
] {
1125+
assert!(
1126+
matches!(
1127+
validate_content(bytes, &config),
1128+
Err(MediaError::MetadataForbidden)
1129+
),
1130+
"accepted unsanitized iOS UIKit {name} fixture"
1131+
);
1132+
}
1133+
}
1134+
10901135
#[test]
10911136
fn test_rejects_png_metadata_and_trailing_payload() {
10921137
let config = test_config();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# UIKit media fixtures
2+
3+
These 2 x 2 fixtures were produced on an iOS simulator with UIKit, not by a generic image encoder.
4+
5+
## Regeneration
6+
7+
1. Create a small source image and run this program against the simulator SDK:
8+
9+
```swift
10+
import Foundation
11+
import UIKit
12+
13+
let arguments = CommandLine.arguments
14+
let source = try Data(contentsOf: URL(fileURLWithPath: arguments[1]))
15+
guard
16+
let image = UIImage(data: source),
17+
let png = image.pngData(),
18+
let jpeg = image.jpegData(compressionQuality: 1.0)
19+
else {
20+
fatalError("UIKit could not encode the source image")
21+
}
22+
try png.write(to: URL(fileURLWithPath: arguments[2]))
23+
try jpeg.write(to: URL(fileURLWithPath: arguments[3]))
24+
```
25+
26+
Compile and run it with the active Xcode toolchain:
27+
28+
```sh
29+
SDK_PATH="$(xcrun --sdk iphonesimulator --show-sdk-path)"
30+
xcrun --sdk iphonesimulator swiftc \
31+
-sdk "$SDK_PATH" \
32+
-target arm64-apple-ios16.0-simulator \
33+
reencode.swift -o reencode
34+
xcrun simctl spawn booted ./reencode \
35+
source.png uikit-encoded.png uikit-encoded.jpg
36+
```
37+
38+
2. Copy the encoded files into both fixture directories:
39+
40+
```sh
41+
cp uikit-encoded.png mobile/ios/RunnerTests/Fixtures/UIKitEncoded.png
42+
cp uikit-encoded.jpg mobile/ios/RunnerTests/Fixtures/UIKitEncoded.jpg
43+
cp uikit-encoded.png crates/buzz-media/tests/fixtures/ios/
44+
cp uikit-encoded.jpg crates/buzz-media/tests/fixtures/ios/
45+
```
46+
47+
3. Add a temporary Runner test that loads `UIKitEncoded.png` and `UIKitEncoded.jpg`, calls `MediaSanitizer.scrubPng` and `MediaSanitizer.scrubJpeg`, and writes those outputs to `uikit-sanitized.png` and `uikit-sanitized.jpg`. Run it once, copy the files here, then remove the temporary test.
48+
4. Run `cmp` on each encoded copy to confirm that the Runner and Rust fixtures are byte-identical.
49+
5. Run `cargo test -p buzz-media test_ios_uikit` to verify that UIKit's encoded output is rejected and the matching sanitizer output is accepted by the relay contract.
50+
51+
Regenerate both encoded and sanitized pairs whenever UIKit encoding or `MediaSanitizer` changes. Do not update only the sanitized files, because the test is intended to cover the exact encoder-to-sanitizer boundary.
896 Bytes
Loading
312 Bytes
Loading
696 Bytes
Loading
107 Bytes
Loading

‎mobile/ios/Podfile.lock‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
PODS:
22
- app_badge_plus (1.2.10):
33
- Flutter
4+
- app_links (6.4.1):
5+
- Flutter
46
- connectivity_plus (0.0.1):
57
- Flutter
68
- Flutter (1.0.0)
@@ -25,6 +27,7 @@ PODS:
2527

2628
DEPENDENCIES:
2729
- app_badge_plus (from `.symlinks/plugins/app_badge_plus/ios`)
30+
- app_links (from `.symlinks/plugins/app_links/ios`)
2831
- connectivity_plus (from `.symlinks/plugins/connectivity_plus/ios`)
2932
- Flutter (from `Flutter`)
3033
- flutter_secure_storage_darwin (from `.symlinks/plugins/flutter_secure_storage_darwin/darwin`)
@@ -38,6 +41,8 @@ DEPENDENCIES:
3841
EXTERNAL SOURCES:
3942
app_badge_plus:
4043
:path: ".symlinks/plugins/app_badge_plus/ios"
44+
app_links:
45+
:path: ".symlinks/plugins/app_links/ios"
4146
connectivity_plus:
4247
:path: ".symlinks/plugins/connectivity_plus/ios"
4348
Flutter:
@@ -59,6 +64,7 @@ EXTERNAL SOURCES:
5964

6065
SPEC CHECKSUMS:
6166
app_badge_plus: 09939f19a075cc742cc155d8ed85e6d8601f0104
67+
app_links: 3dbc685f76b1693c66a6d9dd1e9ab6f73d97dc0a
6268
connectivity_plus: cb623214f4e1f6ef8fe7403d580fdad517d2f7dd
6369
Flutter: cabc95a1d2626b1b06e7179b784ebcf0c0cde467
6470
flutter_secure_storage_darwin: acdb3f316ed05a3e68f856e0353b133eec373a23

‎mobile/ios/Runner.xcodeproj/project.pbxproj‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/* Begin PBXBuildFile section */
1010
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
1111
331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C807B294A618700263BE5 /* RunnerTests.swift */; };
12+
331C809B294A63AB00263BE5 /* MediaSanitizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C809A294A618700263BE5 /* MediaSanitizer.swift */; };
13+
331C809D294A63AB00263BE5 /* UIKitEncoded.png in Resources */ = {isa = PBXBuildFile; fileRef = 331C809C294A618700263BE5 /* UIKitEncoded.png */; };
14+
331C809F294A63AB00263BE5 /* UIKitEncoded.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 331C809E294A618700263BE5 /* UIKitEncoded.jpg */; };
1215
33ADD70AB275E0EC81295559 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8906419FB4E98B4B12B7A56F /* Pods_Runner.framework */; };
1316
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
1417
42C129326CE4E1B8E617B9CD /* Pods_RunnerTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD6B899582D0416ADBD8A68F /* Pods_RunnerTests.framework */; };
@@ -47,6 +50,9 @@
4750
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
4851
30CE81D3D1E0B195EF2A6390 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
4952
331C807B294A618700263BE5 /* RunnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunnerTests.swift; sourceTree = "<group>"; };
53+
331C809A294A618700263BE5 /* MediaSanitizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaSanitizer.swift; sourceTree = "<group>"; };
54+
331C809C294A618700263BE5 /* UIKitEncoded.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = UIKitEncoded.png; sourceTree = "<group>"; };
55+
331C809E294A618700263BE5 /* UIKitEncoded.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = UIKitEncoded.jpg; sourceTree = "<group>"; };
5056
331C8081294A63A400263BE5 /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5157
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
5258
42F36AA401015EC51F984FEA /* Pods-RunnerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.debug.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.debug.xcconfig"; sourceTree = "<group>"; };
@@ -93,10 +99,20 @@
9399
isa = PBXGroup;
94100
children = (
95101
331C807B294A618700263BE5 /* RunnerTests.swift */,
102+
331C80A0294A618700263BE5 /* Fixtures */,
96103
);
97104
path = RunnerTests;
98105
sourceTree = "<group>";
99106
};
107+
331C80A0294A618700263BE5 /* Fixtures */ = {
108+
isa = PBXGroup;
109+
children = (
110+
331C809E294A618700263BE5 /* UIKitEncoded.jpg */,
111+
331C809C294A618700263BE5 /* UIKitEncoded.png */,
112+
);
113+
path = Fixtures;
114+
sourceTree = "<group>";
115+
};
100116
746EA2D40EB8F35E67C48311 /* Pods */ = {
101117
isa = PBXGroup;
102118
children = (
@@ -153,6 +169,7 @@
153169
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */,
154170
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */,
155171
74858FAE1ED2DC5600515810 /* AppDelegate.swift */,
172+
331C809A294A618700263BE5 /* MediaSanitizer.swift */,
156173
7884E8672EC3CC0400C636F2 /* SceneDelegate.swift */,
157174
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */,
158175
);
@@ -256,6 +273,8 @@
256273
isa = PBXResourcesBuildPhase;
257274
buildActionMask = 2147483647;
258275
files = (
276+
331C809F294A63AB00263BE5 /* UIKitEncoded.jpg in Resources */,
277+
331C809D294A63AB00263BE5 /* UIKitEncoded.png in Resources */,
259278
);
260279
runOnlyForDeploymentPostprocessing = 0;
261280
};
@@ -381,6 +400,7 @@
381400
buildActionMask = 2147483647;
382401
files = (
383402
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
403+
331C809B294A63AB00263BE5 /* MediaSanitizer.swift in Sources */,
384404
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
385405
7884E8682EC3CC0700C636F2 /* SceneDelegate.swift in Sources */,
386406
);
@@ -496,8 +516,10 @@
496516
buildSettings = {
497517
BUNDLE_LOADER = "$(TEST_HOST)";
498518
CODE_SIGN_STYLE = Automatic;
519+
COMPRESS_PNG_FILES = NO;
499520
CURRENT_PROJECT_VERSION = 1;
500521
GENERATE_INFOPLIST_FILE = YES;
522+
STRIP_PNG_TEXT = NO;
501523
MARKETING_VERSION = 1.0;
502524
PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER).RunnerTests";
503525
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -514,8 +536,10 @@
514536
buildSettings = {
515537
BUNDLE_LOADER = "$(TEST_HOST)";
516538
CODE_SIGN_STYLE = Automatic;
539+
COMPRESS_PNG_FILES = NO;
517540
CURRENT_PROJECT_VERSION = 1;
518541
GENERATE_INFOPLIST_FILE = YES;
542+
STRIP_PNG_TEXT = NO;
519543
MARKETING_VERSION = 1.0;
520544
PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER).RunnerTests";
521545
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -530,8 +554,10 @@
530554
buildSettings = {
531555
BUNDLE_LOADER = "$(TEST_HOST)";
532556
CODE_SIGN_STYLE = Automatic;
557+
COMPRESS_PNG_FILES = NO;
533558
CURRENT_PROJECT_VERSION = 1;
534559
GENERATE_INFOPLIST_FILE = YES;
560+
STRIP_PNG_TEXT = NO;
535561
MARKETING_VERSION = 1.0;
536562
PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER).RunnerTests";
537563
PRODUCT_NAME = "$(TARGET_NAME)";

‎mobile/ios/Runner/AppDelegate.swift‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,27 @@ import UserNotifications
5858
return
5959
}
6060

61-
let sanitizedData: Data?
62-
switch mimeType {
63-
case "image/png":
64-
sanitizedData = image.pngData()
65-
case "image/jpeg":
66-
sanitizedData = image.jpegData(compressionQuality: 1.0)
67-
case "image/webp":
68-
sanitizedData = image.pngData()
69-
default:
70-
sanitizedData = nil
71-
}
72-
73-
guard let sanitizedData else {
61+
do {
62+
guard let sanitizedData = try MediaSanitizer.sanitizeImage(image, mimeType: mimeType) else {
63+
result(
64+
FlutterError(
65+
code: "sanitize_failed",
66+
message: "Unable to sanitize picked image.",
67+
details: mimeType
68+
)
69+
)
70+
return
71+
}
72+
result(FlutterStandardTypedData(bytes: sanitizedData))
73+
} catch {
7474
result(
7575
FlutterError(
7676
code: "sanitize_failed",
7777
message: "Unable to sanitize picked image.",
7878
details: mimeType
7979
)
8080
)
81-
return
8281
}
83-
84-
result(FlutterStandardTypedData(bytes: sanitizedData))
8582
case "transcodeImageToJpeg":
8683
guard let typedData = call.arguments as? FlutterStandardTypedData else {
8784
result(
@@ -94,9 +91,7 @@ import UserNotifications
9491
return
9592
}
9693

97-
guard let image = UIImage(data: typedData.data),
98-
let jpegData = image.jpegData(compressionQuality: 1.0)
99-
else {
94+
guard let image = UIImage(data: typedData.data) else {
10095
result(
10196
FlutterError(
10297
code: "transcode_failed",
@@ -107,7 +102,27 @@ import UserNotifications
107102
return
108103
}
109104

110-
result(FlutterStandardTypedData(bytes: jpegData))
105+
do {
106+
guard let jpegData = try MediaSanitizer.encodeJpeg(image) else {
107+
result(
108+
FlutterError(
109+
code: "transcode_failed",
110+
message: "Unable to convert picked image to JPEG.",
111+
details: nil
112+
)
113+
)
114+
return
115+
}
116+
result(FlutterStandardTypedData(bytes: jpegData))
117+
} catch {
118+
result(
119+
FlutterError(
120+
code: "transcode_failed",
121+
message: "Unable to convert picked image to JPEG.",
122+
details: nil
123+
)
124+
)
125+
}
111126
case "transcodeVideoToMp4":
112127
guard let sourcePath = call.arguments as? String else {
113128
result(

0 commit comments

Comments
 (0)