From 585b68134970f6b71e7b7559cb869d5a4cba99e1 Mon Sep 17 00:00:00 2001 From: "jonas@sicking.cc" Date: Tue, 18 Mar 2008 17:27:56 -0700 Subject: [PATCH] Bug 413161: Make nsIPrincipal::Origin ignore changes to document.domain. r/sr=dveditz --- caps/idl/nsIPrincipal.idl | 7 ++-- caps/include/nsPrincipal.h | 1 - caps/src/nsPrincipal.cpp | 32 +++++-------------- caps/src/nsScriptSecurityManager.cpp | 48 ++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/caps/idl/nsIPrincipal.idl b/caps/idl/nsIPrincipal.idl index 9128230298d..127f8949ca5 100644 --- a/caps/idl/nsIPrincipal.idl +++ b/caps/idl/nsIPrincipal.idl @@ -51,7 +51,7 @@ interface nsIURI; [ptr] native JSContext(JSContext); [ptr] native JSPrincipals(JSPrincipals); -[scriptable, uuid(7292475e-2821-4602-9d00-228476696428)] +[scriptable, uuid(b8268b9a-2403-44ed-81e3-614075c92034)] interface nsIPrincipal : nsISerializable { /** @@ -142,9 +142,8 @@ interface nsIPrincipal : nsISerializable [noscript] attribute nsIURI domain; /** - * The origin of this principal's domain, if non-null, or its - * codebase URI otherwise. An origin is defined as: - * scheme + host + port. + * The origin of this principal's codebase URI. + * An origin is defined as: scheme + host + port. */ // XXXcaa this should probably be turned into an nsIURI. // The system principal's origin should be some caps namespace diff --git a/caps/include/nsPrincipal.h b/caps/include/nsPrincipal.h index a4e5ad41d4b..1a933163efd 100755 --- a/caps/include/nsPrincipal.h +++ b/caps/include/nsPrincipal.h @@ -140,7 +140,6 @@ protected: nsCOMPtr mCodebase; nsCOMPtr mDomain; - nsCOMPtr mOrigin; PRPackedBool mTrusted; PRPackedBool mInitialized; // If mCodebaseImmutable is true, mCodebase is non-null and immutable diff --git a/caps/src/nsPrincipal.cpp b/caps/src/nsPrincipal.cpp index 91aebbce410..043becdd3f3 100755 --- a/caps/src/nsPrincipal.cpp +++ b/caps/src/nsPrincipal.cpp @@ -128,9 +128,6 @@ nsPrincipal::Init(const nsACString& aCertFingerprint, mCodebase = NS_TryToMakeImmutable(aCodebase); mCodebaseImmutable = URIIsImmutable(mCodebase); - // Invalidate our cached origin - mOrigin = nsnull; - nsresult rv; if (!aCertFingerprint.IsEmpty()) { rv = SetCertificate(aCertFingerprint, aSubjectName, aPrettyName, aCert); @@ -172,14 +169,12 @@ nsPrincipal::GetOrigin(char **aOrigin) { *aOrigin = nsnull; - if (!mOrigin) { - nsIURI* uri = mDomain ? mDomain : mCodebase; - if (uri) { - mOrigin = NS_GetInnermostURI(uri); - } + nsCOMPtr origin; + if (mCodebase) { + origin = NS_GetInnermostURI(mCodebase); } - if (!mOrigin) { + if (!origin) { NS_ASSERTION(mCert, "No Domain or Codebase for a non-cert principal"); return NS_ERROR_FAILURE; } @@ -191,14 +186,14 @@ nsPrincipal::GetOrigin(char **aOrigin) // XXX this should be removed in favor of the solution in // bug 160042. PRBool isChrome; - nsresult rv = mOrigin->SchemeIs("chrome", &isChrome); + nsresult rv = origin->SchemeIs("chrome", &isChrome); if (NS_SUCCEEDED(rv) && !isChrome) { - rv = mOrigin->GetHostPort(hostPort); + rv = origin->GetHostPort(hostPort); } if (NS_SUCCEEDED(rv) && !isChrome) { nsCAutoString scheme; - rv = mOrigin->GetScheme(scheme); + rv = origin->GetScheme(scheme); NS_ENSURE_SUCCESS(rv, rv); *aOrigin = ToNewCString(scheme + NS_LITERAL_CSTRING("://") + hostPort); } @@ -206,7 +201,7 @@ nsPrincipal::GetOrigin(char **aOrigin) // Some URIs (e.g., nsSimpleURI) don't support host. Just // get the full spec. nsCAutoString spec; - rv = mOrigin->GetSpec(spec); + rv = origin->GetSpec(spec); NS_ENSURE_SUCCESS(rv, rv); *aOrigin = ToNewCString(spec); } @@ -565,9 +560,6 @@ nsPrincipal::SetURI(nsIURI* aURI) { mCodebase = NS_TryToMakeImmutable(aURI); mCodebaseImmutable = URIIsImmutable(mCodebase); - - // Invalidate our cached origin - mOrigin = nsnull; } @@ -676,9 +668,6 @@ nsPrincipal::SetDomain(nsIURI* aDomain) // Domain has changed, forget cached security policy SetSecurityPolicy(nsnull); - // Invalidate our cached origin - mOrigin = nsnull; - return NS_OK; } @@ -720,9 +709,6 @@ nsPrincipal::InitFromPersistent(const char* aPrefName, mCodebaseImmutable = URIIsImmutable(mCodebase); mTrusted = aTrusted; - - // Invalidate our cached origin - mOrigin = nsnull; } rv = mJSPrincipals.Init(this, aToken); @@ -1094,8 +1080,6 @@ nsPrincipal::Write(nsIObjectOutputStream* aStream) return rv; } - // mOrigin is an optimization; don't bother serializing it. - rv = aStream->Write8(mTrusted); if (NS_FAILED(rv)) { return rv; diff --git a/caps/src/nsScriptSecurityManager.cpp b/caps/src/nsScriptSecurityManager.cpp index a9b9d35bf00..78446e66e83 100644 --- a/caps/src/nsScriptSecurityManager.cpp +++ b/caps/src/nsScriptSecurityManager.cpp @@ -1068,6 +1068,40 @@ nsScriptSecurityManager::CheckSameOriginDOMProp(nsIPrincipal* aSubject, return NS_ERROR_DOM_PROP_ACCESS_DENIED; } +static +nsresult +GetPrincipalDomainOrigin(nsIPrincipal* aPrincipal, + nsACString& aOrigin) +{ + aOrigin.Truncate(); + + nsCOMPtr uri; + aPrincipal->GetDomain(getter_AddRefs(uri)); + if (!uri) { + aPrincipal->GetURI(getter_AddRefs(uri)); + } + + NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED); + + nsCAutoString hostPort; + + nsresult rv = uri->GetHostPort(hostPort); + if (NS_SUCCEEDED(rv)) { + nsCAutoString scheme; + rv = uri->GetScheme(scheme); + NS_ENSURE_SUCCESS(rv, rv); + aOrigin = scheme + NS_LITERAL_CSTRING("://") + hostPort; + } + else { + // Some URIs (e.g., nsSimpleURI) don't support host. Just + // get the full spec. + rv = uri->GetSpec(aOrigin); + NS_ENSURE_SUCCESS(rv, rv); + } + + return NS_OK; +} + nsresult nsScriptSecurityManager::LookupPolicy(nsIPrincipal* aPrincipal, ClassInfoData& aClassData, @@ -1099,9 +1133,9 @@ nsScriptSecurityManager::LookupPolicy(nsIPrincipal* aPrincipal, printf("DomainLookup "); #endif - nsXPIDLCString origin; - if (NS_FAILED(rv = aPrincipal->GetOrigin(getter_Copies(origin)))) - return rv; + nsCAutoString origin; + rv = GetPrincipalDomainOrigin(aPrincipal, origin); + NS_ENSURE_SUCCESS(rv, rv); char *start = origin.BeginWriting(); const char *nextToLastDot = nsnull; @@ -2670,13 +2704,13 @@ nsScriptSecurityManager::CheckConfirmDialog(JSContext* cx, nsIPrincipal* aPrinci if (NS_FAILED(rv)) return PR_FALSE; - nsXPIDLCString val; + nsCAutoString val; PRBool hasCert; aPrincipal->GetHasCertificate(&hasCert); if (hasCert) rv = aPrincipal->GetPrettyName(val); else - rv = aPrincipal->GetOrigin(getter_Copies(val)); + rv = GetPrincipalDomainOrigin(aPrincipal, val); if (NS_FAILED(rv)) return PR_FALSE; @@ -2791,14 +2825,14 @@ nsScriptSecurityManager::EnableCapability(const char *capability) if (canEnable != nsIPrincipal::ENABLE_GRANTED) { - nsXPIDLCString val; + nsCAutoString val; PRBool hasCert; nsresult rv; principal->GetHasCertificate(&hasCert); if (hasCert) rv = principal->GetPrettyName(val); else - rv = principal->GetOrigin(getter_Copies(val)); + rv = GetPrincipalDomainOrigin(principal, val); if (NS_FAILED(rv)) return rv;