Regression of #99: '+' in state parameter is corrupted in authorization redirect responses

Aperta Adatta ai principianti
#4,194 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
80/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java, spring

Direzione di ricerca

Inizia con UaaAuthorizationEndpoint.encode() in server/src/main/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpoint.java, quindi esamina le asserzioni di reindirizzamento esistenti in server/src/test/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpointTest.java. L’issue descrive la modifica della codifica e fornisce un caso di test di regressione. Il lavoro è completato quando un + in state viene restituito come %2B e il test verifica l’URL di reindirizzamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What version of UAA are you running?

Reproducible on current develop — the relevant code in
UaaAuthorizationEndpoint.encode() uses UriUtils.encodeQueryParam, which does not percent-encode +.

How are you deploying the UAA?
  • locally only using gradlew (also observed in a production deployment)
What did you do?

Started an authorization code flow where the client-generated state contains a
properly percent-encoded + (e.g. standard Base64 output):

GET /oauth/authorize?client_id=login&response_type=code&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback&state=a%2Bb
What did you expect to see?

The redirect back to the client should return the state with the + still
percent-encoded, so that form-urlencoded decoding restores the original value:

Location: https://example.com/callback?code=...&state=a%2Bb

RFC 6749 (Section 4.1.2 / Appendix B) requires authorization response parameters
to be serialized using the application/x-www-form-urlencoded format, in which a
literal + represents a space and a plus sign must be encoded as %2B.

What did you see instead?
Location: https://example.com/callback?code=...&state=a+b

Note the asymmetry: = is encoded (%3D) but + is not. The client's
form-urlencoded decoding (browsers, servlet containers, standard OAuth client
libraries) turns + into a space, so the client receives "a b" and state
validation fails, breaking the login flow.

Root cause: UaaAuthorizationEndpoint.encode() delegates to Spring's
UriUtils.encodeQueryParam, which implements lenient RFC 3986 query encoding.
+ is a sub-delim that is legal in a query string, so it is left as-is — but
it is ambiguous under the form-urlencoded rules mandated by RFC 6749.

This is a regression of #99 (fixed in 2014 via
spring-projects/spring-security-oauth#152, which used strict URI-variable
encoding). The bug returned when AuthorizationEndpoint was vendored into
UaaAuthorizationEndpoint and the redirect building switched to
UriUtils.encodeQueryParam. The existing test assertion
contains("access_token=TOKEN_VALUE+%3D") in UaaAuthorizationEndpointTest
pins the incorrect half-encoded behavior.

It rarely surfaces in practice because most clients generate state as
base64url/hex/UUID (no +), but any client using standard Base64 state is
broken.

Fix:

In class server/src/main/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpoint.java
Update the method as
private String encode(String value) {
// RFC 3986 allows a literal '+' in a query, but RFC 6749 redirect params
// are form-urlencoded where '+' means space - so encode '+' as '%2B'
// (https://github.com/cloudfoundry/uaa/issues/99)
return value == null ? null : UriUtils.encodeQueryParam(value, "UTF-8").replace("+", "%2B");
}

Test case
server/src/test/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpointTest.java
@Test
void approvePercentEncodesPlusSignInState() {
AuthorizationRequest authorizationRequest = getAuthorizationRequest(
"foo", "http://anywhere.com", "+Jse6b2trAEcoPElv+ftGQ==", "read", Collections.singleton("code"));
model.put(UaaAuthorizationEndpoint.AUTHORIZATION_REQUEST, authorizationRequest);
model.put(UaaAuthorizationEndpoint.ORIGINAL_AUTHORIZATION_REQUEST, uaaAuthorizationEndpoint.unmodifiableMap(authorizationRequest));

    Map<String, String> approvalParameters = new HashMap<>();
    approvalParameters.put("user_oauth_approval", "true");

    when(authorizationCodeServices.createAuthorizationCode(any(OAuth2Authentication.class))).thenReturn("code");

    View view = uaaAuthorizationEndpoint.approveOrDeny(approvalParameters, model, sessionStatus, principal);

    assertThat(view).isInstanceOf(RedirectView.class);
    assertThat(((RedirectView) view).getUrl())
            .contains("state=%2BJse6b2trAEcoPElv%2BftGQ%3D%3D");
}
Lingua principale
Java
Stelle
1.6k
Fork
847
Merge medio
2g 1h
PR unite (30g)
48

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudfoundry/uaa

Tutte le issue di cloudfoundry/uaa

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.