Class ContentSecurityPolicyManager

java.lang.Object
com.jalios.jcms.security.ContentSecurityPolicyManager
All Implemented Interfaces:
JcmsConstants, JaliosConstants, JPropertiesListener

public class ContentSecurityPolicyManager extends Object implements JcmsConstants, JPropertiesListener
Content Security Policy implementation in JCMS.

The following properties can be defined to configure this implementation :

 # Is CSP enabled for this site ?
 # default is true since JPlatform 11
 channel.security.csp.enabled: true
 
 # Value of the "Content-Security-Policy" HTTP Header.
 # If present, string 'NONCEVALUE' is replaced for each request with a random nonce value.  
 # default is empty as a fine tuning is required for each site
 channel.security.csp.csp-header: 
 
 # Value of the "Content-Security-Policy-Report-Only" HTTP Header.
 # If present, string 'NONCEVALUE' is replaced for each request with a random nonce value.  
 # default is empty as a fine tuning is required for each site
 channel.security.csp.csp-report-only-header: default-src 'self'; script-src 'self' 'nonce-NONCEVALUE'; report-uri https://endpoint.example.com; report-to endpoint_name
 
 # Space or coma separated list of resource path(s) excluded from CSP processing.
 # Use with care, you should not have to exclude anything !!!
 # default is empty
 # Examples of resource paths :
 #  "https://host.example.com/mywebapp/subdir/my.jsp?x=y"       : subdir/my.jsp
 #  "https://host.example.com/mywebapp/subdir/images/test.gif   : subdir/images/test.gif
 #  "https://host.example.com/mywebapp/MyServlet/action?x=y     : MyServlet/action
 channel.security.csp.excluded-path:
 
 
Since:
jcms-9.0
  • Field Details

    • CSP_NONCE_REQUEST_ATTRIBUTE

      public static final String CSP_NONCE_REQUEST_ATTRIBUTE
      Request name attribute used to store the nonce computed for a request.

      Corresponding value is a String.

      See Also:
    • CSP_INITIALIZED_REQUEST_ATTRIBUTE

      public static final String CSP_INITIALIZED_REQUEST_ATTRIBUTE
      Request name attribute indicating CSP was initialized for a request.

      Corresponding value is a Boolean.

      See Also:
    • CSP_HTTP_HEADER

      public static final String CSP_HTTP_HEADER
      Name of the HTTP header used to send CSP policy.
      See Also:
    • CSP_REPORT_ONLY_HTTP_HEADER

      public static final String CSP_REPORT_ONLY_HTTP_HEADER
      Name of the HTTP header used to send the CSP policy used in report only mode.
      See Also:
  • Method Details

    • getConfiguration

      Retrieve the currenly active configuration.

      DO NOT use this method to modify the configuration. Use ContentSecurityPolicyFilter.

      Returns:
      the current configuration, never null
    • getInstance

      public static ContentSecurityPolicyManager getInstance()
      Retrieve the singleton instance of this manager.
      Returns:
      the ContentSecurityPolicyManager singleton
    • propertiesChange

      public void propertiesChange(JProperties properties)
      Description copied from interface: JPropertiesListener
      Invoked after properties have been modified in JCMS and save on disk.

      You cannot alter the value received in parameters.

      Note that properties parameter may be null, a limited set of site properties, or all site properties.

      To check that a property has been modified, reload the "current" property instead using channel.getProperties or channel.getProperty.

      Specified by:
      propertiesChange in interface JPropertiesListener
      Parameters:
      properties - the properties which have been submitted to change
    • configure

      public static void configure(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
      Initialize the Content Security Policy for the specified request/response.
      • Build a nonce value for use in HTML tags
      • Add appropriate HTTP headers to the response according to site configuration
      This method is reentrant : process will only performed once per request even if invoked several times.
      Parameters:
      request - the current HttpServletRequest, must not be null
      response - the current HttpServletResponse, must not be null
      Since:
      jcms-9.0
    • getNonce

      public static String getNonce(javax.servlet.http.HttpServletRequest request)
      Retrieve the Content Security Policy nonce value to use in HTML scripts tags.
      Parameters:
      request - the current request in which nonce was initialized by configure(HttpServletRequest, HttpServletResponse)
      Returns:
      a nonce or an empty string if CSP was not initialized, never return null
      Since:
      9.0
    • canAccessCspAdmin

      public boolean canAccessCspAdmin(Member mbr)
      Check if the specified member is authorized to access CSP admin console.
      Parameters:
      mbr - the mbr to check
      Returns:
      true if authorized false otherwise