importScripts() inside a service worker should ensure that the response has a JavaScr...
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 22 Dec 2017 21:23:39 +0000 (21:23 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 22 Dec 2017 21:23:39 +0000 (21:23 +0000)
https://bugs.webkit.org/show_bug.cgi?id=181103

Reviewed by Youenn Fablet.

LayoutTests/imported/w3c:

Rebaseline WPT test now that it fails later. The test expects that the registration promise be
rejected with a SecurityError. However, we reject it with a TypeError instead, which is the
result of the Network error from importScripts().

The spec does say that importScripts() should report a network error when the MIME type is
invalid:
- https://w3c.github.io/ServiceWorker/#importscripts (step 6)

Later on, the spec says that if "an uncaught runtime script error occurs" when running the script,
we should resolve the registration with a TypeError:
- https://w3c.github.io/ServiceWorker/#update (step 9.6)

Therefore, our behavior seems correct and I cannot find in the spec a reason why the test would
expect a SecurityError here.

* web-platform-tests/service-workers/service-worker/registration-mime-types.https-expected.txt:

Source/WebCore:

importScripts() inside a service worker should ensure that the response has a JavaScript
MIME type, as per:
- https://w3c.github.io/ServiceWorker/#importscripts (step 6)

No new tests, rebaselined existing test.

* workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::importScripts):
* workers/WorkerScriptLoader.cpp:
(WebCore::WorkerScriptLoader::didReceiveResponse):
* workers/WorkerScriptLoader.h:
(WebCore::WorkerScriptLoader::responseMIMEType const):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@226275 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/imported/w3c/ChangeLog
LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/registration-mime-types.https-expected.txt
Source/WebCore/ChangeLog
Source/WebCore/workers/WorkerGlobalScope.cpp
Source/WebCore/workers/WorkerScriptLoader.cpp
Source/WebCore/workers/WorkerScriptLoader.h

index d592d21..c0c3648 100644 (file)
@@ -1,5 +1,29 @@
 2017-12-22  Chris Dumez  <cdumez@apple.com>
 
+        importScripts() inside a service worker should ensure that the response has a JavaScript MIME type
+        https://bugs.webkit.org/show_bug.cgi?id=181103
+
+        Reviewed by Youenn Fablet.
+
+        Rebaseline WPT test now that it fails later. The test expects that the registration promise be
+        rejected with a SecurityError. However, we reject it with a TypeError instead, which is the
+        result of the Network error from importScripts().
+
+        The spec does say that importScripts() should report a network error when the MIME type is
+        invalid:
+        - https://w3c.github.io/ServiceWorker/#importscripts (step 6)
+
+        Later on, the spec says that if "an uncaught runtime script error occurs" when running the script,
+        we should resolve the registration with a TypeError:
+        - https://w3c.github.io/ServiceWorker/#update (step 9.6)
+
+        Therefore, our behavior seems correct and I cannot find in the spec a reason why the test would
+        expect a SecurityError here.
+
+        * web-platform-tests/service-workers/service-worker/registration-mime-types.https-expected.txt:
+
+2017-12-22  Chris Dumez  <cdumez@apple.com>
+
         [Service Workers] Implement "Soft Update" algorithm
         https://bugs.webkit.org/show_bug.cgi?id=180702
         <rdar://problem/36163461>
index a0ce705..1d98128 100644 (file)
@@ -1,8 +1,8 @@
 
 PASS Registering script with no MIME type 
 PASS Registering script with bad MIME type 
-FAIL Registering script that imports script with no MIME type assert_unreached: Should have rejected: Registration of no MIME type imported script should fail. Reached unreachable code
-FAIL Registering script that imports script with bad MIME type assert_unreached: Should have rejected: Registration of plain text imported script should fail. Reached unreachable code
+FAIL Registering script that imports script with no MIME type assert_throws: Registration of no MIME type imported script should fail. function "function () { throw e }" threw object "TypeError: NetworkError:  A network error occurred." that is not a DOMException SecurityError: property "code" is equal to undefined, expected 18
+FAIL Registering script that imports script with bad MIME type assert_throws: Registration of plain text imported script should fail. function "function () { throw e }" threw object "TypeError: NetworkError:  A network error occurred." that is not a DOMException SecurityError: property "code" is equal to undefined, expected 18
 PASS Registering script with good MIME type application/ecmascript 
 PASS Registering script that imports script with good MIME type application/ecmascript 
 PASS Registering script with good MIME type application/javascript 
index 6daf8e7..33196da 100644 (file)
@@ -1,5 +1,25 @@
 2017-12-22  Chris Dumez  <cdumez@apple.com>
 
+        importScripts() inside a service worker should ensure that the response has a JavaScript MIME type
+        https://bugs.webkit.org/show_bug.cgi?id=181103
+
+        Reviewed by Youenn Fablet.
+
+        importScripts() inside a service worker should ensure that the response has a JavaScript
+        MIME type, as per:
+        - https://w3c.github.io/ServiceWorker/#importscripts (step 6)
+
+        No new tests, rebaselined existing test.
+
+        * workers/WorkerGlobalScope.cpp:
+        (WebCore::WorkerGlobalScope::importScripts):
+        * workers/WorkerScriptLoader.cpp:
+        (WebCore::WorkerScriptLoader::didReceiveResponse):
+        * workers/WorkerScriptLoader.h:
+        (WebCore::WorkerScriptLoader::responseMIMEType const):
+
+2017-12-22  Chris Dumez  <cdumez@apple.com>
+
         [Service Workers] Implement "Soft Update" algorithm
         https://bugs.webkit.org/show_bug.cgi?id=180702
         <rdar://problem/36163461>
index 2f94814..4486def 100644 (file)
@@ -33,6 +33,7 @@
 #include "IDBConnectionProxy.h"
 #include "ImageBitmapOptions.h"
 #include "InspectorInstrumentation.h"
+#include "MIMETypeRegistry.h"
 #include "Performance.h"
 #include "ScheduledAction.h"
 #include "ScriptSourceCode.h"
@@ -265,8 +266,10 @@ ExceptionOr<void> WorkerGlobalScope::importScripts(const Vector<String>& urls)
     FetchOptions::Cache cachePolicy = FetchOptions::Cache::Default;
 
 #if ENABLE(SERVICE_WORKER)
-    if (is<ServiceWorkerGlobalScope>(*this)) {
-        // FIXME: Fully implement https://w3c.github.io/ServiceWorker/#importscripts.
+    bool isServiceWorkerGlobalScope = is<ServiceWorkerGlobalScope>(*this);
+    if (isServiceWorkerGlobalScope) {
+        // FIXME: We need to add support for the 'imported scripts updated' flag as per:
+        // https://w3c.github.io/ServiceWorker/#importscripts
         auto& serviceWorkerGlobalScope = downcast<ServiceWorkerGlobalScope>(*this);
         auto& registration = serviceWorkerGlobalScope.registration();
         if (registration.updateViaCache() == ServiceWorkerUpdateViaCache::None || registration.needsUpdate())
@@ -287,6 +290,11 @@ ExceptionOr<void> WorkerGlobalScope::importScripts(const Vector<String>& urls)
         if (scriptLoader->failed())
             return Exception { NetworkError };
 
+#if ENABLE(SERVICE_WORKER)
+        if (isServiceWorkerGlobalScope && !MIMETypeRegistry::isSupportedJavaScriptMIMEType(scriptLoader->responseMIMEType()))
+            return Exception { NetworkError };
+#endif
+
         InspectorInstrumentation::scriptImported(*this, scriptLoader->identifier(), scriptLoader->script());
 
         NakedPtr<JSC::Exception> exception;
index 5b7bc3b..3564c06 100644 (file)
@@ -132,6 +132,7 @@ void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const Reso
     }
 
     m_responseURL = response.url();
+    m_responseMIMEType = response.mimeType();
     m_responseEncoding = response.textEncodingName();
     if (m_client)
         m_client->didReceiveResponse(identifier, response);
index d688b38..f732fc4 100644 (file)
@@ -60,6 +60,7 @@ public:
     String script();
     const URL& url() const { return m_url; }
     const URL& responseURL() const;
+    const String& responseMIMEType() const { return m_responseMIMEType; }
     bool failed() const { return m_failed; }
     unsigned long identifier() const { return m_identifier; }
     const ResourceError& error() const { return m_error; }
@@ -85,6 +86,7 @@ private:
     StringBuilder m_script;
     URL m_url;
     URL m_responseURL;
+    String m_responseMIMEType;
     unsigned long m_identifier { 0 };
     bool m_failed { false };
     bool m_finishing { false };