java.lang.Object
org.htmlunit.html.csp.ContentSecurityPolicy
- All Implemented Interfaces:
Serializable
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
The Content-Security-Policy of a page. Contains the enforced policies (from the Content-Security-Policy headers and meta elements) and the report only policies (from the Content-Security-Policy-Report-Only headers).
The Content-Security-Policy of a page. Contains the enforced policies (from the Content-Security-Policy headers and meta elements) and the report only policies (from the Content-Security-Policy-Report-Only headers).
All the check methods return false if one of the enforced policies does not allow
the action. For every policy (enforced or report only) that does not allow the action a
securitypolicyviolation event is fired.
- Author:
- Ronald Brill
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddMetaPolicy(HtmlPage page, String serializedPolicy) Adds a policy defined by a meta element to the given page.static booleanallowsBaseUri(HtmlPage page, URL url) Checks if the given url is allowed as base url of the document (base-uri).static booleanallowsConnection(HtmlPage page, URL url, URL reportedUrl) Checks if a connection (XMLHttpRequest, fetch(), ...) to the given url is allowed (connect-src).static booleanallowsEval(HtmlPage page, String source, int lineNumber) Checks if the evaluation of strings as code (eval(), new Function(), setTimeout(string)) is allowed.static booleanallowsExternalScript(HtmlPage page, DomElement element, URL url) Checks if the given external script is allowed.static booleanallowsExternalScript(HtmlPage page, DomElement element, URL url, URL reportedUrl) Checks if the given external script is allowed.static booleanallowsExternalStyle(HtmlPage page, DomElement element, URL url) Checks if loading the external stylesheet (link element or @import) is allowed (style-src-elem).static booleanallowsExternalStyle(HtmlPage page, DomElement element, URL url, URL reportedUrl) Checks if loading the external stylesheet (link element or @import) is allowed (style-src-elem).static booleanallowsFormAction(HtmlPage page, URL url, URL reportedUrl) Checks if the submission of a form to the given url is allowed (form-action).static booleanallowsFrame(HtmlPage page, URL url) Checks if loading the given url into a frame or iframe is allowed (frame-src, with fallback to child-src and default-src).static booleanallowsImage(HtmlPage page, URL url) Checks if loading the given image url is allowed (img-src, with fallback to default-src).static booleanallowsImage(HtmlPage page, URL url, URL reportedUrl) Checks if loading the given image url is allowed (img-src, with fallback to default-src).static booleanallowsInlineScript(HtmlPage page, DomElement element, String source) Checks if the given inline script is allowed.static booleanallowsInlineStyle(HtmlPage page, DomElement element, String source) Checks if the inline style element (style-src-elem) is allowed.static booleanallowsJavaScriptUrl(HtmlPage page, DomElement element, String source) Checks if the given javascript: url is allowed.static booleanallowsMedia(DomElement element, String src) Checks if the url of a media element (audio, video), a source element or a track element is allowed (media-src, with fallback to default-src).static booleanallowsObject(DomElement element, String src) Checks if the url of an object or embed element is allowed (object-src, with fallback to default-src).static booleanallowsScriptAsAttribute(HtmlPage page, DomElement element, String source) Checks if the given event handler attribute is allowed.static booleanallowsStyleAttribute(HtmlPage page, DomElement element, String source) Checks if the style attribute (style-src-attr) is allowed.static booleanallowsWorker(HtmlPage page, URL url) Checks if creating a worker with the given script url is allowed (worker-src, with fallback to child-src, script-src and default-src).static voidFires the securitypolicyviolation events of the violations detected by the current thread so far.static ContentSecurityPolicyfromResponse(WebResponse webResponse) Creates the policy for the given response.static voidhideNonceIfNeeded(DomElement element) Nonce hiding: if the page has a policy delivered by a header, the nonce attribute of an element added to the page is replaced by an empty string (the nonce IDL attribute still returns the nonce).static ContentSecurityPolicyReturns a copy of the policy of the creator for documents with a local scheme url (about:, data:, blob:).static URLupgradeInsecureNavigation(HtmlPage page, URL url) Upgrade Insecure Requests for top level navigations: the url is upgraded only if the host and port are the same as the host and port of the page (or of one of its ancestors) having the upgrade-insecure-requests directive.static voidupgradeInsecureNavigationRedirects(HtmlPage page, WebRequest request) Upgrade Insecure Requests for the redirects of a top level navigation.static voidupgradeInsecureRedirects(HtmlPage page, WebRequest request) Upgrade Insecure Requests for the redirects of a request (subresources, nested navigations and form submissions): the redirect targets are upgraded like the request itself.static URLupgradeInsecureRequest(HtmlPage page, URL url) Upgrade Insecure Requests (upgrade-insecure-requests directive): returns the url to be used for a request of the page; used for subresources, nested navigations (frames) and form submissions.
-
Field Details
-
HEADER_REPORT_ONLY
The name of the report only header.- See Also:
-
-
Method Details
-
fromResponse
Creates the policy for the given response.- Parameters:
webResponse- the response- Returns:
- the policy or
nullif the response has no policy header
-
inherit
Returns a copy of the policy of the creator for documents with a local scheme url (about:, data:, blob:). Like the browsers, these documents inherit the policy of the document creating them; for frames this is the parent document, for windows opened by window.open() the opener. The copy is independent from the policy of the creator (policies added later by meta elements are not shared) and 'self' refers to the origin of the creator.- Parameters:
url- the url of the new documentwindow- the window the new document is loaded into- Returns:
- the inherited policy or
null
-
addMetaPolicy
Adds a policy defined by a meta element to the given page. The policy is parsed with the meta restrictions (frame-ancestors, sandbox and report-uri are ignored). Depending on the browser, commas split the content into several policies (Chrome) or are part of the single policy (Firefox).- Parameters:
page- the pageserializedPolicy- the policy
-
allowsInlineScript
Checks if the given inline script is allowed.- Parameters:
page- the pageelement- the script elementsource- the script source- Returns:
trueif the script is allowed
-
allowsExternalScript
Checks if the given external script is allowed.- Parameters:
page- the pageelement- the script elementurl- the url of the script- Returns:
trueif the script is allowed
-
allowsExternalScript
public static boolean allowsExternalScript(HtmlPage page, DomElement element, URL url, URL reportedUrl) Checks if the given external script is allowed.- Parameters:
page- the pageelement- the script elementurl- the url of the script (or the target of a redirect)reportedUrl- the url reported as blocked uri (for redirects the url of the script)- Returns:
trueif the script is allowed
-
allowsScriptAsAttribute
Checks if the given event handler attribute is allowed.- Parameters:
page- the pageelement- the elementsource- the source of the event handler- Returns:
trueif the event handler is allowed
-
allowsJavaScriptUrl
Checks if the given javascript: url is allowed.- Parameters:
page- the pageelement- the element (link, form) ornullsource- the source of the javascript url (including the 'javascript:' prefix)- Returns:
trueif the javascript url is allowed
-
allowsConnection
Checks if a connection (XMLHttpRequest, fetch(), ...) to the given url is allowed (connect-src).- Parameters:
page- the pageurl- the url to connect toreportedUrl- the url reported in case of a violation; for redirects this is the url of the initial request- Returns:
trueif the connection is allowed
-
allowsInlineStyle
Checks if the inline style element (style-src-elem) is allowed.- Parameters:
page- the pageelement- the style elementsource- the content of the style element- Returns:
trueif the style is allowed
-
allowsStyleAttribute
Checks if the style attribute (style-src-attr) is allowed. Like the browsers, style changes done by the CSSOM (e.g. element.style.color = 'red') are not checked.- Parameters:
page- the pageelement- the element with the style attributesource- the value of the style attribute- Returns:
trueif the style attribute is allowed
-
allowsExternalStyle
Checks if loading the external stylesheet (link element or @import) is allowed (style-src-elem).- Parameters:
page- the pageelement- the link element (the nonce is taken from this element) ornullfor @importurl- the url of the stylesheet- Returns:
trueif the stylesheet is allowed
-
allowsExternalStyle
public static boolean allowsExternalStyle(HtmlPage page, DomElement element, URL url, URL reportedUrl) Checks if loading the external stylesheet (link element or @import) is allowed (style-src-elem).- Parameters:
page- the pageelement- the link element (the nonce is taken from this element) ornullfor @importurl- the url of the stylesheet (or the target of a redirect)reportedUrl- the url reported as blocked uri (for redirects the url of the stylesheet)- Returns:
trueif the stylesheet is allowed
-
allowsFrame
Checks if loading the given url into a frame or iframe is allowed (frame-src, with fallback to child-src and default-src). Like the browsers, the violation is reported for the document and the blocked url is reported without path if it is cross origin (navigations may leak information).- Parameters:
page- the page containing the frameurl- the url to load into the frame (for redirects the target of the redirect)- Returns:
trueif loading is allowed
-
allowsObject
Checks if the url of an object or embed element is allowed (object-src, with fallback to default-src). HtmlUnit does not load the content of these elements; like the browsers, the url is checked anyway to report the violation.- Parameters:
element- the object or embed elementsrc- the value of the data (object) or src (embed) attribute- Returns:
trueif the url is allowed (or the attribute is empty or invalid)
-
allowsMedia
Checks if the url of a media element (audio, video), a source element or a track element is allowed (media-src, with fallback to default-src). HtmlUnit does not load media; like the browsers, the url is checked anyway to report the violation.- Parameters:
element- the elementsrc- the value of the src attribute- Returns:
trueif the url is allowed (or the attribute is empty or invalid)
-
allowsImage
Checks if loading the given image url is allowed (img-src, with fallback to default-src).- Parameters:
page- the page containing the imageurl- the url of the image- Returns:
trueif the image is allowed
-
allowsImage
Checks if loading the given image url is allowed (img-src, with fallback to default-src).- Parameters:
page- the page containing the imageurl- the url of the image (or the target of a redirect)reportedUrl- the url reported as blocked uri (for redirects the url of the image)- Returns:
trueif the image is allowed
-
upgradeInsecureRequest
Upgrade Insecure Requests (upgrade-insecure-requests directive): returns the url to be used for a request of the page; used for subresources, nested navigations (frames) and form submissions.If the page (or one of its ancestors) has an enforced policy containing upgrade-insecure-requests, http urls are upgraded to https and ws urls to wss (port 80 is replaced by the default port). Like the browsers, loopback hosts (localhost, 127.0.0.0/8, ::1) are not upgraded.
- Parameters:
page- the page doing the requesturl- the url of the request- Returns:
- the upgraded url or the url itself
-
upgradeInsecureRedirects
Upgrade Insecure Requests for the redirects of a request (subresources, nested navigations and form submissions): the redirect targets are upgraded like the request itself.- Parameters:
page- the page doing the requestrequest- the request
-
allowsWorker
Checks if creating a worker with the given script url is allowed (worker-src, with fallback to child-src, script-src and default-src).- Parameters:
page- the page creating the workerurl- the url of the worker script- Returns:
trueif the worker is allowed
-
allowsBaseUri
Checks if the given url is allowed as base url of the document (base-uri). There is no fallback to default-src for this directive.- Parameters:
page- the page containing the base elementurl- the resolved url of the base element- Returns:
trueif the base url is allowed
-
allowsFormAction
Checks if the submission of a form to the given url is allowed (form-action). There is no fallback to default-src for this directive.- Parameters:
page- the page containing the formurl- the url the form is submitted to (for javascript: urls the whole url)reportedUrl- the url reported in case of a violation; for redirects this is the url of the form submission- Returns:
trueif the submission is allowed
-
allowsEval
Checks if the evaluation of strings as code (eval(), new Function(), setTimeout(string)) is allowed.- Parameters:
page- the pagesource- the source to evaluatelineNumber- the line number of the code calling eval or0if not known- Returns:
trueif the evaluation is allowed
-
firePendingViolations
public static void firePendingViolations()Fires the securitypolicyviolation events of the violations detected by the current thread so far. Called after a script element was processed and when the outermost script execution has finished. -
hideNonceIfNeeded
Nonce hiding: if the page has a policy delivered by a header, the nonce attribute of an element added to the page is replaced by an empty string (the nonce IDL attribute still returns the nonce). Firefox does this only for enforced policies.- Parameters:
element- the element added to the page
-