Bug 1342694 - Get correct principal when using OffscreenCanvas. r=jgilbert draft
authorMorris Tseng <mtseng@mozilla.com>
Wed, 01 Mar 2017 14:34:00 +0800
changeset 490674 0c1e7e989c1eaeffea3b890103be095402241db3
parent 490433 1bc2ad020aee2830e0a7941f10958dbec108c254
child 547352 a2f41f9235f92859db8b1fab716a60e50146008d
push id47200
push userbmo:mtseng@mozilla.com
push dateWed, 01 Mar 2017 06:36:54 +0000
reviewersjgilbert
bugs1342694
milestone54.0a1
Bug 1342694 - Get correct principal when using OffscreenCanvas. r=jgilbert MozReview-Commit-ID: 87v7snX3oF1
dom/canvas/WebGLTextureUpload.cpp
--- a/dom/canvas/WebGLTextureUpload.cpp
+++ b/dom/canvas/WebGLTextureUpload.cpp
@@ -349,17 +349,30 @@ WebGLContext::FromDomElem(const char* fu
     //////
 
     // While it's counter-intuitive, the shape of the SFEResult API means that we should
     // try to pull out a surface first, and then, if we do pull out a surface, check
     // CORS/write-only/etc..
 
     if (!sfer.mCORSUsed) {
         auto& srcPrincipal = sfer.mPrincipal;
-        nsIPrincipal* dstPrincipal = GetCanvas()->NodePrincipal();
+        nsIPrincipal* dstPrincipal;
+        if (mOffscreenCanvas) {
+          MOZ_ASSERT(NS_IsMainThread());
+          nsCOMPtr<nsIGlobalObject> global = mOffscreenCanvas->GetParentObject();
+          nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global);
+          if (!window || !window->GetDoc()) {
+            out_error->Throw(NS_ERROR_FAILURE);
+            return nullptr;
+          }
+
+          dstPrincipal = window->GetDoc()->NodePrincipal();
+        } else {
+          dstPrincipal = GetCanvas()->NodePrincipal();
+        }
 
         if (!dstPrincipal->Subsumes(srcPrincipal)) {
             GenerateWarning("%s: Cross-origin elements require CORS.", funcName);
             out_error->Throw(NS_ERROR_DOM_SECURITY_ERR);
             return nullptr;
         }
     }