spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

OAuth2 Authorization Server accepts malformed PKCE code challenges

Open gtaylor-exactsciences opened this issue 1 month ago • 3 comments

Describe the bug

Spring Authorization Server accepts malformed S256 code_challenge values at the authorization endpoint. With an authenticated user, a challenge that is too short or contains an invalid character results in an authorization code. With an anonymous user, a too-short challenge proceeds to the login redirect. This is not a a PKCE bypass.

To Reproduce

Clone the sample below and run mvn test. It uses Spring Boot 4.1.1, which manages Spring Security 7.1.1, and the default authorization server configuration. The suite has one passing valid-challenge control and three failing malformed-challenge assertions.

Expected behavior

Reject a malformed code_challenge as invalid_request during authorization request validation, regardless of whether the user is authenticated. Rejection should happen before either issuing a code or redirecting to login.

RFC 7636 §4.2 defines the challenge’s length and allowed characters. RFC 6749 §4.1.1 places request validation before user authentication, and §4.1.2.1 defines invalid_request for an invalid parameter value. The Spring Security 7.1.1 challenge validator checks presence and method, but not challenge length or characters.

Sample

Spring-Auth-Server-PKCE-Challenge-Validation-Repro

I’m happy to contribute a fix and tests if desired.

Acknowledgement: AI coding assistance was used in preparing this report.

gtaylor-exactsciences avatar Oct 07 '26 20:10 gtaylor-exactsciences

Hi @gtaylor-exactsciences — thanks for the clear report and repro.

If you are not already starting on a PR, I would like to take this: tighten OAuth2AuthorizationCodeRequestAuthenticationValidator to enforce RFC 7636 §4.2 length (43–128) and unreserved charset for code_challenge, reject with invalid_request before login/code issuance, and add regression tests covering the cases in your sample.

Please say if you prefer to keep it; otherwise I will open a PR shortly.

L00PLeGeNd avatar Oct 15 '26 11:10 L00PLeGeNd

2. Hi @gtaylor-exactsciences — thanks for the clear report and repro. If you are not already starting on a PR, I would like to take this: tighten OAuth2AuthorizationCodeRequestAuthenticationValidator to enforce RFC 7636 §4.2 length (43–128) and unreserved charset for code_challenge, reject with invalid_request before login/code issuance, and add regression tests covering the cases in your sample. Please say if you prefer to keep it; otherwise I will open a PR shortly.

@L00PLeGeNd I'm happy for you to resolve. Thank you!

gtaylor-exactsciences avatar Oct 15 '26 12:10 gtaylor-exactsciences

@gtaylor-exactsciences Thank you — much appreciated.

I opened #19890 with the RFC 7636 length/charset checks and regression tests. Happy to adjust if you or the maintainers have feedback on the validation details.

L00PLeGeNd avatar Oct 15 '26 13:10 L00PLeGeNd