Bug 783260 - Push some principals on esr10. v1 a=akeybl
authorBobby Holley <bobbyholley@gmail.com>
Fri, 14 Sep 2012 12:17:49 +0200
changeset 81990 ee8351424c56b8e969db8acaf093dd7218dd458a
parent 81989 b9ee5ec9d1724f83e3382424a9eb0f5ad1f89908
child 81991 cf5c29f970194c74169c0c0ba7c0fbaf0674aa74
push id267
push userbobbyholley@gmail.com
push dateFri, 14 Sep 2012 10:19:09 +0000
reviewersakeybl
bugs783260
milestone10.0.8esrpre
Bug 783260 - Push some principals on esr10. v1 a=akeybl
dom/plugins/base/nsJSNPRuntime.cpp
--- a/dom/plugins/base/nsJSNPRuntime.cpp
+++ b/dom/plugins/base/nsJSNPRuntime.cpp
@@ -51,16 +51,18 @@
 #include "nsIDocument.h"
 #include "nsIJSRuntimeService.h"
 #include "nsIJSContextStack.h"
 #include "nsIXPConnect.h"
 #include "nsIDOMElement.h"
 #include "prmem.h"
 #include "nsIContent.h"
 #include "nsIPluginInstanceOwner.h"
+#include "nsContentUtils.h"
+#include "nsIScriptSecurityManager.h"
 
 #define NPRUNTIME_JSCLASS_NAME "NPObject JS wrapper class"
 
 using namespace mozilla::plugins::parent;
 
 #include "mozilla/plugins/PluginScriptableObjectParent.h"
 using mozilla::plugins::PluginScriptableObjectParent;
 using mozilla::plugins::ParentNPObject;
@@ -611,16 +613,37 @@ nsJSObjWrapper::NP_Invalidate(NPObject *
 static JSBool
 GetProperty(JSContext *cx, JSObject *obj, NPIdentifier id, jsval *rval)
 {
   NS_ASSERTION(NPIdentifierIsInt(id) || NPIdentifierIsString(id),
                "id must be either string or int!\n");
   return ::JS_GetPropertyById(cx, obj, NPIdentifierToJSId(id), rval);
 }
 
+class AutoPushContextPrincipal
+{
+  public:
+    AutoPushContextPrincipal(JSContext *cx) : mCx(cx)
+    {
+        mSSM = nsContentUtils::GetSecurityManager();
+        if (!mSSM)
+            return;
+        mSSM->PushContextPrincipal(cx, NULL, mSSM->GetCxSubjectPrincipal(mCx));
+    }
+    ~AutoPushContextPrincipal()
+    {
+        if (mSSM)
+            mSSM->PopContextPrincipal(mCx);
+    }
+
+  private:
+    JSContext *mCx;
+    nsIScriptSecurityManager *mSSM;
+};
+
 // static
 bool
 nsJSObjWrapper::NP_HasMethod(NPObject *npobj, NPIdentifier id)
 {
   NPP npp = NPPStack::Peek();
   JSContext *cx = GetJSContext(npp);
 
   if (!cx) {
@@ -637,16 +660,17 @@ nsJSObjWrapper::NP_HasMethod(NPObject *n
   nsJSObjWrapper *npjsobj = (nsJSObjWrapper *)npobj;
 
   AutoCXPusher pusher(cx);
   JSAutoRequest ar(cx);
   JSAutoEnterCompartment ac;
 
   if (!ac.enter(cx, npjsobj->mJSObj))
     return false;
+  AutoPushContextPrincipal prinPush(cx);
 
   AutoJSExceptionReporter reporter(cx);
 
   jsval v;
   JSBool ok = GetProperty(cx, npjsobj->mJSObj, id, &v);
 
   return ok && !JSVAL_IS_PRIMITIVE(v) &&
     ::JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(v));
@@ -676,16 +700,17 @@ doInvoke(NPObject *npobj, NPIdentifier m
   jsval fv;
 
   AutoCXPusher pusher(cx);
   JSAutoRequest ar(cx);
   JSAutoEnterCompartment ac;
 
   if (!ac.enter(cx, npjsobj->mJSObj))
     return false;
+  AutoPushContextPrincipal prinPush(cx);
 
   AutoJSExceptionReporter reporter(cx);
 
   if (method != NPIdentifier_VOID) {
     if (!GetProperty(cx, npjsobj->mJSObj, method, &fv) ||
         ::JS_TypeOfValue(cx, fv) != JSTYPE_FUNCTION) {
       return false;
     }
@@ -791,16 +816,17 @@ nsJSObjWrapper::NP_HasProperty(NPObject 
 
   AutoCXPusher pusher(cx);
   JSAutoRequest ar(cx);
   AutoJSExceptionReporter reporter(cx);
   JSAutoEnterCompartment ac;
 
   if (!ac.enter(cx, npjsobj->mJSObj))
     return false;
+  AutoPushContextPrincipal prinPush(cx);
 
   NS_ASSERTION(NPIdentifierIsInt(id) || NPIdentifierIsString(id),
                "id must be either string or int!\n");
   ok = ::JS_HasPropertyById(cx, npjsobj->mJSObj, NPIdentifierToJSId(id), &found);
   return ok && found;
 }
 
 // static
@@ -826,16 +852,17 @@ nsJSObjWrapper::NP_GetProperty(NPObject 
 
   AutoCXPusher pusher(cx);
   JSAutoRequest ar(cx);
   AutoJSExceptionReporter reporter(cx);
   JSAutoEnterCompartment ac;
 
   if (!ac.enter(cx, npjsobj->mJSObj))
     return false;
+  AutoPushContextPrincipal prinPush(cx);
 
   jsval v;
   return (GetProperty(cx, npjsobj->mJSObj, id, &v) &&
           JSValToNPVariant(npp, cx, v, result));
 }
 
 // static
 bool
@@ -861,16 +888,17 @@ nsJSObjWrapper::NP_SetProperty(NPObject 
 
   AutoCXPusher pusher(cx);
   JSAutoRequest ar(cx);
   AutoJSExceptionReporter reporter(cx);
   JSAutoEnterCompartment ac;
 
   if (!ac.enter(cx, npjsobj->mJSObj))
     return false;
+  AutoPushContextPrincipal prinPush(cx);
 
   jsval v = NPVariantToJSVal(npp, cx, value);
   js::AutoValueRooter tvr(cx, v);
 
   NS_ASSERTION(NPIdentifierIsInt(id) || NPIdentifierIsString(id),
                "id must be either string or int!\n");
   ok = ::JS_SetPropertyById(cx, npjsobj->mJSObj, NPIdentifierToJSId(id), &v);
 
@@ -903,16 +931,17 @@ nsJSObjWrapper::NP_RemoveProperty(NPObje
   AutoCXPusher pusher(cx);
   JSAutoRequest ar(cx);
   AutoJSExceptionReporter reporter(cx);
   jsval deleted = JSVAL_FALSE;
   JSAutoEnterCompartment ac;
 
   if (!ac.enter(cx, npjsobj->mJSObj))
     return false;
+  AutoPushContextPrincipal prinPush(cx);
 
   NS_ASSERTION(NPIdentifierIsInt(id) || NPIdentifierIsString(id),
                "id must be either string or int!\n");
   ok = ::JS_DeletePropertyById2(cx, npjsobj->mJSObj, NPIdentifierToJSId(id), &deleted);
   if (ok && deleted == JSVAL_TRUE) {
     // FIXME: See bug 425823, we shouldn't need to do this, and once
     // that bug is fixed we can remove this code.
 
@@ -958,16 +987,17 @@ nsJSObjWrapper::NP_Enumerate(NPObject *n
 
   AutoCXPusher pusher(cx);
   JSAutoRequest ar(cx);
   AutoJSExceptionReporter reporter(cx);
   JSAutoEnterCompartment ac;
 
   if (!ac.enter(cx, npjsobj->mJSObj))
     return false;
+  AutoPushContextPrincipal prinPush(cx);
 
   JSIdArray *ida = ::JS_Enumerate(cx, npjsobj->mJSObj);
   if (!ida) {
     return false;
   }
 
   *count = ida->length;
   *idarray = (NPIdentifier *)PR_Malloc(*count * sizeof(NPIdentifier));