Bug 1708116 - Use NetAddr::InitFromString instead of PR_StringToNetAddr r=necko-reviewers,dragana

Differential Revision: https://phabricator.services.mozilla.com/D113752
This commit is contained in:
Valentin Gosu 2021-05-06 10:06:46 +00:00
Родитель 6de6a52f1e
Коммит 301361bdfb
22 изменённых файлов: 74 добавлений и 149 удалений

Просмотреть файл

@ -82,9 +82,7 @@ already_AddRefed<UDPSocket> UDPSocket::Constructor(const GlobalObject& aGlobal,
// check if localAddress is a valid IPv4/6 address
NS_ConvertUTF16toUTF8 address(localAddress);
PRNetAddr prAddr;
PRStatus status = PR_StringToNetAddr(address.BeginReading(), &prAddr);
if (status != PR_SUCCESS) {
if (!net::HostIsIPLiteral(address)) {
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
return nullptr;
}

Просмотреть файл

@ -578,18 +578,10 @@ bool nsHTTPSOnlyUtils::LoopbackOrLocalException(nsIURI* aURI) {
return true;
}
// The local-ip and loopback checks expect a NetAddr struct. We only have a
// host-string but can convert it to a NetAddr by first converting it to
// PRNetAddr.
PRNetAddr tempAddr;
memset(&tempAddr, 0, sizeof(PRNetAddr));
// PR_StringToNetAddr does not properly initialize the output buffer in the
// case of IPv6 input. See bug 223145.
if (PR_StringToNetAddr(asciiHost.get(), &tempAddr) != PR_SUCCESS) {
mozilla::net::NetAddr addr;
if (NS_FAILED(addr.InitFromString(asciiHost))) {
return false;
}
mozilla::net::NetAddr addr(&tempAddr);
// Loopback IPs are always exempt
if (addr.IsLoopbackAddr()) {
return true;

Просмотреть файл

@ -233,17 +233,12 @@ bool nsMixedContentBlocker::IsPotentiallyTrustworthyLoopbackHost(
return true;
}
PRNetAddr tempAddr;
memset(&tempAddr, 0, sizeof(PRNetAddr));
if (PR_StringToNetAddr(PromiseFlatCString(aAsciiHost).get(), &tempAddr) !=
PR_SUCCESS) {
using namespace mozilla::net;
NetAddr addr;
if (NS_FAILED(addr.InitFromString(aAsciiHost))) {
return false;
}
using namespace mozilla::net;
NetAddr addr(&tempAddr);
// Step 4 of
// https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy says
// we should only consider [::1]/128 as a potentially trustworthy IPv6

Просмотреть файл

@ -49,6 +49,7 @@
#include "nsUnicharUtils.h"
#include "mozilla/net/HttpAuthUtils.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/net/DNS.h"
using mozilla::Base64Decode;
@ -564,13 +565,16 @@ bool nsHttpNegotiateAuth::TestBoolPref(const char* pref) {
bool nsHttpNegotiateAuth::TestNonFqdn(nsIURI* uri) {
nsAutoCString host;
PRNetAddr addr;
if (!TestBoolPref(kNegotiateAuthAllowNonFqdn)) return false;
if (!TestBoolPref(kNegotiateAuthAllowNonFqdn)) {
return false;
}
if (NS_FAILED(uri->GetAsciiHost(host))) return false;
if (NS_FAILED(uri->GetAsciiHost(host))) {
return false;
}
// return true if host does not contain a dot and is not an ip address
return !host.IsEmpty() && !host.Contains('.') &&
PR_StringToNetAddr(host.BeginReading(), &addr) != PR_SUCCESS;
!mozilla::net::HostIsIPLiteral(host);
}

Просмотреть файл

@ -128,17 +128,15 @@ already_AddRefed<AddrInfo> NetworkConnectivityService::MapNAT64IPs(
// Returns true if a prefix was read and saved to the argument
static inline bool NAT64PrefixFromPref(NetAddr* prefix) {
nsAutoCString nat64PrefixPref;
PRNetAddr prAddr{};
nsresult rv = Preferences::GetCString(
"network.connectivity-service.nat64-prefix", nat64PrefixPref);
if (NS_FAILED(rv) || nat64PrefixPref.IsEmpty() ||
PR_StringToNetAddr(nat64PrefixPref.get(), &prAddr) != PR_SUCCESS ||
prAddr.raw.family != PR_AF_INET6) {
NS_FAILED(prefix->InitFromString(nat64PrefixPref)) ||
prefix->raw.family != PR_AF_INET6) {
return false;
}
PRNetAddrToNetAddr(&prAddr, prefix);
return true;
}

Просмотреть файл

@ -980,9 +980,7 @@ bool ProxyAutoConfig::MyIPAddress(const JS::CallArgs& aArgs) {
}
} else {
// we can still do the fancy multi homing thing if the host is a literal
PRNetAddr tempAddr;
memset(&tempAddr, 0, sizeof(PRNetAddr));
if ((PR_StringToNetAddr(mRunningHost.get(), &tempAddr) == PR_SUCCESS) &&
if (HostIsIPLiteral(mRunningHost) &&
(!MyIPAddressTryHost(mRunningHost, kTimeout, aArgs, &rvalAssigned) ||
rvalAssigned)) {
return rvalAssigned;

Просмотреть файл

@ -743,16 +743,10 @@ nsresult nsIOService::RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan) {
return rv;
}
PRNetAddr prAddr;
if (PR_StringToNetAddr(host.BeginReading(), &prAddr) != PR_SUCCESS) {
// The redirect wasn't to an IP literal, so there's probably no need
// to trigger the captive portal detection right now. It can wait.
return NS_OK;
}
NetAddr netAddr(&prAddr);
if (netAddr.IsIPAddrLocal()) {
// Redirects to local IP addresses are probably captive portals
NetAddr addr;
// If the redirect wasn't to an IP literal, so there's probably no need
// to trigger the captive portal detection right now. It can wait.
if (NS_SUCCEEDED(addr.InitFromString(host)) && addr.IsIPAddrLocal()) {
RecheckCaptivePortal();
}
@ -947,13 +941,9 @@ nsIOService::HostnameIsLocalIPAddress(nsIURI* aURI, bool* aResult) {
*aResult = false;
PRNetAddr addr;
PRStatus result = PR_StringToNetAddr(host.get(), &addr);
if (result == PR_SUCCESS) {
NetAddr netAddr(&addr);
if (netAddr.IsIPAddrLocal()) {
*aResult = true;
}
NetAddr addr;
if (NS_SUCCEEDED(addr.InitFromString(host)) && addr.IsIPAddrLocal()) {
*aResult = true;
}
return NS_OK;
@ -974,13 +964,9 @@ nsIOService::HostnameIsSharedIPAddress(nsIURI* aURI, bool* aResult) {
*aResult = false;
PRNetAddr addr;
PRStatus result = PR_StringToNetAddr(host.get(), &addr);
if (result == PR_SUCCESS) {
NetAddr netAddr(&addr);
if (netAddr.IsIPAddrShared()) {
*aResult = true;
}
NetAddr addr;
if (NS_SUCCEEDED(addr.InitFromString(host)) && addr.IsIPAddrShared()) {
*aResult = true;
}
return NS_OK;

Просмотреть файл

@ -519,30 +519,19 @@ nsUDPSocket::Init2(const nsACString& aAddr, int32_t aPort,
return NS_ERROR_INVALID_ARG;
}
PRNetAddr prAddr;
memset(&prAddr, 0, sizeof(prAddr));
if (PR_StringToNetAddr(aAddr.BeginReading(), &prAddr) != PR_SUCCESS) {
return NS_ERROR_FAILURE;
}
if (aPort < 0) {
aPort = 0;
}
switch (prAddr.raw.family) {
case PR_AF_INET:
prAddr.inet.port = PR_htons(aPort);
break;
case PR_AF_INET6:
prAddr.ipv6.port = PR_htons(aPort);
break;
default:
MOZ_ASSERT_UNREACHABLE("Dont accept address other than IPv4 and IPv6");
return NS_ERROR_ILLEGAL_VALUE;
NetAddr addr;
if (NS_FAILED(addr.InitFromString(aAddr, uint16_t(aPort)))) {
return NS_ERROR_FAILURE;
}
NetAddr addr;
PRNetAddrToNetAddr(&prAddr, &addr);
if (addr.raw.family != PR_AF_INET && addr.raw.family != PR_AF_INET6) {
MOZ_ASSERT_UNREACHABLE("Dont accept address other than IPv4 and IPv6");
return NS_ERROR_ILLEGAL_VALUE;
}
return InitWithAddress(&addr, aPrincipal, aAddressReuse, aOptionalArgc);
}

Просмотреть файл

@ -26,6 +26,7 @@
#include "nsEscape.h"
#include "nsDOMString.h"
#include "mozilla/net/rust_helper.h"
#include "mozilla/net/DNS.h"
using namespace mozilla;
@ -889,9 +890,7 @@ bool net_IsValidHostName(const nsACString& host) {
return true;
// Might be a valid IPv6 link-local address containing a percent sign
nsAutoCString strhost(host);
PRNetAddr addr;
return PR_StringToNetAddr(strhost.get(), &addr) == PR_SUCCESS;
return mozilla::net::HostIsIPLiteral(host);
}
bool net_IsValidIPv4Addr(const nsACString& aAddr) {

Просмотреть файл

@ -179,6 +179,11 @@ bool IsLoopbackHostname(const nsACString& aAsciiHost) {
StringEndsWith(host, ".localhost"_ns);
}
bool HostIsIPLiteral(const nsACString& aAsciiHost) {
NetAddr addr;
return NS_SUCCEEDED(addr.InitFromString(aAsciiHost));
}
bool NetAddr::IsIPAddrAny() const {
if (this->raw.family == AF_INET) {
if (this->inet.ip == htonl(INADDR_ANY)) {

Просмотреть файл

@ -13,7 +13,6 @@
#include "prnetdb.h"
#include "plstr.h"
#include "nsISupportsImpl.h"
#include "mozilla/LinkedList.h"
#include "mozilla/MemoryReporting.h"
#include "nsTArray.h"
@ -288,6 +287,8 @@ void NetAddrToPRNetAddr(const NetAddr* addr, PRNetAddr* prAddr);
bool IsLoopbackHostname(const nsACString& aAsciiHost);
bool HostIsIPLiteral(const nsACString& aAsciiHost);
} // namespace net
} // namespace mozilla

Просмотреть файл

@ -312,8 +312,7 @@ bool FindAddrOverride(const nsACString& aHost, uint16_t aAddressFamily,
if (aAddressFamily != AF_UNSPEC && ip.raw.family != aAddressFamily) {
continue;
}
NetAddr addr(&ip);
addresses.AppendElement(addr);
addresses.AppendElement(ip);
}
if (!cname) {
@ -412,10 +411,7 @@ NS_IMPL_ISUPPORTS(NativeDNSResolverOverride, nsINativeDNSResolverOverride)
NS_IMETHODIMP NativeDNSResolverOverride::AddIPOverride(
const nsACString& aHost, const nsACString& aIPLiteral) {
PRNetAddr tempAddr;
// Unfortunately, PR_StringToNetAddr does not properly initialize
// the output buffer in the case of IPv6 input. See bug 223145.
memset(&tempAddr, 0, sizeof(PRNetAddr));
NetAddr tempAddr;
if (aIPLiteral.Equals("N/A"_ns)) {
AutoWriteLock lock(mLock);
@ -424,8 +420,7 @@ NS_IMETHODIMP NativeDNSResolverOverride::AddIPOverride(
return NS_OK;
}
if (PR_StringToNetAddr(nsCString(aIPLiteral).get(), &tempAddr) !=
PR_SUCCESS) {
if (NS_FAILED(tempAddr.InitFromString(aIPLiteral))) {
return NS_ERROR_UNEXPECTED;
}

Просмотреть файл

@ -15,6 +15,7 @@
#include "mozilla/RWLock.h"
#include "nsTArray.h"
#include "prio.h"
#include "mozilla/net/DNS.h"
#if defined(XP_WIN)
# define DNSQUERY_AVAILABLE 1
@ -74,7 +75,7 @@ class NativeDNSResolverOverride : public nsINativeDNSResolverOverride {
virtual ~NativeDNSResolverOverride() = default;
mozilla::RWLock mLock{"NativeDNSResolverOverride"};
nsTHashMap<nsCStringHashKey, nsTArray<PRNetAddr>> mOverrides;
nsTHashMap<nsCStringHashKey, nsTArray<NetAddr>> mOverrides;
nsTHashMap<nsCStringHashKey, nsCString> mCnames;
friend bool FindAddrOverride(const nsACString& aHost, uint16_t aAddressFamily,

Просмотреть файл

@ -43,14 +43,9 @@ NativeDNSResolverOverrideParent::GetSingleton() {
NS_IMETHODIMP NativeDNSResolverOverrideParent::AddIPOverride(
const nsACString& aHost, const nsACString& aIPLiteral) {
PRNetAddr tempAddr;
// Unfortunately, PR_StringToNetAddr does not properly initialize
// the output buffer in the case of IPv6 input. See bug 223145.
memset(&tempAddr, 0, sizeof(PRNetAddr));
NetAddr tempAddr;
if (!aIPLiteral.Equals("N/A"_ns) &&
PR_StringToNetAddr(nsCString(aIPLiteral).get(), &tempAddr) !=
PR_SUCCESS) {
NS_FAILED(tempAddr.InitFromString(aIPLiteral))) {
return NS_ERROR_UNEXPECTED;
}

Просмотреть файл

@ -503,9 +503,8 @@ nsresult TRR::ReceivePush(nsIHttpChannel* pushed, nsHostRecord* pushedRec) {
uri->GetQuery(query);
}
PRNetAddr tempAddr;
if (NS_FAILED(DohDecodeQuery(query, mHost, mType)) ||
(PR_StringToNetAddr(mHost.get(), &tempAddr) == PR_SUCCESS)) { // literal
HostIsIPLiteral(mHost)) { // literal
LOG(("TRR::ReceivePush failed to decode %s\n", mHost.get()));
return NS_ERROR_UNEXPECTED;
}

Просмотреть файл

@ -901,13 +901,12 @@ bool nsDNSService::DNSForbiddenByActiveProxy(const nsACString& aHostname,
}
// We should avoid doing DNS when a proxy is in use.
PRNetAddr tempAddr;
NetAddr tempAddr;
if (StaticPrefs::network_proxy_type() ==
nsIProtocolProxyService::PROXYCONFIG_MANUAL &&
mHasSocksProxy && StaticPrefs::network_proxy_socks_remote_dns()) {
// Allow IP lookups through, but nothing else.
if (PR_StringToNetAddr(nsCString(aHostname).get(), &tempAddr) !=
PR_SUCCESS) {
if (!HostIsIPLiteral(aHostname)) {
return true;
}
}

Просмотреть файл

@ -24,7 +24,7 @@
#include "nsNetCID.h"
#include "nsNetUtil.h"
#include "nsServiceManagerUtils.h"
#include "prnetdb.h"
#include "mozilla/net/DNS.h"
namespace etld_dafsa {
@ -314,9 +314,7 @@ nsresult nsEffectiveTLDService::GetBaseDomainInternal(
}
// Check if we're dealing with an IPv4/IPv6 hostname, and return
PRNetAddr addr;
PRStatus result = PR_StringToNetAddr(aHostname.get(), &addr);
if (result == PR_SUCCESS) {
if (mozilla::net::HostIsIPLiteral(aHostname)) {
// Update the MRU table if in use.
if (entry) {
entry->Set(TLDCacheEntry{aHostname, ""_ns, NS_ERROR_HOST_IS_IP_ADDRESS});

Просмотреть файл

@ -937,15 +937,14 @@ already_AddRefed<nsHostRecord> nsHostResolver::InitLoopbackRecord(
RefPtr<nsHostRecord> rec = InitRecord(key);
nsTArray<NetAddr> addresses;
PRNetAddr prAddr;
memset(&prAddr, 0, sizeof(prAddr));
NetAddr addr;
if (key.af == PR_AF_INET || key.af == PR_AF_UNSPEC) {
MOZ_RELEASE_ASSERT(PR_StringToNetAddr("127.0.0.1", &prAddr) == PR_SUCCESS);
addresses.AppendElement(NetAddr(&prAddr));
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(addr.InitFromString("127.0.0.1"_ns)));
addresses.AppendElement(addr);
}
if (key.af == PR_AF_INET6 || key.af == PR_AF_UNSPEC) {
MOZ_RELEASE_ASSERT(PR_StringToNetAddr("::1", &prAddr) == PR_SUCCESS);
addresses.AppendElement(NetAddr(&prAddr));
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(addr.InitFromString("::1"_ns)));
addresses.AppendElement(addr);
}
RefPtr<AddrInfo> ai =
@ -990,17 +989,11 @@ nsresult nsHostResolver::ResolveHost(const nsACString& aHost,
}
// Used to try to parse to an IP address literal.
PRNetAddr tempAddr;
// Unfortunately, PR_StringToNetAddr does not properly initialize
// the output buffer in the case of IPv6 input. See bug 223145.
memset(&tempAddr, 0, sizeof(PRNetAddr));
if (IS_OTHER_TYPE(type) &&
(PR_StringToNetAddr(host.get(), &tempAddr) == PR_SUCCESS)) {
NetAddr tempAddr;
if (IS_OTHER_TYPE(type) && (NS_SUCCEEDED(tempAddr.InitFromString(host)))) {
// For by-type queries the host cannot be IP literal.
return NS_ERROR_UNKNOWN_HOST;
}
memset(&tempAddr, 0, sizeof(PRNetAddr));
RefPtr<nsResolveHostCallback> callback(aCallback);
// if result is set inside the lock, then we need to issue the
@ -1091,8 +1084,7 @@ nsresult nsHostResolver::ResolveHost(const nsACString& aHost,
LOG((" Using cached address for IP Literal [%s].\n", host.get()));
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_LITERAL);
result = rec;
} else if (addrRec &&
PR_StringToNetAddr(host.get(), &tempAddr) == PR_SUCCESS) {
} else if (addrRec && NS_SUCCEEDED(tempAddr.InitFromString(host))) {
// try parsing the host name as an IP address literal to short
// circuit full host resolution. (this is necessary on some
// platforms like Win9x. see bug 219376 for more details.)
@ -1100,8 +1092,7 @@ nsresult nsHostResolver::ResolveHost(const nsACString& aHost,
// ok, just copy the result into the host record, and be
// done with it! ;-)
addrRec->addr = MakeUnique<NetAddr>();
PRNetAddrToNetAddr(&tempAddr, addrRec->addr.get());
addrRec->addr = MakeUnique<NetAddr>(tempAddr);
// put reference to host record on stack...
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_LITERAL);
result = rec;

Просмотреть файл

@ -118,23 +118,10 @@ static void AddrToString(NetAddr& netAddr, nsACString& addrStr) {
static nsresult StringAndPortToNetAddr(nsACString& remoteAddrStr,
uint16_t remotePort, NetAddr* netAddr) {
memset(netAddr, 0, sizeof(*netAddr));
PRNetAddr remotePRAddr;
memset(&remotePRAddr, 0, sizeof(remotePRAddr));
PRStatus prRv =
PR_StringToNetAddr(remoteAddrStr.BeginReading(), &remotePRAddr);
MOZ_ASSERT(prRv == PR_SUCCESS);
if (prRv != PR_SUCCESS) {
if (NS_FAILED(netAddr->InitFromString(remoteAddrStr, remotePort))) {
return NS_ERROR_FAILURE;
}
PRNetAddrToNetAddr(&remotePRAddr, netAddr);
if (netAddr->raw.family == AF_INET6) {
netAddr->inet6.port = htons(remotePort);
} else {
netAddr->inet.port = htons(remotePort);
}
return NS_OK;
}

Просмотреть файл

@ -1820,9 +1820,9 @@ nsresult nsHttpChannel::ProcessSecurityHeaders() {
// If the channel is not a hostname, but rather an IP, do not process STS
// or PKP headers
PRNetAddr hostAddr;
if (PR_SUCCESS == PR_StringToNetAddr(asciiHost.get(), &hostAddr))
if (HostIsIPLiteral(asciiHost)) {
return NS_OK;
}
// mSecurityInfo may not always be present, and if it's not then it is okay
// to just disregard any security headers since we know nothing about the

Просмотреть файл

@ -524,16 +524,12 @@ bool nsHttpConnectionInfo::UsingProxy() {
}
bool nsHttpConnectionInfo::HostIsLocalIPLiteral() const {
PRNetAddr prAddr;
NetAddr netAddr;
// If the host/proxy host is not an IP address literal, return false.
if (ProxyHost()) {
if (PR_StringToNetAddr(ProxyHost(), &prAddr) != PR_SUCCESS) {
return false;
}
} else if (PR_StringToNetAddr(Origin(), &prAddr) != PR_SUCCESS) {
nsAutoCString host(ProxyHost() ? ProxyHost() : Origin());
if (NS_FAILED(netAddr.InitFromString(host))) {
return false;
}
NetAddr netAddr(&prAddr);
return netAddr.IsIPAddrLocal();
}

Просмотреть файл

@ -52,13 +52,12 @@ StaticRefPtr<nsHttpNTLMAuth> nsHttpNTLMAuth::gSingleton;
static bool IsNonFqdn(nsIURI* uri) {
nsAutoCString host;
PRNetAddr addr;
if (NS_FAILED(uri->GetAsciiHost(host))) return false;
if (NS_FAILED(uri->GetAsciiHost(host))) {
return false;
}
// return true if host does not contain a dot and is not an ip address
return !host.IsEmpty() && !host.Contains('.') &&
PR_StringToNetAddr(host.BeginReading(), &addr) != PR_SUCCESS;
return !host.IsEmpty() && !host.Contains('.') && !HostIsIPLiteral(host);
}
// Check to see if we should use our generic (internal) NTLM auth module.