Skip to content

Commit

Permalink
Add configuration property for RemoteIpValve's trusted proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan authored and snicoll committed Jul 18, 2022
1 parent 69050a9 commit 7d61295
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,11 @@ public static class Remoteip {
+ "172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}|" //
+ "0:0:0:0:0:0:0:1|::1";

/**
* Regular expression defining proxies that are trusted when they appear in the remoteIpHeader header.
*/
private String trustedProxies;

/**
* Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
*/
Expand Down Expand Up @@ -1041,6 +1046,13 @@ public void setRemoteIpHeader(String remoteIpHeader) {
this.remoteIpHeader = remoteIpHeader;
}

public String getTrustedProxies() {
return trustedProxies;
}

public void setTrustedProxies(String trustedProxies) {
this.trustedProxies = trustedProxies;
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory)
}
// The internal proxies default to a list of "safe" internal IP addresses
valve.setInternalProxies(remoteIpProperties.getInternalProxies());
valve.setTrustedProxies(remoteIpProperties.getTrustedProxies());
try {
valve.setHostHeader(remoteIpProperties.getHostHeader());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void testTomcatBinding() {
map.put("server.tomcat.remoteip.protocol-header", "X-Forwarded-Protocol");
map.put("server.tomcat.remoteip.remote-ip-header", "Remote-Ip");
map.put("server.tomcat.remoteip.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
map.put("server.tomcat.remoteip.trusted-proxies", "proxy1|proxy2|proxy3");
map.put("server.tomcat.reject-illegal-header", "false");
map.put("server.tomcat.background-processor-delay", "10");
map.put("server.tomcat.relaxed-path-chars", "|,<");
Expand All @@ -152,6 +153,7 @@ void testTomcatBinding() {
assertThat(tomcat.getRemoteip().getRemoteIpHeader()).isEqualTo("Remote-Ip");
assertThat(tomcat.getRemoteip().getProtocolHeader()).isEqualTo("X-Forwarded-Protocol");
assertThat(tomcat.getRemoteip().getInternalProxies()).isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
assertThat(tomcat.getRemoteip().getTrustedProxies()).isEqualTo("proxy1|proxy2|proxy3");
assertThat(tomcat.isRejectIllegalHeader()).isFalse();
assertThat(tomcat.getBackgroundProcessorDelay()).hasSeconds(10);
assertThat(tomcat.getRelaxedPathChars()).containsExactly('|', '<');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ void customRemoteIpValve() {
bind("server.tomcat.remoteip.remote-ip-header=x-my-remote-ip-header",
"server.tomcat.remoteip.protocol-header=x-my-protocol-header",
"server.tomcat.remoteip.internal-proxies=192.168.0.1",
"server.tomcat.remoteip.trusted-proxies=proxy1|proxy2",
"server.tomcat.remoteip.host-header=x-my-forward-host",
"server.tomcat.remoteip.port-header=x-my-forward-port",
"server.tomcat.remoteip.protocol-header-https-value=On");
Expand All @@ -238,6 +239,7 @@ void customRemoteIpValve() {
assertThat(remoteIpValve.getHostHeader()).isEqualTo("x-my-forward-host");
assertThat(remoteIpValve.getPortHeader()).isEqualTo("x-my-forward-port");
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
assertThat(remoteIpValve.getTrustedProxies()).isEqualTo("proxy1|proxy2");
}

@Test
Expand Down

0 comments on commit 7d61295

Please # to comment.