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

Special characters in request parameters not fully encoded when generating links from controller methods #1485

Closed
bedaka opened this issue Mar 9, 2021 · 7 comments
Assignees
Labels
in: core Core parts of the project type: bug
Milestone

Comments

@bedaka
Copy link

bedaka commented Mar 9, 2021

Hello,

when generating Links with WebMvcLinkBuilder containing OffsetDateTime the date does not get transformed correctly resulting in a malfunctioning link.

Example:
Passing an OffsetDateTime like 2020-01-01T14:00:00.000+01:00 results in:

https://myurl.com/service?date=2020-01-01T14:00:00.000+01:00
where the "+" before the offset gets interpreted by the browser as a space " " instead of:
https://myurl.com/service?date=2020-01-01T14:00:00.000%2b01:00.

Setup:

Links are generated with spring-boot-starter-hateoas in version 2.4.3 without custom config.
Example:

public void addLinks(MyModel model, OffsetDateTime date) {
      model.add(
          linkTo(
              methodOn(Controller.class)
                  .getFunction(date))
              .withRel("myLink"));
    } 

Is this expected behavior or in fact a bug?

Thank you.

@bedaka bedaka changed the title WebMvcLinkBuilder generates links without formating WebMvcLinkBuilder generates links without encoding Mar 9, 2021
@bclozel
Copy link
Member

bclozel commented Jun 21, 2021

It looks like another case of this issue was reported against Spring Boot: spring-projects/spring-framework#27078

@pax95
Copy link

pax95 commented Jun 21, 2021

Any update on this ?. It causes no clean upgrade path from previous versions.

@odrotbohm
Copy link
Member

This seems to be slightly more tricky than anticipated. We're using Spring Framework's UriUtils.encodeQueryParam(…) method to encode all query parameters. That (of course) only encodes characters deemed invalud for the URI component and a + is indeed an allowed character for query parameters (see the BNF section here and the definition of pchar) and thus, doesn't process it. I.e. this problem doesn't seem to be Spring HATEOAS specific: neither explicitly setting ….encode() on the UriComponentsBuilder changes the output, nor does URI creation via Spring Framework's own MvcUriComponentsBuilder create a different result. Interestingly, UriComponentsBuilder.fromUriString(…) also properly extracts the original String value (i.e. does not try to unencode the plus sign). I guess I'll have to take this to the team for further discussion.

@odrotbohm
Copy link
Member

I wonder whether the original expectation is actually correct. + is a valid URI parameter character and if at all, also the colon and the dots would have to be encoded.

@gregturn
Copy link
Contributor

Since “+” is effectively a reserved word in URIs, it needs to be encoded. Or else it will be misprocessed. But we may need to encode not at the quert parameter level. Maybe the whole thing?

Maybe we need to spot the usage of “+” on a parameter and do this. Can we spot when to decode a whole URI?

@odrotbohm
Copy link
Member

I don't think we (as in "the Spring HATEOAS codebase") should do anything about encoding in particular, except using Spring Framework APIs. Let's wait for the team discussion for now.

@odrotbohm odrotbohm added this to the 1.4 M2 milestone Jul 28, 2021
odrotbohm added a commit that referenced this issue Jul 28, 2021
We now use the encoding rules defined in RFC 6570 (URI templates, API introduced in #1583) to prepare request parameters added to the URIs rendered.
@odrotbohm odrotbohm changed the title WebMvcLinkBuilder generates links without encoding Special characters in request parameters not fully encoded when generating links from controller methods Jul 28, 2021
@odrotbohm
Copy link
Member

This should be fixed in the latest 1.4 snapshots. Note that we now fully encode the request parameters according to URI template RFC rules, which means that not only the plus gets encoded but also the colon.

odrotbohm added a commit that referenced this issue Jul 28, 2021
We now use the encoding rules defined in RFC 6570 (URI templates, API introduced in #1583) to prepare request parameters added to the URIs rendered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Core parts of the project type: bug
Projects
None yet
Development

No branches or pull requests

5 participants