Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: consider /dns/localhost as private address #221

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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 @@
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
}

Check warning on line 124 in net/private.go

View check run for this annotation

Codecov / codecov/patch

net/private.go#L122-L124

Added lines #L122 - L124 were not covered by tests
}
for _, pd := range privateUseDomains {
if strings.HasSuffix(dnsAddr, pd) {
if isSubDomain(dnsAddr, pd) {
isPublic = false
break
}
Expand All @@ -130,6 +135,13 @@
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 {
sukunrt marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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