Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
238c22cd
Commit
238c22cd
authored
Nov 16, 2016
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish content security policy contribution
See gh-7373 See gh-7373
parent
d7bbea63
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
15 deletions
+23
-15
SecurityProperties.java
...ework/boot/autoconfigure/security/SecurityProperties.java
+7
-4
SpringBootWebSecurityConfiguration.java
...onfigure/security/SpringBootWebSecurityConfiguration.java
+6
-4
SpringBootWebSecurityConfigurationTests.java
...ure/security/SpringBootWebSecurityConfigurationTests.java
+10
-7
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java
View file @
238c22cd
...
...
@@ -175,15 +175,17 @@ public class SecurityProperties implements SecurityPrerequisite {
}
public
enum
ContentSecurityPolicyMode
{
/**
* Use the
{@code Content-Security-Policy}
header.
* Use the
'Content-Security-Policy'
header.
*/
DEFAULT
,
/**
* Use the
{@code Content-Security-Policy-Report-Only}
header.
* Use the
'Content-Security-Policy-Report-Only'
header.
*/
REPORT_ONLY
}
/**
...
...
@@ -212,7 +214,7 @@ public class SecurityProperties implements SecurityPrerequisite {
private
String
contentSecurityPolicy
;
/**
*
Whether to use the "Content-Security-Policy" or "Content-Security-Policy-Report-Only" header
.
*
Security policy mode
.
*/
private
ContentSecurityPolicyMode
contentSecurityPolicyMode
=
ContentSecurityPolicyMode
.
DEFAULT
;
...
...
@@ -265,7 +267,8 @@ public class SecurityProperties implements SecurityPrerequisite {
return
this
.
contentSecurityPolicyMode
;
}
public
void
setContentSecurityPolicyMode
(
ContentSecurityPolicyMode
contentSecurityPolicyMode
)
{
public
void
setContentSecurityPolicyMode
(
ContentSecurityPolicyMode
contentSecurityPolicyMode
)
{
this
.
contentSecurityPolicyMode
=
contentSecurityPolicyMode
;
}
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SpringBootWebSecurityConfiguration.java
View file @
238c22cd
...
...
@@ -29,6 +29,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
;
import
org.springframework.boot.autoconfigure.security.SecurityProperties.Headers
;
import
org.springframework.boot.autoconfigure.security.SecurityProperties.Headers.ContentSecurityPolicyMode
;
import
org.springframework.boot.autoconfigure.web.ErrorController
;
import
org.springframework.boot.autoconfigure.web.ServerProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
...
...
@@ -109,12 +110,13 @@ public class SpringBootWebSecurityConfiguration {
configurer
.
contentTypeOptions
().
disable
();
}
if
(
StringUtils
.
hasText
(
headers
.
getContentSecurityPolicy
()))
{
if
(
headers
.
getContentSecurityPolicyMode
()
==
Headers
.
ContentSecurityPolicyMode
.
DEFAULT
)
{
configurer
.
contentSecurityPolicy
(
headers
.
getContentSecurityPolicy
());
String
policyDirectives
=
headers
.
getContentSecurityPolicy
();
ContentSecurityPolicyMode
mode
=
headers
.
getContentSecurityPolicyMode
();
if
(
mode
==
ContentSecurityPolicyMode
.
DEFAULT
)
{
configurer
.
contentSecurityPolicy
(
policyDirectives
);
}
else
{
assert
headers
.
getContentSecurityPolicyMode
()
==
Headers
.
ContentSecurityPolicyMode
.
REPORT_ONLY
;
configurer
.
contentSecurityPolicy
(
headers
.
getContentSecurityPolicy
()).
reportOnly
();
configurer
.
contentSecurityPolicy
(
policyDirectives
).
reportOnly
();
}
}
if
(!
headers
.
isXss
())
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SpringBootWebSecurityConfigurationTests.java
View file @
238c22cd
...
...
@@ -214,7 +214,8 @@ public class SpringBootWebSecurityConfigurationTests {
is
(
notNullValue
())))
.
andExpect
(
MockMvcResultMatchers
.
header
().
string
(
"X-Frame-Options"
,
is
(
notNullValue
())))
.
andExpect
(
MockMvcResultMatchers
.
header
().
doesNotExist
(
"Content-Security-Policy"
));
.
andExpect
(
MockMvcResultMatchers
.
header
()
.
doesNotExist
(
"Content-Security-Policy"
));
}
@Test
...
...
@@ -250,9 +251,10 @@ public class SpringBootWebSecurityConfigurationTests {
.
getBean
(
"springSecurityFilterChain"
,
Filter
.
class
))
.
build
();
mockMvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/"
))
.
andExpect
(
MockMvcResultMatchers
.
header
().
string
(
"Content-Security-Policy"
,
is
(
"default-src 'self';"
)))
.
andExpect
(
MockMvcResultMatchers
.
header
().
doesNotExist
(
"Content-Security-Policy-Report-Only"
));
.
andExpect
(
MockMvcResultMatchers
.
header
()
.
string
(
"Content-Security-Policy"
,
is
(
"default-src 'self';"
)))
.
andExpect
(
MockMvcResultMatchers
.
header
()
.
doesNotExist
(
"Content-Security-Policy-Report-Only"
));
}
@Test
...
...
@@ -266,9 +268,10 @@ public class SpringBootWebSecurityConfigurationTests {
.
getBean
(
"springSecurityFilterChain"
,
Filter
.
class
))
.
build
();
mockMvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/"
))
.
andExpect
(
MockMvcResultMatchers
.
header
().
string
(
"Content-Security-Policy-Report-Only"
,
is
(
"default-src 'self';"
)))
.
andExpect
(
MockMvcResultMatchers
.
header
().
doesNotExist
(
"Content-Security-Policy"
));
.
andExpect
(
MockMvcResultMatchers
.
header
().
string
(
"Content-Security-Policy-Report-Only"
,
is
(
"default-src 'self';"
)))
.
andExpect
(
MockMvcResultMatchers
.
header
()
.
doesNotExist
(
"Content-Security-Policy"
));
}
@Configuration
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment