commit ced20992ca5676af21cfa411b4c2d523e564ce8a
parent 2ea1bc2d9d551e1c758ded38d5404faf3581c3a2
Author: Marc Stibane <marc@taler.net>
Date: Fri, 14 Aug 2026 11:25:54 +0200
AI: guard bease64 images
Diffstat:
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/TalerWallet1/Helper/Image+fallback.swift b/TalerWallet1/Helper/Image+fallback.swift
@@ -32,15 +32,20 @@ extension Image {
}
}
+ /// Decode an `ImageDataUrl` as the protocol defines it: a data URL with an explicit
+ /// mediatype (`image/png` or `image/jpeg`) and the `base64` parameter.
+ /// Anything else must be rejected without ever being resolved: the string comes from
+ /// the merchant, and `NSData(contentsOf:)` would *fetch* an `http(s):` URL - blocking
+ /// the main thread and telling the merchant when (and how often) the user looks at
+ /// the transaction - or read a local file for a `file:` URL.
init?(imageBase64: String) {
- if let url = NSURL(string: imageBase64) {
- if let data = NSData(contentsOf: url as URL) {
- if let uiImage = UIImage(data: data as Data) {
- self.init(uiImage: uiImage)
- return
- }
- }
- }
- return nil
+ let anchored: String.CompareOptions = [.caseInsensitive, .anchored]
+ guard let dataUrlPrefix = imageBase64.range(of: "data:image/png;base64,", options: anchored)
+ ?? imageBase64.range(of: "data:image/jpeg;base64,", options: anchored),
+ let data = Data(base64Encoded: String(imageBase64[dataUrlPrefix.upperBound...])),
+ let uiImage = UIImage(data: data)
+ else { return nil }
+
+ self.init(uiImage: uiImage)
}
}