Move IP check

This commit is contained in:
Neel Chauhan 2024-10-17 19:24:34 -04:00
parent 990d165937
commit f1e9f58b47

View file

@ -674,6 +674,14 @@ func checkIsRelayURLAcceptable(
if util.IsHostnameLocal(hostname) { if util.IsHostnameLocal(hostname) {
return fmt.Errorf("rejected Relay URL: private hostnames are not allowed") return fmt.Errorf("rejected Relay URL: private hostnames are not allowed")
} }
ip := net.ParseIP(hostname)
// Otherwise it's a domain name, or an invalid IP.
if ip != nil {
// We should probably use a ready library for this.
if !isRemoteAddress(ip) {
return fmt.Errorf("rejected Relay URL: private IPs are not allowed")
}
} else {
ipArray, err := net.LookupIP(hostname) ipArray, err := net.LookupIP(hostname)
if err != nil { if err != nil {
return fmt.Errorf("Could not look up IP") return fmt.Errorf("Could not look up IP")
@ -683,15 +691,6 @@ func checkIsRelayURLAcceptable(
return fmt.Errorf("rejected Relay URL: private IPs are not allowed") return fmt.Errorf("rejected Relay URL: private IPs are not allowed")
} }
} }
ip := net.ParseIP(hostname)
// Otherwise it's a domain name, or an invalid IP.
if ip != nil {
// We should probably use a ready library for this.
if !isRemoteAddress(ip) {
return fmt.Errorf("rejected Relay URL: private IPs are not allowed")
}
} else {
// move net.LookupIP(hostname) and isRemoteAddress checks here
} }
} }
if !allowNonTLSRelay && parsedRelayURL.Scheme != "wss" { if !allowNonTLSRelay && parsedRelayURL.Scheme != "wss" {