Package com.jalios.jcms.security
Class ContentSecurityPolicy
java.lang.Object
com.jalios.jcms.security.ContentSecurityPolicy
-
Constructor Summary
ConstructorsConstructorDescriptionContentSecurityPolicy(String header) Build a new ContentSecurityPolicy from specified header.ContentSecurityPolicy(String header, boolean isReportOnly) Build a new ContentSecurityPolicy from specified header -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFetchDirectiveHosts(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.PolicyReturn the underlying Policy object from the salvation API.booleanisEmpty()Check if this CSP policy is empty (no directives were declared) or notbooleanCheck if this policy was created for the report-only policytoString()Return the RAW unformatted header value (without any valid nonce), for debugging purpose.booleanuseNonce()Check if this policy use nonce pattern"nonce-NONCEVALUE"that should be replaced at runtime with a value unique for each request/response.
-
Constructor Details
-
ContentSecurityPolicy
Build a new ContentSecurityPolicy from specified header.- Parameters:
header- the RAW header value of this policy
-
ContentSecurityPolicy
Build a new ContentSecurityPolicy from specified header- Parameters:
header- the RAW header value of this policyisReportOnly- false for an enforced policy, true for a report-only police
-
-
Method Details
-
addFetchDirectiveHosts
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
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
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
-