Skip to content

Truly independent web browser

LibWeb: Taint 2D canvases through patterns and image bitmaps

Andreas Kling10f443c993fa
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rs
index f51a695aad5..81f13248745 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rs
@@ -485,7 +485,15 @@ impl ContextState {
/// Clears the bitmap of an OffscreenCanvas when transferToImageBitmap() takes it.
pub(crate) fn replace_bitmap_with_cleared_bitmap(&self) {
self.cached_readback.replace(None);
- self.origin_clean.set(true);
+ // NB: The new bitmap is origin-clean, but the fill and stroke styles only taint the context when they are set.
+ // A pattern that is not origin-clean and is still a style of the context, or of a state on the stack,
+ // could then draw onto a clean bitmap, so the flag stays unset while one is.
+ let holds_a_tainted_style = self
+ .states
+ .borrow()
+ .iter()
+ .any(|state| state.fill_style.is_not_origin_clean() || state.stroke_style.is_not_origin_clean());
+ self.origin_clean.set(!holds_a_tainted_style);
if !self.has_backing_storage() {
return;
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/drawing_state.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/drawing_state.rs
index dcc6cdadcd4..f4a176b76aa 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/drawing_state.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/drawing_state.rs
@@ -38,6 +38,11 @@ impl FillOrStrokeStyle {
}
}
+ /// Whether the style is a pattern that is marked as not origin-clean.
+ pub(crate) fn is_not_origin_clean(self) -> bool {
+ matches!(self, Self::Pattern(pattern) if pattern.is_not_origin_clean())
+ }
+
pub(crate) fn as_color(self) -> Option<Color> {
match self {
Self::Color(color) => Some(color),
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_image.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_image.rs
index 17d7df57987..0ffc221ca1d 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_image.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_image.rs
@@ -93,8 +93,13 @@ impl CanvasImageSource {
pub(crate) fn is_not_origin_clean(self) -> bool {
match self.cpp_source() {
Some((source, kind)) => kind.is_not_origin_clean(source),
- // FIXME: image's bitmap's origin-clean flag is false.
- None => false,
+ None => {
+ let Self::ImageBitmap(image_bitmap) = self else {
+ unreachable!("an image source that is not implemented in C++ is an ImageBitmap");
+ };
+ // image's bitmap's origin-clean flag is false.
+ !image_bitmap.origin_clean()
+ }
}
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_fill_stroke_styles.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_fill_stroke_styles.rs
index 49b981bcf2f..48ef4dddc21 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_fill_stroke_styles.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_fill_stroke_styles.rs
@@ -65,8 +65,11 @@ impl<K: CanvasKind> RenderingContext2D<K> {
}
DOMStringOrCanvasGradientOrCanvasPattern::CanvasGradient(gradient) => FillOrStrokeStyle::Gradient(gradient),
DOMStringOrCanvasGradientOrCanvasPattern::CanvasPattern(pattern) => {
- // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set
- // this's origin-clean flag to false.
+ // 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's
+ // origin-clean flag to false.
+ if pattern.is_not_origin_clean() {
+ self.set_origin_clean(false);
+ }
FillOrStrokeStyle::Pattern(pattern)
}
};
@@ -106,8 +109,11 @@ impl<K: CanvasKind> RenderingContext2D<K> {
}
DOMStringOrCanvasGradientOrCanvasPattern::CanvasGradient(gradient) => FillOrStrokeStyle::Gradient(gradient),
DOMStringOrCanvasGradientOrCanvasPattern::CanvasPattern(pattern) => {
- // FIXME: 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set
- // this's origin-clean flag to false.
+ // 2. If the given value is a CanvasPattern object that is marked as not origin-clean, then set this's
+ // origin-clean flag to false.
+ if pattern.is_not_origin_clean() {
+ self.set_origin_clean(false);
+ }
FillOrStrokeStyle::Pattern(pattern)
}
};
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs
index a97e4b591d6..8b6cc7d685b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs
@@ -87,6 +87,9 @@ pub(crate) struct CanvasPattern {
repetition: Canvas2DPatternRepetition,
#[gc(untraced)]
transform: Cell<Option<AffineTransform>>,
+ /// Whether the pattern is marked as not origin-clean, as its image was.
+ #[gc(untraced)]
+ not_origin_clean: Cell<bool>,
}
impl Wrappable for CanvasPattern {}
@@ -101,6 +104,7 @@ impl CanvasPattern {
image,
repetition,
transform: Cell::new(None),
+ not_origin_clean: Cell::new(false),
},
)
}
@@ -140,7 +144,10 @@ impl CanvasPattern {
};
let pattern = Self::create(vm, frame, repetition);
- // FIXME: 7. If image is not origin-clean, then mark pattern as not origin-clean.
+ // 7. If image is not origin-clean, then mark pattern as not origin-clean.
+ if image.is_not_origin_clean() {
+ pattern.not_origin_clean.set(true);
+ }
// 8. Return pattern.
Ok(Some(pattern))
@@ -150,6 +157,11 @@ impl CanvasPattern {
self.image.as_ref()
}
+ /// Whether the pattern is marked as not origin-clean.
+ pub(crate) fn is_not_origin_clean(&self) -> bool {
+ self.not_origin_clean.get()
+ }
+
pub(crate) fn repetition(&self) -> Canvas2DPatternRepetition {
self.repetition
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
index ddc53f11ffe..eb553a8c226 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
@@ -154,6 +154,26 @@ pub unsafe extern "C" fn web_image_bitmap_close(image_bitmap: *mut c_void) {
unsafe { object_from_abi::<ImageBitmap>(image_bitmap) }.close_bitmap();
}
+/// https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
+///
+/// # Safety
+///
+/// `image_bitmap` must be a live cell.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_image_bitmap_origin_clean(image_bitmap: *mut c_void) -> bool {
+ // SAFETY: The caller passes a live cell.
+ unsafe { object_from_abi::<ImageBitmap>(image_bitmap) }.origin_clean()
+}
+
+/// # Safety
+///
+/// `image_bitmap` must be a live cell.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_image_bitmap_set_origin_clean(image_bitmap: *mut c_void, origin_clean: bool) {
+ // SAFETY: The caller passes a live cell.
+ unsafe { object_from_abi::<ImageBitmap>(image_bitmap) }.set_origin_clean(origin_clean);
+}
+
/// The Gfx::DecodedImageFrame of the bitmap data of the ImageBitmap, or null if it has none.
///
/// # Safety
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
index 0f3fda192db..211bc7f0cba 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
@@ -9,6 +9,7 @@
use core::cell::{Cell, RefCell};
use core::ffi::c_void;
+use ak::Utf16String;
use libgfx_rust::image_frame::ImageFrameHandle;
use libjs_rust::api::{RawNativeFunctionResult, Trace, Vm};
@@ -16,7 +17,7 @@ use crate::bindings::interface::{Ref, Wrappable, allocate};
use crate::bindings::structured_data::{
Serializable, SerializationReader, SerializationWriter, TransferDataReader, TransferDataWriter, Transferable,
};
-use crate::bindings::{CallContext, WebIdlResult};
+use crate::bindings::{CallContext, Exception, WebIdlResult};
unsafe extern "C" {
// Defined in Libraries/LibWeb/HTML/ImageBitmap.cpp, which encodes and decodes the Gfx::Bitmap of a frame. A frame
@@ -73,9 +74,11 @@ pub(crate) struct ImageBitmap {
height: u32,
#[gc(untraced)]
detached: Cell<bool>,
- // FIXME: An ImageBitmap object's bitmap has an origin-clean flag, which indicates whether the bitmap is tainted by
- // content from a different origin. The flag is initially set to true and may be changed to false by the
- // steps of createImageBitmap().
+ /// An ImageBitmap object's bitmap has an origin-clean flag, which indicates whether the bitmap is tainted by
+ /// content from a different origin. The flag is initially set to true and may be changed to false by the steps of
+ /// createImageBitmap().
+ #[gc(untraced)]
+ origin_clean: Cell<bool>,
}
impl Wrappable for ImageBitmap {
@@ -104,6 +107,7 @@ impl ImageBitmap {
width,
height,
detached: Cell::new(false),
+ origin_clean: Cell::new(true),
}
}
@@ -129,6 +133,15 @@ impl ImageBitmap {
self.detached.get()
}
+ /// https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
+ pub(crate) fn origin_clean(&self) -> bool {
+ self.origin_clean.get()
+ }
+
+ pub(crate) fn set_origin_clean(&self, origin_clean: bool) {
+ self.origin_clean.set(origin_clean);
+ }
+
/// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#dom-imagebitmap-width
pub(crate) fn width(&self, _: &mut CallContext) -> WebIdlResult<u32> {
Ok(self.width_of_bitmap())
@@ -180,7 +193,13 @@ impl Serializable for ImageBitmap {
serialized: &mut SerializationWriter,
_for_storage: bool,
) -> WebIdlResult<()> {
- // FIXME: 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
+ // 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
+ if !self.origin_clean.get() {
+ return Err(Exception::Dom(
+ "DataCloneError",
+ Utf16String::from_ascii(b"Cannot serialize an ImageBitmap that is not origin-clean"),
+ ));
+ }
// 2. Set serialized.[[BitmapData]] to a copy of value's bitmap data.
// SAFETY: The writer is lent for the steps, and the frame lives while the bitmap data is set.
@@ -207,7 +226,13 @@ impl Transferable for ImageBitmap {
/// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:transfer-steps
fn transfer_steps(&self, _: &mut CallContext, data_holder: &mut TransferDataWriter) -> WebIdlResult<()> {
- // FIXME: 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
+ // 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
+ if !self.origin_clean.get() {
+ return Err(Exception::Dom(
+ "DataCloneError",
+ Utf16String::from_ascii(b"Cannot transfer an ImageBitmap that is not origin-clean"),
+ ));
+ }
// 2. Set dataHolder.[[BitmapData]] to value's bitmap data.
// NB: The pixels go to the shared memory of the transfer if there is room for them there, and inline otherwise.
diff --git a/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cpp b/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cpp
index 71d2e873174..0d9cc9fbb7b 100644
--- a/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cpp
+++ b/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cpp
@@ -44,7 +44,6 @@
#include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
#include <LibWeb/HTML/Canvas/RemoteCanvas2DTransport.h>
#include <LibWeb/HTML/CanvasRenderingContext2D.h>
-#include <LibWeb/HTML/DecodedImageData.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLCanvasElement.h>
@@ -720,29 +719,7 @@ void web_canvas_2d_image_source_size(void* source_wrapper, CanvasImageSourceKind
// https://html.spec.whatwg.org/multipage/canvas.html#the-image-argument-is-not-origin-clean
bool web_canvas_2d_image_is_not_origin_clean(void* source_wrapper, CanvasImageSourceKind kind)
{
- // An object image is not origin-clean if, switching on image's type:
- return image_source_from(source_wrapper, kind).visit(
- // HTMLOrSVGImageElement
- [](GC::Ref<HTMLImageElement> image) {
- // image's current request's image data is CORS-cross-origin.
- return image->current_request().image_data()->is_cors_cross_origin();
- },
- [](GC::Ref<SVG::SVGImageElement>) {
- // FIXME: image's current request's image data is CORS-cross-origin.
- return false;
- },
- [](GC::Ref<HTML::HTMLVideoElement>) {
- // FIXME: image's media data is CORS-cross-origin.
- return false;
- },
- // HTMLCanvasElement or OffscreenCanvas
- [](OneOf<GC::Ref<HTMLCanvasElement>, GC::Ref<OffscreenCanvas>> auto const& canvas) {
- // image's bitmap's origin-clean flag is false.
- return !canvas->is_origin_clean();
- },
- [](GC::Ref<ImageBitmap> const&) -> bool {
- VERIFY_NOT_REACHED();
- });
+ return image_is_not_origin_clean(image_source_from(source_wrapper, kind));
}
// The id of the canvas that a canvas or an OffscreenCanvas draws into, readied to be drawn, if it is drawn by its id.
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
index 993c3caf3be..034977a8046 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
@@ -6,6 +6,7 @@
*/
#include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
+#include <LibWeb/HTML/DecodedImageData.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/ImageBitmap.h>
#include <LibWeb/HTML/ImageRequest.h>
@@ -89,4 +90,36 @@ WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasI
return { CanvasImageSourceUsability::Good };
}
+// https://html.spec.whatwg.org/multipage/canvas.html#the-image-argument-is-not-origin-clean
+bool image_is_not_origin_clean(CanvasImageSource const& image)
+{
+ // An object image is not origin-clean if, switching on image's type:
+ return image.visit(
+ // HTMLOrSVGImageElement
+ [](GC::Ref<HTMLImageElement> image) {
+ // image's current request's image data is CORS-cross-origin.
+ return image->current_request().image_data()->is_cors_cross_origin();
+ },
+ [](GC::Ref<SVG::SVGImageElement>) {
+ // FIXME: image's current request's image data is CORS-cross-origin.
+ return false;
+ },
+ // HTMLVideoElement
+ [](GC::Ref<HTMLVideoElement>) {
+ // FIXME: image's media data is CORS-cross-origin.
+ return false;
+ },
+ // HTMLCanvasElement
+ // OffscreenCanvas
+ [](OneOf<GC::Ref<HTMLCanvasElement>, GC::Ref<OffscreenCanvas>> auto const& canvas) {
+ // image's bitmap's origin-clean flag is false.
+ return !canvas->is_origin_clean();
+ },
+ // ImageBitmap
+ [](GC::Ref<ImageBitmap> image_bitmap) {
+ // image's bitmap's origin-clean flag is false.
+ return !image_bitmap->is_origin_clean();
+ });
+}
+
}
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h
index 3a25d886c51..a575b043c3e 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.h
@@ -29,4 +29,7 @@ enum class CanvasImageSourceUsability {
// https://html.spec.whatwg.org/multipage/canvas.html#check-the-usability-of-the-image-argument
WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasImageSource const&);
+// https://html.spec.whatwg.org/multipage/canvas.html#the-image-argument-is-not-origin-clean
+bool image_is_not_origin_clean(CanvasImageSource const&);
+
}
diff --git a/Libraries/LibWeb/HTML/ImageBitmap.cpp b/Libraries/LibWeb/HTML/ImageBitmap.cpp
index 14a726f4bb9..9285b8055a7 100644
--- a/Libraries/LibWeb/HTML/ImageBitmap.cpp
+++ b/Libraries/LibWeb/HTML/ImageBitmap.cpp
@@ -213,6 +213,8 @@ u32 web_image_bitmap_width(void const* image_bitmap);
u32 web_image_bitmap_height(void const* image_bitmap);
bool web_image_bitmap_is_detached(void const* image_bitmap);
void web_image_bitmap_close(void* image_bitmap);
+bool web_image_bitmap_origin_clean(void const* image_bitmap);
+void web_image_bitmap_set_origin_clean(void* image_bitmap, bool origin_clean);
Gfx::DecodedImageFrame const* web_image_bitmap_frame(void const* image_bitmap);
// What Bindings/Rust/src/html/image_bitmap.rs asks of C++, which owns the encoding of a Gfx::Bitmap: a bitmap that it
@@ -289,6 +291,16 @@ void ImageBitmap::close()
web_image_bitmap_close(this);
}
+bool ImageBitmap::is_origin_clean() const
+{
+ return web_image_bitmap_origin_clean(this);
+}
+
+void ImageBitmap::set_origin_clean(bool origin_clean)
+{
+ web_image_bitmap_set_origin_clean(this, origin_clean);
+}
+
Gfx::Bitmap const* ImageBitmap::bitmap() const
{
if (auto const* frame = web_image_bitmap_frame(this))
diff --git a/Libraries/LibWeb/HTML/ImageBitmap.h b/Libraries/LibWeb/HTML/ImageBitmap.h
index 6b5b295c320..a8425ecf794 100644
--- a/Libraries/LibWeb/HTML/ImageBitmap.h
+++ b/Libraries/LibWeb/HTML/ImageBitmap.h
@@ -57,6 +57,10 @@ public:
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#dom-imagebitmap-close
void close();
+ // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-clean
+ bool is_origin_clean() const;
+ void set_origin_clean(bool);
+
// The bitmap of the bitmap data, which the ImageBitmap keeps until it is closed or transferred.
Gfx::Bitmap const* bitmap() const;
diff --git a/Libraries/LibWeb/HTML/OffscreenCanvas.cpp b/Libraries/LibWeb/HTML/OffscreenCanvas.cpp
index 3ef263a6fc5..306334c22c5 100644
--- a/Libraries/LibWeb/HTML/OffscreenCanvas.cpp
+++ b/Libraries/LibWeb/HTML/OffscreenCanvas.cpp
@@ -218,6 +218,8 @@ WebIDL::ExceptionOr<GC::Ref<ImageBitmap>> OffscreenCanvas::transfer_to_image_bit
// 3. Let image be a newly created ImageBitmap object that references the same underlying bitmap data as this OffscreenCanvas object's bitmap.
auto image = ImageBitmap::create(get_bitmap_from_surface());
+ // NB: The bitmap data that image references keeps the origin-clean flag of this OffscreenCanvas object's bitmap.
+ image->set_origin_clean(is_origin_clean());
// 4. Set this OffscreenCanvas object's bitmap to reference a newly created bitmap of the same dimensions and color space as the previous bitmap, and with its pixels initialized to transparent black, or opaque black if the rendering context' s alpha is false.
if (auto* context = m_context.get_pointer<GC::Ref<OffscreenCanvasRenderingContext2D>>())
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index 7b4778208db..8868c2d449f 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -588,7 +588,8 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap_impl(JS::Realm& realm,
}
auto image_bitmap = ImageBitmap::create(cropped_bitmap_or_error.release_value());
- // FIXME: 2. Set the origin-clean flag of the imageBitmap's bitmap to the same value as the origin-clean flag of image's bitmap.
+ // 2. Set the origin-clean flag of the imageBitmap's bitmap to the same value as the origin-clean flag of image's bitmap.
+ image_bitmap->set_origin_clean(canvas->is_origin_clean());
// 3. Queue a global task, using the bitmap task source, to resolve promise with imageBitmap.
queue_global_task(Task::Source::BitmapTask, realm.global_object(), GC::create_function(GC::Heap::the(), [p, image_bitmap] {
@@ -616,7 +617,8 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap_impl(JS::Realm& realm,
}
auto image_bitmap = ImageBitmap::create(cropped_bitmap_or_error.release_value());
- // FIXME: 2. Set the origin-clean flag of imageBitmap's bitmap to the same value as the origin-clean flag of image's bitmap.
+ // 2. Set the origin-clean flag of imageBitmap's bitmap to the same value as the origin-clean flag of image's bitmap.
+ image_bitmap->set_origin_clean(source_image_bitmap->is_origin_clean());
// 3. Queue a global task, using the bitmap task source, to resolve promise with imageBitmap.
queue_global_task(Task::Source::BitmapTask, realm.global_object(), GC::create_function(GC::Heap::the(), [p, image_bitmap] {
@@ -672,7 +674,9 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap_impl(JS::Realm& realm,
}
auto image_bitmap = ImageBitmap::create(cropped_bitmap_or_error.release_value());
- // FIXME: 4. If image is not origin-clean, then set the origin-clean flag of imageBitmap's bitmap to false.
+ // 4. If image is not origin-clean, then set the origin-clean flag of imageBitmap's bitmap to false.
+ if (image_is_not_origin_clean(image_element))
+ image_bitmap->set_origin_clean(false);
// 5. Queue a global task, using the bitmap task source, to resolve promise with imageBitmap.
queue_global_task(Task::Source::BitmapTask, realm.global_object(), GC::create_function(GC::Heap::the(), [p, image_bitmap] {
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 5d99f03770e..f3dfb4e1d2b 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -606,6 +606,12 @@ Text/input/wpt-import/webvtt/parsing/file-parsing/tests/ids.html
Text/input/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.html
Text/input/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.html
+; Read back or clone the pixels of their own images, which are cross-origin for a page with a file: URL.
+Text/input/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.paint.repeat.basic.html
+Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-bounds.html
+Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.html
+Text/input/HTML/ImageBitmap-retains-dimensions-when-serialized-or-transferred.html
+
[Skipped]
; Needs vertical-rl inline geometry fidelity beyond the binary transpose
Ref/input/wpt-import/css/css-position/position-absolute-in-inline-006.html
diff --git a/Tests/LibWeb/Text/expected/canvas/tainted-pattern-and-image-bitmap.txt b/Tests/LibWeb/Text/expected/canvas/tainted-pattern-and-image-bitmap.txt
new file mode 100644
index 00000000000..00598097046
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/tainted-pattern-and-image-bitmap.txt
@@ -0,0 +1,25 @@
+Pattern created: readable
+Pattern set as fillStyle: SecurityError
+Pattern filled: SecurityError
+Pattern set as strokeStyle: SecurityError
+Pattern of another context: SecurityError
+Clean pattern: readable
+ImageBitmap of image drawn: SecurityError
+Pattern of ImageBitmap: SecurityError
+ImageBitmap of ImageBitmap drawn: SecurityError
+ImageBitmap of canvas drawn: SecurityError
+ImageBitmap of OffscreenCanvas drawn: SecurityError
+transferToImageBitmap drawn: SecurityError
+Pattern set as fillStyle of OffscreenCanvas: SecurityError
+Pattern fillStyle after transferToImageBitmap: SecurityError
+Pattern strokeStyle of saved state after transferToImageBitmap: SecurityError
+Image drawn before transferToImageBitmap: readable
+structuredClone: DataCloneError
+structuredClone with transfer: DataCloneError
+Not detached after the failed transfer: true
+drawImage after the failed transfer: no throw
+Drawn after the failed transfer: SecurityError
+structuredClone with transfer of a clean ImageBitmap after it: DataCloneError
+Clean ImageBitmap after the failed transfer: 255,0,0,255
+structuredClone of clean ImageBitmap: no throw
+structuredClone of clean ImageBitmap with transfer: no throw
diff --git a/Tests/LibWeb/Text/input/canvas/tainted-pattern-and-image-bitmap.html b/Tests/LibWeb/Text/input/canvas/tainted-pattern-and-image-bitmap.html
new file mode 100644
index 00000000000..68a93e4d4a2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/tainted-pattern-and-image-bitmap.html
@@ -0,0 +1,208 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ // A cross-origin image loaded without CORS taints what it reaches: a pattern made from it taints the canvas whose
+ // fill or stroke style it becomes, and an ImageBitmap made from it taints the canvas it is drawn onto and cannot be
+ // cloned or transferred.
+
+ // 1x1 opaque red PNG.
+ const redPng =
+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==";
+
+ function readback(label, canvas) {
+ try {
+ canvas.getContext("2d").getImageData(0, 0, 1, 1);
+ println(`${label}: readable`);
+ } catch (error) {
+ println(`${label}: ${error.name}`);
+ }
+ }
+
+ function report(label, fn) {
+ try {
+ fn();
+ println(`${label}: no throw`);
+ } catch (error) {
+ println(`${label}: ${error.name}`);
+ }
+ }
+
+ function newCanvas() {
+ const canvas = document.createElement("canvas");
+ canvas.width = 1;
+ canvas.height = 1;
+ return canvas;
+ }
+
+ promiseTest(async () => {
+ const imageURL = await httpTestServer().createEcho("GET", "/tainted-pattern-and-image-bitmap.png", {
+ status: 200,
+ headers: { "Content-Type": "image/png" },
+ body_encoding: "base64",
+ body: redPng,
+ });
+ const image = new Image();
+ await new Promise((resolve, reject) => {
+ image.onload = resolve;
+ image.onerror = reject;
+ image.src = imageURL;
+ });
+
+ // createPattern() and fillStyle.
+ {
+ const canvas = newCanvas();
+ const context = canvas.getContext("2d");
+ const pattern = context.createPattern(image, "repeat");
+ readback("Pattern created", canvas);
+ context.fillStyle = pattern;
+ readback("Pattern set as fillStyle", canvas);
+ context.fillRect(0, 0, 1, 1);
+ readback("Pattern filled", canvas);
+ }
+
+ // strokeStyle.
+ {
+ const canvas = newCanvas();
+ const context = canvas.getContext("2d");
+ context.strokeStyle = context.createPattern(image, "no-repeat");
+ readback("Pattern set as strokeStyle", canvas);
+ }
+
+ // A pattern made by another context taints the context whose style it becomes.
+ {
+ const pattern = newCanvas().getContext("2d").createPattern(image, "repeat");
+ const canvas = newCanvas();
+ canvas.getContext("2d").fillStyle = pattern;
+ readback("Pattern of another context", canvas);
+ }
+
+ // A pattern of a clean image does not taint.
+ {
+ const source = newCanvas();
+ const canvas = newCanvas();
+ const context = canvas.getContext("2d");
+ context.fillStyle = context.createPattern(source, "repeat");
+ context.fillRect(0, 0, 1, 1);
+ readback("Clean pattern", canvas);
+ }
+
+ // createImageBitmap() of the image, then drawImage().
+ const bitmap = await createImageBitmap(image);
+ {
+ const canvas = newCanvas();
+ canvas.getContext("2d").drawImage(bitmap, 0, 0);
+ readback("ImageBitmap of image drawn", canvas);
+ }
+
+ // createPattern() of the ImageBitmap.
+ {
+ const canvas = newCanvas();
+ const context = canvas.getContext("2d");
+ context.fillStyle = context.createPattern(bitmap, "repeat");
+ readback("Pattern of ImageBitmap", canvas);
+ }
+
+ // createImageBitmap() of the ImageBitmap.
+ {
+ const copy = await createImageBitmap(bitmap);
+ const canvas = newCanvas();
+ canvas.getContext("2d").drawImage(copy, 0, 0);
+ readback("ImageBitmap of ImageBitmap drawn", canvas);
+ }
+
+ // createImageBitmap() of a tainted canvas and of a tainted OffscreenCanvas.
+ {
+ const tainted = newCanvas();
+ tainted.getContext("2d").drawImage(image, 0, 0);
+ const fromCanvas = await createImageBitmap(tainted);
+ const canvas = newCanvas();
+ canvas.getContext("2d").drawImage(fromCanvas, 0, 0);
+ readback("ImageBitmap of canvas drawn", canvas);
+
+ const offscreen = new OffscreenCanvas(1, 1);
+ offscreen.getContext("2d").drawImage(image, 0, 0);
+ const fromOffscreen = await createImageBitmap(offscreen);
+ const other = newCanvas();
+ other.getContext("2d").drawImage(fromOffscreen, 0, 0);
+ readback("ImageBitmap of OffscreenCanvas drawn", other);
+ }
+
+ // transferToImageBitmap() of a tainted OffscreenCanvas.
+ {
+ const offscreen = new OffscreenCanvas(1, 1);
+ offscreen.getContext("2d").drawImage(image, 0, 0);
+ const transferred = offscreen.transferToImageBitmap();
+ const canvas = newCanvas();
+ canvas.getContext("2d").drawImage(transferred, 0, 0);
+ readback("transferToImageBitmap drawn", canvas);
+ }
+
+ // The fillStyle setter of an OffscreenCanvas context.
+ {
+ const offscreen = new OffscreenCanvas(1, 1);
+ offscreen.getContext("2d").fillStyle = offscreen.getContext("2d").createPattern(image, "repeat");
+ readback("Pattern set as fillStyle of OffscreenCanvas", offscreen);
+ }
+
+ // transferToImageBitmap() gives the OffscreenCanvas a new, clean bitmap, but a pattern that is not
+ // origin-clean and is still a style of the context, or of a saved state, keeps the context tainted.
+ {
+ const offscreen = new OffscreenCanvas(1, 1);
+ const context = offscreen.getContext("2d");
+ context.fillStyle = context.createPattern(image, "repeat");
+ offscreen.transferToImageBitmap();
+ context.fillRect(0, 0, 1, 1);
+ readback("Pattern fillStyle after transferToImageBitmap", offscreen);
+ }
+ {
+ const offscreen = new OffscreenCanvas(1, 1);
+ const context = offscreen.getContext("2d");
+ context.strokeStyle = context.createPattern(image, "repeat");
+ context.save();
+ context.strokeStyle = "green";
+ offscreen.transferToImageBitmap();
+ context.restore();
+ context.strokeRect(0, 0, 1, 1);
+ readback("Pattern strokeStyle of saved state after transferToImageBitmap", offscreen);
+ }
+ {
+ const offscreen = new OffscreenCanvas(1, 1);
+ const context = offscreen.getContext("2d");
+ context.drawImage(image, 0, 0);
+ offscreen.transferToImageBitmap();
+ readback("Image drawn before transferToImageBitmap", offscreen);
+ }
+
+ // An ImageBitmap that is not origin-clean can be neither serialized nor transferred.
+ report("structuredClone", () => structuredClone(bitmap));
+ report("structuredClone with transfer", () => structuredClone(bitmap, { transfer: [bitmap] }));
+ println(`Not detached after the failed transfer: ${bitmap.width === 1 && bitmap.height === 1}`);
+
+ // The bitmap data stays after the failed transfer: drawImage() of a bitmap without bitmap data draws nothing
+ // and does not taint, so a taint shows that the bitmap is drawn.
+ {
+ const canvas = newCanvas();
+ report("drawImage after the failed transfer", () => canvas.getContext("2d").drawImage(bitmap, 0, 0));
+ readback("Drawn after the failed transfer", canvas);
+ }
+
+ // A clean ImageBitmap later in the transfer list of the failed transfer keeps its pixels.
+ {
+ const red = await createImageBitmap(new ImageData(new Uint8ClampedArray([255, 0, 0, 255]), 1, 1));
+ report("structuredClone with transfer of a clean ImageBitmap after it", () =>
+ structuredClone([bitmap, red], { transfer: [bitmap, red] })
+ );
+ const canvas = newCanvas();
+ const context = canvas.getContext("2d");
+ context.drawImage(red, 0, 0);
+ println(`Clean ImageBitmap after the failed transfer: ${context.getImageData(0, 0, 1, 1).data.join(",")}`);
+ }
+
+ // A clean ImageBitmap still can.
+ const clean = await createImageBitmap(new ImageData(1, 1));
+ report("structuredClone of clean ImageBitmap", () => structuredClone(clean));
+ report("structuredClone of clean ImageBitmap with transfer", () =>
+ structuredClone(clean, { transfer: [clean] })
+ );
+ });
+</script>