Class CspViolationReporter

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

public final class CspViolationReporter 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 CspMode of the current service: nothing in CspMode.OFF, a warning logged once per call site and detail in CspMode.WARN, and a CspViolationException in CspMode.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:
      CspViolationException - in the CspMode.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:
      CspViolationException - in the CspMode.STRICT mode
    • isEnabled

      public static boolean isEnabled()
      Checks whether string JavaScript is reported for the current service, i.e. whether its CspMode is CspMode.WARN or CspMode.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