From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- toolkit/system/commonproxy/ProxyUtils.cpp | 162 +++++++++++++++++++++ toolkit/system/commonproxy/ProxyUtils.h | 21 +++ toolkit/system/commonproxy/moz.build | 18 +++ .../tests/gtest/TestProxyBypassRules.cpp | 47 ++++++ toolkit/system/commonproxy/tests/gtest/moz.build | 18 +++ 5 files changed, 266 insertions(+) create mode 100644 toolkit/system/commonproxy/ProxyUtils.cpp create mode 100644 toolkit/system/commonproxy/ProxyUtils.h create mode 100644 toolkit/system/commonproxy/moz.build create mode 100644 toolkit/system/commonproxy/tests/gtest/TestProxyBypassRules.cpp create mode 100644 toolkit/system/commonproxy/tests/gtest/moz.build (limited to 'toolkit/system/commonproxy') diff --git a/toolkit/system/commonproxy/ProxyUtils.cpp b/toolkit/system/commonproxy/ProxyUtils.cpp new file mode 100644 index 0000000000..2c110db38b --- /dev/null +++ b/toolkit/system/commonproxy/ProxyUtils.cpp @@ -0,0 +1,162 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "ProxyUtils.h" + +#include "mozilla/IntegerRange.h" +#include "nsReadableUtils.h" +#include "nsTArray.h" +#include "prnetdb.h" +#include "prtypes.h" + +namespace mozilla { +namespace toolkit { +namespace system { + +/** + * Normalize the short IP form into the complete form. + * For example, it converts "192.168" into "192.168.0.0" + */ +static void NormalizeAddr(const nsACString& aAddr, nsCString& aNormalized) { + nsTArray addr; + ParseString(aAddr, '.', addr); + aNormalized = + StringJoin("."_ns, IntegerRange(4), [&addr](nsACString& dst, size_t i) { + if (i < addr.Length()) { + dst.Append(addr[i]); + } else { + dst.Append('0'); + } + }); +} + +static PRUint32 MaskIPv4Addr(PRUint32 aAddr, uint16_t aMaskLen) { + if (aMaskLen == 32) { + return aAddr; + } + return PR_htonl(PR_ntohl(aAddr) & (~0L << (32 - aMaskLen))); +} + +static void MaskIPv6Addr(PRIPv6Addr& aAddr, uint16_t aMaskLen) { + if (aMaskLen == 128) { + return; + } + + if (aMaskLen > 96) { + aAddr.pr_s6_addr32[3] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[3]) & (~0L << (128 - aMaskLen))); + } else if (aMaskLen > 64) { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[2]) & (~0L << (96 - aMaskLen))); + } else if (aMaskLen > 32) { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = 0; + aAddr.pr_s6_addr32[1] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[1]) & (~0L << (64 - aMaskLen))); + } else { + aAddr.pr_s6_addr32[3] = 0; + aAddr.pr_s6_addr32[2] = 0; + aAddr.pr_s6_addr32[1] = 0; + aAddr.pr_s6_addr32[0] = + PR_htonl(PR_ntohl(aAddr.pr_s6_addr32[0]) & (~0L << (32 - aMaskLen))); + } + + return; +} + +static bool IsMatchMask(const nsACString& aHost, const nsACString& aOverride) { + nsresult rv; + + auto tokenEnd = aOverride.FindChar('/'); + if (tokenEnd == -1) { + return false; + } + + nsAutoCString prefixStr( + Substring(aOverride, tokenEnd + 1, aOverride.Length() - tokenEnd - 1)); + auto maskLen = prefixStr.ToInteger(&rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return false; + } + + nsAutoCString override(aOverride); + NormalizeAddr(Substring(aOverride, 0, tokenEnd), override); + + PRNetAddr prAddrHost; + PRNetAddr prAddrOverride; + if (PR_SUCCESS != + PR_StringToNetAddr(PromiseFlatCString(aHost).get(), &prAddrHost) || + PR_SUCCESS != PR_StringToNetAddr(override.get(), &prAddrOverride)) { + return false; + } + + if (prAddrHost.raw.family == PR_AF_INET && + prAddrOverride.raw.family == PR_AF_INET) { + return MaskIPv4Addr(prAddrHost.inet.ip, maskLen) == + MaskIPv4Addr(prAddrOverride.inet.ip, maskLen); + } else if (prAddrHost.raw.family == PR_AF_INET6 && + prAddrOverride.raw.family == PR_AF_INET6) { + MaskIPv6Addr(prAddrHost.ipv6.ip, maskLen); + MaskIPv6Addr(prAddrOverride.ipv6.ip, maskLen); + + return memcmp(&prAddrHost.ipv6.ip, &prAddrOverride.ipv6.ip, + sizeof(PRIPv6Addr)) == 0; + } + + return false; +} + +static bool IsMatchWildcard(const nsACString& aHost, + const nsACString& aOverride) { + nsAutoCString host(aHost); + nsAutoCString override(aOverride); + + int32_t overrideLength = override.Length(); + int32_t tokenStart = 0; + int32_t offset = 0; + bool star = false; + + while (tokenStart < overrideLength) { + int32_t tokenEnd = override.FindChar('*', tokenStart); + if (tokenEnd == tokenStart) { + // Star is the first character in the token. + star = true; + tokenStart++; + // If the character following the '*' is a '.' character then skip + // it so that "*.foo.com" allows "foo.com". + if (override.FindChar('.', tokenStart) == tokenStart) { + nsAutoCString token(Substring(override, tokenStart + 1, + overrideLength - tokenStart - 1)); + if (host.Equals(token)) { + return true; + } + } + } else { + if (tokenEnd == -1) { + tokenEnd = overrideLength; // no '*' char, match rest of string + } + nsAutoCString token( + Substring(override, tokenStart, tokenEnd - tokenStart)); + offset = host.Find(token, offset); + if (offset == -1 || (!star && offset)) { + return false; + } + star = false; + tokenStart = tokenEnd; + offset += token.Length(); + } + } + + return (star || (offset == static_cast(host.Length()))); +} + +bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride) { + return IsMatchMask(aHost, aOverride) || IsMatchWildcard(aHost, aOverride); +} + +} // namespace system +} // namespace toolkit +} // namespace mozilla diff --git a/toolkit/system/commonproxy/ProxyUtils.h b/toolkit/system/commonproxy/ProxyUtils.h new file mode 100644 index 0000000000..16c451c547 --- /dev/null +++ b/toolkit/system/commonproxy/ProxyUtils.h @@ -0,0 +1,21 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_toolkit_system_commonproxy_ProxyUtils_h +#define mozilla_toolkit_system_commonproxy_ProxyUtils_h + +#include "nsString.h" + +namespace mozilla { +namespace toolkit { +namespace system { + +bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride); + +} // namespace system +} // namespace toolkit +} // namespace mozilla + +#endif // mozilla_toolkit_system_commonproxy_ProxyUtils_h diff --git a/toolkit/system/commonproxy/moz.build b/toolkit/system/commonproxy/moz.build new file mode 100644 index 0000000000..6f93b46e9a --- /dev/null +++ b/toolkit/system/commonproxy/moz.build @@ -0,0 +1,18 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +with Files("**"): + BUG_COMPONENT = ("Core", "Networking") + +TEST_DIRS += ["tests/gtest"] + +SOURCES += ["ProxyUtils.cpp"] + +FINAL_LIBRARY = "xul" + +LOCAL_INCLUDES += [ + "/netwerk/base", +] diff --git a/toolkit/system/commonproxy/tests/gtest/TestProxyBypassRules.cpp b/toolkit/system/commonproxy/tests/gtest/TestProxyBypassRules.cpp new file mode 100644 index 0000000000..3047088d62 --- /dev/null +++ b/toolkit/system/commonproxy/tests/gtest/TestProxyBypassRules.cpp @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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 "ProxyUtils.h" + +using namespace mozilla::toolkit::system; + +TEST(CommonProxy, TestProxyBypassRules) +{ + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("mozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_FALSE(IsHostProxyEntry("notmozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.org"_ns, "*.mozilla.org"_ns)); + EXPECT_TRUE(IsHostProxyEntry("www.mozilla.com"_ns, "*.mozilla.*"_ns)); +} + +TEST(CommonProxy, TestProxyBypassRulesIPv4) +{ + EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.*"_ns)); + EXPECT_FALSE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.2.*"_ns)); + + EXPECT_TRUE(IsHostProxyEntry("10.1.2.3"_ns, "10.0.0.0/8"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/16"_ns)); + EXPECT_FALSE(IsHostProxyEntry("192.168.192.1"_ns, "192.168/17"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.192.1"_ns, "192.168.128/17"_ns)); + EXPECT_TRUE(IsHostProxyEntry("192.168.1.1"_ns, "192.168.1.1/32"_ns)); +} + +TEST(CommonProxy, TestProxyBypassRulesIPv6) +{ + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/64"_ns)); + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/80"_ns)); + EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/80"_ns)); + EXPECT_TRUE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0000:0000:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/96"_ns)); + EXPECT_FALSE(IsHostProxyEntry("2001:0DB8:ABCD:0012:0123:4567:89AB:CDEF"_ns, + "2001:db8:abcd:0012::0/96"_ns)); +} diff --git a/toolkit/system/commonproxy/tests/gtest/moz.build b/toolkit/system/commonproxy/tests/gtest/moz.build new file mode 100644 index 0000000000..3df0793605 --- /dev/null +++ b/toolkit/system/commonproxy/tests/gtest/moz.build @@ -0,0 +1,18 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +UNIFIED_SOURCES += [ + "TestProxyBypassRules.cpp", +] + +LOCAL_INCLUDES += [ + "../..", +] + +FINAL_LIBRARY = "xul-gtest" + +if CONFIG["CC_TYPE"] in ("clang", "gcc"): + CXXFLAGS += ["-Wshadow"] -- cgit v1.2.3