Skip to content

Truly independent web browser

LibWeb: Mark message events fired from C++ as trusted

Shannon Booth4eabaffa4a8a
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
index 1897dee252c..d14f9a24d0b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
@@ -605,8 +605,8 @@ pub unsafe extern "C" fn web_rust_html_create_storage_event(vm: *mut c_void, rea
.as_ptr()
}
-/// The Event of a new MessageEvent in the realm, for C++ to fire. Its origin is the origin if there is one, and the
-/// empty string otherwise.
+/// The Event of a new trusted MessageEvent in the realm, for C++ to fire. Its origin is the origin if there is one,
+/// and the empty string otherwise.
///
/// # Safety
///
@@ -643,9 +643,9 @@ pub unsafe extern "C" fn web_rust_html_create_message_event(
let origin = (!origin.is_null()).then(|| unsafe { (*origin).to_origin() });
// SAFETY: The caller lends the type for the call.
let r#type: Utf16String = unsafe { Utf16FlyString::from_raw_borrowed(r#type) }.into();
- MessageEvent::create(vm, realm, &r#type, event_init, origin)
- .event()
- .as_ptr()
+ let event = MessageEvent::create(vm, realm, &r#type, event_init, origin);
+ event.event().set_is_trusted(true);
+ event.event().as_ptr()
}
/// The Event of a new MessageEvent in the realm whose type is the empty string, for createEvent().
diff --git a/Libraries/LibWeb/HTML/MessageEvent.h b/Libraries/LibWeb/HTML/MessageEvent.h
index 5179b92bf2b..ac7cf4d3730 100644
--- a/Libraries/LibWeb/HTML/MessageEvent.h
+++ b/Libraries/LibWeb/HTML/MessageEvent.h
@@ -26,8 +26,8 @@ struct MessageEventInit {
GC::Ptr<MessagePort> source;
};
-// The Event of a new MessageEvent in the realm, whose origin is the origin if there is one, and the empty string
-// otherwise.
+// The Event of a new trusted MessageEvent in the realm, for the implementation to fire. Its origin is the origin if
+// there is one, and the empty string otherwise.
WEB_API GC::Ref<DOM::Event> create_message_event(JS::Realm&, Utf16FlyString const& type, MessageEventInit const&, Optional<URL::Origin> const& origin = {});
// The Event of a new MessageEvent in the realm whose type is the empty string, for createEvent().
diff --git a/Tests/LibWeb/Text/expected/WebRTC/data-channel-loopback.txt b/Tests/LibWeb/Text/expected/WebRTC/data-channel-loopback.txt
index 8f177d4985a..0044c2a60c2 100644
--- a/Tests/LibWeb/Text/expected/WebRTC/data-channel-loopback.txt
+++ b/Tests/LibWeb/Text/expected/WebRTC/data-channel-loopback.txt
@@ -3,6 +3,7 @@ negotiated: true
trickle ICE: true
remote label: echo
text payload: hello 🐦
+message isTrusted: true
binary payload: 0,1,127,255
blob payload: true
real stats report: true
diff --git a/Tests/LibWeb/Text/expected/WebSocket/message-event-is-trusted.txt b/Tests/LibWeb/Text/expected/WebSocket/message-event-is-trusted.txt
new file mode 100644
index 00000000000..8727108de4f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebSocket/message-event-is-trusted.txt
@@ -0,0 +1 @@
+isTrusted: true
diff --git a/Tests/LibWeb/Text/expected/Workers/shared-worker-connect-event-is-trusted.txt b/Tests/LibWeb/Text/expected/Workers/shared-worker-connect-event-is-trusted.txt
new file mode 100644
index 00000000000..8727108de4f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Workers/shared-worker-connect-event-is-trusted.txt
@@ -0,0 +1 @@
+isTrusted: true
diff --git a/Tests/LibWeb/Text/input/WebRTC/data-channel-loopback.html b/Tests/LibWeb/Text/input/WebRTC/data-channel-loopback.html
index 214f0f9693b..115ffab5783 100644
--- a/Tests/LibWeb/Text/input/WebRTC/data-channel-loopback.html
+++ b/Tests/LibWeb/Text/input/WebRTC/data-channel-loopback.html
@@ -37,7 +37,9 @@ asyncTest(async done => {
println(`remote label: ${remote.label}`);
let message = new Promise(resolve => remote.onmessage = resolve);
channel.send("hello 🐦");
- println(`text payload: ${(await message).data}`);
+ const textEvent = await message;
+ println(`text payload: ${textEvent.data}`);
+ println(`message isTrusted: ${textEvent.isTrusted}`);
message = new Promise(resolve => remote.onmessage = resolve);
channel.send(new Uint8Array([0, 1, 127, 255]));
println(`binary payload: ${Array.from(new Uint8Array((await message).data))}`);
diff --git a/Tests/LibWeb/Text/input/WebSocket/message-event-is-trusted.html b/Tests/LibWeb/Text/input/WebSocket/message-event-is-trusted.html
new file mode 100644
index 00000000000..76cfac40d47
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebSocket/message-event-is-trusted.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ const ws = new WebSocket(`ws://localhost:${internals.getEchoServerPort()}/`);
+ ws.onopen = () => ws.send("hello");
+ ws.onmessage = event => {
+ println(`isTrusted: ${event.isTrusted}`);
+ ws.close();
+ done();
+ };
+ ws.onerror = () => {
+ println("error");
+ done();
+ };
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Workers/shared-worker-connect-event-is-trusted.html b/Tests/LibWeb/Text/input/Workers/shared-worker-connect-event-is-trusted.html
new file mode 100644
index 00000000000..3e7160f58cd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Workers/shared-worker-connect-event-is-trusted.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const scriptURL = await server.createEcho("GET", "/shared-worker-connect-event-is-trusted.js", {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: "onconnect = event => event.ports[0].postMessage(event.isTrusted);",
+ });
+ const pageURL = await server.createEcho("GET", "/shared-worker-connect-event-is-trusted.html", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<script>
+ const worker = new SharedWorker(${JSON.stringify(scriptURL)}, ${JSON.stringify(uniqueID())});
+ worker.port.onmessage = event => parent.postMessage(event.data, "*");
+ <\/script>`,
+ });
+
+ const iframe = document.createElement("iframe");
+ const message = new Promise(resolve => (window.onmessage = event => resolve(event.data)));
+ iframe.src = pageURL;
+ document.body.appendChild(iframe);
+ println(`isTrusted: ${await message}`);
+ iframe.remove();
+ });
+</script>