Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 4844cb4

Browse files
Add crossOrigin property to <img> tag used for decoding (#54961)
Adds the `crossOrigin` property to the `<img>` tag used for decoding. This allows us to use cross-origin images in CanvasKit as if it were loaded from the same origin, as long as the CORS headers are properly set on the server hosting the image. In the case where the image doesn't have proper CORS headers, this change causes an error to occur while attempting to decode, rather than later on in Skia when the image is converted into a texture. Hitting the cross-origin error later causes Skia to behave in undefined ways. Progress towards flutter/flutter#149843 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 652a43b commit 4844cb4

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

lib/web_ui/lib/src/engine/html_image_element_codec.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ abstract class HtmlImageElementCodec implements ui.Codec {
4545
imgElement = createDomHTMLImageElement();
4646
imgElement!.src = src;
4747
setJsProperty<String>(imgElement!, 'decoding', 'async');
48+
setJsProperty<String>(imgElement!, 'crossOrigin', 'anonymous');
4849

4950
// Ignoring the returned future on purpose because we're communicating
5051
// through the `completer`.

lib/web_ui/lib/src/engine/safe_browser_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ T getJsProperty<T>(Object object, String name) {
4040
}
4141

4242
const Set<String> _safeJsProperties = <String>{
43+
'crossOrigin',
4344
'decoding',
4445
'__flutter_state',
4546
};

lib/web_ui/test/canvaskit/image_golden_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ Future<void> testMain() async {
253253
}
254254
});
255255

256+
test('crossOrigin requests cause an error', () async {
257+
final String otherOrigin =
258+
domWindow.location.origin.replaceAll('localhost', '127.0.0.1');
259+
bool gotError = false;
260+
try {
261+
final ui.Codec _ = await renderer.instantiateImageCodecFromUrl(
262+
Uri.parse('$otherOrigin/test_images/1x1.png'));
263+
} catch (e) {
264+
gotError = true;
265+
}
266+
expect(gotError, isTrue, reason: 'Should have got CORS error');
267+
});
268+
256269
_testCkAnimatedImage();
257270

258271
test('isAvif', () {

0 commit comments

Comments
 (0)