Title: [224439] trunk
Revision
224439
Author
[email protected]
Date
2017-11-03 16:09:28 -0700 (Fri, 03 Nov 2017)

Log Message

Requests handled by Service Worker should not go through preflighting
https://bugs.webkit.org/show_bug.cgi?id=179250

Patch by Youenn Fablet <[email protected]> on 2017-11-03
Reviewed by Alex Christensen.

Source/WebCore:

Test: http/tests/workers/service/service-worker-crossorigin-fetch.html
In case of cross origin requests needed preflighting that may be served through SW, the following is done:
- Bypass preflight
- Put service workers mode as Only so that if SW is not handling the request, the load will fail
- If load fails, restart DocumentThreadableLoader load with preflight.

Additional testing should be added when we properly handle the case where no fetch event handler is registered in the service worker.

* loader/DocumentThreadableLoader.cpp:
(WebCore::DocumentThreadableLoader::makeCrossOriginAccessRequest):
(WebCore::DocumentThreadableLoader::didFail):
* loader/DocumentThreadableLoader.h:
* loader/ResourceLoaderOptions.h:
* loader/cache/CachedResourceRequest.cpp:
(WebCore::CachedResourceRequest::setSelectedServiceWorkerIdentifierIfNeeded):
* platform/network/ResourceErrorBase.h:

Source/WebKit:

* WebProcess/Network/WebLoaderStrategy.cpp:
(WebKit::WebLoaderStrategy::scheduleLoad):
* WebProcess/Storage/WebSWClientConnection.cpp:
(WebKit::WebSWClientConnection::startFetch):
* WebProcess/Storage/WebServiceWorkerProvider.cpp:
(WebKit::shouldHandleFetch):

LayoutTests:

* http/tests/workers/service/resources/service-worker-crossorigin-fetch-worker.js: Added.
(event.event.request.url.indexOf):
(event.event.request.url.endsWith):
* http/tests/workers/service/resources/service-worker-crossorigin-fetch.js: Added.
(done):
(async.logStatus):
(async.test):
* http/tests/workers/service/service-worker-crossorigin-fetch-expected.txt: Added.
* http/tests/workers/service/service-worker-crossorigin-fetch.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (224438 => 224439)


--- trunk/LayoutTests/ChangeLog	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/LayoutTests/ChangeLog	2017-11-03 23:09:28 UTC (rev 224439)
@@ -1,3 +1,20 @@
+2017-11-03  Youenn Fablet  <[email protected]>
+
+        Requests handled by Service Worker should not go through preflighting
+        https://bugs.webkit.org/show_bug.cgi?id=179250
+
+        Reviewed by Alex Christensen.
+
+        * http/tests/workers/service/resources/service-worker-crossorigin-fetch-worker.js: Added.
+        (event.event.request.url.indexOf):
+        (event.event.request.url.endsWith):
+        * http/tests/workers/service/resources/service-worker-crossorigin-fetch.js: Added.
+        (done):
+        (async.logStatus):
+        (async.test):
+        * http/tests/workers/service/service-worker-crossorigin-fetch-expected.txt: Added.
+        * http/tests/workers/service/service-worker-crossorigin-fetch.html: Added.
+
 2017-11-03  Alex Christensen  <[email protected]>
 
         LayoutTest http/tests/loading/basic-auth-resend-wrong-credentials.html is flaky on WK2

Added: trunk/LayoutTests/http/tests/workers/service/resources/service-worker-crossorigin-fetch-worker.js (0 => 224439)


--- trunk/LayoutTests/http/tests/workers/service/resources/service-worker-crossorigin-fetch-worker.js	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/resources/service-worker-crossorigin-fetch-worker.js	2017-11-03 23:09:28 UTC (rev 224439)
@@ -0,0 +1,15 @@
+var status = "no status";
+self.addEventListener("fetch", (event) => {
+    if (event.request.url.indexOf("status") !== -1) {
+        event.respondWith(new Response(null, {status: 200, statusText: status}));
+        return;
+    }
+    if (!event.request.url.endsWith(".fromserviceworker")) {
+        state = "unknown url";
+        event.respondWith(new Response(null, {status: 404, statusText: "Not Found"}));
+        return;
+    }
+    // Changing cors fetch into same origin fetch.
+    status = event.request.url.substring(21, event.request.url.length - 18) + " through " + "fetch";
+    event.respondWith(fetch(event.request.url.substring(21, event.request.url.length - 18)));
+});

Added: trunk/LayoutTests/http/tests/workers/service/resources/service-worker-crossorigin-fetch.js (0 => 224439)


--- trunk/LayoutTests/http/tests/workers/service/resources/service-worker-crossorigin-fetch.js	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/resources/service-worker-crossorigin-fetch.js	2017-11-03 23:09:28 UTC (rev 224439)
@@ -0,0 +1,36 @@
+function done()
+{
+    finishSWTest();
+}
+
+async function logStatus()
+{
+    var response = await fetch("status");
+    log("Status is " + response.statusText);
+}
+
+async function test()
+{
+    try {
+        // Triggering potential prefligh through custom header.
+        try {
+            var response = await fetch("http://localhost:8080/resources/square100.png.fromserviceworker", {headers: {"custom": "header"}});
+            log("Failed: fetch suceeded unexpectedly");
+        } catch(e) {
+            log("PASS: Fetch failed as expected with: " + e);
+        }        
+
+        await navigator.serviceWorker.register("resources/service-worker-crossorigin-fetch-worker.js", { });
+
+        var response = await fetch("http://localhost:8080/resources/square100.png.fromserviceworker", {headers: {"custom": "header"}});
+        var buffer =  await response.arrayBuffer();
+        log("PASS: Got response with buffer byte length being " + buffer.byteLength);
+
+        await logStatus();
+    } catch(e) {
+        log("Got exception: " + e);
+    }
+    finishSWTest();
+}
+
+test();

Added: trunk/LayoutTests/http/tests/workers/service/service-worker-crossorigin-fetch-expected.txt (0 => 224439)


--- trunk/LayoutTests/http/tests/workers/service/service-worker-crossorigin-fetch-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/service-worker-crossorigin-fetch-expected.txt	2017-11-03 23:09:28 UTC (rev 224439)
@@ -0,0 +1,5 @@
+CONSOLE MESSAGE: Fetch API cannot load http://localhost:8080/resources/square100.png.fromserviceworker. Origin http://127.0.0.1:8000 is not allowed by Access-Control-Allow-Origin.
+PASS: Fetch failed as expected with: TypeError: Origin http://127.0.0.1:8000 is not allowed by Access-Control-Allow-Origin.
+PASS: Got response with buffer byte length being 12940
+Status is /resources/square100.png through fetch
+

Added: trunk/LayoutTests/http/tests/workers/service/service-worker-crossorigin-fetch.html (0 => 224439)


--- trunk/LayoutTests/http/tests/workers/service/service-worker-crossorigin-fetch.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/service-worker-crossorigin-fetch.html	2017-11-03 23:09:28 UTC (rev 224439)
@@ -0,0 +1,8 @@
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script src=""
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (224438 => 224439)


--- trunk/Source/WebCore/ChangeLog	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebCore/ChangeLog	2017-11-03 23:09:28 UTC (rev 224439)
@@ -1,3 +1,27 @@
+2017-11-03  Youenn Fablet  <[email protected]>
+
+        Requests handled by Service Worker should not go through preflighting
+        https://bugs.webkit.org/show_bug.cgi?id=179250
+
+        Reviewed by Alex Christensen.
+
+        Test: http/tests/workers/service/service-worker-crossorigin-fetch.html
+        In case of cross origin requests needed preflighting that may be served through SW, the following is done:
+        - Bypass preflight
+        - Put service workers mode as Only so that if SW is not handling the request, the load will fail
+        - If load fails, restart DocumentThreadableLoader load with preflight.
+
+        Additional testing should be added when we properly handle the case where no fetch event handler is registered in the service worker.
+
+        * loader/DocumentThreadableLoader.cpp:
+        (WebCore::DocumentThreadableLoader::makeCrossOriginAccessRequest):
+        (WebCore::DocumentThreadableLoader::didFail):
+        * loader/DocumentThreadableLoader.h:
+        * loader/ResourceLoaderOptions.h:
+        * loader/cache/CachedResourceRequest.cpp:
+        (WebCore::CachedResourceRequest::setSelectedServiceWorkerIdentifierIfNeeded):
+        * platform/network/ResourceErrorBase.h:
+
 2017-11-03  Zalan Bujtas  <[email protected]>
 
         Remove redundant LayoutStateMaintainer argument (RenderView&)

Modified: trunk/Source/WebCore/loader/DocumentThreadableLoader.cpp (224438 => 224439)


--- trunk/Source/WebCore/loader/DocumentThreadableLoader.cpp	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebCore/loader/DocumentThreadableLoader.cpp	2017-11-03 23:09:28 UTC (rev 224439)
@@ -148,6 +148,17 @@
     if ((m_options.preflightPolicy == ConsiderPreflight && isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields())) || m_options.preflightPolicy == PreventPreflight)
         makeSimpleCrossOriginAccessRequest(WTFMove(request));
     else {
+#if ENABLE(SERVICE_WORKER)
+        if (m_options.serviceWorkersMode == ServiceWorkersMode::All && m_async) {
+            if (m_options.serviceWorkerIdentifier || document().activeServiceWorker()) {
+                ASSERT(!m_bypassingPreflightForServiceWorkerRequest);
+                m_bypassingPreflightForServiceWorkerRequest = WTFMove(request);
+                m_options.serviceWorkersMode = ServiceWorkersMode::Only;
+                loadRequest(ResourceRequest { m_bypassingPreflightForServiceWorkerRequest.value() }, SkipSecurityCheck);
+                return;
+            }
+        }
+#endif
         m_simpleRequest = false;
         if (CrossOriginPreflightResultCache::singleton().canSkipPreflight(securityOrigin().toString(), request.url(), m_options.storedCredentialsPolicy, request.httpMethod(), request.httpHeaderFields()))
             preflightSuccess(WTFMove(request));
@@ -392,6 +403,13 @@
 void DocumentThreadableLoader::didFail(unsigned long, const ResourceError& error)
 {
     ASSERT(m_client);
+#if ENABLE(SERVICE_WORKER)
+    if (m_bypassingPreflightForServiceWorkerRequest) {
+        m_options.serviceWorkersMode = ServiceWorkersMode::None;
+        makeCrossOriginAccessRequest(WTFMove(m_bypassingPreflightForServiceWorkerRequest.value()));
+        return;
+    }
+#endif
     logErrorAndFail(error);
 }
 

Modified: trunk/Source/WebCore/loader/DocumentThreadableLoader.h (224438 => 224439)


--- trunk/Source/WebCore/loader/DocumentThreadableLoader.h	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebCore/loader/DocumentThreadableLoader.h	2017-11-03 23:09:28 UTC (rev 224439)
@@ -133,6 +133,9 @@
         std::optional<HTTPHeaderMap> m_originalHeaders;
 
         ShouldLogError m_shouldLogError;
+#if ENABLE(SERVICE_WORKER)
+        std::optional<ResourceRequest> m_bypassingPreflightForServiceWorkerRequest;
+#endif
     };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/loader/ResourceLoaderOptions.h (224438 => 224439)


--- trunk/Source/WebCore/loader/ResourceLoaderOptions.h	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebCore/loader/ResourceLoaderOptions.h	2017-11-03 23:09:28 UTC (rev 224439)
@@ -96,6 +96,7 @@
 enum class ServiceWorkersMode {
     All,
     None,
+    Only // An error will happen if service worker is not handling the fetch. Used to bypass preflight safely.
 };
 
 enum class ContentEncodingSniffingPolicy {

Modified: trunk/Source/WebCore/loader/cache/CachedResourceRequest.cpp (224438 => 224439)


--- trunk/Source/WebCore/loader/cache/CachedResourceRequest.cpp	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebCore/loader/cache/CachedResourceRequest.cpp	2017-11-03 23:09:28 UTC (rev 224439)
@@ -281,7 +281,7 @@
     if (isPotentialNavigationOrSubresourceRequest(m_options.destination))
         return;
 
-    if (m_options.serviceWorkersMode != ServiceWorkersMode::All)
+    if (m_options.serviceWorkersMode == ServiceWorkersMode::None)
         return;
     if (m_options.serviceWorkerIdentifier)
         return;

Modified: trunk/Source/WebCore/platform/network/ResourceErrorBase.h (224438 => 224439)


--- trunk/Source/WebCore/platform/network/ResourceErrorBase.h	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebCore/platform/network/ResourceErrorBase.h	2017-11-03 23:09:28 UTC (rev 224439)
@@ -60,7 +60,7 @@
 
     static bool compare(const ResourceError&, const ResourceError&);
 
-    void setType(Type);
+    WEBCORE_EXPORT void setType(Type);
     Type type() const { return m_type; }
 
 protected:

Modified: trunk/Source/WebKit/ChangeLog (224438 => 224439)


--- trunk/Source/WebKit/ChangeLog	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebKit/ChangeLog	2017-11-03 23:09:28 UTC (rev 224439)
@@ -1,3 +1,17 @@
+2017-11-03  Youenn Fablet  <[email protected]>
+
+        Requests handled by Service Worker should not go through preflighting
+        https://bugs.webkit.org/show_bug.cgi?id=179250
+
+        Reviewed by Alex Christensen.
+
+        * WebProcess/Network/WebLoaderStrategy.cpp:
+        (WebKit::WebLoaderStrategy::scheduleLoad):
+        * WebProcess/Storage/WebSWClientConnection.cpp:
+        (WebKit::WebSWClientConnection::startFetch):
+        * WebProcess/Storage/WebServiceWorkerProvider.cpp:
+        (WebKit::shouldHandleFetch):
+
 2017-11-03  Chris Dumez  <[email protected]>
 
         REGRESSION(r223718): Leaking WebProcessPool after reconfiguration

Modified: trunk/Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp (224438 => 224439)


--- trunk/Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp	2017-11-03 23:09:28 UTC (rev 224439)
@@ -219,6 +219,14 @@
     WebServiceWorkerProvider::singleton().handleFetch(resourceLoader, resource, sessionID, [trackingParameters, sessionID, shouldClearReferrerOnHTTPSToHTTPRedirect, maximumBufferingTime = maximumBufferingTime(resource), resourceLoader = makeRef(resourceLoader)] (ServiceWorkerClientFetch::Result result) mutable {
         if (result != ServiceWorkerClientFetch::Result::Unhandled)
             return;
+        if (resourceLoader->options().serviceWorkersMode == ServiceWorkersMode::Only) {
+            callOnMainThread([resourceLoader = WTFMove(resourceLoader)] {
+                auto error = internalError(resourceLoader->request().url());
+                error.setType(ResourceError::Type::AccessControl);
+                resourceLoader->didFail(error);
+            });
+            return;
+        }
 
         LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be scheduled through ServiceWorker handle fetch algorithm", resourceLoader->url().string().latin1().data());
         WebProcess::singleton().webLoaderStrategy().scheduleLoadFromNetworkProcess(resourceLoader.get(), resourceLoader->originalRequest(), trackingParameters, sessionID, shouldClearReferrerOnHTTPSToHTTPRedirect, maximumBufferingTime);

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp (224438 => 224439)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2017-11-03 23:09:28 UTC (rev 224439)
@@ -114,7 +114,7 @@
 
 Ref<ServiceWorkerClientFetch> WebSWClientConnection::startFetch(WebServiceWorkerProvider& provider, Ref<WebCore::ResourceLoader>&& loader, uint64_t identifier, ServiceWorkerClientFetch::Callback&& callback)
 {
-    ASSERT(loader->options().serviceWorkersMode == ServiceWorkersMode::All);
+    ASSERT(loader->options().serviceWorkersMode != ServiceWorkersMode::None);
     // FIXME: Decide whether to assert for loader->options().serviceWorkerIdentifier once we have a story for navigation loads.
 
     send(Messages::WebSWServerConnection::StartFetch(identifier, loader->options().serviceWorkerIdentifier, loader->originalRequest(), loader->options()));

Modified: trunk/Source/WebKit/WebProcess/Storage/WebServiceWorkerProvider.cpp (224438 => 224439)


--- trunk/Source/WebKit/WebProcess/Storage/WebServiceWorkerProvider.cpp	2017-11-03 23:02:16 UTC (rev 224438)
+++ trunk/Source/WebKit/WebProcess/Storage/WebServiceWorkerProvider.cpp	2017-11-03 23:09:28 UTC (rev 224439)
@@ -61,7 +61,7 @@
 
 static inline bool shouldHandleFetch(const WebSWClientConnection& connection, CachedResource* resource, const ResourceLoaderOptions& options)
 {
-    if (options.serviceWorkersMode != ServiceWorkersMode::All)
+    if (options.serviceWorkersMode == ServiceWorkersMode::None)
         return false;
 
     if (isPotentialNavigationOrSubresourceRequest(options.destination))
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to