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

オープン 初心者向け
#4,194 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
80/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java, spring

調査の方向性

server/src/main/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpoint.java の UaaAuthorizationEndpoint.encode() から始め、次に server/src/test/java/org/cloudfoundry/identity/uaa/oauth/UaaAuthorizationEndpointTest.java にある既存のリダイレクトに関するアサーションを確認してください。この issue にはエンコーディングの変更が記載され、回帰テストケースが示されています。state 内の + が %2B として返され、テストでリダイレクト URL が検証されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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");
}
主要言語
Java
スター
1.6k
フォーク
847
平均マージ
2日 1時間
マージ済み PR(30日)
48

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudfoundry/uaa のほかの issue

cloudfoundry/uaa の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。