Bug 1011024 - Fix nsScriptSecurityManager::GetChannelPrincipal so that it doesn't fail to get the correct nsIPrincipal for some resource documents. r=bz

This commit is contained in:
Jonathan Watt 2014-05-16 23:59:36 +01:00
parent 2111418e01
commit 3cf8ef7d61
3 changed files with 40 additions and 12 deletions

View File

@ -10,8 +10,9 @@ interface nsIURI;
interface nsIChannel;
interface nsIDocShell;
interface nsIDomainPolicy;
interface nsILoadContext;
[scriptable, uuid(2565769a-eaec-47a1-a076-605f5294d286)]
[scriptable, uuid(9875f4b2-f9cd-41d1-a461-fe14956823ac)]
interface nsIScriptSecurityManager : nsIXPCSecurityManager
{
/**
@ -122,6 +123,14 @@ interface nsIScriptSecurityManager : nsIXPCSecurityManager
in unsigned long appId,
in boolean inMozBrowser);
/**
* Returns a principal that has the appId and inMozBrowser of the load
* context.
* @param loadContext to get appId/inMozBrowser from.
*/
nsIPrincipal getLoadContextCodebasePrincipal(in nsIURI uri,
in nsILoadContext loadContext);
/**
* Returns a principal that has the appId and inMozBrowser of the docshell
* inside a mozbrowser frame.

View File

@ -11,6 +11,7 @@
#include "js/OldDebugAPI.h"
#include "xpcprivate.h"
#include "XPCWrapper.h"
#include "nsILoadContext.h"
#include "nsIServiceManager.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIScriptContext.h"
@ -295,11 +296,12 @@ nsScriptSecurityManager::GetChannelPrincipal(nsIChannel* aChannel,
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocShell> docShell;
NS_QueryNotificationCallbacks(aChannel, docShell);
if (docShell) {
return GetDocShellCodebasePrincipal(uri, docShell, aPrincipal);
nsCOMPtr<nsILoadContext> loadContext;
NS_QueryNotificationCallbacks(aChannel, loadContext);
if (loadContext) {
return GetLoadContextCodebasePrincipal(uri, loadContext, aPrincipal);
}
return GetCodebasePrincipalInternal(uri, UNKNOWN_APP_ID,
@ -969,6 +971,22 @@ nsScriptSecurityManager::GetAppCodebasePrincipal(nsIURI* aURI,
return GetCodebasePrincipalInternal(aURI, aAppId, aInMozBrowser, aPrincipal);
}
NS_IMETHODIMP
nsScriptSecurityManager::
GetLoadContextCodebasePrincipal(nsIURI* aURI,
nsILoadContext* aLoadContext,
nsIPrincipal** aPrincipal)
{
uint32_t appId;
aLoadContext->GetAppId(&appId);
bool isInBrowserElement;
aLoadContext->GetIsInBrowserElement(&isInBrowserElement);
return GetCodebasePrincipalInternal(aURI,
appId,
isInBrowserElement,
aPrincipal);
}
NS_IMETHODIMP
nsScriptSecurityManager::GetDocShellCodebasePrincipal(nsIURI* aURI,
nsIDocShell* aDocShell,

View File

@ -28,6 +28,7 @@
#include "mozilla/Telemetry.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsILoadContext.h"
#include "nsUnicharUtils.h"
#include "nsContentList.h"
#include "nsIObserver.h"
@ -2337,21 +2338,21 @@ nsDocument::ResetToURI(nsIURI *aURI, nsILoadGroup *aLoadGroup,
nsIScriptSecurityManager *securityManager =
nsContentUtils::GetSecurityManager();
if (securityManager) {
nsCOMPtr<nsIDocShell> docShell(mDocumentContainer);
nsCOMPtr<nsILoadContext> loadContext(mDocumentContainer);
if (!docShell && aLoadGroup) {
if (!loadContext && aLoadGroup) {
nsCOMPtr<nsIInterfaceRequestor> cbs;
aLoadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
docShell = do_GetInterface(cbs);
loadContext = do_GetInterface(cbs);
}
MOZ_ASSERT(docShell,
"must be in a docshell or pass in an explicit principal");
MOZ_ASSERT(loadContext,
"must have a load context or pass in an explicit principal");
nsCOMPtr<nsIPrincipal> principal;
nsresult rv = securityManager->
GetDocShellCodebasePrincipal(mDocumentURI, docShell,
getter_AddRefs(principal));
GetLoadContextCodebasePrincipal(mDocumentURI, loadContext,
getter_AddRefs(principal));
if (NS_SUCCEEDED(rv)) {
SetPrincipal(principal);
}