Title: [293841] trunk
Revision
293841
Author
[email protected]
Date
2022-05-05 09:34:33 -0700 (Thu, 05 May 2022)

Log Message

BroadcastChannel instance in data URL dedicated worker can communicate with creator context
https://bugs.webkit.org/show_bug.cgi?id=240016

Reviewed by Youenn Fablet.

For workers, we used to dispatch to the loader document and then get the origin from that loader document.
This normally works because the worker usually inherits the origin from its loader document. However, when
the worker is loaded from a data URL, its origin is opaque:
- https://html.spec.whatwg.org/multipage/workers.html#set-up-a-worker-environment-settings-object

To address the issue, we now get the security origin directly from the BroadcastChannel's
ScriptExecutionContext, before dispatching to the main thread.

This led to one issue though because I now have to call isolatedCopy() on the SecurityOrigin to pass it to
the main thread. In particular, 2 BroadcastChannels from the same opaque origin should be able to talk to
each other. This used to work because the SecurityOrigin objects would have the same pointers and
SecurityOrigin::isSameOriginAs() would check for pointer equality. However, now that I call isolatedCopy(),
we get new & different pointers so this wouldn't work anymore. To address this, I replaced the m_isUnique
boolean data member on SecurityOrigin with a global uniqueOriginIdentifier and we check this identifier
to see if 2 opaque/unique SecurityOrigin objects are for the same opaque/unique origin. This means the
check will still work if the SecurityOrigin objects are isolated-copied or even IPC'd.

* LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin.html:
Resync to get test coverage from upstream WPT.

* Source/WebCore/dom/BroadcastChannel.cpp:
(WebCore::BroadcastChannel::MainThreadBridge::registerChannel):
* Source/WebCore/page/SecurityOrigin.cpp:
(WebCore::SecurityOrigin::isSameOriginAs const):
(WebCore::SecurityOrigin::equal const):
* Source/WebCore/page/SecurityOrigin.h:
(WebCore::SecurityOrigin::isUnique const):
(WebCore::SecurityOrigin::encode const):
(WebCore::SecurityOrigin::decode):

Canonical link: https://commits.webkit.org/250314@main

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin-expected.txt (293840 => 293841)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin-expected.txt	2022-05-05 16:29:25 UTC (rev 293840)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin-expected.txt	2022-05-05 16:34:33 UTC (rev 293841)
@@ -1,4 +1,6 @@
 
 PASS Opaque origin should be serialized to "null"
 PASS BroadcastChannel messages from opaque origins should be self-contained
+PASS BroadcastChannel messages from data URL dedicated workers should be self-contained
+PASS BroadcastChannel messages from data URL shared workers should be self-contained
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin.html (293840 => 293841)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin.html	2022-05-05 16:29:25 UTC (rev 293840)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin.html	2022-05-05 16:34:33 UTC (rev 293841)
@@ -79,6 +79,101 @@
     document.body.appendChild(iframe1);
     });
 }, "BroadcastChannel messages from opaque origins should be self-contained");
+
+const data_url_worker_src = (channel_name, worker_name) => {
+  const source = `
+const handler = (reply) => {
+  let bc2 = new BroadcastChannel("${channel_name}");
+  bc2._onmessage_ = (e) => {
+    if (e.data == "from-${worker_name}") {
+      reply("${worker_name}-done");
+    } else {
+      reply("fail");
+    }
+  };
+  let bc3 = new BroadcastChannel("${channel_name}");
+  bc3.postMessage("from-${worker_name}");
+};
+// For dedicated workers:
+self.addEventListener("message", () => handler(self.postMessage));
+// For shared workers:
+self.addEventListener("connect", (e) => {
+  var port = e.ports[0];
+  handler(msg => port.postMessage(msg));
+});
+`;
+  return "data:,".concat(encodeURIComponent(source));
+}
+
+promise_test(t => {
+  return new Promise((resolve, reject) => {
+    const channel_name = "opaque-origin-test-3";
+    const bc1 = new BroadcastChannel(channel_name);
+    bc1._onmessage_ = e => { reject("Received message from an opaque origin"); };
+
+    // Same as the previous test but with data URL dedicated workers (which
+    // should have opaque origins per the HTML spec).
+    const worker_name_prefix = "data-url-dedicated-worker";
+    const worker_1_name = `${worker_name_prefix}-1`;
+    const worker_2_name = `${worker_name_prefix}-2`;
+
+    const handler = e => {
+      if(e.data == `${worker_1_name}-done`) {
+        const worker2 = new Worker(data_url_worker_src(channel_name, worker_2_name));
+        t.add_cleanup(() => worker2.terminate());
+        worker2.addEventListener("message", handler);
+        worker2.postMessage("go!");
+      } else if(e.data == `${worker_2_name}-done`) {
+        resolve();
+      } else if(e.data == "fail") {
+        reject("One opaque origin received a message from the other");
+      } else {
+        reject("An unexpected error occurred");
+      }
+    };
+
+    let worker1 = new Worker(data_url_worker_src(channel_name, worker_1_name));
+    t.add_cleanup(() => worker1.terminate());
+    worker1.addEventListener("message", handler);
+    worker1.postMessage("go!");
+    });
+}, "BroadcastChannel messages from data URL dedicated workers should be self-contained");
+
+promise_test(() => {
+  return new Promise((resolve, reject) => {
+    const channel_name = "opaque-origin-test-4";
+    const bc1 = new BroadcastChannel(channel_name);
+    bc1._onmessage_ = e => { reject("Received message from an opaque origin"); };
+
+    // Same as the previous test but with data URL shared workers (which
+    // should have opaque origins per the HTML spec).
+    const worker_name_prefix = "data-url-shared-worker";
+    const worker_1_name = `${worker_name_prefix}-1`;
+    const worker_2_name = `${worker_name_prefix}-2`;
+
+    const handler = e => {
+      if(e.data == `${worker_1_name}-done`) {
+        const worker_script = data_url_worker_src(channel_name, worker_2_name);
+        const worker2 = new SharedWorker(worker_script, worker_2_name);
+        worker2.port.addEventListener("message", handler);
+        worker2.port.start();
+        worker2.port.postMessage("go!");
+      } else if(e.data == `${worker_2_name}-done`) {
+        resolve();
+      } else if(e.data == "fail") {
+        reject("One opaque origin received a message from the other");
+      } else {
+        reject("An unexpected error occurred");
+      }
+    };
+
+    const worker_script = data_url_worker_src(channel_name, worker_2_name);
+    const worker1 = new SharedWorker(worker_script, worker_1_name);
+    worker1.port.addEventListener("message", handler);
+    worker1.port.start();
+    worker1.port.postMessage("go!");
+    });
+}, "BroadcastChannel messages from data URL shared workers should be self-contained");
 //-->
 </script>
 </body>

Added: trunk/LayoutTests/platform/mac-wk1/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin-expected.txt (0 => 293841)


--- trunk/LayoutTests/platform/mac-wk1/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/platform/mac-wk1/imported/w3c/web-platform-tests/webmessaging/broadcastchannel/opaque-origin-expected.txt	2022-05-05 16:34:33 UTC (rev 293841)
@@ -0,0 +1,6 @@
+
+PASS Opaque origin should be serialized to "null"
+PASS BroadcastChannel messages from opaque origins should be self-contained
+PASS BroadcastChannel messages from data URL dedicated workers should be self-contained
+FAIL BroadcastChannel messages from data URL shared workers should be self-contained promise_test: Unhandled rejection with value: object "NotSupportedError: Shared workers are not supported"
+

Modified: trunk/Source/WTF/wtf/Markable.h (293840 => 293841)


--- trunk/Source/WTF/wtf/Markable.h	2022-05-05 16:29:25 UTC (rev 293840)
+++ trunk/Source/WTF/wtf/Markable.h	2022-05-05 16:34:33 UTC (rev 293841)
@@ -147,6 +147,9 @@
         return std::optional<T>(*this);
     }
 
+    template<typename Encoder> void encode(Encoder&) const;
+    template<typename Decoder> static std::optional<Markable> decode(Decoder&);
+
 private:
     T m_value;
 };
@@ -166,6 +169,36 @@
 template <typename T, typename Traits> constexpr bool operator!=(const Markable<T, Traits>& x, const T& v) { return !(x == v); }
 template <typename T, typename Traits> constexpr bool operator!=(const T& v, const Markable<T, Traits>& x) { return !(v == x); }
 
+template <typename T, typename Traits>
+template<typename Encoder>
+void Markable<T, Traits>::encode(Encoder& encoder) const
+{
+    bool isEmpty = Traits::isEmptyValue(m_value);
+    encoder << isEmpty;
+    if (!isEmpty)
+        encoder << m_value;
+}
+
+template <typename T, typename Traits>
+template<typename Decoder>
+std::optional<Markable<T, Traits>> Markable<T, Traits>::decode(Decoder& decoder)
+{
+    std::optional<bool> isEmpty;
+    decoder >> isEmpty;
+    if (!isEmpty)
+        return std::nullopt;
+
+    if (*isEmpty)
+        return Markable { };
+
+    std::optional<T> value;
+    decoder >> value;
+    if (!value)
+        return std::nullopt;
+
+    return Markable { WTFMove(*value) };
+}
+
 } // namespace WTF
 
 using WTF::Markable;

Modified: trunk/Source/WebCore/dom/BroadcastChannel.cpp (293840 => 293841)


--- trunk/Source/WebCore/dom/BroadcastChannel.cpp	2022-05-05 16:29:25 UTC (rev 293840)
+++ trunk/Source/WebCore/dom/BroadcastChannel.cpp	2022-05-05 16:34:33 UTC (rev 293841)
@@ -120,8 +120,9 @@
 
 void BroadcastChannel::MainThreadBridge::registerChannel()
 {
-    ensureOnMainThread([this, contextIdentifier = m_broadcastChannel->scriptExecutionContext()->identifier()](auto& document) {
-        m_origin = PartitionedSecurityOrigin { shouldPartitionOrigin(document) ? document.topOrigin() : document.securityOrigin(), document.securityOrigin() };
+    auto securityOrigin = m_broadcastChannel->scriptExecutionContext()->securityOrigin()->isolatedCopy();
+    ensureOnMainThread([this, contextIdentifier = m_broadcastChannel->scriptExecutionContext()->identifier(), securityOrigin = WTFMove(securityOrigin)](auto& document) {
+        m_origin = PartitionedSecurityOrigin { shouldPartitionOrigin(document) ? Ref { document.topOrigin() } : securityOrigin.copyRef(), securityOrigin.copyRef() };
         if (auto* page = document.page())
             page->broadcastChannelRegistry().registerChannel(*m_origin, m_name, m_identifier);
         channelToContextIdentifier().add(m_identifier, contextIdentifier);

Modified: trunk/Source/WebCore/page/SecurityOrigin.cpp (293840 => 293841)


--- trunk/Source/WebCore/page/SecurityOrigin.cpp	2022-05-05 16:29:25 UTC (rev 293840)
+++ trunk/Source/WebCore/page/SecurityOrigin.cpp	2022-05-05 16:34:33 UTC (rev 293841)
@@ -195,7 +195,7 @@
 SecurityOrigin::SecurityOrigin()
     : m_data { emptyString(), emptyString(), std::nullopt }
     , m_domain { emptyString() }
-    , m_isUnique { true }
+    , m_uniqueOriginIdentifier { UniqueOriginIdentifier::generateThreadSafe() }
     , m_isPotentiallyTrustworthy { false }
 {
 }
@@ -204,7 +204,7 @@
     : m_data { other->m_data.isolatedCopy() }
     , m_domain { other->m_domain.isolatedCopy() }
     , m_filePath { other->m_filePath.isolatedCopy() }
-    , m_isUnique { other->m_isUnique }
+    , m_uniqueOriginIdentifier { other->m_uniqueOriginIdentifier }
     , m_universalAccess { other->m_universalAccess }
     , m_domainWasSetInDOM { other->m_domainWasSetInDOM }
     , m_canLoadLocalResources { other->m_canLoadLocalResources }
@@ -277,7 +277,7 @@
         return true;
 
     if (isUnique() || other.isUnique())
-        return false;
+        return m_uniqueOriginIdentifier == other.m_uniqueOriginIdentifier;
 
     // Here are two cases where we should permit access:
     //
@@ -432,7 +432,7 @@
         return true;
 
     if (isUnique() || other.isUnique())
-        return false;
+        return m_uniqueOriginIdentifier == other.m_uniqueOriginIdentifier;
 
     return isSameSchemeHostPort(other);
 }
@@ -575,6 +575,9 @@
 {
     if (other == this)
         return true;
+
+    if (isUnique() || other->isUnique())
+        return m_uniqueOriginIdentifier == other->m_uniqueOriginIdentifier;
     
     if (!isSameSchemeHostPort(*other))
         return false;

Modified: trunk/Source/WebCore/page/SecurityOrigin.h (293840 => 293841)


--- trunk/Source/WebCore/page/SecurityOrigin.h	2022-05-05 16:29:25 UTC (rev 293840)
+++ trunk/Source/WebCore/page/SecurityOrigin.h	2022-05-05 16:34:33 UTC (rev 293841)
@@ -28,9 +28,11 @@
 
 #pragma once
 
+#include "ProcessQualified.h"
 #include "SecurityOriginData.h"
 #include <wtf/EnumTraits.h>
 #include <wtf/Hasher.h>
+#include <wtf/Markable.h>
 #include <wtf/ThreadSafeRefCounted.h>
 #include <wtf/text/WTFString.h>
 
@@ -155,7 +157,7 @@
     // There's a subtle difference between a unique origin and an origin that
     // has the SandboxOrigin flag set. The latter implies the former, and, in
     // addition, the SandboxOrigin flag is inherited by iframes.
-    bool isUnique() const { return m_isUnique; }
+    bool isUnique() const { return !!m_uniqueOriginIdentifier; }
 
     // Marks a file:// origin as being in a domain defined by its path.
     // FIXME 81578: The naming of this is confusing. Files with restricted access to other local files
@@ -225,10 +227,13 @@
     enum ShouldAllowFromThirdParty { AlwaysAllowFromThirdParty, MaybeAllowFromThirdParty };
     WEBCORE_EXPORT bool canAccessStorage(const SecurityOrigin*, ShouldAllowFromThirdParty = MaybeAllowFromThirdParty) const;
 
+    enum UniqueOriginIdentifierType { };
+    using UniqueOriginIdentifier = ProcessQualified<ObjectIdentifier<UniqueOriginIdentifierType>>;
+
     SecurityOriginData m_data;
     String m_domain;
     String m_filePath;
-    bool m_isUnique { false };
+    Markable<UniqueOriginIdentifier, UniqueOriginIdentifier::MarkableTraits> m_uniqueOriginIdentifier;
     bool m_universalAccess { false };
     bool m_domainWasSetInDOM { false };
     bool m_canLoadLocalResources { false };
@@ -249,7 +254,7 @@
     encoder << m_data;
     encoder << m_domain;
     encoder << m_filePath;
-    encoder << m_isUnique;
+    encoder << m_uniqueOriginIdentifier;
     encoder << m_universalAccess;
     encoder << m_domainWasSetInDOM;
     encoder << m_canLoadLocalResources;
@@ -273,7 +278,7 @@
         return nullptr;
     if (!decoder.decode(origin->m_filePath))
         return nullptr;
-    if (!decoder.decode(origin->m_isUnique))
+    if (!decoder.decode(origin->m_uniqueOriginIdentifier))
         return nullptr;
     if (!decoder.decode(origin->m_universalAccess))
         return nullptr;

Modified: trunk/Source/WebCore/platform/ProcessQualified.h (293840 => 293841)


--- trunk/Source/WebCore/platform/ProcessQualified.h	2022-05-05 16:29:25 UTC (rev 293840)
+++ trunk/Source/WebCore/platform/ProcessQualified.h	2022-05-05 16:34:33 UTC (rev 293841)
@@ -27,6 +27,7 @@
 
 #include "ProcessIdentifier.h"
 #include <wtf/Hasher.h>
+#include <wtf/Markable.h>
 #include <wtf/text/StringConcatenateNumbers.h>
 #include <wtf/text/TextStream.h>
 
@@ -98,6 +99,11 @@
     template<typename Encoder> void encode(Encoder& encoder) const { encoder << m_object << m_processIdentifier; }
     template<typename Decoder> static std::optional<ProcessQualified> decode(Decoder&);
 
+    struct MarkableTraits {
+        static bool isEmptyValue(const ProcessQualified& identifier) { return !identifier; }
+        static constexpr ProcessQualified emptyValue() { return { }; }
+    };
+
 private:
     T m_object;
     ProcessIdentifier m_processIdentifier;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to