2015-06-16 22:27:45 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
#include "nsPermission.h"
|
2015-06-10 19:48:22 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-06-06 23:59:31 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2015-06-10 19:48:22 +03:00
|
|
|
#include "nsIEffectiveTLDService.h"
|
2015-06-16 22:27:45 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
// nsPermission Implementation
|
|
|
|
|
2013-06-06 23:59:31 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsPermission, nullptr, 0, {0})
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_CI(nsPermission, nsIPermission)
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::nsPermission(nsIPrincipal* aPrincipal, const nsACString& aType,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCapability, uint32_t aExpireType,
|
|
|
|
int64_t aExpireTime)
|
2015-06-10 19:48:22 +03:00
|
|
|
: mPrincipal(aPrincipal),
|
2003-06-18 04:52:59 +04:00
|
|
|
mType(aType),
|
|
|
|
mCapability(aCapability),
|
2009-10-17 01:01:04 +04:00
|
|
|
mExpireType(aExpireType),
|
|
|
|
mExpireTime(aExpireTime) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-20 11:35:21 +03:00
|
|
|
already_AddRefed<nsPermission> nsPermission::Create(nsIPrincipal* aPrincipal,
|
|
|
|
const nsACString& aType,
|
|
|
|
uint32_t aCapability,
|
|
|
|
uint32_t aExpireType,
|
|
|
|
int64_t aExpireTime) {
|
|
|
|
NS_ENSURE_TRUE(aPrincipal, nullptr);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
mozilla::BasePrincipal::Cast(aPrincipal)
|
|
|
|
->CloneStrippingUserContextIdAndFirstPartyDomain();
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(principal, nullptr);
|
|
|
|
|
|
|
|
RefPtr<nsPermission> permission =
|
|
|
|
new nsPermission(principal, aType, aCapability, aExpireType, aExpireTime);
|
|
|
|
return permission.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-23 22:37:31 +04:00
|
|
|
NS_IMETHODIMP
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::GetPrincipal(nsIPrincipal** aPrincipal) {
|
|
|
|
nsCOMPtr<nsIPrincipal> copy = mPrincipal;
|
|
|
|
copy.forget(aPrincipal);
|
2012-08-23 22:37:31 +04:00
|
|
|
return NS_OK;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2003-06-18 04:52:59 +04:00
|
|
|
NS_IMETHODIMP
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::GetType(nsACString& aType) {
|
|
|
|
aType = mType;
|
2012-08-23 22:37:31 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::GetCapability(uint32_t* aCapability) {
|
|
|
|
*aCapability = mCapability;
|
2003-03-22 04:24:51 +03:00
|
|
|
return NS_OK;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2003-06-18 04:52:59 +04:00
|
|
|
NS_IMETHODIMP
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::GetExpireType(uint32_t* aExpireType) {
|
|
|
|
*aExpireType = mExpireType;
|
2001-03-29 06:11:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-18 04:52:59 +04:00
|
|
|
NS_IMETHODIMP
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::GetExpireTime(int64_t* aExpireTime) {
|
|
|
|
*aExpireTime = mExpireTime;
|
2001-03-29 06:11:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-10-17 01:01:04 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::Matches(nsIPrincipal* aPrincipal, bool aExactHost,
|
|
|
|
bool* aMatches) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrincipal);
|
|
|
|
NS_ENSURE_ARG_POINTER(aMatches);
|
|
|
|
|
|
|
|
*aMatches = false;
|
|
|
|
|
2016-09-20 11:35:21 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
mozilla::BasePrincipal::Cast(aPrincipal)
|
|
|
|
->CloneStrippingUserContextIdAndFirstPartyDomain();
|
|
|
|
|
|
|
|
if (!principal) {
|
|
|
|
*aMatches = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-10 19:48:22 +03:00
|
|
|
// If the principals are equal, then they match.
|
2016-09-20 11:35:21 +03:00
|
|
|
if (mPrincipal->Equals(principal)) {
|
2015-06-10 19:48:22 +03:00
|
|
|
*aMatches = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-16 22:27:45 +03:00
|
|
|
// If we are matching with an exact host, we're done now - the permissions
|
|
|
|
// don't match otherwise, we need to start comparing subdomains!
|
|
|
|
if (aExactHost) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-06-10 19:48:22 +03:00
|
|
|
|
2015-06-16 22:27:45 +03:00
|
|
|
// Compare their OriginAttributes
|
2017-01-12 19:38:48 +03:00
|
|
|
const mozilla::OriginAttributes& theirAttrs =
|
|
|
|
principal->OriginAttributesRef();
|
|
|
|
const mozilla::OriginAttributes& ourAttrs = mPrincipal->OriginAttributesRef();
|
2015-06-10 19:48:22 +03:00
|
|
|
|
2015-06-16 22:27:45 +03:00
|
|
|
if (theirAttrs != ourAttrs) {
|
|
|
|
return NS_OK;
|
2015-06-10 19:48:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> theirURI;
|
2016-09-20 11:35:21 +03:00
|
|
|
nsresult rv = principal->GetURI(getter_AddRefs(theirURI));
|
2015-06-10 19:48:22 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> ourURI;
|
|
|
|
rv = mPrincipal->GetURI(getter_AddRefs(ourURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-06-16 22:27:45 +03:00
|
|
|
// Compare schemes
|
|
|
|
nsAutoCString theirScheme;
|
|
|
|
rv = theirURI->GetScheme(theirScheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString ourScheme;
|
|
|
|
rv = ourURI->GetScheme(ourScheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (theirScheme != ourScheme) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare ports
|
|
|
|
int32_t theirPort;
|
|
|
|
rv = theirURI->GetPort(&theirPort);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int32_t ourPort;
|
|
|
|
rv = ourURI->GetPort(&ourPort);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (theirPort != ourPort) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the host or any subdomain of their host matches.
|
2015-06-10 19:48:22 +03:00
|
|
|
nsAutoCString theirHost;
|
|
|
|
rv = theirURI->GetHost(theirHost);
|
|
|
|
if (NS_FAILED(rv) || theirHost.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString ourHost;
|
|
|
|
rv = ourURI->GetHost(ourHost);
|
|
|
|
if (NS_FAILED(rv) || ourHost.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
|
|
|
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
|
|
|
if (!tldService) {
|
|
|
|
NS_ERROR("Should have a tld service!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-06-16 22:27:45 +03:00
|
|
|
// This loop will not loop forever, as GetNextSubDomain will eventually fail
|
|
|
|
// with NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS.
|
2015-06-10 19:48:22 +03:00
|
|
|
while (theirHost != ourHost) {
|
|
|
|
rv = tldService->GetNextSubDomain(theirHost, theirHost);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
|
|
|
|
return NS_OK;
|
|
|
|
} else {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aMatches = true;
|
2009-10-17 01:01:04 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-06-10 19:48:22 +03:00
|
|
|
nsPermission::MatchesURI(nsIURI* aURI, bool aExactHost, bool* aMatches) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
mozilla::OriginAttributes attrs;
|
2015-08-18 10:01:42 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
mozilla::BasePrincipal::CreateCodebasePrincipal(aURI, attrs);
|
|
|
|
NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE);
|
2015-06-10 19:48:22 +03:00
|
|
|
|
|
|
|
return Matches(principal, aExactHost, aMatches);
|
2009-10-17 01:01:04 +04:00
|
|
|
}
|