2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Boris Zbarsky <bzbarsky@mit.edu> (Original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the principal that has no rights and can't be accessed by
|
|
|
|
* anything other than itself and chrome; null principals are not
|
|
|
|
* same-origin with anything but themselves.
|
|
|
|
*/
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsNullPrincipal.h"
|
2008-08-27 15:11:02 -07:00
|
|
|
#include "nsNullPrincipalURI.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsNetCID.h"
|
2008-02-26 19:45:29 -08:00
|
|
|
#include "nsDOMError.h"
|
|
|
|
#include "nsScriptSecurityManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2010-06-22 09:59:57 -07:00
|
|
|
NS_IMPL_CLASSINFO(nsNullPrincipal, NULL, nsIClassInfo::MAIN_THREAD_ONLY,
|
|
|
|
NS_NULLPRINCIPAL_CID)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE2_CI(nsNullPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER2(nsNullPrincipal,
|
|
|
|
nsIPrincipal,
|
|
|
|
nsISerializable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsNullPrincipal::AddRef()
|
|
|
|
{
|
2012-03-09 01:48:50 -08:00
|
|
|
NS_PRECONDITION(PRInt32(refcount) >= 0, "illegal refcnt");
|
|
|
|
nsrefcnt count = PR_ATOMIC_INCREMENT(&refcount);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LOG_ADDREF(this, count, "nsNullPrincipal", sizeof(*this));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsNullPrincipal::Release()
|
|
|
|
{
|
2012-03-09 01:48:50 -08:00
|
|
|
NS_PRECONDITION(0 != refcount, "dup release");
|
|
|
|
nsrefcnt count = PR_ATOMIC_DECREMENT(&refcount);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LOG_RELEASE(this, count, "nsNullPrincipal");
|
|
|
|
if (count == 0) {
|
2010-07-05 02:42:18 -07:00
|
|
|
delete this;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNullPrincipal::nsNullPrincipal()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNullPrincipal::~nsNullPrincipal()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-09-28 07:31:04 -07:00
|
|
|
#define NS_NULLPRINCIPAL_PREFIX NS_NULLPRINCIPAL_SCHEME ":"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsNullPrincipal::Init()
|
|
|
|
{
|
|
|
|
// FIXME: bug 327161 -- make sure the uuid generator is reseeding-resistant.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsID id;
|
|
|
|
rv = uuidgen->GenerateUUIDInPlace(&id);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-01-11 20:30:42 -08:00
|
|
|
char chars[NSID_LENGTH];
|
|
|
|
id.ToProvidedString(chars);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-11 20:30:42 -08:00
|
|
|
PRUint32 suffixLen = NSID_LENGTH - 1;
|
2011-10-10 22:50:08 -07:00
|
|
|
PRUint32 prefixLen = ArrayLength(NS_NULLPRINCIPAL_PREFIX) - 1;
|
2007-09-28 07:31:04 -07:00
|
|
|
|
|
|
|
// Use an nsCString so we only do the allocation once here and then share
|
|
|
|
// with nsJSPrincipals
|
|
|
|
nsCString str;
|
|
|
|
str.SetCapacity(prefixLen + suffixLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-28 07:31:04 -07:00
|
|
|
str.Append(NS_NULLPRINCIPAL_PREFIX);
|
2007-03-22 10:30:00 -07:00
|
|
|
str.Append(chars);
|
2008-08-27 15:11:02 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (str.Length() != prefixLen + suffixLen) {
|
2007-09-28 07:31:04 -07:00
|
|
|
NS_WARNING("Out of memory allocating null-principal URI");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:11:02 -07:00
|
|
|
mURI = new nsNullPrincipalURI(str);
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-09 01:48:50 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNullPrincipal::GetScriptLocation(nsACString &aStr)
|
|
|
|
{
|
|
|
|
mURI->GetSpec(aStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void nsNullPrincipal::dumpImpl()
|
|
|
|
{
|
|
|
|
nsCAutoString str;
|
|
|
|
mURI->GetSpec(str);
|
|
|
|
fprintf(stderr, "nsNullPrincipal (%p) = %s\n", this, str.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-03-09 01:48:50 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* nsIPrincipal implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetPreferences(char** aPrefName, char** aID,
|
|
|
|
char** aSubjectName,
|
|
|
|
char** aGrantedList, char** aDeniedList,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aIsTrusted)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// The null principal should never be written to preferences.
|
|
|
|
*aPrefName = nsnull;
|
|
|
|
*aID = nsnull;
|
|
|
|
*aSubjectName = nsnull;
|
|
|
|
*aGrantedList = nsnull;
|
|
|
|
*aDeniedList = nsnull;
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsTrusted = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNullPrincipal::Equals(nsIPrincipal *aOther, bool *aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Just equal to ourselves. Note that nsPrincipal::Equals will return false
|
|
|
|
// for us since we have a unique domain/origin/etc.
|
|
|
|
*aResult = (aOther == this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-19 04:31:54 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNullPrincipal::EqualsIgnoringDomain(nsIPrincipal *aOther, bool *aResult)
|
2011-05-19 04:31:54 -07:00
|
|
|
{
|
|
|
|
return Equals(aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetHashValue(PRUint32 *aResult)
|
|
|
|
{
|
|
|
|
*aResult = (NS_PTR_TO_INT32(this) >> 2);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
|
|
|
|
{
|
|
|
|
// We don't actually do security policy caching. And it's not like anyone
|
|
|
|
// can set a security policy for us anyway.
|
|
|
|
*aSecurityPolicy = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
|
|
|
|
{
|
|
|
|
// We don't actually do security policy caching. And it's not like anyone
|
|
|
|
// can set a security policy for us anyway.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::CanEnableCapability(const char *aCapability,
|
|
|
|
PRInt16 *aResult)
|
|
|
|
{
|
|
|
|
// Null principal can enable no capabilities.
|
|
|
|
*aResult = nsIPrincipal::ENABLE_DENIED;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::IsCapabilityEnabled(const char *aCapability,
|
|
|
|
void *aAnnotation,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Nope. No capabilities, I say!
|
2011-10-17 07:59:28 -07:00
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::EnableCapability(const char *aCapability, void **aAnnotation)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Didn't I say it? NO CAPABILITIES!");
|
|
|
|
*aAnnotation = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetURI(nsIURI** aURI)
|
|
|
|
{
|
|
|
|
return NS_EnsureSafeToReturn(mURI, aURI);
|
|
|
|
}
|
|
|
|
|
2010-01-22 13:38:21 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
|
|
|
|
{
|
|
|
|
// CSP on a null principal makes no sense
|
|
|
|
*aCsp = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
|
|
|
|
{
|
|
|
|
// CSP on a null principal makes no sense
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetDomain(nsIURI** aDomain)
|
|
|
|
{
|
|
|
|
return NS_EnsureSafeToReturn(mURI, aDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SetDomain(nsIURI* aDomain)
|
|
|
|
{
|
|
|
|
// I think the right thing to do here is to just throw... Silently failing
|
|
|
|
// seems counterproductive.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetOrigin(char** aOrigin)
|
|
|
|
{
|
|
|
|
*aOrigin = nsnull;
|
|
|
|
|
|
|
|
nsCAutoString str;
|
|
|
|
nsresult rv = mURI->GetSpec(str);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aOrigin = ToNewCString(str);
|
|
|
|
NS_ENSURE_TRUE(*aOrigin, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNullPrincipal::GetHasCertificate(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetFingerprint(nsACString& aID)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetPrettyName(nsACString& aName)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNullPrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// We don't subsume anything except ourselves. Note that nsPrincipal::Equals
|
|
|
|
// will return false for us, since we're not about:blank and not Equals to
|
|
|
|
// reasonable nsPrincipals.
|
|
|
|
*aResult = (aOther == this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-23 09:41:25 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::SubsumesIgnoringDomain(nsIPrincipal *aOther, bool *aResult)
|
|
|
|
{
|
|
|
|
return Subsumes(aOther, aResult);
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:45:29 -08:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNullPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport)
|
2008-02-26 19:45:29 -08:00
|
|
|
{
|
|
|
|
if (aReport) {
|
|
|
|
nsScriptSecurityManager::ReportError(
|
|
|
|
nsnull, NS_LITERAL_STRING("CheckSameOriginError"), mURI, aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetSubjectName(nsACString& aName)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::GetCertificate(nsISupports** aCertificate)
|
|
|
|
{
|
|
|
|
*aCertificate = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsISerializable implementation
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::Read(nsIObjectInputStream* aStream)
|
|
|
|
{
|
|
|
|
// no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI
|
|
|
|
// is not really relevant.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
|
|
|
|
{
|
|
|
|
// no-op: CID is sufficient to create a useful nsNullPrincipal, since the URI
|
|
|
|
// is not really relevant.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|