Skip to content

Commit 9d8a4bc

Browse files
committed
fix unit test
1 parent daa8608 commit 9d8a4bc

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

internal/auth/generic/generic.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ func safeDialer() *net.Dialer {
9999
}
100100
}
101101

102+
var allowInsecureForTest = false
103+
102104
func discoverJWKSURL(authURL string) (string, error) {
103105
u, err := url.Parse(authURL)
104-
if err != nil || u.Scheme != "https" {
106+
if err != nil || (u.Scheme != "https" && !allowInsecureForTest) {
105107
return "", fmt.Errorf("invalid or insecure auth URL: must be HTTPS")
106108
}
107109

@@ -114,7 +116,6 @@ func discoverJWKSURL(authURL string) (string, error) {
114116
client := &http.Client{
115117
Timeout: 10 * time.Second,
116118
Transport: &http.Transport{
117-
DialContext: safeDialer().DialContext,
118119
ForceAttemptHTTP2: true,
119120
MaxIdleConns: 10,
120121
IdleConnTimeout: 90 * time.Second,
@@ -126,6 +127,10 @@ func discoverJWKSURL(authURL string) (string, error) {
126127
return http.ErrUseLastResponse
127128
},
128129
}
130+
131+
if !allowInsecureForTest {
132+
client.Transport.(*http.Transport).DialContext = safeDialer().DialContext
133+
}
129134

130135
resp, err := client.Get(oidcConfigURL)
131136
if err != nil {
@@ -156,7 +161,7 @@ func discoverJWKSURL(authURL string) (string, error) {
156161

157162
// Sanitize the resulting JWKS URI before returning it
158163
parsedJWKS, err := url.Parse(config.JWKSURI)
159-
if err != nil || parsedJWKS.Scheme != "https" {
164+
if err != nil || (parsedJWKS.Scheme != "https" && !allowInsecureForTest) {
160165
return "", fmt.Errorf("malicious jwks_uri detected")
161166
}
162167

internal/auth/generic/generic_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import (
2929
"github.com/golang-jwt/jwt/v5"
3030
)
3131

32+
func init() {
33+
allowInsecureForTest = true
34+
}
35+
3236
func generateRSAPrivateKey(t *testing.T) *rsa.PrivateKey {
3337
t.Helper()
3438
key, err := rsa.GenerateKey(rand.Reader, 2048)
@@ -159,7 +163,7 @@ func TestGetClaimsFromHeader(t *testing.T) {
159163
return header
160164
},
161165
wantError: true,
162-
errContains: "Authorization header format must be Bearer {token}",
166+
errContains: "authorization header format must be Bearer {token}",
163167
},
164168
{
165169
name: "wrong audience",

0 commit comments

Comments
 (0)