Skip to content

Commit 572a436

Browse files
committed
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
2 parents 55e8a97 + dd2f20f commit 572a436

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Artsiom Yudovin
6262
* @author Andrew McGhie
6363
* @author Rafiullah Hamedy
64+
* @author Dirk Deyne
6465
* @since 1.0.0
6566
*/
6667
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
@@ -395,6 +396,18 @@ public static class Tomcat {
395396
*/
396397
private List<String> additionalTldSkipPatterns = new ArrayList<>();
397398

399+
/**
400+
* Comma-separated list of additional unencoded characters that should be allowed
401+
* in URI paths. Only "< > [ \ ] ^ ` { | }" are allowed.
402+
*/
403+
private List<Character> relaxedPathChars = new ArrayList<>();
404+
405+
/**
406+
* Comma-separated list of additional unencoded characters that should be allowed
407+
* in URI query strings. Only "< > [ \ ] ^ ` { | }" are allowed.
408+
*/
409+
private List<Character> relaxedQueryChars = new ArrayList<>();
410+
398411
/**
399412
* Static resource configuration.
400413
*/
@@ -553,6 +566,22 @@ public void setAdditionalTldSkipPatterns(List<String> additionalTldSkipPatterns)
553566
this.additionalTldSkipPatterns = additionalTldSkipPatterns;
554567
}
555568

569+
public List<Character> getRelaxedPathChars() {
570+
return this.relaxedPathChars;
571+
}
572+
573+
public void setRelaxedPathChars(List<Character> relaxedPathChars) {
574+
this.relaxedPathChars = relaxedPathChars;
575+
}
576+
577+
public List<Character> getRelaxedQueryChars() {
578+
return this.relaxedQueryChars;
579+
}
580+
581+
public void setRelaxedQueryChars(List<Character> relaxedQueryChars) {
582+
this.relaxedQueryChars = relaxedQueryChars;
583+
}
584+
556585
public Resource getResource() {
557586
return this.resource;
558587
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.autoconfigure.web.embedded;
1818

1919
import java.time.Duration;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
2022

2123
import org.apache.catalina.Lifecycle;
2224
import org.apache.catalina.valves.AccessLogValve;
@@ -51,6 +53,7 @@
5153
* @author Artsiom Yudovin
5254
* @author Chentao Qu
5355
* @author Andrew McGhie
56+
* @author Dirk Deyne
5457
* @since 2.0.0
5558
*/
5659
public class TomcatWebServerFactoryCustomizer
@@ -102,6 +105,10 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
102105
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
103106
propertyMapper.from(tomcatProperties::getProcessorCache)
104107
.to((processorCache) -> customizeProcessorCache(factory, processorCache));
108+
propertyMapper.from(tomcatProperties::getRelaxedPathChars).as(this::joinCharacters).whenHasText()
109+
.to((relaxedChars) -> customizeRelaxedPathChars(factory, relaxedChars));
110+
propertyMapper.from(tomcatProperties::getRelaxedQueryChars).as(this::joinCharacters).whenHasText()
111+
.to((relaxedChars) -> customizeRelaxedQueryChars(factory, relaxedChars));
105112
customizeStaticResources(factory);
106113
customizeErrorReportValve(properties.getError(), factory);
107114
}
@@ -149,6 +156,18 @@ private void customizeConnectionTimeout(ConfigurableTomcatWebServerFactory facto
149156
});
150157
}
151158

159+
private void customizeRelaxedPathChars(ConfigurableTomcatWebServerFactory factory, String relaxedChars) {
160+
factory.addConnectorCustomizers((connector) -> connector.setAttribute("relaxedPathChars", relaxedChars));
161+
}
162+
163+
private void customizeRelaxedQueryChars(ConfigurableTomcatWebServerFactory factory, String relaxedChars) {
164+
factory.addConnectorCustomizers((connector) -> connector.setAttribute("relaxedQueryChars", relaxedChars));
165+
}
166+
167+
private String joinCharacters(List<Character> content) {
168+
return content.stream().map(String::valueOf).collect(Collectors.joining());
169+
}
170+
152171
private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory) {
153172
Tomcat tomcatProperties = this.serverProperties.getTomcat();
154173
String protocolHeader = tomcatProperties.getProtocolHeader();

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,76 @@
20892089
}
20902090
],
20912091
"hints": [
2092+
{
2093+
"name": "server.tomcat.relaxed-query-chars",
2094+
"values": [
2095+
{
2096+
"value": "<"
2097+
},
2098+
{
2099+
"value": ">"
2100+
},
2101+
{
2102+
"value": "["
2103+
},
2104+
{
2105+
"value": "\\"
2106+
},
2107+
{
2108+
"value": "]"
2109+
},
2110+
{
2111+
"value": "^"
2112+
},
2113+
{
2114+
"value": "`"
2115+
},
2116+
{
2117+
"value": "{"
2118+
},
2119+
{
2120+
"value": "|"
2121+
},
2122+
{
2123+
"value": "}"
2124+
}
2125+
]
2126+
},
2127+
{
2128+
"name": "server.tomcat.relaxed-path-chars",
2129+
"values": [
2130+
{
2131+
"value": "<"
2132+
},
2133+
{
2134+
"value": ">"
2135+
},
2136+
{
2137+
"value": "["
2138+
},
2139+
{
2140+
"value": "\\"
2141+
},
2142+
{
2143+
"value": "]"
2144+
},
2145+
{
2146+
"value": "^"
2147+
},
2148+
{
2149+
"value": "`"
2150+
},
2151+
{
2152+
"value": "{"
2153+
},
2154+
{
2155+
"value": "|"
2156+
},
2157+
{
2158+
"value": "}"
2159+
}
2160+
]
2161+
},
20922162
{
20932163
"name": "spring.liquibase.change-log",
20942164
"providers": [

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ void testTomcatBinding() {
127127
map.put("server.tomcat.remote-ip-header", "Remote-Ip");
128128
map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
129129
map.put("server.tomcat.background-processor-delay", "10");
130+
map.put("server.tomcat.relaxed-path-chars", "|,<");
131+
map.put("server.tomcat.relaxed-query-chars", "^ , | ");
130132
bind(map);
131133
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
132134
Accesslog accesslog = tomcat.getAccesslog();
@@ -146,6 +148,8 @@ void testTomcatBinding() {
146148
assertThat(tomcat.getProtocolHeader()).isEqualTo("X-Forwarded-Protocol");
147149
assertThat(tomcat.getInternalProxies()).isEqualTo("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
148150
assertThat(tomcat.getBackgroundProcessorDelay()).isEqualTo(Duration.ofSeconds(10));
151+
assertThat(tomcat.getRelaxedPathChars()).containsExactly('|', '<');
152+
assertThat(tomcat.getRelaxedQueryChars()).containsExactly('^', '|');
149153
}
150154

151155
@Test

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ void customStaticResourceCacheTtl() {
198198
});
199199
}
200200

201+
@Test
202+
void customRelaxedPathChars() {
203+
bind("server.tomcat.relaxed-path-chars=|,^");
204+
customizeAndRunServer((server) -> assertThat(
205+
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
206+
.getRelaxedPathChars()).isEqualTo("|^"));
207+
}
208+
209+
@Test
210+
void customRelaxedQueryChars() {
211+
bind("server.tomcat.relaxed-query-chars=^ , | ");
212+
customizeAndRunServer((server) -> assertThat(
213+
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
214+
.getRelaxedQueryChars()).isEqualTo("^|"));
215+
}
216+
201217
@Test
202218
void deduceUseForwardHeaders() {
203219
this.environment.setProperty("DYNO", "-");

0 commit comments

Comments
 (0)