From 85b52e428b3e53a3ec3abf38be156f266f62d0cf Mon Sep 17 00:00:00 2001 From: Tom Tung Date: Fri, 25 Aug 2017 16:36:59 +0800 Subject: [PATCH] Bug 1393439 - P1: Don't return TypeError for no-cors mode and don't check SRI for the hidden opaque body. r=bkelly MozReview-Commit-ID: 1IspF2IlqqP --HG-- extra : rebase_source : acee9a04c1be416fa8c779308062d3aebdba8b6a --- dom/fetch/FetchDriver.cpp | 41 +++++++++++++++++++++++++-------------- dom/fetch/Request.cpp | 5 ----- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/dom/fetch/FetchDriver.cpp b/dom/fetch/FetchDriver.cpp index 6bc6d5c95f4f..17aafe9535b3 100644 --- a/dom/fetch/FetchDriver.cpp +++ b/dom/fetch/FetchDriver.cpp @@ -43,6 +43,21 @@ namespace mozilla { namespace dom { +namespace { + +bool +ShouldCheckSRI(const InternalRequest* const aRequest, + const InternalResponse* const aResponse) +{ + MOZ_DIAGNOSTIC_ASSERT(aRequest); + MOZ_DIAGNOSTIC_ASSERT(aResponse); + + return !aRequest->GetIntegrity().IsEmpty() && + aResponse->Type() != ResponseType::Error; +} + +} // anonymous namespace + NS_IMPL_ISUPPORTS(FetchDriver, nsIStreamListener, nsIChannelEventSink, nsIInterfaceRequestor, nsIThreadRetargetableStreamListener) @@ -426,8 +441,7 @@ FetchDriver::BeginAndGetFilteredResponse(InternalResponse* aResponse, MOZ_ASSERT(filteredResponse); MOZ_ASSERT(mObserver); - if (filteredResponse->Type() == ResponseType::Error || - mRequest->GetIntegrity().IsEmpty()) { + if (!ShouldCheckSRI(mRequest, filteredResponse)) { mObserver->OnResponseAvailable(filteredResponse); #ifdef DEBUG mResponseAvailableCalled = true; @@ -599,10 +613,8 @@ FetchDriver::OnStartRequest(nsIRequest* aRequest, // sure the Response is fully initialized before calling this. mResponse = BeginAndGetFilteredResponse(response, foundOpaqueRedirect); - // From "Main Fetch" step 17: SRI-part1. - if (mResponse->Type() != ResponseType::Error && - !mRequest->GetIntegrity().IsEmpty() && - mSRIMetadata.IsEmpty()) { + // From "Main Fetch" step 19: SRI-part1. + if (ShouldCheckSRI(mRequest, mResponse) && mSRIMetadata.IsEmpty()) { nsIConsoleReportCollector* aReporter = nullptr; if (mObserver) { aReporter = mObserver->GetReporter(); @@ -738,9 +750,10 @@ FetchDriver::OnDataAvailable(nsIRequest* aRequest, MOZ_ASSERT(mResponse); MOZ_ASSERT(mPipeOutputStream); - // From "Main Fetch" step 17: SRI-part2. - if (mResponse->Type() != ResponseType::Error && - !mRequest->GetIntegrity().IsEmpty()) { + // From "Main Fetch" step 19: SRI-part2. + // Note: Avoid checking the hidden opaque body. + if (mResponse->Type() != ResponseType::Opaque && + ShouldCheckSRI(mRequest, mResponse)) { MOZ_ASSERT(mSRIDataVerifier); SRIVerifierAndOutputHolder holder(mSRIDataVerifier, mPipeOutputStream); @@ -777,9 +790,8 @@ FetchDriver::OnStopRequest(nsIRequest* aRequest, MOZ_ASSERT(mResponse); MOZ_ASSERT(!mResponse->IsError()); - // From "Main Fetch" step 17: SRI-part3. - if (mResponse->Type() != ResponseType::Error && - !mRequest->GetIntegrity().IsEmpty()) { + // From "Main Fetch" step 19: SRI-part3. + if (ShouldCheckSRI(mRequest, mResponse)) { MOZ_ASSERT(mSRIDataVerifier); nsCOMPtr channel = do_QueryInterface(aRequest); @@ -810,9 +822,8 @@ FetchDriver::OnStopRequest(nsIRequest* aRequest, } if (mObserver) { - if (mResponse->Type() != ResponseType::Error && - !mRequest->GetIntegrity().IsEmpty()) { - //From "Main Fetch" step 23: Process response. + // From "Main Fetch" step 19.1, 19.2: Process response. + if (ShouldCheckSRI(mRequest, mResponse)) { MOZ_ASSERT(mResponse); mObserver->OnResponseAvailable(mResponse); #ifdef DEBUG diff --git a/dom/fetch/Request.cpp b/dom/fetch/Request.cpp index cc559c28a4aa..198377e28f21 100644 --- a/dom/fetch/Request.cpp +++ b/dom/fetch/Request.cpp @@ -551,11 +551,6 @@ Request::Constructor(const GlobalObject& aGlobal, return nullptr; } - if (!request->GetIntegrity().IsEmpty()) { - aRv.ThrowTypeError(); - return nullptr; - } - requestHeaders->SetGuard(HeadersGuardEnum::Request_no_cors, aRv); if (aRv.Failed()) { return nullptr;