-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix support for punycode and non-default ports in OIDC Discovery Prov…
…ider (#2453) This change fixes the recently added domain validation in the OIDC Discovery Provider to adequately accomodate punycode and properly account for host:port values in the `Host` field. It also rewords some of the error messages produced by misconfiguration. Signed-off-by: Andrew Harding <[email protected]>
- Loading branch information
Showing
8 changed files
with
228 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/net/idna" | ||
) | ||
|
||
type DomainPolicy = func(domain string) error | ||
|
||
// DomainAllowlist returns a policy that allows any domain in the given domains | ||
func DomainAllowlist(domains ...string) (DomainPolicy, error) { | ||
allowlist := make(map[string]struct{}, len(domains)) | ||
for _, domain := range domains { | ||
domainKey, err := toDomainKey(domain) | ||
if err != nil { | ||
return nil, err | ||
} | ||
allowlist[domainKey] = struct{}{} | ||
} | ||
return func(domain string) error { | ||
domainKey, err := toDomainKey(domain) | ||
if err != nil { | ||
return err | ||
} | ||
if _, allowed := allowlist[domainKey]; !allowed { | ||
return fmt.Errorf("domain %q is not allowed", domain) | ||
} | ||
return nil | ||
}, nil | ||
} | ||
|
||
// AllowAnyDomain returns a policy that allows any domain | ||
func AllowAnyDomain() DomainPolicy { | ||
return func(domain string) error { | ||
_, err := toDomainKey(domain) | ||
return err | ||
} | ||
} | ||
|
||
func toDomainKey(domain string) (string, error) { | ||
punycode, err := idna.Lookup.ToASCII(domain) | ||
if err != nil { | ||
return "", fmt.Errorf("domain %q is not a valid domain name: %w", domain, err) | ||
} | ||
if punycode != domain { | ||
return "", fmt.Errorf("domain %q must already be punycode encoded", domain) | ||
} | ||
return domain, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDomainAllowlist(t *testing.T) { | ||
t.Run("unicode", func(t *testing.T) { | ||
_, err := DomainAllowlist("😬.test") | ||
assert.EqualError(t, err, `domain "😬.test" must already be punycode encoded`) | ||
}) | ||
|
||
t.Run("punycode", func(t *testing.T) { | ||
policy, err := DomainAllowlist("xn--n38h.test") | ||
require.NoError(t, err) | ||
assert.EqualError(t, policy("😬.test"), `domain "😬.test" must already be punycode encoded`) | ||
assert.NoError(t, policy("xn--n38h.test")) | ||
assert.EqualError(t, policy("bad.test"), `domain "bad.test" is not allowed`) | ||
}) | ||
|
||
t.Run("ascii", func(t *testing.T) { | ||
policy, err := DomainAllowlist("ascii.test") | ||
require.NoError(t, err) | ||
assert.NoError(t, policy("ascii.test")) | ||
assert.EqualError(t, policy("bad.test"), `domain "bad.test" is not allowed`) | ||
}) | ||
|
||
t.Run("invalid domain in config", func(t *testing.T) { | ||
_, err := DomainAllowlist("invalid/domain.test") | ||
assert.EqualError(t, err, `domain "invalid/domain.test" is not a valid domain name: idna: disallowed rune U+002F`) | ||
}) | ||
|
||
t.Run("invalid domain on lookup", func(t *testing.T) { | ||
policy, err := DomainAllowlist() | ||
require.NoError(t, err) | ||
assert.EqualError(t, policy("invalid/domain.test"), `domain "invalid/domain.test" is not a valid domain name: idna: disallowed rune U+002F`) | ||
}) | ||
} | ||
|
||
func TestAllowAnyDomain(t *testing.T) { | ||
policy := AllowAnyDomain() | ||
assert.NoError(t, policy("foo")) | ||
assert.NoError(t, policy("bar")) | ||
assert.NoError(t, policy("baz")) | ||
assert.EqualError(t, policy("invalid/domain.test"), `domain "invalid/domain.test" is not a valid domain name: idna: disallowed rune U+002F`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.