mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: reject PKCE code_verifier below RFC 7636 length floor (#28003)
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>
This commit is contained in:
@@ -163,17 +163,31 @@ func validateRedirectURIs(uris []string, tokenEndpointAuthMethod OAuth2TokenEndp
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Custom scheme validation for public clients (RFC 8252 section 7.1)
|
||||
if isPublicClient {
|
||||
// For public clients, custom schemes should follow RFC 8252 recommendations
|
||||
// Should be reverse domain notation based on domain under their control
|
||||
if !isValidCustomScheme(uri.Scheme) {
|
||||
return xerrors.Errorf("redirect URI at index %d: custom scheme %s should use reverse domain notation (e.g. com.example.app)", i, uri.Scheme)
|
||||
}
|
||||
} else if isPublicClient {
|
||||
// mailto, tel, and sms hand off to a mail client, dialer, or SMS
|
||||
// app rather than returning control to the application that
|
||||
// started the flow. A public client has no other way to obtain
|
||||
// its authorization code, so registering one of these would
|
||||
// produce a client that can never complete authorization.
|
||||
//
|
||||
// This check runs only for public clients because that is how
|
||||
// custom-scheme validation was scoped before this change, not
|
||||
// because these three schemes are known to be safe for a
|
||||
// confidential client's redirect; confidential clients were
|
||||
// never subject to any scheme-shape check beyond validateScheme
|
||||
// and remain so here.
|
||||
switch uri.Scheme {
|
||||
case "mailto", "tel", "sms":
|
||||
return xerrors.Errorf("redirect URI at index %d: public clients may not use the %s scheme", i, uri.Scheme)
|
||||
}
|
||||
// For confidential clients, custom schemes are less common but allowed
|
||||
}
|
||||
// Beyond that, custom schemes need no further check: validateScheme
|
||||
// already blocked the ones that are dangerous in a redirect context,
|
||||
// and RFC 8252 §7.1 only recommends reverse-domain notation rather
|
||||
// than requiring it. Rejecting bare schemes such as vscode:// or
|
||||
// jetbrains:// would penalize the native and CLI apps this client
|
||||
// type exists for; PKCE, not the scheme's spelling, is what secures
|
||||
// the redirect.
|
||||
|
||||
// Prevent URI fragments (RFC 6749 section 3.1.2)
|
||||
if uri.Fragment != "" || strings.Contains(uriStr, "#") {
|
||||
@@ -295,22 +309,3 @@ func isLoopbackAddress(hostname string) bool {
|
||||
hostname == "127.0.0.1" ||
|
||||
hostname == "::1"
|
||||
}
|
||||
|
||||
// isValidCustomScheme validates custom schemes for public clients (RFC 8252)
|
||||
func isValidCustomScheme(scheme string) bool {
|
||||
// For security and RFC compliance, require reverse domain notation
|
||||
// Should contain at least one period and not be a well-known scheme
|
||||
if !strings.Contains(scheme, ".") {
|
||||
return false
|
||||
}
|
||||
|
||||
// Block schemes that look like well-known protocols
|
||||
wellKnownSchemes := []string{"http", "https", "ftp", "mailto", "tel", "sms"}
|
||||
for _, wellKnown := range wellKnownSchemes {
|
||||
if strings.EqualFold(scheme, wellKnown) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user