Skip to content

Fix '+' character in query parameters is not encoded when remove query parameter #3799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

raccoonback
Copy link
Contributor

Description

When a + character is included in an HTTP query parameter, it is currently interpreted as a space in the BeforeFilterFunctions.removeRequestParameter(), RemoveRequestParameterGatewayFilterFactory, RewriteRequestParameterGatewayFilterFactory. This may result in the parameter being misinterpreted on the server side compared to its original intent.

In this PR, we have applied precise encoding based on StandardCharsets.UTF_8 or replaced the use of UriUtils.encodeQueryParams() with a MvcUtils.encodeQueryParams() or ServerWebExchangeUtils.encodeQueryParams() custom utility to ensure that the + character is correctly encoded as %2B.

Changes:

  • Replacing + with %2B after standard encoding
  • Added corresponding unit test

(The BeforeFilterFunctions.removeRequestParameter(), RemoveRequestParameterGatewayFilterFactory, RewriteRequestParameterGatewayFilterFactory is only present in the 4.1.x branch.)

For example.

[Remove query parameter]

  • Let's assume that we remove foo query parameter.
[AS-IS]
"http://localhost/path?foo=A&bar=B%2B" -> "http://localhost/path?bar=B " (The value of the 'bar' query parameter is encoded as a 'B '.)

[TO-BE]
"http://localhost/path?foo=A&bar=B%2B" -> "http://localhost/path?bar=B%2B"

[RewriteRequestParameterGatewayFilterFactory]

Signed-off-by: raccoonback <kosb15@naver.com>
Signed-off-by: raccoonback <kosb15@naver.com>
@@ -127,7 +128,8 @@ void removeRequestParameterWithEncodedRemainParameters() {

assertThat(result.param("foo")).isEmpty();
assertThat(result.param("baz[]")).isPresent().hasValue("qux[]");
assertThat(result.uri().toString()).hasToString("http://localhost/path?baz%5B%5D=qux%5B%5D");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this test was asserting for the presence of the bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanjbaxter
Correct. This test ensures that quux=corge+ is properly encoded.

@raccoonback raccoonback requested a review from ryanjbaxter May 20, 2025 23:33
@ryanjbaxter ryanjbaxter merged commit 6fc7ee7 into spring-cloud:4.1.x May 22, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants