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

Open Beginner friendly
#4,194 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
80/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java, spring

Research direction

Start with UaaAuthorizationEndpoint.encode() in server/src/main/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpoint.java, then review the existing redirect assertions in server/src/test/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpointTest.java. The issue describes the encoding change and provides a regression test case. Done when a + in state is returned as %2B and the test verifies the redirect URL.

Written by the indexing model from the issue text.

Description

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");
}
Dominant language
Java
Stars
1.6k
Forks
847
Avg merge
2d 1h
Merged PRs (30d)
48

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudfoundry/uaa

All issues in cloudfoundry/uaa

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.