Class ContentSecurityPolicy

java.lang.Object
com.jalios.jcms.security.ContentSecurityPolicy

public class ContentSecurityPolicy extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
    Build a new ContentSecurityPolicy from specified header.
    ContentSecurityPolicy(String header, boolean isReportOnly)
    Build a new ContentSecurityPolicy from specified header
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addFetchDirectiveHosts(String kindStr, String hostsStr)
    Add a custom host to fetch directive to this policy.
    Return the policy header to send, replacing any nonce pattern with the specified nonce value.
    com.shapesecurity.salvation2.Policy
    Return the underlying Policy object from the salvation API.
    boolean
    Check if this CSP policy is empty (no directives were declared) or not
    boolean
    Check if this policy was created for the report-only policy
    Return the RAW unformatted header value (without any valid nonce), for debugging purpose.
    boolean
    Check if this policy use nonce pattern "nonce-NONCEVALUE" that should be replaced at runtime with a value unique for each request/response.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ContentSecurityPolicy

      public ContentSecurityPolicy(String header)
      Build a new ContentSecurityPolicy from specified header.
      Parameters:
      header - the RAW header value of this policy
    • ContentSecurityPolicy

      public ContentSecurityPolicy(String header, boolean isReportOnly)
      Build a new ContentSecurityPolicy from specified header
      Parameters:
      header - the RAW header value of this policy
      isReportOnly - false for an enforced policy, true for a report-only police
  • Method Details

    • addFetchDirectiveHosts

      public void addFetchDirectiveHosts(String kindStr, String hostsStr)
      Add a custom host to fetch directive to this policy.

      Note: If this policy is empty, the directive is silently ignored.

      Parameters:
      kindStr - the name of the fetch directive (e.g. : "img-src", "frame-src", ...)
      hostsStr - one or more hosts to add, separated by spaces (e.g. : "https://*.example.com https://cdn.example.com/sub/path/")
    • isEmpty

      public boolean isEmpty()
      Check if this CSP policy is empty (no directives were declared) or not
      Returns:
      true if no directives were specified, false if at least one directive was specified
    • getHeader

      public String getHeader(String nonce)
      Return the policy header to send, replacing any nonce pattern with the specified nonce value.
      Parameters:
      nonce - nonce value which MUST BE unique for each request/response
      Returns:
      a value suitable to be sent as HTTP header, may return empty string if CSP was not configured
    • getPolicy

      public com.shapesecurity.salvation2.Policy getPolicy()
      Return the underlying Policy object from the salvation API.

      Warning : this method may be removed at any time.
      To modify the policy, use utility method : addFetchDirectiveHosts(String, String).

      Returns:
      a Policy object from the salvation API. MAY return null policy is empty
    • isReportOnly

      public boolean isReportOnly()
      Check if this policy was created for the report-only policy
      Returns:
      true if report only, false if enforced polic
    • useNonce

      public boolean useNonce()
      Check if this policy use nonce pattern "nonce-NONCEVALUE" that should be replaced at runtime with a value unique for each request/response.
      Returns:
      true if there is a nonce to replace, false otherwise
    • toString

      public String toString()
      Return the RAW unformatted header value (without any valid nonce), for debugging purpose.

      DO NOT use this method to retrieve the value to be sent in real header

      Overrides:
      toString in class Object