Class ContentSecurityPolicyHeaderWriter

java.lang.Object
org.springframework.security.web.header.writers.ContentSecurityPolicyHeaderWriter
All Implemented Interfaces:
HeaderWriter

public final class ContentSecurityPolicyHeaderWriter extends Object implements HeaderWriter

Provides support for Content Security Policy (CSP) Level 2.

CSP provides a mechanism for web applications to mitigate content injection vulnerabilities, such as cross-site scripting (XSS). CSP is a declarative policy that allows web application authors to inform the client (user-agent) about the sources from which the application expects to load resources.

For example, a web application can declare that it only expects to load script from specific, trusted sources. This declaration allows the client to detect and block malicious scripts injected into the application by an attacker.

A declaration of a security policy contains a set of security policy directives (for example, script-src and object-src), each responsible for declaring the restrictions for a particular resource type. The list of directives defined can be found at Directives.

Each directive has a name and value. For detailed syntax on writing security policies, see Syntax and Algorithms.

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 servlet request attribute. A ContentSecurityPolicyNonceGeneratingFilter can be configured to generate a unique secure random ContentSecurityPolicyNonce attribute for each request.

For example, if the configured policyDirectives is script-src 'self' 'nonce-{nonce}', and a ContentSecurityPolicyNonceGeneratingFilter has set a ContentSecurityPolicyNonce of "Nc3n83cnSAd3wc3Sasdfn9", then the written HTTP header value would be script-src 'self' 'nonce-Nc3n83cnSAd3wc3Sasdfn9'.

This implementation of HeaderWriter writes one of the following headers:

  • Content-Security-Policy
  • Content-Security-Policy-Report-Only

By default, the Content-Security-Policy header is included in the response. However, calling setReportOnly(boolean) with true will include the Content-Security-Policy-Report-Only header in the response. NOTE: The supplied security policy directive(s) will be used for whichever header is enabled (included).

CSP is not intended as a first line of defense against content injection vulnerabilities. Instead, CSP is used to reduce the harm caused by content injection attacks. As a first line of defense against content injection, web application authors should validate their input and encode their output.

Since:
4.1
See Also:
  • Constructor Details

    • ContentSecurityPolicyHeaderWriter

      public ContentSecurityPolicyHeaderWriter()
      Creates a new instance. Default value: default-src 'self'
    • ContentSecurityPolicyHeaderWriter

      public ContentSecurityPolicyHeaderWriter(String policyDirectives)
      Creates a new instance.
      Parameters:
      policyDirectives - maps to setPolicyDirectives(String)
      Throws:
      IllegalArgumentException - if policyDirectives is null or empty
  • Method Details

    • writeHeaders

      public void writeHeaders(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
      Writes the Content-Security-Policy (or report-only) header to the response.
      Specified by:
      writeHeaders in interface HeaderWriter
      Parameters:
      request - the request
      response - the response
      See Also:
    • setRequestMatcher

      public void setRequestMatcher(RequestMatcher requestMatcher)
      Sets the RequestMatcher which determines whether CSP should be written. The default is to write CSP unconditionally.
      Parameters:
      requestMatcher - the RequestMatcher to use
      Throws:
      IllegalArgumentException - if requestMatcher is null
      Since:
      7.2
    • setPolicyDirectives

      public void setPolicyDirectives(String policyDirectives)
      Sets the security policy directive(s) to be used in the response header. The policyDirectives may contain {nonce} as placeholders to be replaced.
      Parameters:
      policyDirectives - the security policy directive(s)
      Throws:
      IllegalArgumentException - if policyDirectives is null or empty
    • setReportOnly

      public void setReportOnly(boolean reportOnly)
      If true, includes the Content-Security-Policy-Report-Only header in the response, otherwise, defaults to the Content-Security-Policy header.
      Parameters:
      reportOnly - set to true for reporting policy violations only
    • toString

      public String toString()
      Overrides:
      toString in class Object