Skip to content

Commit

Permalink
net: consider /dns/localhost as private address (#221)
Browse files Browse the repository at this point in the history
* manet: consider /dns/localhost as private address

* fix naming
  • Loading branch information
sukunrt authored Oct 12, 2023
1 parent a124954 commit f9a66bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
29 changes: 24 additions & 5 deletions net/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ var privateUseDomains = []string{
// MDNS
".local",

// RFC 6761: Users may assume that IPv4 and IPv6 address queries for localhost names will
// always resolve to the respective IP loopback address
".localhost",
// RFC 6761: No central authority for .test names
".test",
}

// RFC 6761: Users may assume that IPv4 and IPv6 address queries for localhost names will
// always resolve to the respective IP loopback address
const localHostDomain = ".localhost"

func init() {
Private4 = parseCIDR(privateCIDR4)
Private6 = parseCIDR(privateCIDR6)
Expand Down Expand Up @@ -112,14 +113,18 @@ func IsPublicAddr(a ma.Multiaddr) bool {
case ma.P_DNS, ma.P_DNS4, ma.P_DNS6, ma.P_DNSADDR:
dnsAddr := c.Value()
isPublic = true
if isSubdomain(dnsAddr, localHostDomain) {
isPublic = false
return false
}
for _, ud := range unResolvableDomains {
if strings.HasSuffix(dnsAddr, ud) {
if isSubdomain(dnsAddr, ud) {
isPublic = false
return false
}
}
for _, pd := range privateUseDomains {
if strings.HasSuffix(dnsAddr, pd) {
if isSubdomain(dnsAddr, pd) {
isPublic = false
break
}
Expand All @@ -130,6 +135,13 @@ func IsPublicAddr(a ma.Multiaddr) bool {
return isPublic
}

// isSubdomain checks if child is sub domain of parent. It also returns true if child and parent are
// the same domain.
// Parent must have a "." prefix.
func isSubdomain(child, parent string) bool {
return strings.HasSuffix(child, parent) || child == parent[1:]
}

// IsPrivateAddr returns true if the IP part of the mutiaddr is in a private network
func IsPrivateAddr(a ma.Multiaddr) bool {
isPrivate := false
Expand All @@ -141,6 +153,13 @@ func IsPrivateAddr(a ma.Multiaddr) bool {
isPrivate = inAddrRange(net.IP(c.RawValue()), Private4)
case ma.P_IP6:
isPrivate = inAddrRange(net.IP(c.RawValue()), Private6)
case ma.P_DNS, ma.P_DNS4, ma.P_DNS6, ma.P_DNSADDR:
dnsAddr := c.Value()
if isSubdomain(dnsAddr, localHostDomain) {
isPrivate = true
}
// We don't check for privateUseDomains because private use domains can
// resolve to public IP addresses
}
return false
})
Expand Down
10 changes: 10 additions & 0 deletions net/private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func TestIsPublicAddr(t *testing.T) {
isPublic: false,
isPrivate: false, // You can configure .local domains in local networks to return public addrs
},
{
addr: ma.StringCast("/dns/localhost/udp/1/quic-v1"),
isPublic: false,
isPrivate: true,
},
{
addr: ma.StringCast("/dns/a.localhost/tcp/1"),
isPublic: false,
isPrivate: true,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand Down

0 comments on commit f9a66bc

Please sign in to comment.