Bug 1160890 - Part 2: ImportScripts() should return muted errors with 3rd party scripts. r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Tue, 14 Jul 2015 16:08:38 +0100
changeset 252903 37c0bb092ec2b3812f095d02755d708638861906
parent 252902 22e8c544040543b89df9c046079553f861faf846
child 252904 5daadd05f7127fb47c31db5d3ebde1643cd8ffc8
push id29048
push userryanvm@gmail.com
push dateWed, 15 Jul 2015 03:36:39 +0000
treeherdermozilla-central@892594bdad30 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1160890
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1160890 - Part 2: ImportScripts() should return muted errors with 3rd party scripts. r=smaug
dom/workers/ScriptLoader.cpp
--- a/dom/workers/ScriptLoader.cpp
+++ b/dom/workers/ScriptLoader.cpp
@@ -270,16 +270,18 @@ struct ScriptLoadInfo
     ToBeCached,
 
     // Something went wrong or the worker went away.
     Cancel
   };
 
   CacheStatus mCacheStatus;
 
+  Maybe<bool> mMutedErrorFlag;
+
   bool Finished() const
   {
     return mLoadingFinished && !mCachePromise && !mChannel;
   }
 };
 
 class ScriptLoaderRunnable;
 
@@ -942,22 +944,43 @@ private:
     aLoadInfo.mChannel = nullptr;
 
     if (NS_FAILED(aStatus)) {
       return aStatus;
     }
 
     NS_ASSERTION(aString, "This should never be null!");
 
-    // Make sure we're not seeing the result of a 404 or something by checking
-    // the 'requestSucceeded' attribute on the http channel.
     nsCOMPtr<nsIRequest> request;
     nsresult rv = aLoader->GetRequest(getter_AddRefs(request));
     NS_ENSURE_SUCCESS(rv, rv);
 
+    nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
+    MOZ_ASSERT(channel);
+
+    nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
+    NS_ASSERTION(ssm, "Should never be null!");
+
+    nsCOMPtr<nsIPrincipal> channelPrincipal;
+    rv = ssm->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal));
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+
+    nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
+    if (!principal) {
+      WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
+      MOZ_ASSERT(parentWorker, "Must have a parent!");
+      principal = parentWorker->GetPrincipal();
+    }
+
+    aLoadInfo.mMutedErrorFlag.emplace(principal->Subsumes(channelPrincipal));
+
+    // Make sure we're not seeing the result of a 404 or something by checking
+    // the 'requestSucceeded' attribute on the http channel.
     nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
     if (httpChannel) {
       bool requestSucceeded;
       rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
       NS_ENSURE_SUCCESS(rv, rv);
 
       if (!requestSucceeded) {
         return NS_ERROR_NOT_AVAILABLE;
@@ -983,19 +1006,16 @@ private:
       nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
                                       NS_LITERAL_CSTRING("DOM"), parentDoc,
                                       nsContentUtils::eDOM_PROPERTIES,
                                       "EmptyWorkerSourceWarning");
     } else if (!aLoadInfo.mScriptTextBuf) {
       return NS_ERROR_FAILURE;
     }
 
-    nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
-    NS_ASSERTION(channel, "This should never fail!");
-
     // Figure out what we actually loaded.
     nsCOMPtr<nsIURI> finalURI;
     rv = NS_GetFinalChannelURI(channel, getter_AddRefs(finalURI));
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCString filename;
     rv = finalURI->GetSpec(filename);
     NS_ENSURE_SUCCESS(rv, rv);
@@ -1091,16 +1111,28 @@ private:
                         const mozilla::dom::ChannelInfo& aChannelInfo,
                         UniquePtr<PrincipalInfo> aPrincipalInfo)
   {
     AssertIsOnMainThread();
     MOZ_ASSERT(aIndex < mLoadInfos.Length());
     ScriptLoadInfo& loadInfo = mLoadInfos[aIndex];
     MOZ_ASSERT(loadInfo.mCacheStatus == ScriptLoadInfo::Cached);
 
+    nsCOMPtr<nsIPrincipal> responsePrincipal =
+      PrincipalInfoToPrincipal(*aPrincipalInfo);
+
+    nsIPrincipal* principal = mWorkerPrivate->GetPrincipal();
+    if (!principal) {
+      WorkerPrivate* parentWorker = mWorkerPrivate->GetParent();
+      MOZ_ASSERT(parentWorker, "Must have a parent!");
+      principal = parentWorker->GetPrincipal();
+    }
+
+    loadInfo.mMutedErrorFlag.emplace(principal->Subsumes(responsePrincipal));
+
     // May be null.
     nsIDocument* parentDoc = mWorkerPrivate->GetDocument();
 
     MOZ_ASSERT(!loadInfo.mScriptTextBuf);
 
     nsresult rv =
       nsScriptLoader::ConvertToUTF16(nullptr, aString, aStringLen,
                                      NS_LITERAL_STRING("UTF-8"), parentDoc,
@@ -1113,18 +1145,16 @@ private:
         mWorkerPrivate->SetBaseURI(finalURI);
       }
 
       mozilla::DebugOnly<nsIPrincipal*> principal = mWorkerPrivate->GetPrincipal();
       MOZ_ASSERT(principal);
       nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
       MOZ_ASSERT(loadGroup);
 
-      nsCOMPtr<nsIPrincipal> responsePrincipal =
-        PrincipalInfoToPrincipal(*aPrincipalInfo);
       mozilla::DebugOnly<bool> equal = false;
       MOZ_ASSERT(responsePrincipal && NS_SUCCEEDED(responsePrincipal->Equals(principal, &equal)));
       MOZ_ASSERT(equal);
 
       mWorkerPrivate->InitChannelInfo(aChannelInfo);
       mWorkerPrivate->SetPrincipal(responsePrincipal, loadGroup);
     }
 
@@ -1707,16 +1737,19 @@ ScriptExecutorRunnable::WorkerRun(JSCont
     JS::CompileOptions options(aCx);
     options.setFileAndLine(filename.get(), 1)
            .setNoScriptRval(true);
 
     if (mScriptLoader.mWorkerScriptType == DebuggerScript) {
       options.setVersion(JSVERSION_LATEST);
     }
 
+    MOZ_ASSERT(loadInfo.mMutedErrorFlag.isSome());
+    options.setMutedErrors(loadInfo.mMutedErrorFlag.valueOr(true));
+
     JS::SourceBufferHolder srcBuf(loadInfo.mScriptTextBuf,
                                   loadInfo.mScriptTextLength,
                                   JS::SourceBufferHolder::GiveOwnership);
     loadInfo.mScriptTextBuf = nullptr;
     loadInfo.mScriptTextLength = 0;
 
     JS::Rooted<JS::Value> unused(aCx);
     if (!JS::Evaluate(aCx, options, srcBuf, &unused)) {