spring-projects / spring-projects/spring-hateoas
Encoded forward slashes in URL cause wrong links
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 476
- PR merge metrics
- No merged PRs in 30d
Description
For example when calling this URL: http://localhost:8080/things/%2Fsome%2Fthing, then this is produced in the controller method:
@RequestMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<ThingResource> get(@PathVariable("id") String id) {
Link link = linkTo(ThingController.class).slash(id).withSelfRel();
System.out.println(link.getHref());
//...
}
It prints http://localhost:8080/things/some/thing/things/some/thing where it should produce http://localhost:8080/things/%2Fsome%2Fthing.
There are two problems:
- It takes the decoded path-variable as the base-path of the request mapping. This only happens when there is a leading
%2F. If the leading character is another one the mapping works - It appends the id unencoded where it should be encoded (I guess)
I'm well aware that slashes in path variables are problematic, but they do work in Spring MVC now, see https://jira.spring.io/browse/SPR-7919 & https://jira.spring.io/browse/SPR-11101
The problem could be in ServletUriComponentsBuilder.fromServletMapping(HttpServletRequest) where a UrlPathHelper is instantiated which defaults to urlDecode = true. The fix for having encoded slashes in Spring MVC is to set urlDecode = false, I guess that's related. Maybe that could be made configurable?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with ServletUriComponentsBuilder.fromServletMapping(HttpServletRequest) and the UrlPathHelper configuration mentioned in the report. Reproduce the encoded-slash request and trace how the decoded path variable becomes the mapping base and how the id is appended. Done means linkTo(...).slash(id) preserves the leading %2F and produces the expected URL without duplicated path segments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100