Bug 1321874 - Part 1: Revert the script loader changes from bug 1321868; r=bkelly

This commit is contained in:
Ehsan Akhgari 2016-12-17 11:31:13 -05:00
parent 300ee74fdb
commit f89384d1a3
2 changed files with 30 additions and 125 deletions

View File

@ -35,13 +35,13 @@
#include "nsITimedChannel.h"
#include "nsIScriptElement.h"
#include "nsIDOMHTMLScriptElement.h"
#include "nsIDocShell.h"
#include "nsContentUtils.h"
#include "nsUnicharUtils.h"
#include "nsAutoPtr.h"
#include "nsIXPConnect.h"
#include "nsError.h"
#include "nsThreadUtils.h"
#include "nsDocShell.h"
#include "nsDocShellCID.h"
#include "nsIContentSecurityPolicy.h"
#include "mozilla/Logging.h"
@ -1301,8 +1301,6 @@ nsScriptLoader::StartLoad(nsScriptLoadRequest *aRequest, const nsAString &aType,
RefPtr<nsScriptLoadHandler> handler =
new nsScriptLoadHandler(this, aRequest, sriDataVerifier.forget());
rv = handler->Init();
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIIncrementalStreamLoader> loader;
rv = NS_NewIncrementalStreamLoader(getter_AddRefs(loader), handler);
@ -2090,10 +2088,6 @@ nsScriptLoader::FillCompileOptionsForRequest(const AutoJSAPI&jsapi,
return rv;
}
if (mDocument) {
mDocument->NoteScriptTrackingStatus(aRequest->mURL, aRequest->IsTracking());
}
bool isScriptElement = !aRequest->IsModuleRequest() ||
aRequest->AsModuleRequest()->IsTopLevel();
aOptions->setIntroductionType(isScriptElement ? "scriptElement"
@ -2456,7 +2450,7 @@ nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const uint8_t* aData,
}
nsresult
nsScriptLoader::OnStreamComplete(nsIChannel* aChannel,
nsScriptLoader::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
nsISupports* aContext,
nsresult aChannelStatus,
nsresult aSRIStatus,
@ -2467,6 +2461,11 @@ nsScriptLoader::OnStreamComplete(nsIChannel* aChannel,
NS_ASSERTION(request, "null request in stream complete handler");
NS_ENSURE_TRUE(request, NS_ERROR_FAILURE);
nsCOMPtr<nsIRequest> channelRequest;
aLoader->GetRequest(getter_AddRefs(channelRequest));
nsCOMPtr<nsIChannel> channel;
channel = do_QueryInterface(channelRequest);
nsresult rv = NS_OK;
if (!request->mIntegrity.IsEmpty() &&
NS_SUCCEEDED((rv = aSRIStatus))) {
@ -2477,14 +2476,14 @@ nsScriptLoader::OnStreamComplete(nsIChannel* aChannel,
if (mDocument && mDocument->GetDocumentURI()) {
mDocument->GetDocumentURI()->GetAsciiSpec(sourceUri);
}
rv = aSRIDataVerifier->Verify(request->mIntegrity, aChannel, sourceUri,
rv = aSRIDataVerifier->Verify(request->mIntegrity, channel, sourceUri,
mReporter);
mReporter->FlushConsoleReports(mDocument);
if (NS_FAILED(rv)) {
rv = NS_ERROR_SRI_CORRUPT;
}
} else {
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
nsCOMPtr<nsILoadInfo> loadInfo = channel->GetLoadInfo();
if (loadInfo->GetEnforceSRI()) {
MOZ_LOG(SRILogHelper::GetSriLog(), mozilla::LogLevel::Debug,
@ -2503,7 +2502,7 @@ nsScriptLoader::OnStreamComplete(nsIChannel* aChannel,
}
if (NS_SUCCEEDED(rv)) {
rv = PrepareLoadedRequest(request, aChannel, aChannelStatus, aString);
rv = PrepareLoadedRequest(request, aLoader, aChannelStatus, aString);
}
if (NS_FAILED(rv)) {
@ -2616,7 +2615,7 @@ nsScriptLoader::MaybeMoveToLoadedList(nsScriptLoadRequest* aRequest)
nsresult
nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
nsIChannel* aChannel,
nsIIncrementalStreamLoader* aLoader,
nsresult aStatus,
mozilla::Vector<char16_t> &aString)
{
@ -2634,8 +2633,13 @@ nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
return NS_ERROR_NOT_AVAILABLE;
}
nsresult rv;
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
// If the load returned an error page, then we need to abort
nsCOMPtr<nsIRequest> req;
nsresult rv = aLoader->GetRequest(getter_AddRefs(req));
NS_ASSERTION(req, "StreamLoader's request went away prematurely");
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(req);
if (httpChannel) {
bool requestSucceeded;
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
@ -2651,12 +2655,13 @@ nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
}
}
nsCOMPtr<nsIChannel> channel = do_QueryInterface(req);
// If this load was subject to a CORS check; don't flag it with a
// separate origin principal, so that it will treat our document's
// principal as the origin principal
if (aRequest->mCORSMode == CORS_NONE) {
rv = nsContentUtils::GetSecurityManager()->
GetChannelResultPrincipal(aChannel, getter_AddRefs(aRequest->mOriginPrincipal));
GetChannelResultPrincipal(channel, getter_AddRefs(aRequest->mOriginPrincipal));
NS_ENSURE_SUCCESS(rv, rv);
}
@ -2686,13 +2691,13 @@ nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
// When loading a module, only responses with a JavaScript MIME type are
// acceptable.
nsAutoCString mimeType;
aChannel->GetContentType(mimeType);
channel->GetContentType(mimeType);
NS_ConvertUTF8toUTF16 typeString(mimeType);
if (!nsContentUtils::IsJavascriptMIMEType(typeString)) {
return NS_ERROR_FAILURE;
}
aChannel->GetURI(getter_AddRefs(request->mBaseURL));
channel->GetURI(getter_AddRefs(request->mBaseURL));
// Attempt to compile off main thread.
rv = AttemptAsyncScriptCompile(request);
@ -2839,53 +2844,15 @@ nsScriptLoadHandler::nsScriptLoadHandler(nsScriptLoader *aScriptLoader,
: mScriptLoader(aScriptLoader),
mRequest(aRequest),
mSRIDataVerifier(aSRIDataVerifier),
mChannelStatus(NS_OK),
mSRIStatus(NS_OK),
mClassificationStatus(NS_ERROR_NOT_INITIALIZED),
mDecoder(),
mBuffer()
{
}
nsresult
nsScriptLoadHandler::Init()
{
nsCOMPtr<nsIURIClassifier> uriClassifier =
do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID);
if (!uriClassifier) {
return NS_ERROR_FAILURE;
}
PrincipalOriginAttributes attrs;
nsIDocShell* docShell = nullptr;
if (auto doc = mScriptLoader->GetDocument()) {
docShell = doc->GetDocShell();
}
if (!docShell) {
return NS_ERROR_FAILURE;
}
attrs.InheritFromDocShellToDoc(nsDocShell::Cast(docShell)->GetOriginAttributes(), nullptr);
nsCOMPtr<nsIPrincipal> prin =
BasePrincipal::CreateCodebasePrincipal(mRequest->mURI, attrs);
NS_ENSURE_TRUE(prin, NS_ERROR_FAILURE);
bool expectCallback = false;
uriClassifier->Classify(prin, /* aTrackingProtectionEnabled = */ true,
this, &expectCallback);
if (!expectCallback) {
// If we don't expect to receive a callback, set the classification status
// eagerly.
mClassificationStatus = NS_OK;
}
return NS_OK;
}
{}
nsScriptLoadHandler::~nsScriptLoadHandler()
{}
NS_IMPL_ISUPPORTS(nsScriptLoadHandler,
nsIIncrementalStreamLoaderObserver,
nsIURIClassifierCallback)
NS_IMPL_ISUPPORTS(nsScriptLoadHandler, nsIIncrementalStreamLoaderObserver)
NS_IMETHODIMP
nsScriptLoadHandler::OnIncrementalData(nsIIncrementalStreamLoader* aLoader,
@ -3061,33 +3028,7 @@ nsScriptLoadHandler::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
}
}
nsCOMPtr<nsIRequest> request;
aLoader->GetRequest(getter_AddRefs(request));
MOZ_ASSERT(request, "How can we not have a request here?!");
mChannel = do_QueryInterface(request);
mChannelStatus = aStatus;
return MaybeInvokeOnStreamComplete();
}
NS_IMETHODIMP
nsScriptLoadHandler::OnClassifyComplete(nsresult aResult)
{
MOZ_ASSERT(mClassificationStatus == NS_ERROR_NOT_INITIALIZED);
MOZ_ASSERT(!mRequest->mIsTracking);
mClassificationStatus = aResult;
mRequest->mIsTracking = mClassificationStatus == NS_ERROR_TRACKING_URI;
return MaybeInvokeOnStreamComplete();
}
nsresult
nsScriptLoadHandler::MaybeInvokeOnStreamComplete()
{
// Run the script loader's callback if both the load and classification have
// been finished.
if (mChannel && mClassificationStatus != NS_ERROR_NOT_INITIALIZED) {
// we have to mediate and use mRequest.
return mScriptLoader->OnStreamComplete(mChannel, mRequest, mChannelStatus,
mSRIStatus, mBuffer, mSRIDataVerifier);
}
return NS_OK;
// we have to mediate and use mRequest.
return mScriptLoader->OnStreamComplete(aLoader, mRequest, aStatus, mSRIStatus,
mBuffer, mSRIDataVerifier);
}

View File

@ -21,7 +21,6 @@
#include "nsAutoPtr.h"
#include "nsIDocument.h"
#include "nsIIncrementalStreamLoader.h"
#include "nsIURIClassifier.h"
#include "nsURIHashKey.h"
#include "mozilla/CORSMode.h"
#include "mozilla/dom/SRIMetadata.h"
@ -84,7 +83,6 @@ public:
mIsXSLT(false),
mIsCanceled(false),
mWasCompiledOMT(false),
mIsTracking(false),
mOffThreadToken(nullptr),
mScriptTextBuf(nullptr),
mScriptTextLength(0),
@ -134,11 +132,6 @@ public:
return mOffThreadToken ? &mOffThreadToken : nullptr;
}
bool IsTracking() const
{
return mIsTracking;
}
enum class Progress {
Loading,
Compiling,
@ -172,7 +165,6 @@ public:
bool mIsXSLT; // True if we live in mXSLTRequests.
bool mIsCanceled; // True if we have been explicitly canceled.
bool mWasCompiledOMT; // True if the script has been compiled off main thread.
bool mIsTracking; // True if the script comes from a source on our tracking protection list.
void* mOffThreadToken; // Off-thread parsing token.
nsString mSourceMapURL; // Holds source map url for loaded scripts
char16_t* mScriptTextBuf; // Holds script text for non-inline scripts. Don't
@ -277,14 +269,6 @@ public:
mDocument = nullptr;
}
/**
* Returns the document reference.
*/
nsIDocument* GetDocument() const
{
return mDocument;
}
/**
* Add an observer for all scripts loaded through this loader.
*
@ -414,7 +398,7 @@ public:
* nsScriptLoadHandler object which observes the IncrementalStreamLoader
* loading the script.
*/
nsresult OnStreamComplete(nsIChannel* aChannel,
nsresult OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
nsISupports* aContext,
nsresult aChannelStatus,
nsresult aSRIStatus,
@ -577,7 +561,7 @@ private:
uint32_t NumberOfProcessors();
nsresult PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
nsIChannel* aChannel,
nsIIncrementalStreamLoader* aLoader,
nsresult aStatus,
mozilla::Vector<char16_t> &aString);
@ -663,22 +647,17 @@ private:
nsCOMPtr<nsIConsoleReportCollector> mReporter;
};
class nsScriptLoadHandler final : public nsIIncrementalStreamLoaderObserver,
private nsIURIClassifierCallback
class nsScriptLoadHandler final : public nsIIncrementalStreamLoaderObserver
{
public:
explicit nsScriptLoadHandler(nsScriptLoader* aScriptLoader,
nsScriptLoadRequest *aRequest,
mozilla::dom::SRICheckDataVerifier *aSRIDataVerifier);
nsresult Init();
NS_DECL_ISUPPORTS
NS_DECL_NSIINCREMENTALSTREAMLOADEROBSERVER
private:
NS_DECL_NSIURICLASSIFIERCALLBACK
virtual ~nsScriptLoadHandler();
/*
@ -696,12 +675,6 @@ private:
const uint8_t* aData, uint32_t aDataLength,
bool aEndOfStream);
/*
* Call the script loader OnClassifyComplete if both the load and the
* classification have finished.
*/
nsresult MaybeInvokeOnStreamComplete();
// ScriptLoader which will handle the parsed script.
RefPtr<nsScriptLoader> mScriptLoader;
@ -711,23 +684,14 @@ private:
// SRI data verifier.
nsAutoPtr<mozilla::dom::SRICheckDataVerifier> mSRIDataVerifier;
// Status of the channel load.
nsresult mChannelStatus;
// Status of SRI data operations.
nsresult mSRIStatus;
// status of the classification of the script URI.
nsresult mClassificationStatus;
// Unicode decoder for charset.
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
// Accumulated decoded char buffer.
mozilla::Vector<char16_t> mBuffer;
// The channel we loaded the script from.
nsCOMPtr<nsIChannel> mChannel;
};
class nsAutoScriptLoaderDisabler