Skip to content

Commit 3cfb9fa

Browse files
committed
Fixes issue with sub in token claims
Signed-off-by: wassafshahzad <[email protected]>
1 parent 628aae3 commit 3cfb9fa

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

filters/auth/oidc_introspection.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ func (filter *oidcIntrospectionFilter) Request(ctx filters.FilterContext) {
131131
return
132132
}
133133

134-
sub, ok := token.Claims["sub"]
135-
if ok {
136-
authorized(ctx, sub.(string))
137-
} else {
138-
sub := token.Subject
139-
authorized(ctx, sub)
134+
sub, ok := token.Claims["sub"].(string)
135+
if !ok {
136+
unauthorized(ctx, sub, invalidSub, "", "")
137+
return
140138
}
139+
140+
authorized(ctx, sub)
141141
}
142142

143143
func (filter *oidcIntrospectionFilter) Response(filters.FilterContext) {}

filters/auth/oidc_introspection_test.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ func TestCreateOIDCQueryClaimsFilter(t *testing.T) {
139139

140140
func TestOIDCQueryClaimsFilter(t *testing.T) {
141141
for _, tc := range []struct {
142-
msg string
143-
path string
144-
expected int
145-
expectErr bool
146-
args []interface{}
142+
msg string
143+
path string
144+
expected int
145+
expectErr bool
146+
args []interface{}
147+
removeClaims []string
147148
}{
148149
{
149150
msg: "secure sub/path not permitted",
@@ -165,6 +166,17 @@ func TestOIDCQueryClaimsFilter(t *testing.T) {
165166
expected: 200,
166167
expectErr: false,
167168
},
169+
{
170+
msg: "secure sub/path is not permitted",
171+
args: []interface{}{
172+
"/login:groups.#[==\"AppX-Test-Users\"]",
173+
"/:@_:email%\"*@example.org\"",
174+
},
175+
path: "/login/page",
176+
expected: 401,
177+
expectErr: false,
178+
removeClaims: []string{"sub"},
179+
},
168180
{
169181
msg: "generic user path permitted",
170182
args: []interface{}{
@@ -292,7 +304,7 @@ func TestOIDCQueryClaimsFilter(t *testing.T) {
292304
t.Errorf("Failed to parse url %s: %v", proxy.URL, err)
293305
}
294306
reqURL.Path = tc.path
295-
oidcServer := createOIDCServer(proxy.URL+"/redirect", validClient, "mysec", jwt.MapClaims{"groups": []string{"CD-Administrators", "Purchasing-Department", "AppX-Test-Users", "white space"}})
307+
oidcServer := createOIDCServer(proxy.URL+"/redirect", validClient, "mysec", jwt.MapClaims{"groups": []string{"CD-Administrators", "Purchasing-Department", "AppX-Test-Users", "white space"}}, tc.removeClaims)
296308
defer oidcServer.Close()
297309
t.Logf("oidc/auth server URL: %s", oidcServer.URL)
298310
// create filter

filters/auth/oidc_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ var testOpenIDConfig = `{
127127
// returns a localhost instance implementation of an OpenID Connect
128128
// server with configendpoint, tokenendpoint, authenticationserver endpoint, userinfor
129129
// endpoint, jwks endpoint
130-
func createOIDCServer(cb, client, clientsecret string, extraClaims jwt.MapClaims) *httptest.Server {
130+
func createOIDCServer(cb, client, clientsecret string, extraClaims jwt.MapClaims, removeClaims []string) *httptest.Server {
131131
var oidcServer *httptest.Server
132132
oidcServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
133133
switch r.URL.Path {
@@ -233,6 +233,11 @@ func createOIDCServer(cb, client, clientsecret string, extraClaims jwt.MapClaims
233233
for k, v := range extraClaims {
234234
claims[k] = v
235235
}
236+
237+
for _, k := range removeClaims {
238+
delete(claims, k)
239+
}
240+
236241
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
237242

238243
privKey, err := os.ReadFile(keyPath)
@@ -557,7 +562,7 @@ func TestNewOidc(t *testing.T) {
557562
}
558563

559564
func TestCreateFilterOIDC(t *testing.T) {
560-
oidcServer := createOIDCServer("", "", "", nil)
565+
oidcServer := createOIDCServer("", "", "", nil, nil)
561566
defer oidcServer.Close()
562567

563568
for _, tt := range []struct {
@@ -900,7 +905,7 @@ func TestOIDCSetup(t *testing.T) {
900905

901906
t.Logf("redirect URL: %s", redirectURL.String())
902907

903-
oidcServer := createOIDCServer(redirectURL.String(), "valid-client", "mysec", tc.extraClaims)
908+
oidcServer := createOIDCServer(redirectURL.String(), "valid-client", "mysec", tc.extraClaims, nil)
904909
defer oidcServer.Close()
905910
t.Logf("oidc server URL: %s", oidcServer.URL)
906911

0 commit comments

Comments
 (0)