Skip to content

Commit c12a3f4

Browse files
committed
Support explicitly setting forward headers strategy to NONE
Prior to this commit, there was no distinction between explicitly setting forward headers strategy to a value of NONE and not setting it at all. This meant that in a cloud environment, a cloud provider was always checked to see if it was active and using forward headers and there was no way to prevent that. This commit changes the default value of the property to null so that there is a way to determine if the property was explicitly set to NONE. Fixes gh-19333
1 parent 8e285a4 commit c12a3f4

File tree

9 files changed

+76
-5
lines changed

9 files changed

+76
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class ServerProperties {
8484
/**
8585
* Strategy for handling X-Forwarded-* headers.
8686
*/
87-
private ForwardHeadersStrategy forwardHeadersStrategy = ForwardHeadersStrategy.NONE;
87+
private ForwardHeadersStrategy forwardHeadersStrategy;
8888

8989
/**
9090
* Value to use for the Server response header (if empty, no header is sent).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private boolean isPositive(Integer value) {
102102
}
103103

104104
private boolean getOrDeduceUseForwardHeaders() {
105-
if (this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NONE)) {
105+
if (this.serverProperties.getForwardHeadersStrategy() == null) {
106106
CloudPlatform platform = CloudPlatform.getActive(this.environment);
107107
return platform != null && platform.isUsingForwardHeaders();
108108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void customize(NettyReactiveWebServerFactory factory) {
6868
}
6969

7070
private boolean getOrDeduceUseForwardHeaders() {
71-
if (this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NONE)) {
71+
if (this.serverProperties.getForwardHeadersStrategy() == null) {
7272
CloudPlatform platform = CloudPlatform.getActive(this.environment);
7373
return platform != null && platform.isUsingForwardHeaders();
7474
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void customizeRemoteIpValve(ConfigurableTomcatWebServerFactory factory)
201201
}
202202

203203
private boolean getOrDeduceUseForwardHeaders() {
204-
if (this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NONE)) {
204+
if (this.serverProperties.getForwardHeadersStrategy() == null) {
205205
CloudPlatform platform = CloudPlatform.getActive(this.environment);
206206
return platform != null && platform.isUsingForwardHeaders();
207207
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void mapAccessLogProperties(ConfigurableUndertowWebServerFactory factory
123123
}
124124

125125
private boolean getOrDeduceUseForwardHeaders() {
126-
if (this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NONE)) {
126+
if (this.serverProperties.getForwardHeadersStrategy() == null) {
127127
CloudPlatform platform = CloudPlatform.getActive(this.environment);
128128
return platform != null && platform.isUsingForwardHeaders();
129129
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ void defaultUseForwardHeaders() {
8686
verify(factory).setUseForwardHeaders(false);
8787
}
8888

89+
@Test
90+
void forwardHeadersWhenStrategyIsNativeShouldConfigureValve() {
91+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NATIVE);
92+
ConfigurableJettyWebServerFactory factory = mock(ConfigurableJettyWebServerFactory.class);
93+
this.customizer.customize(factory);
94+
verify(factory).setUseForwardHeaders(true);
95+
}
96+
97+
@Test
98+
void forwardHeadersWhenStrategyIsNoneShouldNotConfigureValve() {
99+
this.environment.setProperty("DYNO", "-");
100+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NONE);
101+
ConfigurableJettyWebServerFactory factory = mock(ConfigurableJettyWebServerFactory.class);
102+
this.customizer.customize(factory);
103+
verify(factory).setUseForwardHeaders(false);
104+
}
105+
89106
@Test
90107
void accessLogCanBeCustomized() throws IOException {
91108
File logFile = File.createTempFile("jetty_log", ".log");

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ void setUseForwardHeaders() {
9292
verify(factory).setUseForwardHeaders(true);
9393
}
9494

95+
@Test
96+
void forwardHeadersWhenStrategyIsNativeShouldConfigureValve() {
97+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NATIVE);
98+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
99+
this.customizer.customize(factory);
100+
verify(factory).setUseForwardHeaders(true);
101+
}
102+
103+
@Test
104+
void forwardHeadersWhenStrategyIsNoneShouldNotConfigureValve() {
105+
this.environment.setProperty("DYNO", "-");
106+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NONE);
107+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
108+
this.customizer.customize(factory);
109+
verify(factory).setUseForwardHeaders(false);
110+
}
111+
95112
@Test
96113
void setServerConnectionTimeoutAsZero() {
97114
setupServerConnectionTimeout(Duration.ZERO);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,26 @@ void deduceUseForwardHeaders() {
234234
testRemoteIpValveConfigured();
235235
}
236236

237+
@Test
238+
void defaultUseForwardHeaders() {
239+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
240+
assertThat(factory.getEngineValves()).hasSize(0);
241+
}
242+
243+
@Test
244+
void forwardHeadersWhenStrategyIsNativeShouldConfigureValve() {
245+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NATIVE);
246+
testRemoteIpValveConfigured();
247+
}
248+
249+
@Test
250+
void forwardHeadersWhenStrategyIsNoneShouldNotConfigureValve() {
251+
this.environment.setProperty("DYNO", "-");
252+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NONE);
253+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
254+
assertThat(factory.getEngineValves()).hasSize(0);
255+
}
256+
237257
@Test
238258
void defaultRemoteIpValve() {
239259
// Since 1.1.7 you need to specify at least the protocol

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ void setUseForwardHeaders() {
202202
verify(factory).setUseForwardHeaders(true);
203203
}
204204

205+
@Test
206+
void forwardHeadersWhenStrategyIsNativeShouldConfigureValve() {
207+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NATIVE);
208+
ConfigurableUndertowWebServerFactory factory = mock(ConfigurableUndertowWebServerFactory.class);
209+
this.customizer.customize(factory);
210+
verify(factory).setUseForwardHeaders(true);
211+
}
212+
213+
@Test
214+
void forwardHeadersWhenStrategyIsNoneShouldNotConfigureValve() {
215+
this.environment.setProperty("DYNO", "-");
216+
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NONE);
217+
ConfigurableUndertowWebServerFactory factory = mock(ConfigurableUndertowWebServerFactory.class);
218+
this.customizer.customize(factory);
219+
verify(factory).setUseForwardHeaders(false);
220+
}
221+
205222
private <T> T boundServerOption(Option<T> option) {
206223
Builder builder = Undertow.builder();
207224
ConfigurableUndertowWebServerFactory factory = mockFactory(builder);

0 commit comments

Comments
 (0)