Skip to content

Commit 7d61295

Browse files
aooohansnicoll
authored andcommitted
Add configuration property for RemoteIpValve's trusted proxies
See gh-31576
1 parent 69050a9 commit 7d61295

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,11 @@ public static class Remoteip {
966966
+ "172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}|" //
967967
+ "0:0:0:0:0:0:0:1|::1";
968968

969+
/**
970+
* Regular expression defining proxies that are trusted when they appear in the remoteIpHeader header.
971+
*/
972+
private String trustedProxies;
973+
969974
/**
970975
* Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
971976
*/
@@ -1041,6 +1046,13 @@ public void setRemoteIpHeader(String remoteIpHeader) {
10411046
this.remoteIpHeader = remoteIpHeader;
10421047
}
10431048

1049+
public String getTrustedProxies() {
1050+
return trustedProxies;
1051+
}
1052+
1053+
public void setTrustedProxies(String trustedProxies) {
1054+
this.trustedProxies = trustedProxies;
1055+
}
10441056
}
10451057

10461058
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory)
229229
}
230230
// The internal proxies default to a list of "safe" internal IP addresses
231231
valve.setInternalProxies(remoteIpProperties.getInternalProxies());
232+
valve.setTrustedProxies(remoteIpProperties.getTrustedProxies());
232233
try {
233234
valve.setHostHeader(remoteIpProperties.getHostHeader());
234235
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ void testTomcatBinding() {
129129
map.put("server.tomcat.remoteip.protocol-header", "X-Forwarded-Protocol");
130130
map.put("server.tomcat.remoteip.remote-ip-header", "Remote-Ip");
131131
map.put("server.tomcat.remoteip.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
132+
map.put("server.tomcat.remoteip.trusted-proxies", "proxy1|proxy2|proxy3");
132133
map.put("server.tomcat.reject-illegal-header", "false");
133134
map.put("server.tomcat.background-processor-delay", "10");
134135
map.put("server.tomcat.relaxed-path-chars", "|,<");
@@ -152,6 +153,7 @@ void testTomcatBinding() {
152153
assertThat(tomcat.getRemoteip().getRemoteIpHeader()).isEqualTo("Remote-Ip");
153154
assertThat(tomcat.getRemoteip().getProtocolHeader()).isEqualTo("X-Forwarded-Protocol");
154155
assertThat(tomcat.getRemoteip().getInternalProxies()).isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
156+
assertThat(tomcat.getRemoteip().getTrustedProxies()).isEqualTo("proxy1|proxy2|proxy3");
155157
assertThat(tomcat.isRejectIllegalHeader()).isFalse();
156158
assertThat(tomcat.getBackgroundProcessorDelay()).hasSeconds(10);
157159
assertThat(tomcat.getRelaxedPathChars()).containsExactly('|', '<');

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ void customRemoteIpValve() {
224224
bind("server.tomcat.remoteip.remote-ip-header=x-my-remote-ip-header",
225225
"server.tomcat.remoteip.protocol-header=x-my-protocol-header",
226226
"server.tomcat.remoteip.internal-proxies=192.168.0.1",
227+
"server.tomcat.remoteip.trusted-proxies=proxy1|proxy2",
227228
"server.tomcat.remoteip.host-header=x-my-forward-host",
228229
"server.tomcat.remoteip.port-header=x-my-forward-port",
229230
"server.tomcat.remoteip.protocol-header-https-value=On");
@@ -238,6 +239,7 @@ void customRemoteIpValve() {
238239
assertThat(remoteIpValve.getHostHeader()).isEqualTo("x-my-forward-host");
239240
assertThat(remoteIpValve.getPortHeader()).isEqualTo("x-my-forward-port");
240241
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
242+
assertThat(remoteIpValve.getTrustedProxies()).isEqualTo("proxy1|proxy2");
241243
}
242244

243245
@Test

0 commit comments

Comments
 (0)