Class ContentSecurityPolicyViolationReporter

java.lang.Object
com.vaadin.flow.internal.ContentSecurityPolicyViolationReporter

public final class ContentSecurityPolicyViolationReporter extends Object
Reports calls to APIs that send JavaScript to the browser as a string, which the browser compiles with new Function and a strict Content Security Policy without unsafe-eval blocks.

What happens depends on the ContentSecurityPolicyMode of the current service: nothing in ContentSecurityPolicyMode.OFF, a warning logged once per call site and detail in ContentSecurityPolicyMode.WARN, and a ContentSecurityPolicyViolationException in ContentSecurityPolicyMode.STRICT. Every string-based call is reported, including the ones that Vaadin itself makes. Both the warning and the exception carry a stack trace that starts at the API and leads to the code that called it.

For internal use only. May be renamed or removed in a future release.

Since:
25.4
  • Method Details

    • reportStringJavaScript

      public static void reportStringJavaScript()
      Reports a call to an API that sends JavaScript to the browser as a string. Called by the API itself, which is where the stack trace of the report starts.

      A warning is logged once per call site. Use reportStringJavaScript(String) when the call site does not tell what is reported, e.g. when a component reports from an action that runs before the client response.

      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • reportStringJavaScript

      public static void reportStringJavaScript(String detail)
      Reports a call to an API that sends JavaScript to the browser as a string, with a detail that tells what is reported. Called by the API itself, which is where the stack trace of the report starts.

      The detail is added to the message, and a warning is logged once per call site and detail. This identifies the report when the call site alone does not, e.g. when a component reports from an action that runs before the client response, where the stack does not reach the application code that set the JavaScript. Use reportStringJavaScript() when the call site is enough.

      Parameters:
      detail - a short description of what is reported, e.g. the path of the option that has the JavaScript, not null
      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • reportStringExecuteJs

      public static void reportStringExecuteJs(String expression, int parameterCount)
      Reports a call to executeJs with a string expression. Called by the API itself, which is where the stack trace of the report starts.

      The message adds a declared method for the expression, ready to paste, and says how to use it: called through executeJs(Class) when the call is in code the developer can change, or in an interface marked with JsStringFallback when it is in an add-on that the developer can not change. A warning is logged once per call site and expression, so a call site that sends different strings, such as one that concatenates values into them, is reported for each of them.

      Parameters:
      expression - the expression that is sent, not null
      parameterCount - the number of parameters the expression is sent with
      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • reportStringFunction

      public static void reportStringFunction(String body, int captureCount)
      Reports a function created from a string body, as JsFunction.of and the string addJsInitializer do. Called by the API itself, which is where the stack trace of the report starts.

      Like reportStringExecuteJs(String, int), the message adds the declared method for the body and says how to use it.

      Parameters:
      body - the body of the function, not null
      captureCount - the number of values the function captures
      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • reportStringFunction

      public static void reportStringFunction(String body, int captureCount, List<String> argumentNames)
      Reports a function created from a string body that is then given the names of its runtime arguments with JsFunction.withArguments. Called by the API itself, which is where the stack trace of the report starts.

      Like reportStringFunction(String, int), the message adds the declared method for the body, here with the names in @JsFunctionParameterNames.

      Parameters:
      body - the body of the function, not null
      captureCount - the number of values the function captures
      argumentNames - the names of the runtime arguments, not null
      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • reportStringDynamicImport

      public static void reportStringDynamicImport(String expression)
      Reports a dynamic import given as a string expression. Called by the API itself, which is where the stack trace of the report starts.

      Like reportStringExecuteJs(String, int), the message adds the declared method for the expression and says how to use it.

      Parameters:
      expression - the expression that returns a Promise, not null
      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • reportStringFilter

      public static void reportStringFilter(String filter)
      Reports an event filter given as a string expression. Called by the API itself, which is where the stack trace of the report starts.

      The message says how to declare the filter: as a filter snippet when the call is in code the developer can change, or in JsStringFallback.filters() when it is in an add-on.

      Parameters:
      filter - the filter, not null
      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • reportStringEventData

      public static void reportStringEventData(List<String> eventData)
      Reports event data values given as string expressions. Called by the API itself, which is where the stack trace of the report starts.

      The message says how to declare the values: as event data snippets when the call is in code the developer can change, or in JsStringFallback.eventData() when it is in an add-on.

      Parameters:
      eventData - the values the browser would compile, possibly mapped to an element, not null and not empty
      Throws:
      ContentSecurityPolicyViolationException - in the ContentSecurityPolicyMode.STRICT mode
    • isEnabled

      public static boolean isEnabled()
      Checks whether string JavaScript is reported for the current service, i.e. whether its ContentSecurityPolicyMode is ContentSecurityPolicyMode.WARN or ContentSecurityPolicyMode.STRICT. A caller that does extra work to collect what it reports, e.g. to find the detail for reportStringJavaScript(String), can skip that work when this returns false.
      Returns:
      true if reports are logged or thrown, false if they are ignored