Bug 1616675 - Add isIpAddress to nsIPrincipal r=ckerschb

Adds an infallable boolean attribute to nsIPrincipal that indicates whether
the principal's host is an IP address.

Adds a gtest to exercise that part of the nsIPrincipal.

/**
 * Returns if the principal is an IP address.
 */
[infallible] readonly attribute boolean isIpAddress;

Differential Revision: https://phabricator.services.mozilla.com/D63966

--HG--
extra : moz-landing-system : lando
This commit is contained in:
J.C. Jones 2020-02-26 23:15:43 +00:00
parent 5f3cbafe43
commit 56fa739a54
5 changed files with 73 additions and 0 deletions

View File

@ -25,6 +25,8 @@
#include "mozilla/Components.h"
#include "nsIURIFixup.h"
#include "prnetdb.h"
#include "json/json.h"
#include "nsSerializationHelper.h"
@ -621,6 +623,31 @@ NS_IMETHODIMP BasePrincipal::GetIsOnion(bool* aIsOnion) {
return NS_OK;
}
NS_IMETHODIMP BasePrincipal::GetIsIpAddress(bool* aIsIpAddress) {
*aIsIpAddress = false;
nsCOMPtr<nsIURI> prinURI;
nsresult rv = GetURI(getter_AddRefs(prinURI));
if (NS_FAILED(rv) || !prinURI) {
return NS_OK;
}
nsAutoCString host;
rv = prinURI->GetHost(host);
if (NS_FAILED(rv)) {
return NS_OK;
}
PRNetAddr prAddr;
memset(&prAddr, 0, sizeof(prAddr));
if (PR_StringToNetAddr(host.get(), &prAddr) == PR_SUCCESS) {
*aIsIpAddress = true;
}
return NS_OK;
}
NS_IMETHODIMP
BasePrincipal::SchemeIs(const char* aScheme, bool* aResult) {
*aResult = false;

View File

@ -132,6 +132,7 @@ class BasePrincipal : public nsJSPrincipals {
NS_IMETHOD GetHost(nsACString& aRes) override;
NS_IMETHOD GetPrepath(nsACString& aResult) override;
NS_IMETHOD GetOriginSuffix(nsACString& aOriginSuffix) final;
NS_IMETHOD GetIsIpAddress(bool* aIsIpAddress) override;
NS_IMETHOD GetIsOnion(bool* aIsOnion) override;
NS_IMETHOD GetIsInIsolatedMozBrowserElement(
bool* aIsInIsolatedMozBrowserElement) final;

View File

@ -429,6 +429,11 @@ interface nsIPrincipal : nsISerializable
* Returns true if the URI is an Onion URI
*/
[infallible] readonly attribute boolean isOnion;
/**
* Returns if the principal is for an IP address.
*/
[infallible] readonly attribute boolean isIpAddress;
};
/**

View File

@ -0,0 +1,39 @@
/* 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/. */
#include "gtest/gtest.h"
#include "mozilla/BasePrincipal.h"
#include "nsScriptSecurityManager.h"
using namespace mozilla;
class PrincipalAttributesParam {
public:
nsAutoCString spec;
bool expectIsIpAddress;
};
class PrincipalAttributesTest
: public ::testing::TestWithParam<PrincipalAttributesParam> {};
TEST_P(PrincipalAttributesTest, PrincipalAttributesTest) {
nsCOMPtr<nsIScriptSecurityManager> ssm =
nsScriptSecurityManager::GetScriptSecurityManager();
nsAutoCString spec(GetParam().spec);
nsCOMPtr<nsIPrincipal> principal;
nsresult rv =
ssm->CreateContentPrincipalFromOrigin(spec, getter_AddRefs(principal));
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(principal->GetIsIpAddress(), GetParam().expectIsIpAddress);
}
static const PrincipalAttributesParam kAttributes[] = {
{nsAutoCString("https://mozilla.com"), false},
{nsAutoCString("https://127.0.0.1"), true},
{nsAutoCString("https://[::1]"), true},
};
INSTANTIATE_TEST_CASE_P(TestPrincipalAttributes, PrincipalAttributesTest,
::testing::ValuesIn(kAttributes));

View File

@ -6,6 +6,7 @@
UNIFIED_SOURCES += [
'TestOriginAttributes.cpp',
'TestPrincipalAttributes.cpp',
'TestPrincipalSerialization.cpp'
]