Package com.vaadin.flow.server
Class ContentSecurityPolicy
java.lang.Object
com.vaadin.flow.server.ContentSecurityPolicy
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddSources(String directive, String... sources) Adds sources to a directive, keeping the sources it already has.getSources(String directive) Gets the sources of a directive.removeDirective(String directive) Removes a directive.setDirective(String directive, String... sources) Sets the sources of a directive, replacing any sources it had before.Serializes the policy to the value of aContent-Security-Policyheader.toString()
-
Constructor Details
-
ContentSecurityPolicy
public ContentSecurityPolicy()
-
-
Method Details
-
setDirective
Sets the sources of a directive, replacing any sources it had before. Passing no sources sets a directive without a value, such asupgrade-insecure-requests.- Parameters:
directive- the directive name, e.g."img-src", not nullsources- 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
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 nullsources- 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
Removes a directive. Nothing happens if the directive is not set.- Parameters:
directive- the directive name, not null- Returns:
- this policy, for chaining
-
getSources
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
Serializes the policy to the value of aContent-Security-Policyheader.- Returns:
- the header value, empty if no directive is set
-
toString
-