-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Spring request mapping annotation does not map an encoded URI correctly [SPR-10306] #14940
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
Comments
Rossen Stoyanchev commented URLEncoder is not for encoding URLs as its name would imply, it's actually for encoding HTML form data as its javadoc indicates. Try using UriUtils.encode(..). |
Alexander Hawley commented I apologize for the confusion. The bug is that |
Alexander Hawley commented I have removed the unrelated code, hopefully the issue is clearer now. |
Rossen Stoyanchev commented Once the request URI is decoded, the additional "/" from "foo%25boo", means the lookup path is now "/encoded/foo/boo" and it's no longer possible to interpret without treating it as a path segment. The only thing you could do is set the "urlDecode" property of your HandlerMapping to false in which case the lookup path will remain "/encoded/foo%25boo" while the path variable will still be decoded and so value will be "foo/bar". Keep in mind this property will affect all other request mappings in the same way. |
Alexander Hawley commented Thanks for the follow-up. That was my diagnosis as well. This is what is causing the bug. The URI is decoded incorrectly. If the URI template libraries were modified to use per-parameter encoding and decoding, then you could encode anything in a template parameter. A slash encoded as %2F in a path segment is allowed as per HTTP/URI specifications. I only tested the reserved characters. (Edit: grammar) |
Rossen Stoyanchev commented Hm, I am not sure we're actually saying the same thing.. The URI is decoded correctly. Decoding happens by default for all incoming requests, because it's easier to express mappings that don't have encoded characters. In your case, the decoding actually gets in the way. Hence my recommendation to change the default setting of the "urlDecode" property of the HandlerMapping. That will turn off decoding. In other words, this is all expected behavior, not a bug. Hope it makes sense. |
Alexander Hawley commented You mean to say that you expect the URI template to work this way, requiring a toggle for all-or-nothing decoding? I don't see why the template parameter has anything to do with wanting encoded or decoded text in the rest of the template. That is not how it works in other MVC or URI templating systems. Not to mention, regardless of the toggle switch for request mappings, the URI templates built in the tag libs do not coincide with this behavior. From an implementation perspective, a developer might want to have any of these combinations: template: template: template: template: |
Rossen Stoyanchev commented
Correct. The incoming URL is encoded (e.g. "/foo%20bar"). Request mapping patterns on the other hand are usually decoded (e.g. |
Alexander Hawley commented Okay. I see your point. This functionality could be considered an improvement or feature request. But what about the other related tickets marked as duplicates? Even with the switch to make request mapping use encoded values, the Spring URI template library (for controller redirects) and the Spring tag lib (for view links/forms) do not encode URIs correctly. So if you toggle your request mappings to accept the URI |
Rossen Stoyanchev commented It should be possible to generate the URI from the client side using Spring but it could be easier. Please track #17347. |
Rossen Stoyanchev commented Sorry that was a link to the wrong ticket. See the resolution for #16028. |
Alexander Hawley opened SPR-10306 and commented
When using the Spring request mapping annotation (
org.springframework.web.bind.annotation.RequestMapping
), thevalue
attribute does not map an encoded URI correctly. Some encoded reserved characters are excluded.For example, the slash character
/
, encoded as%2f
, is excluded.Results as follows.
List of strings which contain reserved characters:
Controller action to receive requests:
Results in the URIs & responses (when requested):
In order to catch the request for
foo/boo
(encoded asfoo%2Fboo
), an additional controller action must be used with wildcard instead of path parameter:Any ideas?
Thanks.
-AH
Affects: 3.1 GA
Issue Links:
1 votes, 3 watchers
The text was updated successfully, but these errors were encountered: