mirror of
https://github.com/netbirdio/dex.git
synced 2026-05-22 18:43:53 -07:00
feat: also allow localhost equivalent IP addresses (#3778)
Instead of only checking for "localhost", also validate through net.ParseIP + IsLoopback whether the host is numerically localhost Signed-off-by: Daniel Sonck <daniel@sonck.nl>
This commit is contained in:
+14
-4
@@ -668,7 +668,8 @@ func validateRedirectURI(client storage.Client, redirectURI string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// verify that the host is of form "http://localhost:(port)(path)" or "http://localhost(path)"
|
||||
// verify that the host is of form "http://localhost:(port)(path)", "http://localhost(path)" or numeric form like
|
||||
// "http://127.0.0.1:(port)(path)"
|
||||
u, err := url.Parse(redirectURI)
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -676,11 +677,20 @@ func validateRedirectURI(client storage.Client, redirectURI string) bool {
|
||||
if u.Scheme != "http" {
|
||||
return false
|
||||
}
|
||||
if u.Host == "localhost" {
|
||||
return isHostLocal(u.Host)
|
||||
}
|
||||
|
||||
func isHostLocal(host string) bool {
|
||||
if host == "localhost" || net.ParseIP(host).IsLoopback() {
|
||||
return true
|
||||
}
|
||||
host, _, err := net.SplitHostPort(u.Host)
|
||||
return err == nil && host == "localhost"
|
||||
|
||||
host, _, err := net.SplitHostPort(host)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return host == "localhost" || net.ParseIP(host).IsLoopback()
|
||||
}
|
||||
|
||||
func validateConnectorID(connectors []storage.Connector, connectorID string) bool {
|
||||
|
||||
@@ -452,6 +452,27 @@ func TestValidRedirectURI(t *testing.T) {
|
||||
redirectURI: "http://localhost",
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
client: storage.Client{
|
||||
Public: true,
|
||||
},
|
||||
redirectURI: "http://127.0.0.1:8080/",
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
client: storage.Client{
|
||||
Public: true,
|
||||
},
|
||||
redirectURI: "http://127.0.0.1:991/bar",
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
client: storage.Client{
|
||||
Public: true,
|
||||
},
|
||||
redirectURI: "http://127.0.0.1",
|
||||
wantValid: true,
|
||||
},
|
||||
// Both Public + RedirectURIs configured: Could e.g. be a PKCE-enabled web app.
|
||||
{
|
||||
client: storage.Client{
|
||||
|
||||
Reference in New Issue
Block a user