Skip to content

Commit 4ff3345

Browse files
committed
GH-1524 Document and test request header propagation to the response
Most request headers are copied into the HTTP response. List the headers that are never copied, warn that all other headers including custom credential-like ones are echoed, and recommend spring.cloud.function.http.ignored-headers for those. Add MVC and WebFlux tests that pin the default behavior and the ignored-headers setting. No behavior changes. Resolves #1524 Signed-off-by: Oleg Zhurakousky <oleg.zhurakousky@broadcom.com>
1 parent 0b2abaa commit 4ff3345

3 files changed

Lines changed: 213 additions & 0 deletions

File tree

‎docs/modules/ROOT/pages/spring-cloud-function/standalone-web-applications.adoc‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ This will only export function composition `foo|bar` and function `baz` regardle
103103
By default most request `HttpHeaders` are copied into the response `HttpHeaders`. If you require to filter out certain headers you can provide the names of those headers using
104104
`spring.cloud.function.http.ignored-headers` delimited by comas. For example, `spring.cloud.function.http.ignored-headers=foo,bar`.
105105

106+
The following request headers are never copied into the response: `Authorization`, `Cookie`, `Set-Cookie`, `Proxy-Authorization`, `Proxy-Authenticate`, `Cache-Control`, `Content-Security-Policy`,
107+
the CORS headers (`Access-Control-*`), `Content-Length`, `Transfer-Encoding`, as well as the request-only headers `Accept`, `Content-Type` and `Host`.
108+
109+
IMPORTANT: All other request headers are copied into the response, including custom headers that may carry credentials or other sensitive values (e.g., `X-Api-Key`).
110+
Add such headers to `spring.cloud.function.http.ignored-headers` so they are not echoed back to the client.
111+
Header names in that list must be provided in lower case.
112+
106113
[[crud-rest-with-spring-cloud-function]]
107114
== CRUD REST with Spring Cloud Function
108115

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.function.web.flux;
18+
19+
import java.net.URI;
20+
import java.util.function.Function;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.boot.resttestclient.TestRestTemplate;
27+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
30+
import org.springframework.cloud.function.web.RestApplication;
31+
import org.springframework.cloud.function.web.flux.HeaderPropagationTests.TestConfiguration;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.MediaType;
35+
import org.springframework.http.RequestEntity;
36+
import org.springframework.http.ResponseEntity;
37+
import org.springframework.test.context.ContextConfiguration;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
/**
42+
* Verifies which request headers are copied into the response (reactive). See the
43+
* "Http Headers propagation" section of the web documentation.
44+
*/
45+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
46+
"spring.main.web-application-type=reactive",
47+
"spring.cloud.function.http.ignored-headers=x-skip" })
48+
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
49+
@AutoConfigureTestRestTemplate
50+
public class HeaderPropagationTests {
51+
52+
@Autowired
53+
private TestRestTemplate rest;
54+
55+
private ResponseEntity<String> post() throws Exception {
56+
return this.rest.exchange(RequestEntity.post(new URI("/echo"))
57+
.contentType(MediaType.TEXT_PLAIN)
58+
.header(HttpHeaders.AUTHORIZATION, "Basic c2VjcmV0")
59+
.header(HttpHeaders.COOKIE, "session=abc")
60+
.header(HttpHeaders.CACHE_CONTROL, "no-store")
61+
.header("X-Custom", "custom-value")
62+
.header("X-Api-Key", "key-value")
63+
.header("X-Skip", "skip-value")
64+
.body("hello"), String.class);
65+
}
66+
67+
@Test
68+
public void sensitiveHeadersAreNotCopiedIntoResponse() throws Exception {
69+
HttpHeaders headers = post().getHeaders();
70+
71+
assertThat(headers.containsHeader(HttpHeaders.AUTHORIZATION)).isFalse();
72+
assertThat(headers.containsHeader(HttpHeaders.COOKIE)).isFalse();
73+
assertThat(headers.containsHeader(HttpHeaders.CACHE_CONTROL)).isFalse();
74+
}
75+
76+
@Test
77+
public void otherRequestHeadersAreCopiedIntoResponse() throws Exception {
78+
HttpHeaders headers = post().getHeaders();
79+
80+
assertThat(headers.getFirst("x-custom")).isEqualTo("custom-value");
81+
// not in the built-in list, so it is echoed unless added to ignored-headers
82+
assertThat(headers.getFirst("x-api-key")).isEqualTo("key-value");
83+
}
84+
85+
@Test
86+
public void configuredIgnoredHeadersAreNotCopiedIntoResponse() throws Exception {
87+
HttpHeaders headers = post().getHeaders();
88+
89+
assertThat(headers.containsHeader("x-skip")).isFalse();
90+
}
91+
92+
@EnableAutoConfiguration
93+
@org.springframework.boot.test.context.TestConfiguration
94+
protected static class TestConfiguration {
95+
96+
@Bean
97+
public Function<String, String> echo() {
98+
return v -> v;
99+
}
100+
101+
}
102+
103+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.function.web.mvc;
18+
19+
import java.net.URI;
20+
import java.util.function.Function;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.boot.resttestclient.TestRestTemplate;
27+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
30+
import org.springframework.cloud.function.web.RestApplication;
31+
import org.springframework.cloud.function.web.mvc.HeaderPropagationTests.TestConfiguration;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.MediaType;
35+
import org.springframework.http.RequestEntity;
36+
import org.springframework.http.ResponseEntity;
37+
import org.springframework.test.context.ContextConfiguration;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
/**
42+
* Verifies which request headers are copied into the response (servlet). See the
43+
* "Http Headers propagation" section of the web documentation.
44+
*/
45+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
46+
"spring.main.web-application-type=servlet",
47+
"spring.cloud.function.http.ignored-headers=x-skip" })
48+
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
49+
@AutoConfigureTestRestTemplate
50+
public class HeaderPropagationTests {
51+
52+
@Autowired
53+
private TestRestTemplate rest;
54+
55+
private ResponseEntity<String> post() throws Exception {
56+
return this.rest.exchange(RequestEntity.post(new URI("/echo"))
57+
.contentType(MediaType.TEXT_PLAIN)
58+
.header(HttpHeaders.AUTHORIZATION, "Basic c2VjcmV0")
59+
.header(HttpHeaders.COOKIE, "session=abc")
60+
.header(HttpHeaders.CACHE_CONTROL, "no-store")
61+
.header("X-Custom", "custom-value")
62+
.header("X-Api-Key", "key-value")
63+
.header("X-Skip", "skip-value")
64+
.body("hello"), String.class);
65+
}
66+
67+
@Test
68+
public void sensitiveHeadersAreNotCopiedIntoResponse() throws Exception {
69+
HttpHeaders headers = post().getHeaders();
70+
71+
assertThat(headers.containsHeader(HttpHeaders.AUTHORIZATION)).isFalse();
72+
assertThat(headers.containsHeader(HttpHeaders.COOKIE)).isFalse();
73+
assertThat(headers.containsHeader(HttpHeaders.CACHE_CONTROL)).isFalse();
74+
}
75+
76+
@Test
77+
public void otherRequestHeadersAreCopiedIntoResponse() throws Exception {
78+
HttpHeaders headers = post().getHeaders();
79+
80+
assertThat(headers.getFirst("x-custom")).isEqualTo("custom-value");
81+
// not in the built-in list, so it is echoed unless added to ignored-headers
82+
assertThat(headers.getFirst("x-api-key")).isEqualTo("key-value");
83+
}
84+
85+
@Test
86+
public void configuredIgnoredHeadersAreNotCopiedIntoResponse() throws Exception {
87+
HttpHeaders headers = post().getHeaders();
88+
89+
assertThat(headers.containsHeader("x-skip")).isFalse();
90+
}
91+
92+
@EnableAutoConfiguration
93+
@org.springframework.boot.test.context.TestConfiguration
94+
protected static class TestConfiguration {
95+
96+
@Bean
97+
public Function<String, String> echo() {
98+
return v -> v;
99+
}
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)