mirror of
https://github.com/NotAShelf/watchdog.git
synced 2026-04-16 23:34:09 +00:00
internal/normalize: simplify IP parser; cleanup
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I7a860779a4fe49b8034d66f2abd910fc6a6a6964
This commit is contained in:
parent
98611ca452
commit
fd3a832f7b
2 changed files with 45 additions and 28 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package normalize
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
|
|
@ -21,35 +22,21 @@ func isInternalHost(hostname string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// Check if hostname is an IP address
|
||||
if ip := net.ParseIP(hostname); ip != nil {
|
||||
// Private IPv4 ranges (RFC1918)
|
||||
if strings.HasPrefix(hostname, "10.") ||
|
||||
strings.HasPrefix(hostname, "192.168.") ||
|
||||
strings.HasPrefix(hostname, "172.16.") ||
|
||||
strings.HasPrefix(hostname, "172.17.") ||
|
||||
strings.HasPrefix(hostname, "172.18.") ||
|
||||
strings.HasPrefix(hostname, "172.19.") ||
|
||||
strings.HasPrefix(hostname, "172.20.") ||
|
||||
strings.HasPrefix(hostname, "172.21.") ||
|
||||
strings.HasPrefix(hostname, "172.22.") ||
|
||||
strings.HasPrefix(hostname, "172.23.") ||
|
||||
strings.HasPrefix(hostname, "172.24.") ||
|
||||
strings.HasPrefix(hostname, "172.25.") ||
|
||||
strings.HasPrefix(hostname, "172.26.") ||
|
||||
strings.HasPrefix(hostname, "172.27.") ||
|
||||
strings.HasPrefix(hostname, "172.28.") ||
|
||||
strings.HasPrefix(hostname, "172.29.") ||
|
||||
strings.HasPrefix(hostname, "172.30.") ||
|
||||
strings.HasPrefix(hostname, "172.31.") {
|
||||
if ip.IsPrivate() {
|
||||
return true
|
||||
}
|
||||
|
||||
// IPv6 loopback and local
|
||||
if strings.HasPrefix(hostname, "::1") ||
|
||||
strings.HasPrefix(hostname, "fe80::") ||
|
||||
strings.HasPrefix(hostname, "fc00::") ||
|
||||
strings.HasPrefix(hostname, "fd00::") {
|
||||
// Additional localhost checks for IP formats
|
||||
if ip.IsLoopback() {
|
||||
return true
|
||||
}
|
||||
// Link-local addresses
|
||||
if ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,6 +108,36 @@ func TestExtractReferrerDomain(t *testing.T) {
|
|||
siteDomain: "example.com",
|
||||
want: "internal",
|
||||
},
|
||||
{
|
||||
name: "private IP 172.16.x (RFC1918)",
|
||||
referrer: "http://172.16.0.1/page",
|
||||
siteDomain: "example.com",
|
||||
want: "internal",
|
||||
},
|
||||
{
|
||||
name: "private IP 172.31.x (RFC1918 upper bound)",
|
||||
referrer: "http://172.31.255.1/page",
|
||||
siteDomain: "example.com",
|
||||
want: "internal",
|
||||
},
|
||||
{
|
||||
name: "private IP 172.20.x (middle of range)",
|
||||
referrer: "http://172.20.50.100/page",
|
||||
siteDomain: "example.com",
|
||||
want: "internal",
|
||||
},
|
||||
{
|
||||
name: "public IP 172.15.x (just outside private range)",
|
||||
referrer: "http://172.15.0.1/page",
|
||||
siteDomain: "example.com",
|
||||
want: "other", // not internal, but invalid TLD
|
||||
},
|
||||
{
|
||||
name: "public IP 172.32.x (just outside private range)",
|
||||
referrer: "http://172.32.0.1/page",
|
||||
siteDomain: "example.com",
|
||||
want: "other", // not internal, but invalid TLD
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue