mirror of
https://github.com/coder/coder.git
synced 2026-10-31 19:36:41 +08:00
The token endpoint accepted any non-empty `code_verifier`, so a one-character verifier was enough to authenticate. RFC 7636 §4.1 requires 43 to 128 characters from the unreserved set. That fix plus the related gaps review surfaced in the same path: - Enforce the length and charset floor on the verifier before the S256 comparison runs. - Validate the challenge at the authorize endpoint too. It was only checked for non-emptiness, so a malformed challenge was stored and then failed late at token exchange, blaming the wrong parameter. - A malformed verifier now returns `invalid_request` (RFC 6749 §5.2); a well-formed but wrong one still returns `invalid_grant` (RFC 7636 §4.6). Both looked identical before, so a client had no way to tell a syntax error from a hash mismatch and would retry the same bad verifier forever. - Revoke the authorization code when a PKCE check fails. Without that, a leaked code could be replayed with unlimited verifier guesses for its remaining lifetime, and RFC 6749 §10.5 requires codes to be single use. - Fix verifier generation in `scripts/oauth2/*.sh` and the docs example. They deleted reserved base64 characters instead of translating them to the URL-safe alphabet, so most runs produced verifiers under the new floor. Also carries #28041, which merged into this branch: public clients may register bare custom schemes such as `vscode://` again, with `mailto`, `tel`, and `sms` rejected. Split out of #27873 (public OAuth2 client support). PKCE is already mandatory for every client, so this stands on its own. <details> <summary>Manual verification</summary> Ran against a local dev server on this branch, using a session token and a throwaway app from `scripts/oauth2/setup-test-app.sh`. 1. Happy path unchanged: HTTP 200, verifier length 43. 2. `code_verifier=short`, and a 43-character verifier ending in `!`: both HTTP 400 `invalid_request`, so charset is enforced and not just length. 3. `code_challenge=tooshort` at authorize: HTTP 400 `invalid_request`, no code issued. An empty challenge still hits the older "required and cannot be empty" message. 4. Well-formed but wrong verifier: HTTP 400 `invalid_grant`, distinct from the cases above. 5. Retrying that same code with the correct verifier: HTTP 400, code already revoked by the failed check. 6. `generate-pkce.sh` produces a 43-character verifier (20 out of 20 runs); the docs example produces 128. 7. `scripts/oauth2/test-mcp-oauth2.sh` passes end to end. The two bearer-token failures in its output are a pre-existing script bug (`09c50559f3`, July 2025) that reuses a resource-scoped token against the real API, not a regression here. </details>
201 lines
4.3 KiB
Go
201 lines
4.3 KiB
Go
package oauth2provider_test
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/oauth2provider"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func TestVerifyPKCE(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
verifier string
|
|
challenge string
|
|
expectValid bool
|
|
}{
|
|
{
|
|
name: "ValidPKCE",
|
|
verifier: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
|
|
challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
|
expectValid: true,
|
|
},
|
|
{
|
|
name: "InvalidPKCE",
|
|
verifier: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
|
|
challenge: "wrong_challenge",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "EmptyChallenge",
|
|
verifier: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
|
|
challenge: "",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "EmptyVerifier",
|
|
verifier: "",
|
|
challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "BothEmpty",
|
|
verifier: "",
|
|
challenge: "",
|
|
expectValid: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
result := oauth2provider.VerifyPKCE(tt.challenge, tt.verifier)
|
|
require.Equal(t, tt.expectValid, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPKCES256Generation(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Test that we can generate a valid S256 challenge from a verifier
|
|
verifier := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
|
|
expectedChallenge := "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
|
|
|
|
// Generate challenge using S256 method
|
|
h := sha256.Sum256([]byte(verifier))
|
|
challenge := base64.RawURLEncoding.EncodeToString(h[:])
|
|
|
|
require.Equal(t, expectedChallenge, challenge)
|
|
require.True(t, oauth2provider.VerifyPKCE(challenge, verifier))
|
|
}
|
|
|
|
func TestValidatePKCECodeChallengeMethod(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
method string
|
|
expectError bool
|
|
errorContains string
|
|
}{
|
|
{
|
|
name: "EmptyIsValid",
|
|
method: "",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "S256IsValid",
|
|
method: string(codersdk.OAuth2PKCECodeChallengeMethodS256),
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "PlainIsRejected",
|
|
method: string(codersdk.OAuth2PKCECodeChallengeMethodPlain),
|
|
expectError: true,
|
|
errorContains: "plain",
|
|
},
|
|
{
|
|
name: "UnknownIsRejected",
|
|
method: "unknown_method",
|
|
expectError: true,
|
|
errorContains: "unsupported",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
err := codersdk.ValidatePKCECodeChallengeMethod(tt.method)
|
|
if tt.expectError {
|
|
require.Error(t, err)
|
|
if tt.errorContains != "" {
|
|
require.Contains(t, err.Error(), tt.errorContains)
|
|
}
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidPKCEFormat(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
verifier string
|
|
expectValid bool
|
|
}{
|
|
{
|
|
name: "Empty",
|
|
verifier: "",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "OneCharacter",
|
|
verifier: "a",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "OneBelowMinLength",
|
|
verifier: strings.Repeat("a", 42),
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "AtMinLength",
|
|
verifier: strings.Repeat("a", 43),
|
|
expectValid: true,
|
|
},
|
|
{
|
|
name: "AtMaxLength",
|
|
verifier: strings.Repeat("a", 128),
|
|
expectValid: true,
|
|
},
|
|
{
|
|
name: "OneAboveMaxLength",
|
|
verifier: strings.Repeat("a", 129),
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "AllowedCharacters",
|
|
verifier: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",
|
|
expectValid: true,
|
|
},
|
|
{
|
|
name: "PlusIsRejected",
|
|
verifier: strings.Repeat("a", 42) + "+",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "SlashIsRejected",
|
|
verifier: strings.Repeat("a", 42) + "/",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "EqualsIsRejected",
|
|
verifier: strings.Repeat("a", 42) + "=",
|
|
expectValid: false,
|
|
},
|
|
{
|
|
name: "SpaceIsRejected",
|
|
verifier: strings.Repeat("a", 42) + " ",
|
|
expectValid: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
require.Equal(t, tt.expectValid, oauth2provider.ValidPKCEFormat(tt.verifier))
|
|
})
|
|
}
|
|
}
|