michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * This is the principal that has no rights and can't be accessed by michael@0: * anything other than itself and chrome; null principals are not michael@0: * same-origin with anything but themselves. michael@0: */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsNullPrincipal.h" michael@0: #include "nsNullPrincipalURI.h" michael@0: #include "nsMemory.h" michael@0: #include "nsIUUIDGenerator.h" michael@0: #include "nsID.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIClassInfoImpl.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsError.h" michael@0: #include "nsIScriptSecurityManager.h" michael@0: #include "nsPrincipal.h" michael@0: #include "nsScriptSecurityManager.h" michael@0: #include "pratom.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: NS_IMPL_CLASSINFO(nsNullPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY, michael@0: NS_NULLPRINCIPAL_CID) michael@0: NS_IMPL_QUERY_INTERFACE_CI(nsNullPrincipal, michael@0: nsIPrincipal, michael@0: nsISerializable) michael@0: NS_IMPL_CI_INTERFACE_GETTER(nsNullPrincipal, michael@0: nsIPrincipal, michael@0: nsISerializable) michael@0: michael@0: NS_IMETHODIMP_(MozExternalRefCountType) michael@0: nsNullPrincipal::AddRef() michael@0: { michael@0: NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt"); michael@0: nsrefcnt count = ++refcount; michael@0: NS_LOG_ADDREF(this, count, "nsNullPrincipal", sizeof(*this)); michael@0: return count; michael@0: } michael@0: michael@0: NS_IMETHODIMP_(MozExternalRefCountType) michael@0: nsNullPrincipal::Release() michael@0: { michael@0: NS_PRECONDITION(0 != refcount, "dup release"); michael@0: nsrefcnt count = --refcount; michael@0: NS_LOG_RELEASE(this, count, "nsNullPrincipal"); michael@0: if (count == 0) { michael@0: delete this; michael@0: } michael@0: michael@0: return count; michael@0: } michael@0: michael@0: nsNullPrincipal::nsNullPrincipal() michael@0: { michael@0: } michael@0: michael@0: nsNullPrincipal::~nsNullPrincipal() michael@0: { michael@0: } michael@0: michael@0: #define NS_NULLPRINCIPAL_PREFIX NS_NULLPRINCIPAL_SCHEME ":" michael@0: michael@0: nsresult michael@0: nsNullPrincipal::Init() michael@0: { michael@0: // FIXME: bug 327161 -- make sure the uuid generator is reseeding-resistant. michael@0: nsresult rv; michael@0: nsCOMPtr uuidgen = michael@0: do_GetService("@mozilla.org/uuid-generator;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsID id; michael@0: rv = uuidgen->GenerateUUIDInPlace(&id); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: char chars[NSID_LENGTH]; michael@0: id.ToProvidedString(chars); michael@0: michael@0: uint32_t suffixLen = NSID_LENGTH - 1; michael@0: uint32_t prefixLen = ArrayLength(NS_NULLPRINCIPAL_PREFIX) - 1; michael@0: michael@0: // Use an nsCString so we only do the allocation once here and then share michael@0: // with nsJSPrincipals michael@0: nsCString str; michael@0: str.SetCapacity(prefixLen + suffixLen); michael@0: michael@0: str.Append(NS_NULLPRINCIPAL_PREFIX); michael@0: str.Append(chars); michael@0: michael@0: if (str.Length() != prefixLen + suffixLen) { michael@0: NS_WARNING("Out of memory allocating null-principal URI"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: mURI = new nsNullPrincipalURI(str); michael@0: NS_ENSURE_TRUE(mURI, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsNullPrincipal::GetScriptLocation(nsACString &aStr) michael@0: { michael@0: mURI->GetSpec(aStr); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void nsNullPrincipal::dumpImpl() michael@0: { michael@0: nsAutoCString str; michael@0: mURI->GetSpec(str); michael@0: fprintf(stderr, "nsNullPrincipal (%p) = %s\n", this, str.get()); michael@0: } michael@0: #endif michael@0: michael@0: /** michael@0: * nsIPrincipal implementation michael@0: */ michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::Equals(nsIPrincipal *aOther, bool *aResult) michael@0: { michael@0: // Just equal to ourselves. Note that nsPrincipal::Equals will return false michael@0: // for us since we have a unique domain/origin/etc. michael@0: *aResult = (aOther == this); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::EqualsConsideringDomain(nsIPrincipal *aOther, bool *aResult) michael@0: { michael@0: return Equals(aOther, aResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetHashValue(uint32_t *aResult) michael@0: { michael@0: *aResult = (NS_PTR_TO_INT32(this) >> 2); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetURI(nsIURI** aURI) michael@0: { michael@0: return NS_EnsureSafeToReturn(mURI, aURI); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp) michael@0: { michael@0: NS_IF_ADDREF(*aCsp = mCSP); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp) michael@0: { michael@0: // If CSP was already set, it should not be destroyed! Instead, it should michael@0: // get set anew when a new principal is created. michael@0: if (mCSP) michael@0: return NS_ERROR_ALREADY_INITIALIZED; michael@0: michael@0: mCSP = aCsp; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetDomain(nsIURI** aDomain) michael@0: { michael@0: return NS_EnsureSafeToReturn(mURI, aDomain); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::SetDomain(nsIURI* aDomain) michael@0: { michael@0: // I think the right thing to do here is to just throw... Silently failing michael@0: // seems counterproductive. michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetOrigin(char** aOrigin) michael@0: { michael@0: *aOrigin = nullptr; michael@0: michael@0: nsAutoCString str; michael@0: nsresult rv = mURI->GetSpec(str); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: *aOrigin = ToNewCString(str); michael@0: NS_ENSURE_TRUE(*aOrigin, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult) michael@0: { michael@0: // We don't subsume anything except ourselves. Note that nsPrincipal::Equals michael@0: // will return false for us, since we're not about:blank and not Equals to michael@0: // reasonable nsPrincipals. michael@0: *aResult = (aOther == this); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::SubsumesConsideringDomain(nsIPrincipal *aOther, bool *aResult) michael@0: { michael@0: return Subsumes(aOther, aResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport, bool aAllowIfInheritsPrincipal) michael@0: { michael@0: if (aAllowIfInheritsPrincipal) { michael@0: if (nsPrincipal::IsPrincipalInherited(aURI)) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Also allow the load if the principal of the URI being checked is exactly michael@0: // us ie this. michael@0: nsCOMPtr uriPrinc = do_QueryInterface(aURI); michael@0: if (uriPrinc) { michael@0: nsCOMPtr principal; michael@0: uriPrinc->GetPrincipal(getter_AddRefs(principal)); michael@0: michael@0: if (principal && principal == this) { michael@0: return NS_OK; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (aReport) { michael@0: nsScriptSecurityManager::ReportError( michael@0: nullptr, NS_LITERAL_STRING("CheckSameOriginError"), mURI, aURI); michael@0: } michael@0: michael@0: return NS_ERROR_DOM_BAD_URI; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetJarPrefix(nsACString& aJarPrefix) michael@0: { michael@0: aJarPrefix.Truncate(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetAppStatus(uint16_t* aAppStatus) michael@0: { michael@0: *aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetAppId(uint32_t* aAppId) michael@0: { michael@0: *aAppId = nsIScriptSecurityManager::NO_APP_ID; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement) michael@0: { michael@0: *aIsInBrowserElement = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetUnknownAppId(bool* aUnknownAppId) michael@0: { michael@0: *aUnknownAppId = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal) michael@0: { michael@0: *aIsNullPrincipal = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::GetBaseDomain(nsACString& aBaseDomain) michael@0: { michael@0: // For a null principal, we use our unique uuid as the base domain. michael@0: return mURI->GetPath(aBaseDomain); michael@0: } michael@0: michael@0: /** michael@0: * nsISerializable implementation michael@0: */ michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::Read(nsIObjectInputStream* aStream) michael@0: { michael@0: // no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI michael@0: // is not really relevant. michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNullPrincipal::Write(nsIObjectOutputStream* aStream) michael@0: { michael@0: // no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI michael@0: // is not really relevant. michael@0: return NS_OK; michael@0: } michael@0: