Skip to content

UriComponentsBuilder have problems with squere brakcets "[ ]" #25444

@molsza

Description

@molsza

Affects: 5.2.5

If a query has brackets in its values, then it is not completely encoded, but accepted by the URI implementation.

Problem is with build(boolean) method, which works in an inconsistent way.

if parameter is true, then it validates if URI is completely encoded and fails when there are an unencoded []
when parameter is false, then it selectively encodes the URI, specifically it doesn't encode [], producing again invalid URI.

Look at this test:

  @Test
  public void encoding() throws URISyntaxException {

    URI uri = new URI("http://example.com/some/path?query=[from%20to]");
    try {
      UriComponentsBuilder.fromUri(uri).build(true);
      fail("It wasn't completely encoded URI");
    } catch (IllegalArgumentException e) {
      //good
    }

    //ok, then encode it
    uri = UriComponentsBuilder.fromUri(uri).build(false).toUri();
    //now it is double encoded http://example.com/some/path?query=[from%2520to]

    //so is it encoded now?
    UriComponentsBuilder.fromUri(uri).build(true); //fail, no it is not, square brackets are there
  }

Whatever you do, it is not possible to use UriComponentsBuilder on the above uri and get the valid or at least the same uri as a result.

I am not the only one confused: https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilter.java#L63


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions