Skip to content

Commit

Permalink
improve loopback check; add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
fairclothjm committed Mar 13, 2024
1 parent fffbc6c commit ed6ec0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion path_oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -537,6 +538,17 @@ func (b *jwtAuthBackend) verifyOIDCRequest(stateID string) *oidcRequest {
return nil
}

func isLoopbackAddress(hostname string) bool {
ip := net.ParseIP(hostname)
if ip != nil {
return ip.IsLoopback()
} else {
// localhost is not guaranteed to map back to a loopback interface address
// however, this is historically how the plugin has behaved
return hostname == "localhost"
}
}

// validRedirect checks whether uri is in allowed using special handling for loopback uris.
// Ref: https://tools.ietf.org/html/rfc8252#section-7.3
func validRedirect(uri string, allowed []string) bool {
Expand All @@ -546,7 +558,7 @@ func validRedirect(uri string, allowed []string) bool {
}

// if uri isn't a loopback, just string search the allowed list
if !strutil.StrListContains([]string{"localhost", "127.0.0.1", "::1"}, inputURI.Hostname()) {
if !isLoopbackAddress(inputURI.Hostname()) {
return strutil.StrListContainsCaseInsensitive(allowed, uri)
}

Expand Down
1 change: 1 addition & 0 deletions path_oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ func TestOIDC_ValidRedirect(t *testing.T) {
{"https://example.com/a/b/c", []string{"a", "b", "https://example.com/a/b/c"}, true},
{"https://localhost:9000", []string{"a", "b", "https://localhost:5000"}, true},
{"https://127.0.0.1:9000", []string{"a", "b", "https://127.0.0.1:5000"}, true},
{"https://127.0.0.2:9000", []string{"a", "b", "https://127.0.0.2:5000"}, true},
{"https://[::1]:9000", []string{"a", "b", "https://[::1]:5000"}, true},
{"https://[::1]:9000/x/y?r=42", []string{"a", "b", "https://[::1]:5000/x/y?r=42"}, true},
{"https://EXAMPLE.com:5000", []string{"a", "b", "https://example.com:5000"}, true},
Expand Down

0 comments on commit ed6ec0d

Please sign in to comment.