fix(op): Add mitigation for PKCE Downgrade Attack (#741)

* fix(op): Add mitigation for PKCE downgrade attack

* chore(op): add test for PKCE verification
This commit is contained in:
Ayato 2025-04-29 23:33:31 +09:00 committed by GitHub
parent 5913c5a074
commit 4f0ed79c0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 88 additions and 8 deletions

View file

@ -0,0 +1,75 @@
package op_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/oidc/v3/pkg/op"
)
func TestAuthorizeCodeChallenge(t *testing.T) {
tests := []struct {
name string
codeVerifier string
codeChallenge *oidc.CodeChallenge
want func(t *testing.T, err error)
}{
{
name: "missing both code_verifier and code_challenge",
codeVerifier: "",
codeChallenge: nil,
want: func(t *testing.T, err error) {
assert.Nil(t, err)
},
},
{
name: "valid code_verifier",
codeVerifier: "Hello World!",
codeChallenge: &oidc.CodeChallenge{
Challenge: "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
Method: oidc.CodeChallengeMethodS256,
},
want: func(t *testing.T, err error) {
assert.Nil(t, err)
},
},
{
name: "invalid code_verifier",
codeVerifier: "Hi World!",
codeChallenge: &oidc.CodeChallenge{
Challenge: "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
Method: oidc.CodeChallengeMethodS256,
},
want: func(t *testing.T, err error) {
assert.ErrorContains(t, err, "invalid code_verifier")
},
},
{
name: "code_verifier provided without code_challenge",
codeVerifier: "code_verifier",
codeChallenge: nil,
want: func(t *testing.T, err error) {
assert.ErrorContains(t, err, "code_verifier unexpectedly provided")
},
},
{
name: "empty code_verifier",
codeVerifier: "",
codeChallenge: &oidc.CodeChallenge{
Challenge: "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
Method: oidc.CodeChallengeMethodS256,
},
want: func(t *testing.T, err error) {
assert.ErrorContains(t, err, "code_verifier required")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := op.AuthorizeCodeChallenge(tt.codeVerifier, tt.codeChallenge)
tt.want(t, err)
})
}
}