Class ContentSecurityPolicy.Builder
- Enclosing class:
- ContentSecurityPolicy
Composes the directives defined by
CSP Level 3 into the directive source source; ...
wire format expected by the Content-Security-Policy response header. Typed setters cover the common
directives; directive(String, String...) is a generic escape hatch so new directives never require an API
change. Setting the same directive more than once replaces the previous source list.
Example:
String
Nonces:
Source helper nonce(String) renders a NONCE_PLACEHOLDER as the
token and have the REST layer rewrite it per-response via resolveNonce(String, String); the same nonce is
then stamped onto the inline
See Also:
- Since:
- 10.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe placeholder token used in nonce sources when the actual per-request nonce is not yet known.static final StringThe'none' source-expression keyword.static final StringThe'self' source-expression keyword.static final StringThe'strict-dynamic' source-expression keyword.static final StringThe'unsafe-eval' source-expression keyword.static final StringThe'unsafe-inline' source-expression keyword. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets thebase-uri directive.build()Builds the rendered policy value (the header value only, not including the header name).connectSrc(String... sources) Sets theconnect-src directive.create()Creates a new empty builder.defaultSrc(String... sources) Sets thedefault-src directive.Generic escape hatch for setting any directive by name.Sets thefont-src directive.frameAncestors(String... sources) Sets theframe-ancestors directive.static StringMints a fresh, cryptographically-random CSP nonce.static StringRenders a'sha256-<hash>' (or sha384/sha512) source expression.Sets theimg-src directive.booleanReturnstrue if this builder is configured to emit the report-only header variant.static StringRenders a'nonce-<token>' source expression.Sets theobject-src directive.Flips the rendered header to the observe-onlyContentSecurityPolicy.REPORT_ONLY_NAMEvariant.reportOnly(boolean value) Sets whether the rendered header uses the observe-onlyContentSecurityPolicy.REPORT_ONLY_NAMEvariant.Sets thereport-to directive (the CSP3 reporting-group mechanism).Sets thereport-uri directive (deprecated in CSP3 but still widely supported).static StringresolveNonce(String policyValue, String nonce) Replaces every occurrence ofNONCE_PLACEHOLDERin a rendered policy value with the supplied nonce.static StringRenders a scheme source expression, ensuring a single trailing colon (e.g.Sets thescript-src directive.Creates a builder pre-populated with the nonce-based strict starter preset.Sets thestyle-src directive.toHeader()Builds aContentSecurityPolicyheader bean from this builder.toString()
-
Field Details
-
SELF
The'self' source-expression keyword.- See Also:
-
NONE
The'none' source-expression keyword.- See Also:
-
UNSAFE_INLINE
The'unsafe-inline' source-expression keyword.- See Also:
-
UNSAFE_EVAL
The'unsafe-eval' source-expression keyword.- See Also:
-
STRICT_DYNAMIC
The'strict-dynamic' source-expression keyword.- See Also:
-
NONCE_PLACEHOLDER
The placeholder token used in nonce sources when the actual per-request nonce is not yet known.strictStarter()emits'nonce-{nonce}'; the REST layer substitutes the real nonce per-response viaresolveNonce(String, String).- See Also:
-
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
nonce
Renders a'nonce-<token>' source expression.- Parameters:
token- The base64 nonce value (orNONCE_PLACEHOLDERfor deferred substitution). Must not benull or blank.- Returns:
- The source expression. Never
null . - Throws:
IllegalArgumentException- Iftokenisnull or blank.
-
scheme
Renders a scheme source expression, ensuring a single trailing colon (e.g."https" =>https: ).- Parameters:
scheme- The scheme (with or without a trailing colon). Must not benull or blank.- Returns:
- The scheme source expression. Never
null . - Throws:
IllegalArgumentException- Ifschemeisnull or blank.
-
hash
Renders a'sha256-<hash>' (or sha384/sha512) source expression.- Parameters:
algorithm- One of"sha256" ,"sha384" , or"sha512" . Must not benull or blank.base64Hash- The base64-encoded hash of the inline block. Must not benull or blank.- Returns:
- The hash source expression. Never
null . - Throws:
IllegalArgumentException- If either argument isnull or blank.
-
generateNonce
Mints a fresh, cryptographically-random CSP nonce.Generates 128 bits (16 bytes) from
SecureRandomencoded as an unpadded base64url string. Each call returns a new value; a nonce must never be reused across responses.- Returns:
- A new nonce token. Never
null .
-
resolveNonce
Replaces every occurrence ofNONCE_PLACEHOLDERin a rendered policy value with the supplied nonce.- Parameters:
policyValue- The rendered policy value (may containNONCE_PLACEHOLDER). May benull .nonce- The per-request nonce token. Must not benull .- Returns:
- The policy value with placeholders substituted, or
null ifpolicyValuewasnull . - Throws:
IllegalArgumentException- Ifnonceisnull .
-
create
Creates a new empty builder.- Returns:
- A new builder. Never
null .
-
strictStarter
Creates a builder pre-populated with the nonce-based strict starter preset.The preset is:
default-src 'self'; script-src 'self' 'nonce-{nonce}'; style-src 'self' 'nonce-{nonce}'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'
The
{nonce}tokens areNONCE_PLACEHOLDERs; for the policy to function on Juneau-served HTML (which emits inline<script>/<style>) the REST layer must mint a per-request nonce, substitute it viaresolveNonce(String, String), and stamp the same value onto the emitted inline tags.- Returns:
- A new pre-populated builder. Never
null .
-
defaultSrc
Sets thedefault-src directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
scriptSrc
Sets thescript-src directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
styleSrc
Sets thestyle-src directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
imgSrc
Sets theimg-src directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
connectSrc
Sets theconnect-src directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
fontSrc
Sets thefont-src directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
objectSrc
Sets theobject-src directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
baseUri
Sets thebase-uri directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
frameAncestors
Sets theframe-ancestors directive.- Parameters:
sources- The source expressions. May be empty.- Returns:
- This object.
-
reportUri
Sets thereport-uri directive (deprecated in CSP3 but still widely supported).- Parameters:
uris- The collector URIs. May be empty.- Returns:
- This object.
-
reportTo
Sets thereport-to directive (the CSP3 reporting-group mechanism).- Parameters:
groups- The reporting-group names. May be empty.- Returns:
- This object.
-
directive
Generic escape hatch for setting any directive by name.Replaces any previously-registered source list for the same directive while preserving its original position.
- Parameters:
name- The directive name (e.g."worker-src" ). Must not benull or blank.sources- The source expressions. May be empty ornull (renders the bare directive name).- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifnameisnull or blank.
-
reportOnly
Flips the rendered header to the observe-onlyContentSecurityPolicy.REPORT_ONLY_NAMEvariant.- Returns:
- This object.
-
reportOnly
Sets whether the rendered header uses the observe-onlyContentSecurityPolicy.REPORT_ONLY_NAMEvariant.- Parameters:
value-true to emitContent-Security-Policy-Report-Onlyinstead ofContent-Security-Policy.- Returns:
- This object.
-
isReportOnly
Returnstrue if this builder is configured to emit the report-only header variant.- Returns:
- A boolean flag.
-
build
Builds the rendered policy value (the header value only, not including the header name).Returns an empty string when no directives have been registered.
- Returns:
- The policy value. Never
null .
-
toHeader
Builds aContentSecurityPolicyheader bean from this builder.The header name is
ContentSecurityPolicy.REPORT_ONLY_NAMEwhenisReportOnly()istrue , otherwiseContentSecurityPolicy.NAME.- Returns:
- A new header bean carrying
build(). Nevernull .
-
toString
-