Browse Source

Polishing contribution

Closes gh-30137
pull/31284/head
rstoyanchev 1 year ago
parent
commit
cc296c5033
  1. 15
      spring-web/src/main/java/org/springframework/web/server/adapter/ForwardedHeaderTransformer.java
  2. 22
      spring-web/src/test/java/org/springframework/web/server/adapter/ForwardedHeaderTransformerTests.java

15
spring-web/src/main/java/org/springframework/web/server/adapter/ForwardedHeaderTransformer.java

@ -18,6 +18,7 @@ package org.springframework.web.server.adapter; @@ -18,6 +18,7 @@ package org.springframework.web.server.adapter;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
@ -30,6 +31,7 @@ import org.springframework.lang.Nullable; @@ -30,6 +31,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.ForwardedHeaderUtils;
import org.springframework.web.util.UriComponents;
/**
* Extract values from "Forwarded" and "X-Forwarded-*" headers to override
@ -102,7 +104,7 @@ public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, S @@ -102,7 +104,7 @@ public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, S
if (!this.removeOnly) {
URI originalUri = request.getURI();
HttpHeaders headers = request.getHeaders();
URI uri = ForwardedHeaderUtils.adaptFromForwardedHeaders(originalUri, headers).build(true).toUri();
URI uri = adaptFromForwardedHeaders(originalUri, headers);
builder.uri(uri);
String prefix = getForwardedPrefix(request);
if (prefix != null) {
@ -121,6 +123,17 @@ public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, S @@ -121,6 +123,17 @@ public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, S
return request;
}
private static URI adaptFromForwardedHeaders(URI uri, HttpHeaders headers) {
// GH-30137: assume URI is encoded, but avoid build(true) for more lenient handling
UriComponents components = ForwardedHeaderUtils.adaptFromForwardedHeaders(uri, headers).build();
try {
return new URI(components.toUriString());
}
catch (URISyntaxException ex) {
throw new IllegalStateException("Could not create URI object: " + ex.getMessage(), ex);
}
}
/**
* Whether the request has any Forwarded headers.
* @param request the request

22
spring-web/src/test/java/org/springframework/web/server/adapter/ForwardedHeaderTransformerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-202 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -57,7 +57,7 @@ class ForwardedHeaderTransformerTests { @@ -57,7 +57,7 @@ class ForwardedHeaderTransformerTests {
}
@Test
void xForwardedHeaders() throws Exception {
void xForwardedHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.add("X-Forwarded-Host", "84.198.58.199");
headers.add("X-Forwarded-Port", "443");
@ -70,7 +70,7 @@ class ForwardedHeaderTransformerTests { @@ -70,7 +70,7 @@ class ForwardedHeaderTransformerTests {
}
@Test
void forwardedHeader() throws Exception {
void forwardedHeader() {
HttpHeaders headers = new HttpHeaders();
headers.add("Forwarded", "host=84.198.58.199;proto=https");
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));
@ -80,7 +80,7 @@ class ForwardedHeaderTransformerTests { @@ -80,7 +80,7 @@ class ForwardedHeaderTransformerTests {
}
@Test
void xForwardedPrefix() throws Exception {
void xForwardedPrefix() {
HttpHeaders headers = new HttpHeaders();
headers.add("X-Forwarded-Prefix", "/prefix");
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));
@ -91,7 +91,7 @@ class ForwardedHeaderTransformerTests { @@ -91,7 +91,7 @@ class ForwardedHeaderTransformerTests {
}
@Test // gh-23305
void xForwardedPrefixShouldNotLeadToDecodedPath() throws Exception {
void xForwardedPrefixShouldNotLeadToDecodedPath() {
HttpHeaders headers = new HttpHeaders();
headers.add("X-Forwarded-Prefix", "/prefix");
ServerHttpRequest request = MockServerHttpRequest
@ -107,7 +107,7 @@ class ForwardedHeaderTransformerTests { @@ -107,7 +107,7 @@ class ForwardedHeaderTransformerTests {
}
@Test
void xForwardedPrefixTrailingSlash() throws Exception {
void xForwardedPrefixTrailingSlash() {
HttpHeaders headers = new HttpHeaders();
headers.add("X-Forwarded-Prefix", "/prefix////");
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));
@ -118,7 +118,7 @@ class ForwardedHeaderTransformerTests { @@ -118,7 +118,7 @@ class ForwardedHeaderTransformerTests {
}
@Test // SPR-17525
void shouldNotDoubleEncode() throws Exception {
void shouldNotDoubleEncode() {
HttpHeaders headers = new HttpHeaders();
headers.add("Forwarded", "host=84.198.58.199;proto=https");
@ -133,8 +133,8 @@ class ForwardedHeaderTransformerTests { @@ -133,8 +133,8 @@ class ForwardedHeaderTransformerTests {
assertForwardedHeadersRemoved(request);
}
@Test
void shouldHandleUnencodedUri() throws Exception {
@Test // gh-30137
void shouldHandleUnencodedUri() {
HttpHeaders headers = new HttpHeaders();
headers.add("Forwarded", "host=84.198.58.199;proto=https");
ServerHttpRequest request = MockServerHttpRequest
@ -149,7 +149,7 @@ class ForwardedHeaderTransformerTests { @@ -149,7 +149,7 @@ class ForwardedHeaderTransformerTests {
}
@Test
void shouldConcatenatePrefixes() throws Exception {
void shouldConcatenatePrefixes() {
HttpHeaders headers = new HttpHeaders();
headers.add("X-Forwarded-Prefix", "/first,/second");
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));
@ -160,7 +160,7 @@ class ForwardedHeaderTransformerTests { @@ -160,7 +160,7 @@ class ForwardedHeaderTransformerTests {
}
@Test
void shouldConcatenatePrefixesWithTrailingSlashes() throws Exception {
void shouldConcatenatePrefixesWithTrailingSlashes() {
HttpHeaders headers = new HttpHeaders();
headers.add("X-Forwarded-Prefix", "/first/,/second//");
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));

Loading…
Cancel
Save