Module org.htmlunit

Class ContentSecurityPolicy

java.lang.Object
org.htmlunit.html.csp.ContentSecurityPolicy
All Implemented Interfaces:
Serializable

public final class ContentSecurityPolicy extends Object implements 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).

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 Details

    • HEADER_REPORT_ONLY

      public static final String HEADER_REPORT_ONLY
      The name of the report only header.
      See Also:
  • Method Details

    • fromResponse

      public static ContentSecurityPolicy fromResponse(WebResponse webResponse)
      Creates the policy for the given response.
      Parameters:
      webResponse - the response
      Returns:
      the policy or null if the response has no policy header
    • inherit

      public static ContentSecurityPolicy inherit(URL url, WebWindow window)
      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 document
      window - the window the new document is loaded into
      Returns:
      the inherited policy or null
    • addMetaPolicy

      public static void addMetaPolicy(HtmlPage page, String serializedPolicy)
      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 page
      serializedPolicy - the policy
    • allowsInlineScript

      public static boolean allowsInlineScript(HtmlPage page, DomElement element, String source)
      Checks if the given inline script is allowed.
      Parameters:
      page - the page
      element - the script element
      source - the script source
      Returns:
      true if the script is allowed
    • allowsExternalScript

      public static boolean allowsExternalScript(HtmlPage page, DomElement element, URL url)
      Checks if the given external script is allowed.
      Parameters:
      page - the page
      element - the script element
      url - the url of the script
      Returns:
      true if 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 page
      element - the script element
      url - 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:
      true if the script is allowed
    • allowsScriptAsAttribute

      public static boolean allowsScriptAsAttribute(HtmlPage page, DomElement element, String source)
      Checks if the given event handler attribute is allowed.
      Parameters:
      page - the page
      element - the element
      source - the source of the event handler
      Returns:
      true if the event handler is allowed
    • allowsJavaScriptUrl

      public static boolean allowsJavaScriptUrl(HtmlPage page, DomElement element, String source)
      Checks if the given javascript: url is allowed.
      Parameters:
      page - the page
      element - the element (link, form) or null
      source - the source of the javascript url (including the 'javascript:' prefix)
      Returns:
      true if the javascript url is allowed
    • allowsConnection

      public static boolean allowsConnection(HtmlPage page, URL url, URL reportedUrl)
      Checks if a connection (XMLHttpRequest, fetch(), ...) to the given url is allowed (connect-src).
      Parameters:
      page - the page
      url - the url to connect to
      reportedUrl - the url reported in case of a violation; for redirects this is the url of the initial request
      Returns:
      true if the connection is allowed
    • allowsInlineStyle

      public static boolean allowsInlineStyle(HtmlPage page, DomElement element, String source)
      Checks if the inline style element (style-src-elem) is allowed.
      Parameters:
      page - the page
      element - the style element
      source - the content of the style element
      Returns:
      true if the style is allowed
    • allowsStyleAttribute

      public static boolean allowsStyleAttribute(HtmlPage page, DomElement element, String source)
      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 page
      element - the element with the style attribute
      source - the value of the style attribute
      Returns:
      true if the style attribute is allowed
    • allowsExternalStyle

      public static boolean allowsExternalStyle(HtmlPage page, DomElement element, URL url)
      Checks if loading the external stylesheet (link element or @import) is allowed (style-src-elem).
      Parameters:
      page - the page
      element - the link element (the nonce is taken from this element) or null for @import
      url - the url of the stylesheet
      Returns:
      true if 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 page
      element - the link element (the nonce is taken from this element) or null for @import
      url - 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:
      true if the stylesheet is allowed
    • allowsFrame

      public static boolean allowsFrame(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). 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 frame
      url - the url to load into the frame (for redirects the target of the redirect)
      Returns:
      true if loading is allowed
    • allowsObject

      public static boolean allowsObject(DomElement element, String src)
      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 element
      src - the value of the data (object) or src (embed) attribute
      Returns:
      true if the url is allowed (or the attribute is empty or invalid)
    • allowsMedia

      public static boolean allowsMedia(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). HtmlUnit does not load media; like the browsers, the url is checked anyway to report the violation.
      Parameters:
      element - the element
      src - the value of the src attribute
      Returns:
      true if the url is allowed (or the attribute is empty or invalid)
    • allowsImage

      public static boolean allowsImage(HtmlPage page, URL url)
      Checks if loading the given image url is allowed (img-src, with fallback to default-src).
      Parameters:
      page - the page containing the image
      url - the url of the image
      Returns:
      true if the image is allowed
    • allowsImage

      public static boolean allowsImage(HtmlPage page, URL url, URL reportedUrl)
      Checks if loading the given image url is allowed (img-src, with fallback to default-src).
      Parameters:
      page - the page containing the image
      url - 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:
      true if the image is allowed
    • upgradeInsecureRequest

      public static URL upgradeInsecureRequest(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.

      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 request
      url - the url of the request
      Returns:
      the upgraded url or the url itself
    • upgradeInsecureNavigation

      public static URL upgradeInsecureNavigation(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.
      Parameters:
      page - the page doing the navigation
      url - the url of the navigation
      Returns:
      the upgraded url or the url itself
    • upgradeInsecureRedirects

      public static void upgradeInsecureRedirects(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.
      Parameters:
      page - the page doing the request
      request - the request
    • upgradeInsecureNavigationRedirects

      public static void upgradeInsecureNavigationRedirects(HtmlPage page, WebRequest request)
      Upgrade Insecure Requests for the redirects of a top level navigation.
      Parameters:
      page - the page doing the navigation
      request - the request
    • allowsWorker

      public static boolean allowsWorker(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).
      Parameters:
      page - the page creating the worker
      url - the url of the worker script
      Returns:
      true if the worker is allowed
    • allowsBaseUri

      public static boolean allowsBaseUri(HtmlPage page, URL url)
      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 element
      url - the resolved url of the base element
      Returns:
      true if the base url is allowed
    • allowsFormAction

      public static boolean allowsFormAction(HtmlPage page, URL url, URL reportedUrl)
      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 form
      url - 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:
      true if the submission is allowed
    • allowsEval

      public static boolean allowsEval(HtmlPage page, String source, int lineNumber)
      Checks if the evaluation of strings as code (eval(), new Function(), setTimeout(string)) is allowed.
      Parameters:
      page - the page
      source - the source to evaluate
      lineNumber - the line number of the code calling eval or 0 if not known
      Returns:
      true if 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

      public static void hideNonceIfNeeded(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). Firefox does this only for enforced policies.
      Parameters:
      element - the element added to the page