Skip to content

Commit da84f43

Browse files
hutiefang76velo
andauthored
Fix URI fragment replacement and preserve target escapes (#3633)
* Fix URI fragment replacement and preserve target escapes A replacement URI should not retain a fragment from the previous URI. Keep the fragment during template resolution and preserve percent escapes in target fragments so they remain valid URL text. Fixes #3095 * Drop always-true fragment null check in resolve() and fix empty-fragment test comment Signed-off-by: Marvin Froeder <[email protected]> --------- Signed-off-by: Marvin Froeder <[email protected]> Co-authored-by: Marvin Froeder <[email protected]>
1 parent be25bbb commit da84f43

2 files changed

Lines changed: 120 additions & 2 deletions

File tree

‎core/src/main/java/feign/RequestTemplate.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ public RequestTemplate resolve(Map<String, ?> variables) {
234234

235235
/* add the uri to result */
236236
resolved.uri(uri.toString());
237+
resolved.fragment = this.fragment;
237238

238239
/* headers */
239240
if (!this.headers.isEmpty()) {
@@ -430,6 +431,9 @@ public RequestTemplate insert(int pos, CharSequence value) {
430431
/**
431432
* Set the Uri for the request, replacing the existing uri if set.
432433
*
434+
* <p>A fragment in the given uri replaces any existing fragment, even an empty one. Replacing the
435+
* uri with one that has no fragment clears the existing fragment.
436+
*
433437
* @param uri to use, must be a relative uri.
434438
* @return a RequestTemplate for chaining.
435439
*/
@@ -463,8 +467,12 @@ public RequestTemplate uri(String uri, boolean append) {
463467

464468
int fragmentIndex = uri.indexOf('#');
465469
if (fragmentIndex > -1) {
470+
/* an incoming fragment, even an empty one, replaces any existing fragment */
466471
fragment = uri.substring(fragmentIndex);
467472
uri = uri.substring(0, fragmentIndex);
473+
} else if (!append) {
474+
/* replacing the uri without a fragment clears the existing fragment */
475+
fragment = null;
468476
}
469477

470478
/*
@@ -524,8 +532,9 @@ public RequestTemplate target(String target) {
524532
/* strip the query string */
525533
this.target =
526534
targetUri.getScheme() + "://" + targetUri.getRawAuthority() + targetUri.getRawPath();
527-
if (targetUri.getFragment() != null) {
528-
this.fragment = "#" + targetUri.getFragment();
535+
/* keep the fragment as it was written, the value is never decoded */
536+
if (targetUri.getRawFragment() != null) {
537+
this.fragment = "#" + targetUri.getRawFragment();
529538
}
530539
} catch (IllegalArgumentException iae) {
531540
/* the uri provided is not a valid one, we can't continue */

‎core/src/test/java/feign/RequestTemplateTest.java‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import feign.Request.HttpMethod;
2525
import feign.template.UriUtils;
26+
import java.net.URI;
2627
import java.util.Arrays;
2728
import java.util.Collection;
2829
import java.util.Collections;
@@ -579,6 +580,114 @@ void fragmentShouldBeExtractedWhenQueryParamsExist() {
579580
assertThat(template.queryLine()).isEqualTo("?query=queryValue");
580581
}
581582

583+
@Test
584+
void resolvingUriShouldPreserveFragment() {
585+
RequestTemplate template =
586+
new RequestTemplate().method(HttpMethod.GET).uri("/path/{id}#anchor");
587+
588+
RequestTemplate resolved = template.resolve(Collections.singletonMap("id", "123"));
589+
590+
assertThat(resolved.url()).isEqualTo("/path/123#anchor");
591+
assertThat(resolved.request().url()).isEqualTo("/path/123#anchor");
592+
assertThat(template.url()).isEqualTo("/path/{id}#anchor");
593+
}
594+
595+
@Test
596+
void replacingUriShouldClearFragmentFromPreviousUri() {
597+
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET).uri("/old#stale");
598+
assertThat(template.url()).isEqualTo("/old#stale");
599+
600+
/* cannot set a fragment on the replacement uri, the previous one must not survive */
601+
template.uri("/new?q=v");
602+
603+
assertThat(template.url()).isEqualTo("/new?q=v");
604+
assertThat(template.queryLine()).isEqualTo("?q=v");
605+
}
606+
607+
@Test
608+
void explicitlyEmptyFragmentShouldReplacePreviousFragment() {
609+
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET).uri("/old#stale");
610+
611+
/* a bare '#' replaces the stale fragment with an empty one */
612+
template.uri("/new#");
613+
614+
/*
615+
* RFC 3986 allows an empty fragment and java.net.URI reports it as "" rather than null, so an
616+
* empty fragment is not asserted as stripped, only as no longer being the stale one.
617+
*/
618+
String url = template.url();
619+
assertThat(url).doesNotContain("stale");
620+
assertThat(url).isEqualTo("/new#");
621+
assertThat(URI.create(url).getRawFragment()).isNotNull().isEmpty();
622+
}
623+
624+
@Test
625+
void appendingUriShouldPreserveExistingFragment() {
626+
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET).uri("/base#anchor");
627+
628+
template.uri("/suffix", true);
629+
630+
assertThat(template.url()).isEqualTo("/base/suffix#anchor");
631+
}
632+
633+
@Test
634+
void incomingFragmentShouldReplaceExistingFragment() {
635+
RequestTemplate replaced =
636+
new RequestTemplate().method(HttpMethod.GET).uri("/old#stale").uri("/new#fresh");
637+
assertThat(replaced.url()).isEqualTo("/new#fresh");
638+
639+
RequestTemplate appended =
640+
new RequestTemplate().method(HttpMethod.GET).uri("/base#stale").uri("/suffix#fresh", true);
641+
assertThat(appended.url()).isEqualTo("/base/suffix#fresh");
642+
}
643+
644+
@Test
645+
void targetShouldPreserveRawFragment() {
646+
RequestTemplate template =
647+
new RequestTemplate()
648+
.method(HttpMethod.GET)
649+
.target("https://example.com/a#encoded%20value%2F");
650+
651+
/* the raw fragment is re-appended verbatim, not decoded into " " or "/" */
652+
assertThat(template.url()).isEqualTo("https://example.com/a#encoded%20value%2F");
653+
assertThat(template.headers()).isEmpty();
654+
assertThat(template.queries()).isEmpty();
655+
}
656+
657+
@Test
658+
void targetShouldPreserveRawFragmentWhenQueryIsPresent() {
659+
RequestTemplate template =
660+
new RequestTemplate()
661+
.method(HttpMethod.GET)
662+
.target("https://example.com/a?x=1#encoded%20fragment");
663+
664+
assertThat(template.url()).isEqualTo("https://example.com/a?x=1#encoded%20fragment");
665+
assertThat(template.queries()).containsKey("x");
666+
}
667+
668+
@Test
669+
void targetWithoutFragmentShouldNotClearExistingFragment() {
670+
RequestTemplate template =
671+
new RequestTemplate()
672+
.method(HttpMethod.GET)
673+
.uri("/path#fragment")
674+
.target("https://example.com");
675+
676+
assertThat(template.url()).isEqualTo("https://example.com/path#fragment");
677+
}
678+
679+
@Test
680+
void copyShouldPreserveAndIsolateFragment() {
681+
RequestTemplate original = new RequestTemplate().method(HttpMethod.GET).uri("/path#fragment");
682+
683+
RequestTemplate copy = RequestTemplate.from(original);
684+
assertThat(copy.url()).isEqualTo("/path#fragment");
685+
686+
copy.uri("/other#other-fragment");
687+
assertThat(copy.url()).isEqualTo("/other#other-fragment");
688+
assertThat(original.url()).isEqualTo("/path#fragment");
689+
}
690+
582691
@Test
583692
void urlEncodingRemainsInPlace() {
584693
RequestTemplate template =

0 commit comments

Comments
 (0)