Commit 572a436d authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #17510 from dirkdeyne

* pr/17510:
  Polish "Add support for configuring Tomcat's relaxed path and query chars"
  Add support for configuring Tomcat's relaxed path and query chars

Closes gh-17510
parents 55e8a978 dd2f20fc
...@@ -61,6 +61,7 @@ import org.springframework.util.unit.DataSize; ...@@ -61,6 +61,7 @@ import org.springframework.util.unit.DataSize;
* @author Artsiom Yudovin * @author Artsiom Yudovin
* @author Andrew McGhie * @author Andrew McGhie
* @author Rafiullah Hamedy * @author Rafiullah Hamedy
* @author Dirk Deyne
* @since 1.0.0 * @since 1.0.0
*/ */
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
...@@ -395,6 +396,18 @@ public class ServerProperties { ...@@ -395,6 +396,18 @@ public class ServerProperties {
*/ */
private List<String> additionalTldSkipPatterns = new ArrayList<>(); private List<String> additionalTldSkipPatterns = new ArrayList<>();
/**
* Comma-separated list of additional unencoded characters that should be allowed
* in URI paths. Only "< > [ \ ] ^ ` { | }" are allowed.
*/
private List<Character> relaxedPathChars = new ArrayList<>();
/**
* Comma-separated list of additional unencoded characters that should be allowed
* in URI query strings. Only "< > [ \ ] ^ ` { | }" are allowed.
*/
private List<Character> relaxedQueryChars = new ArrayList<>();
/** /**
* Static resource configuration. * Static resource configuration.
*/ */
...@@ -553,6 +566,22 @@ public class ServerProperties { ...@@ -553,6 +566,22 @@ public class ServerProperties {
this.additionalTldSkipPatterns = additionalTldSkipPatterns; this.additionalTldSkipPatterns = additionalTldSkipPatterns;
} }
public List<Character> getRelaxedPathChars() {
return this.relaxedPathChars;
}
public void setRelaxedPathChars(List<Character> relaxedPathChars) {
this.relaxedPathChars = relaxedPathChars;
}
public List<Character> getRelaxedQueryChars() {
return this.relaxedQueryChars;
}
public void setRelaxedQueryChars(List<Character> relaxedQueryChars) {
this.relaxedQueryChars = relaxedQueryChars;
}
public Resource getResource() { public Resource getResource() {
return this.resource; return this.resource;
} }
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
package org.springframework.boot.autoconfigure.web.embedded; package org.springframework.boot.autoconfigure.web.embedded;
import java.time.Duration; import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.catalina.Lifecycle; import org.apache.catalina.Lifecycle;
import org.apache.catalina.valves.AccessLogValve; import org.apache.catalina.valves.AccessLogValve;
...@@ -51,6 +53,7 @@ import org.springframework.util.unit.DataSize; ...@@ -51,6 +53,7 @@ import org.springframework.util.unit.DataSize;
* @author Artsiom Yudovin * @author Artsiom Yudovin
* @author Chentao Qu * @author Chentao Qu
* @author Andrew McGhie * @author Andrew McGhie
* @author Dirk Deyne
* @since 2.0.0 * @since 2.0.0
*/ */
public class TomcatWebServerFactoryCustomizer public class TomcatWebServerFactoryCustomizer
...@@ -102,6 +105,10 @@ public class TomcatWebServerFactoryCustomizer ...@@ -102,6 +105,10 @@ public class TomcatWebServerFactoryCustomizer
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount)); .to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
propertyMapper.from(tomcatProperties::getProcessorCache) propertyMapper.from(tomcatProperties::getProcessorCache)
.to((processorCache) -> customizeProcessorCache(factory, processorCache)); .to((processorCache) -> customizeProcessorCache(factory, processorCache));
propertyMapper.from(tomcatProperties::getRelaxedPathChars).as(this::joinCharacters).whenHasText()
.to((relaxedChars) -> customizeRelaxedPathChars(factory, relaxedChars));
propertyMapper.from(tomcatProperties::getRelaxedQueryChars).as(this::joinCharacters).whenHasText()
.to((relaxedChars) -> customizeRelaxedQueryChars(factory, relaxedChars));
customizeStaticResources(factory); customizeStaticResources(factory);
customizeErrorReportValve(properties.getError(), factory); customizeErrorReportValve(properties.getError(), factory);
} }
...@@ -149,6 +156,18 @@ public class TomcatWebServerFactoryCustomizer ...@@ -149,6 +156,18 @@ public class TomcatWebServerFactoryCustomizer
}); });
} }
private void customizeRelaxedPathChars(ConfigurableTomcatWebServerFactory factory, String relaxedChars) {
factory.addConnectorCustomizers((connector) -> connector.setAttribute("relaxedPathChars", relaxedChars));
}
private void customizeRelaxedQueryChars(ConfigurableTomcatWebServerFactory factory, String relaxedChars) {
factory.addConnectorCustomizers((connector) -> connector.setAttribute("relaxedQueryChars", relaxedChars));
}
private String joinCharacters(List<Character> content) {
return content.stream().map(String::valueOf).collect(Collectors.joining());
}
private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory) { private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory) {
Tomcat tomcatProperties = this.serverProperties.getTomcat(); Tomcat tomcatProperties = this.serverProperties.getTomcat();
String protocolHeader = tomcatProperties.getProtocolHeader(); String protocolHeader = tomcatProperties.getProtocolHeader();
......
...@@ -2089,6 +2089,76 @@ ...@@ -2089,6 +2089,76 @@
} }
], ],
"hints": [ "hints": [
{
"name": "server.tomcat.relaxed-query-chars",
"values": [
{
"value": "<"
},
{
"value": ">"
},
{
"value": "["
},
{
"value": "\\"
},
{
"value": "]"
},
{
"value": "^"
},
{
"value": "`"
},
{
"value": "{"
},
{
"value": "|"
},
{
"value": "}"
}
]
},
{
"name": "server.tomcat.relaxed-path-chars",
"values": [
{
"value": "<"
},
{
"value": ">"
},
{
"value": "["
},
{
"value": "\\"
},
{
"value": "]"
},
{
"value": "^"
},
{
"value": "`"
},
{
"value": "{"
},
{
"value": "|"
},
{
"value": "}"
}
]
},
{ {
"name": "spring.liquibase.change-log", "name": "spring.liquibase.change-log",
"providers": [ "providers": [
......
...@@ -127,6 +127,8 @@ class ServerPropertiesTests { ...@@ -127,6 +127,8 @@ class ServerPropertiesTests {
map.put("server.tomcat.remote-ip-header", "Remote-Ip"); map.put("server.tomcat.remote-ip-header", "Remote-Ip");
map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"); map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
map.put("server.tomcat.background-processor-delay", "10"); map.put("server.tomcat.background-processor-delay", "10");
map.put("server.tomcat.relaxed-path-chars", "|,<");
map.put("server.tomcat.relaxed-query-chars", "^ , | ");
bind(map); bind(map);
ServerProperties.Tomcat tomcat = this.properties.getTomcat(); ServerProperties.Tomcat tomcat = this.properties.getTomcat();
Accesslog accesslog = tomcat.getAccesslog(); Accesslog accesslog = tomcat.getAccesslog();
...@@ -146,6 +148,8 @@ class ServerPropertiesTests { ...@@ -146,6 +148,8 @@ class ServerPropertiesTests {
assertThat(tomcat.getProtocolHeader()).isEqualTo("X-Forwarded-Protocol"); assertThat(tomcat.getProtocolHeader()).isEqualTo("X-Forwarded-Protocol");
assertThat(tomcat.getInternalProxies()).isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"); assertThat(tomcat.getInternalProxies()).isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
assertThat(tomcat.getBackgroundProcessorDelay()).isEqualTo(Duration.ofSeconds(10)); assertThat(tomcat.getBackgroundProcessorDelay()).isEqualTo(Duration.ofSeconds(10));
assertThat(tomcat.getRelaxedPathChars()).containsExactly('|', '<');
assertThat(tomcat.getRelaxedQueryChars()).containsExactly('^', '|');
} }
@Test @Test
......
...@@ -198,6 +198,22 @@ class TomcatWebServerFactoryCustomizerTests { ...@@ -198,6 +198,22 @@ class TomcatWebServerFactoryCustomizerTests {
}); });
} }
@Test
void customRelaxedPathChars() {
bind("server.tomcat.relaxed-path-chars=|,^");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getRelaxedPathChars()).isEqualTo("|^"));
}
@Test
void customRelaxedQueryChars() {
bind("server.tomcat.relaxed-query-chars=^ , | ");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getRelaxedQueryChars()).isEqualTo("^|"));
}
@Test @Test
void deduceUseForwardHeaders() { void deduceUseForwardHeaders() {
this.environment.setProperty("DYNO", "-"); this.environment.setProperty("DYNO", "-");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment