Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Throw when constructing a SharedWorker in a detached document
https://bugs.webkit.org/show_bug.cgi?id=258477
rdar://110231627

Reviewed by Sihui Liu and Brent Fulgham.

Throw an exception when constructing a SharedWorker in a detached iframe, similarly
to what Chrome and Firefox are already doing. Previously, we would successfully
create the SharedWorker but the topOrigin would be wrong in the case of a third-party
iframe and we would hit an assertion later on.

* LayoutTests/http/tests/workers/shared/resources/start-worker-detached-frame-iframe.html: Added.
* LayoutTests/http/tests/workers/shared/start-worker-detached-frame-expected.txt: Added.
* LayoutTests/http/tests/workers/shared/start-worker-detached-frame.html: Added.
* Source/WebCore/workers/shared/SharedWorker.cpp:
(WebCore::SharedWorker::create):

Canonical link: https://commits.webkit.org/265483@main
  • Loading branch information
cdumez committed Jun 23, 2023
1 parent 55c1230 commit 677b447
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<script>
onload = () => {
let parentWindow = window.parent;
// Detach iframe.
parentWindow.document.getElementById("testFrame").remove();
try {
new SharedWorker('../../navigation/resources/shared-worker-script.js');
parentWindow.testFailed("Starting the shared worker in a detached iframe did not throw an exception");
} catch (e) {
parentWindow.testPassed("Starting the shared worker in a detached iframe threw an exception: " + e);
}
parentWindow.finishJSTest();
};
</script>
@@ -0,0 +1,10 @@
Tests starting a shared worker in a frame that is already detached.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS Starting the shared worker in a detached iframe threw an exception: InvalidStateError: No browsing context
PASS successfullyParsed is true

TEST COMPLETE

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script>
description("Tests starting a shared worker in a frame that is already detached.");
jsTestIsAsync = true;
</script>
<iframe id="testFrame" src="resources/start-worker-detached-frame-iframe.html"></iframe>
</body>
</html>
4 changes: 4 additions & 0 deletions Source/WebCore/workers/shared/SharedWorker.cpp
Expand Up @@ -29,6 +29,7 @@
#include "ClientOrigin.h"
#include "ContentSecurityPolicy.h"
#include "Document.h"
#include "DocumentInlines.h"
#include "EventNames.h"
#include "Logging.h"
#include "MessageChannel.h"
Expand Down Expand Up @@ -71,6 +72,9 @@ ExceptionOr<Ref<SharedWorker>> SharedWorker::create(Document& document, String&&
if (!mainThreadConnection())
return Exception { NotSupportedError, "Shared workers are not supported"_s };

if (!document.hasBrowsingContext())
return Exception { InvalidStateError, "No browsing context"_s };

auto url = document.completeURL(scriptURLString);
if (!url.isValid())
return Exception { SyntaxError, "Invalid script URL"_s };
Expand Down

0 comments on commit 677b447

Please sign in to comment.