parent
a1c4fb3840
commit
cc296c5033
|
|
@ -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;
|
|||
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
|
|||
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
|
|||
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
|
||||
|
|
|
|||
|
|
@ -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 {
|
|||
}
|
||||
|
||||
@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 {
|
|||
}
|
||||
|
||||
@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 {
|
|||
}
|
||||
|
||||
@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 {
|
|||
}
|
||||
|
||||
@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 {
|
|||
}
|
||||
|
||||
@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 {
|
|||
}
|
||||
|
||||
@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 {
|
|||
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 {
|
|||
}
|
||||
|
||||
@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 {
|
|||
}
|
||||
|
||||
@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…
Reference in New Issue