spring-projects / spring-projects/spring-hateoas

Encoded forward slashes in URL cause wrong links

Open
#226 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core type: bug
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:

  1. 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
  2. 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.