Truly independent web browser
LibWeb: Taint canvases with video from CORS-cross-origin responses
Zaggy1024bd99ccf8b40d
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cppindex 30b3a406b26..fb1165b3d93 100644--- a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp+++ b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp@@ -108,9 +108,9 @@ bool image_is_not_origin_clean(CanvasImageSource const& image)return image_data && image_data->is_cors_cross_origin();},// HTMLVideoElement- [](GC::Ref<HTMLVideoElement>) {- // FIXME: image's media data is CORS-cross-origin.- return false;+ [](GC::Ref<HTMLVideoElement> video) {+ // image's media data is CORS-cross-origin.+ return video->media_data_is_cors_cross_origin();},// HTMLCanvasElement// OffscreenCanvasdiff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cppindex ae2b9889c40..ef7afff41ad 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp@@ -1668,6 +1668,8 @@ void HTMLMediaElement::run_remote_mode_resource_fetch_steps(ByteRange byte_rangefetch_algorithms_input.process_response = [self = GC::Ref(*this), byte_range = move(byte_range), fetch_generation](auto response) mutable {auto& fetch_data = self->m_remote_fetch_data;+ auto response_is_cors_cross_origin = response->is_cors_cross_origin();+// FIXME: If the response is CORS cross-origin, we must use its internal response to query any of its data. See:// https://github.com/whatwg/html/issues/9355response = response->unsafe_response();@@ -1705,7 +1707,7 @@ void HTMLMediaElement::run_remote_mode_resource_fetch_steps(ByteRange byte_range// 2. Let updateMedia be to queue a media element task given the media element to run the first appropriate steps from the media data processing// steps list below. (A new task is used for this so that the work described below occurs relative to the appropriate media element event task// source rather than using the networking task source.)- auto update_media = GC::create_function(self->heap(), [weak_self = GC::Weak(self), fetch_generation](ByteBuffer media_data) mutable {+ auto update_media = GC::create_function(self->heap(), [weak_self = GC::Weak(self), fetch_generation, response_is_cors_cross_origin](ByteBuffer media_data) mutable {if (!weak_self)return;if (fetch_generation != weak_self->m_current_fetch_generation)@@ -1715,6 +1717,8 @@ void HTMLMediaElement::run_remote_mode_resource_fetch_steps(ByteRange byte_range// 6. Update the media data with the contents of response's unsafe response obtained in this fashion. response can be CORS-same-origin or// CORS-cross-origin; this affects whether subtitles referenced in the media data are exposed in the API and, for video elements, whether// a canvas gets tainted when the video is drawn on it.+ if (response_is_cors_cross_origin)+ weak_self->m_media_data_is_cors_cross_origin = true;fetch_data->stream->add_chunk_at(fetch_data->offset, media_data.bytes());fetch_data->offset += media_data.size();@@ -2027,6 +2031,13 @@ Optional<Compositing::VideoSinkResourceId> HTMLMediaElement::video_sink_resourcereturn m_active_video_sink->resource_id();}+// https://html.spec.whatwg.org/multipage/media.html#media-data+bool HTMLMediaElement::media_data_is_cors_cross_origin() const+{+ // NB: Media data from a MediaSource or a MediaStream is provided by the page itself.+ return m_media_data_is_cors_cross_origin;+}+Optional<Compositing::CapturedVideoFrame> HTMLMediaElement::capture_current_video_frame() const{if (!m_active_video_sink)@@ -2675,6 +2686,7 @@ void HTMLMediaElement::forget_media_resource_specific_tracks()if (m_playback_manager)m_playback_manager->on_playback_state_change = nullptr;release_active_video_sink();+ m_media_data_is_cors_cross_origin = false;m_audio_tracks->remove_all_tracks();m_video_tracks->remove_all_tracks();update_audio_play_state();diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.hindex ef5491f0aa4..ab0ae7a4bc8 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h@@ -201,6 +201,7 @@ public:Optional<Media::VideoSinkHandle> video_sink_handle() const;Optional<Compositing::CapturedVideoFrame> capture_current_video_frame() const;+ bool media_data_is_cors_cross_origin() const;struct VideoFrameCounts {u64 presented { 0 };@@ -393,6 +394,7 @@ private:// https://html.spec.whatwg.org/multipage/media.html#dom-media-readystateReadyState m_ready_state { ReadyState::HaveNothing };bool m_has_fired_loadeddata { false };+ bool m_media_data_is_cors_cross_origin { false };// https://html.spec.whatwg.org/multipage/media.html#dom-media-seekingbool m_seeking { false };diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 8788d0e309c..2dcd30682a2 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -1,4 +1,8 @@[LoadFromHttpServer]+Text/input/HTML/createImageBitmap-video.html+Text/input/HTML/HTMLVideoElement-capture-before-seek-keeps-frame.html+Text/input/HTML/HTMLVideoElement-cross-origin-media-data-taints-canvas.html+Text/input/HTML/HTMLVideoElement-requestVideoFrameCallback.htmlText/input/wpt-import/fetch/cross-origin-resource-policy/fetch-in-iframe.htmlText/input/wpt-import/fetch/cross-origin-resource-policy/iframe-loads.htmlText/input/wpt-import/fetch/cross-origin-resource-policy/image-loads.htmldiff --git a/Tests/LibWeb/Text/expected/HTML/HTMLVideoElement-cross-origin-media-data-taints-canvas.txt b/Tests/LibWeb/Text/expected/HTML/HTMLVideoElement-cross-origin-media-data-taints-canvas.txtnew file mode 100644index 00000000000..7a0e595b9b3--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/HTMLVideoElement-cross-origin-media-data-taints-canvas.txt@@ -0,0 +1,6 @@+same-origin drawImage, then getImageData: allowed+same-origin createPattern, then getImageData: allowed+same-origin texImage2D: allowed+cross-origin drawImage, then getImageData: SecurityError+cross-origin createPattern, then getImageData: SecurityError+cross-origin texImage2D: SecurityErrordiff --git a/Tests/LibWeb/Text/input/HTML/HTMLVideoElement-cross-origin-media-data-taints-canvas.html b/Tests/LibWeb/Text/input/HTML/HTMLVideoElement-cross-origin-media-data-taints-canvas.htmlnew file mode 100644index 00000000000..f1f15052e40--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/HTMLVideoElement-cross-origin-media-data-taints-canvas.html@@ -0,0 +1,53 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A video whose media data came from a CORS-cross-origin response must not be readable through a canvas: drawing+ // it, or a pattern of it, taints a 2D canvas, and uploading it to WebGL throws. The same video served same-origin+ // stays readable.+ async function loadVideo(url) {+ const video = document.createElement("video");+ video.src = url;+ document.body.append(video);+ if (video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {+ await new Promise((resolve, reject) => {+ video.addEventListener("loadeddata", resolve, { once: true });+ video.addEventListener("error", () => reject(new Error(`load failed with code ${video.error?.code}`)), { once: true });+ });+ }+ return video;+ }++ function outcome(action) {+ try {+ action();+ return "allowed";+ } catch (error) {+ return error.name;+ }+ }++ function check(label, video) {+ const context = document.createElement("canvas").getContext("2d");+ context.drawImage(video, 0, 0);+ println(`${label} drawImage, then getImageData: ${outcome(() => context.getImageData(0, 0, 1, 1))}`);++ const patternContext = document.createElement("canvas").getContext("2d");+ patternContext.fillStyle = patternContext.createPattern(video, "no-repeat");+ patternContext.fillRect(0, 0, 1, 1);+ println(`${label} createPattern, then getImageData: ${outcome(() => patternContext.getImageData(0, 0, 1, 1))}`);++ const gl = document.createElement("canvas").getContext("webgl");+ gl.bindTexture(gl.TEXTURE_2D, gl.createTexture());+ println(`${label} texImage2D: ${outcome(() => gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video))}`);+ }++ promiseTest(async () => {+ const server = httpTestServer();+ const sameOriginURL = server.getStaticURL("Assets/solid-blue-160x120.webm");+ const crossOriginURL = new URL(sameOriginURL);+ crossOriginURL.hostname = uniqueLocalhostHostname("cross-origin-video");++ check("same-origin", await loadVideo(sameOriginURL));+ check("cross-origin", await loadVideo(crossOriginURL.href));+ });+</script>