From 56fa739a5462f52fd781dd4f48d1cb9037f4834b Mon Sep 17 00:00:00 2001 From: "J.C. Jones" Date: Wed, 26 Feb 2020 23:15:43 +0000 Subject: [PATCH] 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 --- caps/BasePrincipal.cpp | 27 ++++++++++++++ caps/BasePrincipal.h | 1 + caps/nsIPrincipal.idl | 5 +++ caps/tests/gtest/TestPrincipalAttributes.cpp | 39 ++++++++++++++++++++ caps/tests/gtest/moz.build | 1 + 5 files changed, 73 insertions(+) create mode 100644 caps/tests/gtest/TestPrincipalAttributes.cpp diff --git a/caps/BasePrincipal.cpp b/caps/BasePrincipal.cpp index a1372f4584bc..ceba393ba85f 100644 --- a/caps/BasePrincipal.cpp +++ b/caps/BasePrincipal.cpp @@ -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 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; diff --git a/caps/BasePrincipal.h b/caps/BasePrincipal.h index e62174629b0f..7c19e16384cf 100644 --- a/caps/BasePrincipal.h +++ b/caps/BasePrincipal.h @@ -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; diff --git a/caps/nsIPrincipal.idl b/caps/nsIPrincipal.idl index c0751be06e76..f86df175f202 100644 --- a/caps/nsIPrincipal.idl +++ b/caps/nsIPrincipal.idl @@ -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; }; /** diff --git a/caps/tests/gtest/TestPrincipalAttributes.cpp b/caps/tests/gtest/TestPrincipalAttributes.cpp new file mode 100644 index 000000000000..3824cd8934bd --- /dev/null +++ b/caps/tests/gtest/TestPrincipalAttributes.cpp @@ -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 {}; + +TEST_P(PrincipalAttributesTest, PrincipalAttributesTest) { + nsCOMPtr ssm = + nsScriptSecurityManager::GetScriptSecurityManager(); + + nsAutoCString spec(GetParam().spec); + nsCOMPtr 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)); diff --git a/caps/tests/gtest/moz.build b/caps/tests/gtest/moz.build index 6fc9b5ef8fa6..bcc102c939c1 100644 --- a/caps/tests/gtest/moz.build +++ b/caps/tests/gtest/moz.build @@ -6,6 +6,7 @@ UNIFIED_SOURCES += [ 'TestOriginAttributes.cpp', + 'TestPrincipalAttributes.cpp', 'TestPrincipalSerialization.cpp' ]