Bug 1298823 - Make Request constructor throw less often, r=qdot
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 01 Mar 2017 11:44:38 +0100
changeset 374339 36730ac6b59ff964fcc0fdfdc9425aad6349272c
parent 374338 a8f45b2afbf3bc116fc9c52d4631e426fb3d5733
child 374340 7b406378bfe5fb840c633fe63262e30c8cfc60e3
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot
bugs1298823
milestone54.0a1
Bug 1298823 - Make Request constructor throw less often, r=qdot
dom/fetch/Request.cpp
dom/tests/mochitest/fetch/test_request.js
dom/workers/test/serviceworkers/fetch_event_worker.js
testing/web-platform/meta/fetch/api/policies/referrer-origin-service-worker.https.html.ini
testing/web-platform/meta/fetch/api/policies/referrer-origin-worker.html.ini
testing/web-platform/meta/fetch/api/policies/referrer-origin.html.ini
testing/web-platform/meta/fetch/api/request/request-init-001.sub.html.ini
--- a/dom/fetch/Request.cpp
+++ b/dom/fetch/Request.cpp
@@ -333,18 +333,17 @@ Request::Constructor(const GlobalObject&
 
   RequestMode mode = aInit.mMode.WasPassed() ? aInit.mMode.Value() : fallbackMode;
   RequestCredentials credentials =
     aInit.mCredentials.WasPassed() ? aInit.mCredentials.Value()
                                    : fallbackCredentials;
 
   if (mode == RequestMode::Navigate ||
       (aInit.IsAnyMemberPresent() && request->Mode() == RequestMode::Navigate)) {
-    aRv.ThrowTypeError<MSG_INVALID_REQUEST_MODE>(NS_LITERAL_STRING("navigate"));
-    return nullptr;
+    mode = RequestMode::Same_origin;
   }
 
   if (aInit.IsAnyMemberPresent()) {
     request->SetReferrer(NS_LITERAL_STRING(kFETCH_CLIENT_REFERRER_STR));
     request->SetReferrerPolicy(ReferrerPolicy::_empty);
   }
   if (aInit.mReferrer.WasPassed()) {
     const nsString& referrer = aInit.mReferrer.Value();
@@ -369,21 +368,17 @@ Request::Constructor(const GlobalObject&
         uri->GetSpec(spec);
         CopyUTF8toUTF16(spec, referrerURL);
         if (!referrerURL.EqualsLiteral(kFETCH_CLIENT_REFERRER_STR)) {
           nsCOMPtr<nsIPrincipal> principal = global->PrincipalOrNull();
           if (principal) {
             nsresult rv = principal->CheckMayLoad(uri, /* report */ false,
                                                   /* allowIfInheritsPrincipal */ false);
             if (NS_FAILED(rv)) {
-              nsAutoCString globalOrigin;
-              principal->GetOrigin(globalOrigin);
-              aRv.ThrowTypeError<MSG_CROSS_ORIGIN_REFERRER_URL>(referrer,
-                                                                NS_ConvertUTF8toUTF16(globalOrigin));
-              return nullptr;
+              referrerURL.AssignLiteral(kFETCH_CLIENT_REFERRER_STR);
             }
           }
         }
       } else {
         RefPtr<URL> url = ParseURLFromWorker(aGlobal, referrer, aRv);
         if (NS_WARN_IF(aRv.Failed())) {
           aRv.ThrowTypeError<MSG_INVALID_REFERRER_URL>(referrer);
           return nullptr;
@@ -398,21 +393,20 @@ Request::Constructor(const GlobalObject&
           nsresult rv = NS_OK;
           // ReferrerSameOriginChecker uses a sync loop to get the main thread
           // to perform the same-origin check.  Overall, on Workers this method
           // can create 3 sync loops (two for constructing URLs and one here) so
           // in the future we may want to optimize it all by off-loading all of
           // this work in a single sync loop.
           RefPtr<ReferrerSameOriginChecker> checker =
             new ReferrerSameOriginChecker(worker, referrerURL, rv);
-          checker->Dispatch(Terminating, aRv);
-          if (aRv.Failed() || NS_FAILED(rv)) {
-            aRv.ThrowTypeError<MSG_CROSS_ORIGIN_REFERRER_URL>(referrer,
-                                                              worker->GetLocationInfo().mOrigin);
-            return nullptr;
+          IgnoredErrorResult error;
+          checker->Dispatch(Terminating, error);
+          if (error.Failed() || NS_FAILED(rv)) {
+            referrerURL.AssignLiteral(kFETCH_CLIENT_REFERRER_STR);
           }
         }
       }
       request->SetReferrer(referrerURL);
     }
   }
 
   if (aInit.mReferrerPolicy.WasPassed()) {
--- a/dom/tests/mochitest/fetch/test_request.js
+++ b/dom/tests/mochitest/fetch/test_request.js
@@ -147,22 +147,19 @@ function testHeaderGuard() {
   ok(r1.headers.has("Non-Simple-Header"), "Default Request header should have guard request and allow setting non-simple header.");
 
   var r2 = new Request("", { mode: "no-cors", headers: headers });
   ok(!r2.headers.has("Cookie"), "no-cors Request header should have guard request-no-cors and prevent setting non-simple header.");
   ok(!r2.headers.has("Non-Simple-Header"), "no-cors Request header should have guard request-no-cors and prevent setting non-simple header.");
 }
 
 function testMode() {
-  try {
-    var req = new Request("http://example.com", {mode: "navigate"});
-    ok(false, "Creating a Request with navigate RequestMode should throw a TypeError");
-  } catch(e) {
-    is(e.name, "TypeError", "Creating a Request with navigate RequestMode should throw a TypeError");
-  }
+  var req = new Request("http://example.com", {mode: "navigate"});
+  ok(true, "Creating a Request with navigate RequestMode should not throw.");
+  is(req.mode, "same-origin", "Request mode becomes same-origin");
 }
 
 function testMethod() {
   // These get normalized.
   var allowed = ["delete", "get", "head", "options", "post", "put" ];
   for (var i = 0; i < allowed.length; ++i) {
     try {
       var r = new Request("", { method: allowed[i] });
--- a/dom/workers/test/serviceworkers/fetch_event_worker.js
+++ b/dom/workers/test/serviceworkers/fetch_event_worker.js
@@ -143,38 +143,31 @@ onfetch = function(ev) {
         headers : {
           "Content-Type": "text/html"
         }
       })
     ));
   }
 
   else if (ev.request.url.includes("navigate.html")) {
-    var navigateModeCorrectlyChecked = false;
     var requests = [ // should not throw
       new Request(ev.request),
       new Request(ev.request, undefined),
       new Request(ev.request, null),
       new Request(ev.request, {}),
       new Request(ev.request, {someUnrelatedProperty: 42}),
+      new Request(ev.request, {method: "GET"}),
     ];
-    try {
-      var request3 = new Request(ev.request, {method: "GET"}); // should throw
-    } catch(e) {
-      navigateModeCorrectlyChecked = requests[0].mode == "navigate";
-    }
-    if (navigateModeCorrectlyChecked) {
-      ev.respondWith(Promise.resolve(
-        new Response("<script>window.frameElement.test_result = true;</script>", {
-          headers : {
-            "Content-Type": "text/html"
-          }
-        })
-      ));
-    }
+    ev.respondWith(Promise.resolve(
+      new Response("<script>window.frameElement.test_result = true;</script>", {
+        headers : {
+          "Content-Type": "text/html"
+        }
+      })
+    ));
   }
 
   else if (ev.request.url.includes("nonexistent_worker_script.js")) {
     ev.respondWith(Promise.resolve(
       new Response("postMessage('worker-intercept-success')", {})
     ));
   }
 
deleted file mode 100644
--- a/testing/web-platform/meta/fetch/api/policies/referrer-origin-service-worker.https.html.ini
+++ /dev/null
@@ -1,6 +0,0 @@
-[referrer-origin-service-worker.https.html]
-  type: testharness
-  [Cross-origin referrer is overridden by client origin]
-    expected: FAIL
-    bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1341223
-
deleted file mode 100644
--- a/testing/web-platform/meta/fetch/api/policies/referrer-origin-worker.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[referrer-origin-worker.html]
-  type: testharness
-  [Cross-origin referrer is overridden by client origin]
-    expected: FAIL
-
deleted file mode 100644
--- a/testing/web-platform/meta/fetch/api/policies/referrer-origin.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[referrer-origin.html]
-  type: testharness
-  [Cross-origin referrer is overridden by client origin]
-    expected: FAIL
-
deleted file mode 100644
--- a/testing/web-platform/meta/fetch/api/request/request-init-001.sub.html.ini
+++ /dev/null
@@ -1,8 +0,0 @@
-[request-init-001.sub.html]
-  type: testharness
-  [Check referrer init value of http://test.url and associated getter]
-    expected: FAIL
-
-  [Check mode init value of navigate and associated getter]
-    expected: FAIL
-