pomerium/internal/urlutil/whitelist.go
Travis Groth c7d243d742
proxy: restrict programmatic URLs to localhost (#2049)
Co-authored-by: Caleb Doxsey <cdoxsey@pomerium.com>
2021-04-01 10:04:49 -04:00

35 lines
765 B
Go

package urlutil
import (
"net"
"net/url"
)
// IsRedirectAllowed returns true if the redirect URL is whitelisted.
func IsRedirectAllowed(redirectURL *url.URL, whitelistDomains []string) bool {
if !(redirectURL.Scheme == "http" || redirectURL.Scheme == "https") {
return false
}
for _, domain := range whitelistDomains {
if domain == "localhost" && IsLoopback(redirectURL) {
return true
} else if redirectURL.Hostname() == domain {
return true
}
}
return false
}
// IsLoopback returns true if the given URL corresponds with a loopback address.
func IsLoopback(u *url.URL) bool {
hostname := u.Hostname()
if hostname == "localhost" {
return true
}
if ip := net.ParseIP(hostname); ip != nil {
return ip.IsLoopback()
}
return false
}