From 28400360ba930e515c010c119bc5cf2cc8c2706b Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 29 Jul 2014 08:47:52 -0700 Subject: [PATCH] Bug 1022229 - Hoist GetAppStatus into a static method on nsScriptSecurityManager. r=bz --- caps/nsPrincipal.cpp | 43 ++------------------------ caps/nsScriptSecurityManager.cpp | 53 ++++++++++++++++++++++++++++++++ caps/nsScriptSecurityManager.h | 2 ++ 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/caps/nsPrincipal.cpp b/caps/nsPrincipal.cpp index 1f1ef425110..29ace168eb4 100644 --- a/caps/nsPrincipal.cpp +++ b/caps/nsPrincipal.cpp @@ -577,48 +577,11 @@ nsPrincipal::Write(nsIObjectOutputStream* aStream) uint16_t nsPrincipal::GetAppStatus() { - NS_WARN_IF_FALSE(mAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID, - "Asking for app status on a principal with an unknown app id"); - // Installed apps have a valid app id (not NO_APP_ID or UNKNOWN_APP_ID) - // and they are not inside a mozbrowser. - if (mAppId == nsIScriptSecurityManager::NO_APP_ID || - mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID || mInMozBrowser) { + if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) { + NS_WARNING("Asking for app status on a principal with an unknown app id"); return nsIPrincipal::APP_STATUS_NOT_INSTALLED; } - - nsCOMPtr appsService = do_GetService(APPS_SERVICE_CONTRACTID); - NS_ENSURE_TRUE(appsService, nsIPrincipal::APP_STATUS_NOT_INSTALLED); - - nsCOMPtr app; - appsService->GetAppByLocalId(mAppId, getter_AddRefs(app)); - NS_ENSURE_TRUE(app, nsIPrincipal::APP_STATUS_NOT_INSTALLED); - - uint16_t status = nsIPrincipal::APP_STATUS_INSTALLED; - NS_ENSURE_SUCCESS(app->GetAppStatus(&status), - nsIPrincipal::APP_STATUS_NOT_INSTALLED); - - nsAutoCString origin; - NS_ENSURE_SUCCESS(GetOrigin(getter_Copies(origin)), - nsIPrincipal::APP_STATUS_NOT_INSTALLED); - nsString appOrigin; - NS_ENSURE_SUCCESS(app->GetOrigin(appOrigin), - nsIPrincipal::APP_STATUS_NOT_INSTALLED); - - // We go from string -> nsIURI -> origin to be sure we - // compare two punny-encoded origins. - nsCOMPtr appURI; - NS_ENSURE_SUCCESS(NS_NewURI(getter_AddRefs(appURI), appOrigin), - nsIPrincipal::APP_STATUS_NOT_INSTALLED); - - nsAutoCString appOriginPunned; - NS_ENSURE_SUCCESS(GetOriginForURI(appURI, getter_Copies(appOriginPunned)), - nsIPrincipal::APP_STATUS_NOT_INSTALLED); - - if (!appOriginPunned.Equals(origin)) { - return nsIPrincipal::APP_STATUS_NOT_INSTALLED; - } - - return status; + return nsScriptSecurityManager::AppStatusForPrincipal(this); } /************************************************************************************************************************/ diff --git a/caps/nsScriptSecurityManager.cpp b/caps/nsScriptSecurityManager.cpp index 83a3b455a97..36050e35a86 100644 --- a/caps/nsScriptSecurityManager.cpp +++ b/caps/nsScriptSecurityManager.cpp @@ -11,6 +11,7 @@ #include "js/OldDebugAPI.h" #include "xpcprivate.h" #include "XPCWrapper.h" +#include "nsIAppsService.h" #include "nsILoadContext.h" #include "nsIServiceManager.h" #include "nsIScriptObjectPrincipal.h" @@ -56,6 +57,7 @@ #include "nsIChromeRegistry.h" #include "nsIContentSecurityPolicy.h" #include "nsIAsyncVerifyRedirectCallback.h" +#include "mozIApplication.h" #include "mozilla/Preferences.h" #include "mozilla/dom/BindingUtils.h" #include @@ -252,6 +254,57 @@ nsScriptSecurityManager::SecurityHashURI(nsIURI* aURI) return NS_SecurityHashURI(aURI); } +uint16_t +nsScriptSecurityManager::AppStatusForPrincipal(nsIPrincipal *aPrin) +{ + uint32_t appId = aPrin->GetAppId(); + bool inMozBrowser = aPrin->GetIsInBrowserElement(); + NS_WARN_IF_FALSE(appId != nsIScriptSecurityManager::UNKNOWN_APP_ID, + "Asking for app status on a principal with an unknown app id"); + // Installed apps have a valid app id (not NO_APP_ID or UNKNOWN_APP_ID) + // and they are not inside a mozbrowser. + if (appId == nsIScriptSecurityManager::NO_APP_ID || + appId == nsIScriptSecurityManager::UNKNOWN_APP_ID || inMozBrowser) + { + return nsIPrincipal::APP_STATUS_NOT_INSTALLED; + } + + nsCOMPtr appsService = do_GetService(APPS_SERVICE_CONTRACTID); + NS_ENSURE_TRUE(appsService, nsIPrincipal::APP_STATUS_NOT_INSTALLED); + + nsCOMPtr app; + appsService->GetAppByLocalId(appId, getter_AddRefs(app)); + NS_ENSURE_TRUE(app, nsIPrincipal::APP_STATUS_NOT_INSTALLED); + + uint16_t status = nsIPrincipal::APP_STATUS_INSTALLED; + NS_ENSURE_SUCCESS(app->GetAppStatus(&status), + nsIPrincipal::APP_STATUS_NOT_INSTALLED); + + nsAutoCString origin; + NS_ENSURE_SUCCESS(aPrin->GetOrigin(getter_Copies(origin)), + nsIPrincipal::APP_STATUS_NOT_INSTALLED); + nsString appOrigin; + NS_ENSURE_SUCCESS(app->GetOrigin(appOrigin), + nsIPrincipal::APP_STATUS_NOT_INSTALLED); + + // We go from string -> nsIURI -> origin to be sure we + // compare two punny-encoded origins. + nsCOMPtr appURI; + NS_ENSURE_SUCCESS(NS_NewURI(getter_AddRefs(appURI), appOrigin), + nsIPrincipal::APP_STATUS_NOT_INSTALLED); + + nsAutoCString appOriginPunned; + NS_ENSURE_SUCCESS(nsPrincipal::GetOriginForURI(appURI, getter_Copies(appOriginPunned)), + nsIPrincipal::APP_STATUS_NOT_INSTALLED); + + if (!appOriginPunned.Equals(origin)) { + return nsIPrincipal::APP_STATUS_NOT_INSTALLED; + } + + return status; + +} + NS_IMETHODIMP nsScriptSecurityManager::GetChannelPrincipal(nsIChannel* aChannel, nsIPrincipal** aPrincipal) diff --git a/caps/nsScriptSecurityManager.h b/caps/nsScriptSecurityManager.h index d41703ad34f..f5189229439 100644 --- a/caps/nsScriptSecurityManager.h +++ b/caps/nsScriptSecurityManager.h @@ -68,6 +68,8 @@ public: static bool SecurityCompareURIs(nsIURI* aSourceURI, nsIURI* aTargetURI); static uint32_t SecurityHashURI(nsIURI* aURI); + static uint16_t AppStatusForPrincipal(nsIPrincipal *aPrin); + static nsresult ReportError(JSContext* cx, const nsAString& messageTag, nsIURI* aSource, nsIURI* aTarget);