Skip to content

Encoded forward slashes in URL cause wrong links #226

Open
@meyertee

Description

@meyertee

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions