mkopylec / charon-spring-boot-starter

Reverse proxy implementation in form of a Spring Boot starter.
Apache License 2.0
240 stars 54 forks source link

RegexRequestPathRewriter rewrites decoded path to encoded #118

Closed jan-krakora closed 3 years ago

jan-krakora commented 3 years ago

When path contains an encoded string like /path/to/aaaa%20bbb, rewriting fails with an exception

java.lang.IllegalArgumentException: Invalid character ' ' for PATH in "/path/to/aaa bbb"
    at org.springframework.web.util.HierarchicalUriComponents.verifyUriComponent(HierarchicalUriComponents.java:416)
    at org.springframework.web.util.HierarchicalUriComponents.access$200(HierarchicalUriComponents.java:53)
    at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.verify(HierarchicalUriComponents.java:912)
    at org.springframework.web.util.HierarchicalUriComponents.verify(HierarchicalUriComponents.java:380)
    at org.springframework.web.util.HierarchicalUriComponents.<init>(HierarchicalUriComponents.java:145)
    at org.springframework.web.util.UriComponentsBuilder.buildInternal(UriComponentsBuilder.java:483)
    at org.springframework.web.util.UriComponentsBuilder.build(UriComponentsBuilder.java:472)
    at com.github.mkopylec.charon.forwarding.interceptors.rewrite.CommonRegexRequestPathRewriter.rewritePath(CommonRegexRequestPathRewriter.java:51)
    at com.github.mkopylec.charon.forwarding.interceptors.rewrite.RegexRequestPathRewriter.forward(RegexRequestPathRewriter.java:22)
    ...

It's because in CommonRegexRequestPathRewriter#rewritePath you get the path in decoded form

String requestPath = uri.getPath();

then rewrite it and build a new path with encoded parameter set to true. Which is wrong.

URI rewrittenUri = fromUri(uri)
                .replacePath(rewrittenPath)
                .build(true)
                .toUri();
jan-krakora commented 3 years ago

Now I see it has been fixed in 4.9.0