Permalink
Browse files

Bug 672026 - Ensure that there is an object principals finder during …

…early startup (r=mrbkap)
  • Loading branch information...
1 parent 5b3d54f commit c71fb6307848a998d47cf3e99f0affeda30ee215 Luke Wagner committed Jul 19, 2011
Showing with 41 additions and 2 deletions.
  1. +36 −1 caps/src/nsScriptSecurityManager.cpp
  2. +5 −1 js/src/xpconnect/shell/xpcshell.cpp
@@ -3325,6 +3325,41 @@ nsScriptSecurityManager::Observe(nsISupports* aObject, const char* aTopic,
return rv;
}
+///////////////////////////////////
+// Default ObjectPrincipalFinder //
+///////////////////////////////////
+
+// The default JSSecurityCallbacks::findObjectPrincipals is necessary since
+// scripts run (and ask for object principals) during startup before
+// nsJSRuntime::Init() has been called (which resets findObjectPrincipals).
+
+// Defined NS_EXPORT for linkage with debug-only assert in xpcshell
+NS_EXPORT JSPrincipals *
+NS_DefaultObjectPrincipalFinder(JSContext *cx, JSObject *obj)
+{
+ nsScriptSecurityManager *ssm = nsScriptSecurityManager::GetScriptSecurityManager();
+ if (!ssm) {
+ return nsnull;
+ }
+
+ nsCOMPtr<nsIPrincipal> principal;
+ nsresult rv = ssm->GetObjectPrincipal(cx, obj, getter_AddRefs(principal));
+ if (NS_FAILED(rv) || !principal) {
+ return nsnull;
+ }
+
+ JSPrincipals *jsPrincipals = nsnull;
+ principal->GetJSPrincipals(cx, &jsPrincipals);
+
+ // nsIPrincipal::GetJSPrincipals() returns a strong reference to the
+ // JS principals, but the caller of this function expects a weak
+ // reference. So we need to release here.
+
+ JSPRINCIPALS_DROP(cx, jsPrincipals);
+
+ return jsPrincipals;
+}
+
/////////////////////////////////////////////
// Constructor, Destructor, Initialization //
/////////////////////////////////////////////
@@ -3397,7 +3432,7 @@ nsresult nsScriptSecurityManager::Init()
static JSSecurityCallbacks securityCallbacks = {
CheckObjectAccess,
NULL,
- NULL,
+ NS_DefaultObjectPrincipalFinder,
ContentSecurityPolicyPermitsJSAction
};
@@ -1750,6 +1750,10 @@ FindObjectPrincipals(JSContext *cx, JSObject *obj)
return gJSPrincipals;
}
+// defined in nsScriptSecurityManager.cpp
+NS_IMPORT JSPrincipals *
+NS_DefaultObjectPrincipalFinder(JSContext *cx, JSObject *obj);
+
int
main(int argc, char **argv, char **envp)
{
@@ -1903,7 +1907,7 @@ main(int argc, char **argv, char **envp)
JSSecurityCallbacks *cb = JS_GetRuntimeSecurityCallbacks(rt);
NS_ASSERTION(cb, "We are assuming that nsScriptSecurityManager::Init() has been run");
- NS_ASSERTION(!cb->findObjectPrincipals, "Your pigeon is in my hole!");
+ NS_ASSERTION(cb->findObjectPrincipals == NS_DefaultObjectPrincipalFinder, "Your pigeon is in my hole!");
cb->findObjectPrincipals = FindObjectPrincipals;
#ifdef TEST_TranslateThis

0 comments on commit c71fb63

Please sign in to comment.