Skip to content

Truly independent web browser

LibWeb: Throw for WebGL texture sources that are not origin-clean

Andreas Kling348dcd8f6217
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs
index 5b984fa18f5..65545d5a2fe 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs
@@ -12,6 +12,7 @@ use core::ffi::c_void;
use core::marker::PhantomData;
use core::ops::Deref;
+use ak::Utf16String;
use libjs_rust::api::{ForeignCellSlot, Gc, Realm, Trace, Visitor, Vm};
use super::canvas::{
@@ -39,6 +40,8 @@ use super::web_gl_uniform_location::WebGLUniformLocation;
use super::web_gl_vertex_array_object::WebGLVertexArrayObject;
use super::web_gl_vertex_array_object_oes::WebGLVertexArrayObjectOES;
use crate::bindings::interface::{Inherits, Interface, Ref, Wrappable, cell_into_abi};
+use crate::bindings::{Exception, WebIdlResult};
+use crate::canvas_2d::canvas::CanvasImageSourceKind;
pub(crate) const MAX_WEBGL_DRAWING_BUFFER_DIMENSION: i32 = 16384;
@@ -354,11 +357,18 @@ impl ContextState {
source: &TexImageSource,
format: u32,
r#type: u32,
- ) -> Option<(Frame, FrameUpload)> {
- // FIXME: If this function is called with an HTMLImageElement or HTMLVideoElement whose origin differs from the
- // origin of the containing Document, or with an HTMLCanvasElement, ImageBitmap or OffscreenCanvas whose
- // bitmap's origin-clean flag is set to false, a SECURITY_ERR exception must be thrown. See Origin
- // Restrictions.
+ ) -> WebIdlResult<Option<(Frame, FrameUpload)>> {
+ // If this function is called with an HTMLImageElement or HTMLVideoElement whose origin differs from the origin
+ // of the containing Document, or with an HTMLCanvasElement, ImageBitmap or OffscreenCanvas whose bitmap's
+ // origin-clean flag is set to false, a SECURITY_ERR exception must be thrown. See Origin Restrictions.
+ // https://registry.khronos.org/webgl/specs/latest/1.0/#ORIGIN_RESTRICTIONS
+ if tex_image_source_is_not_origin_clean(source) {
+ return Err(Exception::Dom(
+ "SecurityError",
+ Utf16String::from_ascii(b"Cannot upload a texture source that is not origin-clean"),
+ ));
+ }
+
let source_frame = match *source {
TexImageSource::ImageBitmap(image_bitmap) => match image_bitmap.frame() {
Some(image_frame) => read_tex_image_frame(&image_frame, format, r#type),
@@ -391,10 +401,10 @@ impl ContextState {
};
let frame = match source_frame {
TexImageSourceFrame::Frame(frame) => frame,
- TexImageSourceFrame::NoFrame => return None,
+ TexImageSourceFrame::NoFrame => return Ok(None),
TexImageSourceFrame::InvalidValue => {
self.set_error(GL_INVALID_VALUE);
- return None;
+ return Ok(None);
}
};
// The first pixel transferred from the source to the WebGL implementation corresponds to the upper left
@@ -406,7 +416,7 @@ impl ContextState {
flip_y,
premultiply_alpha: self.unpack_premultiply_alpha.get(),
};
- Some((frame, upload))
+ Ok(Some((frame, upload)))
}
/// https://registry.khronos.org/webgl/specs/latest/2.0/#PIXEL_STORE_PARAM_CONSTRAINTS
@@ -492,3 +502,17 @@ impl ContextState {
self.transform_feedback_primitives_written.set(None);
}
}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#the-image-argument-is-not-origin-clean
+fn tex_image_source_is_not_origin_clean(source: &TexImageSource) -> bool {
+ match *source {
+ TexImageSource::ImageBitmap(image_bitmap) => !image_bitmap.origin_clean(),
+ TexImageSource::ImageData(_) => false,
+ TexImageSource::HTMLImageElement(object) => CanvasImageSourceKind::HTMLImageElement.is_not_origin_clean(object),
+ TexImageSource::HTMLCanvasElement(object) => {
+ CanvasImageSourceKind::HTMLCanvasElement.is_not_origin_clean(object)
+ }
+ TexImageSource::HTMLVideoElement(object) => CanvasImageSourceKind::HTMLVideoElement.is_not_origin_clean(object),
+ TexImageSource::OffscreenCanvas(object) => CanvasImageSourceKind::OffscreenCanvas.is_not_origin_clean(object),
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs
index ee973415c12..679e19a9a37 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs
@@ -292,7 +292,7 @@ impl RenderingContext<WebGL2> {
return Ok(());
}
- let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
upload.destination_size = Some((width, height));
@@ -420,7 +420,7 @@ impl RenderingContext<WebGL2> {
r#type: u32,
source: TexImageSource,
) -> WebIdlResult<()> {
- let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
upload.destination_size = Some((width, height));
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs
index 5cdc6620e8a..9a43c967eb1 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs
@@ -193,7 +193,7 @@ impl RenderingContext<WebGL2> {
r#type: u32,
source: TexImageSource,
) -> WebIdlResult<()> {
- let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
self.proxy
@@ -221,7 +221,7 @@ impl RenderingContext<WebGL2> {
return Ok(());
}
- let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
upload.destination_size = Some((width, height));
@@ -329,7 +329,7 @@ impl RenderingContext<WebGL2> {
r#type: u32,
source: TexImageSource,
) -> WebIdlResult<()> {
- let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
self.proxy
@@ -350,7 +350,7 @@ impl RenderingContext<WebGL2> {
r#type: u32,
source: TexImageSource,
) -> WebIdlResult<()> {
- let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, mut upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
upload.destination_size = Some((width, height));
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs
index 2719a6c3a1f..ce72a6b5c6c 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs
@@ -301,7 +301,7 @@ impl RenderingContext<WebGL1> {
r#type: u32,
source: TexImageSource,
) -> WebIdlResult<()> {
- let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
self.proxy
@@ -362,7 +362,7 @@ impl RenderingContext<WebGL1> {
r#type: u32,
source: TexImageSource,
) -> WebIdlResult<()> {
- let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type) else {
+ let Some((frame, upload)) = self.read_texture_image_source(&source, format, r#type)? else {
return Ok(());
};
self.proxy
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
index 034977a8046..944ceeb07a8 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
@@ -98,7 +98,9 @@ bool image_is_not_origin_clean(CanvasImageSource const& image)
// 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();
+ // NB: WebGL asks before it knows that the image has image data.
+ auto image_data = image->current_request().image_data();
+ return image_data && image_data->is_cors_cross_origin();
},
[](GC::Ref<SVG::SVGImageElement>) {
// FIXME: image's current request's image data is CORS-cross-origin.
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasHost.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasHost.cpp
index 9f9dcb0a318..c2fd5610eba 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasHost.cpp
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasHost.cpp
@@ -67,7 +67,9 @@ bool CanvasHost::is_origin_clean() const
{
if (auto* context = canvas_2d_context())
return context->origin_clean();
- // FIXME: WebGL and WebGL2 contexts do not track the origin-clean flag yet.
+ // NB: The texture uploads of a WebGL context throw a "SecurityError" DOMException for a source that is not
+ // origin-clean, so its bitmap stays origin-clean. A video is the exception, as long as its media data is
+ // never CORS-cross-origin (see image_is_not_origin_clean()).
return true;
}
diff --git a/Tests/LibWeb/Text/expected/WebGL/tex-image-source-not-origin-clean.txt b/Tests/LibWeb/Text/expected/WebGL/tex-image-source-not-origin-clean.txt
new file mode 100644
index 00000000000..11d20f50e49
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebGL/tex-image-source-not-origin-clean.txt
@@ -0,0 +1,40 @@
+webgl, texImage2D, cross-origin image: SecurityError
+webgl, texSubImage2D, cross-origin image: SecurityError
+webgl, texImage2D, ImageBitmap of the image: SecurityError
+webgl, texSubImage2D, ImageBitmap of the image: SecurityError
+webgl, texImage2D, tainted canvas: SecurityError
+webgl, texSubImage2D, tainted canvas: SecurityError
+webgl, texImage2D, tainted OffscreenCanvas: SecurityError
+webgl, texSubImage2D, tainted OffscreenCanvas: SecurityError
+webgl, texImage2D, clean canvas: no throw
+webgl, texSubImage2D, clean canvas: no throw
+webgl, texImage2D, clean ImageBitmap: no throw
+webgl, texSubImage2D, clean ImageBitmap: no throw
+webgl, error: 0
+webgl, WebGL canvas drawn, then read back: no throw
+webgl2, texImage2D, cross-origin image: SecurityError
+webgl2, texSubImage2D, cross-origin image: SecurityError
+webgl2, texImage2D with a size, cross-origin image: SecurityError
+webgl2, texSubImage2D with a size, cross-origin image: SecurityError
+webgl2, texImage2D, ImageBitmap of the image: SecurityError
+webgl2, texSubImage2D, ImageBitmap of the image: SecurityError
+webgl2, texImage2D with a size, ImageBitmap of the image: SecurityError
+webgl2, texSubImage2D with a size, ImageBitmap of the image: SecurityError
+webgl2, texImage2D, tainted canvas: SecurityError
+webgl2, texSubImage2D, tainted canvas: SecurityError
+webgl2, texImage2D with a size, tainted canvas: SecurityError
+webgl2, texSubImage2D with a size, tainted canvas: SecurityError
+webgl2, texImage2D, tainted OffscreenCanvas: SecurityError
+webgl2, texSubImage2D, tainted OffscreenCanvas: SecurityError
+webgl2, texImage2D with a size, tainted OffscreenCanvas: SecurityError
+webgl2, texSubImage2D with a size, tainted OffscreenCanvas: SecurityError
+webgl2, texImage2D, clean canvas: no throw
+webgl2, texSubImage2D, clean canvas: no throw
+webgl2, texImage2D with a size, clean canvas: no throw
+webgl2, texSubImage2D with a size, clean canvas: no throw
+webgl2, texImage2D, clean ImageBitmap: no throw
+webgl2, texSubImage2D, clean ImageBitmap: no throw
+webgl2, texImage2D with a size, clean ImageBitmap: no throw
+webgl2, texSubImage2D with a size, clean ImageBitmap: no throw
+webgl2, error: 0
+webgl2, WebGL canvas drawn, then read back: no throw
diff --git a/Tests/LibWeb/Text/input/WebGL/tex-image-source-not-origin-clean.html b/Tests/LibWeb/Text/input/WebGL/tex-image-source-not-origin-clean.html
new file mode 100644
index 00000000000..14ffbb5d47f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebGL/tex-image-source-not-origin-clean.html
@@ -0,0 +1,84 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A texture upload from a source that is not origin-clean throws a "SecurityError" DOMException, so a WebGL canvas
+ // stays origin-clean.
+
+ // 1x1 opaque red PNG.
+ const redPng =
+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==";
+
+ function newCanvas() {
+ const canvas = document.createElement("canvas");
+ canvas.width = 1;
+ canvas.height = 1;
+ return canvas;
+ }
+
+ function report(label, fn) {
+ try {
+ fn();
+ println(`${label}: no throw`);
+ } catch (error) {
+ println(`${label}: ${error.name}`);
+ }
+ }
+
+ promiseTest(async () => {
+ const imageURL = await httpTestServer().createEcho("GET", "/tex-image-source-not-origin-clean.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;
+ });
+
+ const taintedCanvas = newCanvas();
+ taintedCanvas.getContext("2d").drawImage(image, 0, 0);
+ const taintedOffscreenCanvas = new OffscreenCanvas(1, 1);
+ taintedOffscreenCanvas.getContext("2d").drawImage(image, 0, 0);
+ const sources = {
+ "cross-origin image": image,
+ "ImageBitmap of the image": await createImageBitmap(image),
+ "tainted canvas": taintedCanvas,
+ "tainted OffscreenCanvas": taintedOffscreenCanvas,
+ "clean canvas": newCanvas(),
+ "clean ImageBitmap": await createImageBitmap(new ImageData(1, 1)),
+ };
+
+ for (const type of ["webgl", "webgl2"]) {
+ const glCanvas = newCanvas();
+ const gl = glCanvas.getContext(type);
+ gl.bindTexture(gl.TEXTURE_2D, gl.createTexture());
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
+ for (const [name, source] of Object.entries(sources)) {
+ report(`${type}, texImage2D, ${name}`, () =>
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source)
+ );
+ report(`${type}, texSubImage2D, ${name}`, () =>
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, source)
+ );
+ if (type === "webgl2") {
+ report(`${type}, texImage2D with a size, ${name}`, () =>
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, source)
+ );
+ report(`${type}, texSubImage2D with a size, ${name}`, () =>
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, source)
+ );
+ }
+ }
+ println(`${type}, error: ${gl.getError()}`);
+
+ const canvas = newCanvas();
+ canvas.getContext("2d").drawImage(glCanvas, 0, 0);
+ report(`${type}, WebGL canvas drawn, then read back`, () =>
+ canvas.getContext("2d").getImageData(0, 0, 1, 1)
+ );
+ }
+ });
+</script>