Class ContentSecurityPolicyServerHttpHeadersWriter
- All Implemented Interfaces:
ServerHttpHeadersWriter
Content-Security-Policy response header with configured policy
directives.
With related directives specified, web clients could block inline <script> or
<style> blocks in the HTML to mitigate XSS attacks injecting malicious inline
blocks. To allow intended inline blocks, a CSP directive (usually script-src or
style-src) may specify a hard-to-guess nonce matching the nonce attributes of
inline HTML blocks.
To ease writing nonce-based CSP headers, this class replaces the {nonce}
placeholder in the policyDirectives with a real nonce value read from a
request attribute. A
ContentSecurityPolicyNonceGeneratingWebFilter can be configured to generate a
unique secure random ContentSecurityPolicyNonce for each request.
For example, if the configured policyDirectives is script-src 'self'
'nonce-{nonce}', and a ContentSecurityPolicyNonceGeneratingWebFilter has set a
ContentSecurityPolicyNonce of "Nc3n83cnSAd3wc3Sasdfn9", then the
written HTTP header value would be
script-src 'self' 'nonce-Nc3n83cnSAd3wc3Sasdfn9'.
- Since:
- 5.1
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidsetExchangeMatcher(ServerWebExchangeMatcher exchangeMatcher) Sets theServerWebExchangeMatcherwhich determines whether CSP should be written.voidsetPolicyDirectives(String policyDirectives) Set the policy directive(s) to be used in the response header.voidsetReportOnly(boolean reportOnly) Set whether to include theContent-Security-Policy-Report-Onlyheader in the response.reactor.core.publisher.Mono<Void> writeHttpHeaders(org.springframework.web.server.ServerWebExchange exchange) Write the headers to the response.
-
Field Details
-
CONTENT_SECURITY_POLICY
- See Also:
-
CONTENT_SECURITY_POLICY_REPORT_ONLY
- See Also:
-
-
Constructor Details
-
ContentSecurityPolicyServerHttpHeadersWriter
public ContentSecurityPolicyServerHttpHeadersWriter()
-
-
Method Details
-
writeHttpHeaders
public reactor.core.publisher.Mono<Void> writeHttpHeaders(org.springframework.web.server.ServerWebExchange exchange) Description copied from interface:ServerHttpHeadersWriterWrite the headers to the response.- Specified by:
writeHttpHeadersin interfaceServerHttpHeadersWriter- Parameters:
exchange-- Returns:
- A Mono which is returned to the
Supplierof theReactiveHttpOutputMessage.beforeCommit(Supplier).
-
setExchangeMatcher
Sets theServerWebExchangeMatcherwhich determines whether CSP should be written. The default is to write CSP unconditionally.- Parameters:
exchangeMatcher- theServerWebExchangeMatcherto use- Throws:
IllegalArgumentException- ifexchangeMatcheris null- Since:
- 7.2
-
setPolicyDirectives
Set the policy directive(s) to be used in the response header. ThepolicyDirectivesmay contain{nonce}as placeholders to be replaced.- Parameters:
policyDirectives- the policy directive(s)- Throws:
IllegalArgumentException- if policyDirectives isnullor empty
-
setReportOnly
public void setReportOnly(boolean reportOnly) Set whether to include theContent-Security-Policy-Report-Onlyheader in the response. Otherwise, defaults to theContent-Security-Policyheader.- Parameters:
reportOnly- whether to only report policy violations
-