Skip to content
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

Prevent duplicated encoding in mvc #3658

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

raccoonback
Copy link

@raccoonback raccoonback commented Jan 5, 2025

Motivation

There were issues with the setPath() and stripPrefix() methods where already encoded URIs were being double-encoded.

[setPath()]
request uri: http://localhost/legacy/path

-> setPath("new é")

result: http://localhost/new/%25C3%25A9 

[setPath()]
request uri: http://localhost/depth1/depth2/depth3/é

-> stripPrefix(2)

result: http://localhost/depth3/%25C3%25A9 

Also, the removeRequestParameter() method was also double-encoding not only request parameters but also paths and other components.

[removeRequestParameter()]

request uri: http://localhost/path/é/last?foo=bar

-> removeRequestParameter("foo")

result: http://localhost/path/%25C3%25A9/last

Key Changes

  • setPath() and stripPrefix() methods have been fixed to prevent double encoding.
[setPath()]
request uri: http://localhost/legacy/path

-> setPath("new é")

result: http://localhost/new/%C3%A9

[setPath()]
request uri: http://localhost/depth1/depth2/depth3/é

-> stripPrefix(2)

result: http://localhost/depth3/%C3%A9
  • removeRequestParameter() has been updated to ensure that only request parameters are encoded.
[removeRequestParameter()]

request uri: http://localhost/path/é/last?foo=bar

-> removeRequestParameter("foo")

result: http://localhost/path/%C3%A9/last

Reference

related with #3131

The setPath method was causing URLs to be encoded twice under certain conditions.
This fix ensures proper handling of parameters to avoid redundant encoding.
The stripPrefix method was causing URLs to be encoded twice under certain conditions.
This fix ensures proper handling of parameters to avoid redundant encoding.
The removeRequestParameter method was causing URLs to be encoded twice under certain conditions.
This fix ensures proper handling of parameters to avoid redundant encoding.
Comment on lines +218 to +223
MultiValueMap<String, String> encodedQueryParams = UriUtils.encodeQueryParams(queryParams);

// remove from uri
URI newUri = UriComponentsBuilder.fromUri(request.uri())
.replaceQueryParams(unmodifiableMultiValueMap(queryParams))
.build()
.replaceQueryParams(unmodifiableMultiValueMap(encodedQueryParams))
.build(true)
Copy link
Author

Choose a reason for hiding this comment

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

This change only re-encodes request parameters, and does not affect other segments such as the request path.

@@ -407,7 +412,7 @@ public static Function<ServerRequest, ServerRequest> stripPrefix(int parts) {

URI prefixedUri = UriComponentsBuilder.fromUri(request.uri())
.replacePath(newPath.toString())
.build()
.build(true)
Copy link
Author

Choose a reason for hiding this comment

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

The stripped path is already encoded, so no additional encoding will be applied.
Furthermore, other parts such as request parameters are not affected.

Comment on lines +357 to +360
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri())
.replacePath(uri.getRawPath())
.build(true)
.toUri();
Copy link
Author

Choose a reason for hiding this comment

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

The modified path is already encoded, so no additional encoding will be applied.
Furthermore, other parts, such as request parameters, are not affected.

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.

2 participants