Class ContentSecurityPolicy

java.lang.Object
com.vaadin.flow.server.ContentSecurityPolicy
All Implemented Interfaces:
Serializable

public class ContentSecurityPolicy extends Object implements Serializable
A mutable Content Security Policy, i.e. a set of directives and the sources of each directive, that is serialized as the value of a Content-Security-Policy HTTP header.

Directive names are case-insensitive and kept in lower case. Directives are written to the header in the order they were first set. Sources are written as given, so keywords need their quotes, for example "'self'" or "'none'".

In the CspMode.STRICT mode, Vaadin creates the policy for each index HTML response and exposes it through IndexHtmlResponse.getContentSecurityPolicy(), so that an IndexHtmlRequestListener can adjust it before the header is sent. In that policy, script-src holds the nonce of the response and should be left unchanged, see IndexHtmlResponse.getContentSecurityPolicy().

Since:
25.4
See Also:
  • Constructor Details

    • ContentSecurityPolicy

      public ContentSecurityPolicy()
  • Method Details

    • setDirective

      public ContentSecurityPolicy setDirective(String directive, String... sources)
      Sets the sources of a directive, replacing any sources it had before. Passing no sources sets a directive without a value, such as upgrade-insecure-requests.
      Parameters:
      directive - the directive name, e.g. "img-src", not null
      sources - the sources, e.g. "'self'" or "https://cdn.example.com", not null
      Returns:
      this policy, for chaining
      Throws:
      IllegalArgumentException - if the directive name or a source is not valid in a header
    • addSources

      public ContentSecurityPolicy addSources(String directive, String... sources)
      Adds sources to a directive, keeping the sources it already has. The directive is created if it is not set. A source the directive already has is not added again.
      Parameters:
      directive - the directive name, e.g. "connect-src", not null
      sources - the sources to add, not null
      Returns:
      this policy, for chaining
      Throws:
      IllegalArgumentException - if the directive name or a source is not valid in a header
    • removeDirective

      public ContentSecurityPolicy removeDirective(String directive)
      Removes a directive. Nothing happens if the directive is not set.
      Parameters:
      directive - the directive name, not null
      Returns:
      this policy, for chaining
    • getSources

      public List<String> getSources(String directive)
      Gets the sources of a directive.
      Parameters:
      directive - the directive name, not null
      Returns:
      an unmodifiable list of the sources, empty if the directive is not set or has no value
    • toHeaderValue

      public String toHeaderValue()
      Serializes the policy to the value of a Content-Security-Policy header.
      Returns:
      the header value, empty if no directive is set
    • toString

      public String toString()
      Overrides:
      toString in class Object