Skip to content

Commit c971276

Browse files
committed
Refine default filtered headers for web data binding
Prior to this commit, HTTP request data binding had been improved to filter out by default the "Priority" header in #34039. This commit extends the set of filtered header names with: "Accept", "Authorization", "Connection", "Cookie", "From", "Host", "Origin", "Priority", "Range", "Referer", "Upgrade". If an application wishes to let those header be bound, it will need to configure the binder and replace the default header predicate by calling `setHeaderPredicate`. Closes gh-34182
1 parent cd2fbb1 commit c971276

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ExtendedWebExchangeDataBinder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,8 @@
4343
*/
4444
public class ExtendedWebExchangeDataBinder extends WebExchangeDataBinder {
4545

46-
private static final Set<String> FILTERED_HEADER_NAMES = Set.of("Priority");
46+
private static final Set<String> FILTERED_HEADER_NAMES = Set.of("Accept", "Authorization", "Connection",
47+
"Cookie", "From", "Host", "Origin", "Priority", "Range", "Referer", "Upgrade");
4748

4849

4950
private Predicate<String> headerPredicate = name -> !FILTERED_HEADER_NAMES.contains(name);

Diff for: spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/InitBinderBindingContextTests.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424

2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.ValueSource;
2628

2729
import org.springframework.beans.testfixture.beans.TestBean;
2830
import org.springframework.core.DefaultParameterNameDiscoverer;
@@ -220,6 +222,23 @@ void headerPredicate() throws Exception {
220222
assertThat(map).containsExactlyInAnyOrderEntriesOf(Map.of("someIntArray", "1", "Some-Int-Array", "1"));
221223
}
222224

225+
@ParameterizedTest
226+
@ValueSource(strings = {"Accept", "Authorization", "Connection",
227+
"Cookie", "From", "Host", "Origin", "Priority", "Range", "Referer", "Upgrade"})
228+
void filteredHeaders(String headerName) throws Exception {
229+
MockServerHttpRequest request = MockServerHttpRequest.get("/path")
230+
.header(headerName, "u1")
231+
.build();
232+
233+
MockServerWebExchange exchange = MockServerWebExchange.from(request);
234+
235+
BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class);
236+
ExtendedWebExchangeDataBinder binder = (ExtendedWebExchangeDataBinder) context.createDataBinder(exchange, null, "", null);
237+
238+
Map<String, Object> map = binder.getValuesToBind(exchange).block();
239+
assertThat(map).isEmpty();
240+
}
241+
223242
private BindingContext createBindingContext(String methodName, Class<?>... parameterTypes) throws Exception {
224243
Object handler = new InitBinderHandler();
225244
Method method = handler.getClass().getMethod(methodName, parameterTypes);

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExtendedServletRequestDataBinder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,7 +53,8 @@
5353
*/
5454
public class ExtendedServletRequestDataBinder extends ServletRequestDataBinder {
5555

56-
private static final Set<String> FILTERED_HEADER_NAMES = Set.of("Priority");
56+
private static final Set<String> FILTERED_HEADER_NAMES = Set.of("Accept", "Authorization", "Connection",
57+
"Cookie", "From", "Host", "Origin", "Priority", "Range", "Referer", "Upgrade");
5758

5859

5960
private Predicate<String> headerPredicate = name -> !FILTERED_HEADER_NAMES.contains(name);

Diff for: spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExtendedServletRequestDataBinderTests.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121
import jakarta.servlet.ServletRequest;
2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
2426

2527
import org.springframework.beans.MutablePropertyValues;
2628
import org.springframework.beans.testfixture.beans.TestBean;
@@ -104,6 +106,19 @@ void uriVarsAndHeadersAddedConditionally() {
104106
assertThat(target.getAge()).isEqualTo(25);
105107
}
106108

109+
@ParameterizedTest
110+
@ValueSource(strings = {"Accept", "Authorization", "Connection",
111+
"Cookie", "From", "Host", "Origin", "Priority", "Range", "Referer", "Upgrade"})
112+
void filteredHeaders(String headerName) {
113+
TestBinder binder = new TestBinder();
114+
115+
MutablePropertyValues mpvs = new MutablePropertyValues();
116+
request.addHeader(headerName, "u1");
117+
binder.addBindValues(mpvs, request);
118+
119+
assertThat(mpvs).isEmpty();
120+
}
121+
107122
@Test
108123
void headerPredicate() {
109124
TestBinder binder = new TestBinder();

0 commit comments

Comments
 (0)