Skip to content

Commit 55d55a9

Browse files
committed
Support nonce-based CSP in XML namespace config
Signed-off-by: Ziqin Wang <[email protected]>
1 parent 9ebbf70 commit 55d55a9

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎config/src/main/java/org/springframework/security/config/http/HeadersBeanDefinitionParser.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.beans.factory.support.ManagedList;
3434
import org.springframework.beans.factory.xml.BeanDefinitionParser;
3535
import org.springframework.beans.factory.xml.ParserContext;
36+
import org.springframework.security.web.header.ContentSecurityPolicyNonceGeneratingFilter;
3637
import org.springframework.security.web.header.HeaderWriterFilter;
3738
import org.springframework.security.web.header.writers.CacheControlHeadersWriter;
3839
import org.springframework.security.web.header.writers.ContentSecurityPolicyHeaderWriter;
@@ -54,6 +55,7 @@
5455
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
5556
import org.springframework.util.StringUtils;
5657
import org.springframework.util.xml.DomUtils;
58+
import org.springframework.web.filter.CompositeFilter;
5759

5860
/**
5961
* Parser for the {@code HeadersFilter}.
@@ -133,6 +135,8 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
133135

134136
private ManagedList<BeanMetadataElement> headerWriters;
135137

138+
private BeanDefinition nonceGeneratingFilter;
139+
136140
@Override
137141
public BeanDefinition parse(Element element, ParserContext parserContext) {
138142
this.headerWriters = new ManagedList<>();
@@ -164,7 +168,13 @@ else if (noWriters) {
164168
return null;
165169
}
166170
builder.addConstructorArgValue(this.headerWriters);
167-
return builder.getBeanDefinition();
171+
BeanDefinition headerWriterFilter = builder.getBeanDefinition();
172+
if (this.nonceGeneratingFilter == null) {
173+
return headerWriterFilter;
174+
}
175+
return BeanDefinitionBuilder.rootBeanDefinition(CompositeFilter.class)
176+
.addPropertyValue("filters", ManagedList.of(this.nonceGeneratingFilter, headerWriterFilter))
177+
.getBeanDefinition();
168178
}
169179

170180
/**
@@ -323,6 +333,9 @@ private void addContentSecurityPolicy(Element contentSecurityPolicyElement, Pars
323333
headersWriter.addPropertyValue("reportOnly", reportOnly);
324334
}
325335
this.headerWriters.add(headersWriter.getBeanDefinition());
336+
this.nonceGeneratingFilter = BeanDefinitionBuilder
337+
.rootBeanDefinition(ContentSecurityPolicyNonceGeneratingFilter.class)
338+
.getBeanDefinition();
326339
}
327340

328341
private void parseReferrerPolicyElement(Element element, ParserContext context) {

‎config/src/test/java/org/springframework/security/config/http/HttpHeadersConfigTests.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import static org.assertj.core.api.Assertions.assertThat;
4646
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
47+
import static org.hamcrest.Matchers.matchesRegex;
4748
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
4849
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4950
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@@ -708,6 +709,16 @@ public void configureWhenContentSecurityPolicyConfiguredWithEmptyDirectivesThenA
708709
() -> this.spring.configLocations(this.xml("ContentSecurityPolicyWithEmptyDirectives")).autowire());
709710
}
710711

712+
@Test
713+
void requestWhenContentSecurityPolicyDirectivesConfiguredWithNoncePlaceholderThenHeaderContainsNonce()
714+
throws Exception {
715+
this.spring.configLocations(this.xml("ContentSecurityPolicyWithNonceBasedDirectives")).autowire();
716+
this.mvc.perform(get("/").secure(true))
717+
.andExpect(status().isOk())
718+
.andExpect(header().string("Content-Security-Policy",
719+
matchesRegex("^script-src 'self' 'nonce-[A-Za-z0-9+/]{22,}={0,2}'$")));
720+
}
721+
711722
@Test
712723
public void requestWhenContentSecurityPolicyConfiguredWithReportOnlyThenIncludesReportOnlyHeader()
713724
throws Exception {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2004-present the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://www.springframework.org/schema/security"
21+
xsi:schemaLocation="
22+
http://www.springframework.org/schema/security
23+
https://www.springframework.org/schema/security/spring-security.xsd
24+
http://www.springframework.org/schema/beans
25+
https://www.springframework.org/schema/beans/spring-beans.xsd">
26+
27+
<http auto-config="true" use-expressions="false" use-authorization-manager="false">
28+
<headers>
29+
<content-security-policy policy-directives="script-src 'self' 'nonce-{nonce}'"/>
30+
</headers>
31+
</http>
32+
33+
<b:bean name="simple" class="org.springframework.security.config.http.HttpHeadersConfigTests.SimpleController"/>
34+
35+
<b:import resource="userservice.xml"/>
36+
</b:beans>

0 commit comments

Comments
 (0)