Skip to content

fix: report string JavaScript where the stack reaches the caller (#27620) (CP: 25.4) - #27626

Merged
vaadin-bot merged 1 commit into
25.4from
cherry-pick-27620-to-25.4-1796222615631
Dec 2, 2026
Merged

vaadin-bot merged 1 commit into
25.4from
cherry-pick-27620-to-25.4-1796222615631

Conversation

@vaadin-bot

Copy link
Copy Markdown
Collaborator

This PR cherry-picks changes from the original PR #27620 to branch 25.4.


Fixes #27614

DX/docs · flow-server · apps and add-ons that send string JavaScript with the Content Security Policy mode set to warn or strict, in particular through the deprecated Shortcuts.setShortcutListenOnElement(String, Component)

With warn or strict, the string shortcut locator was reported only while the response was written. The stack never reached the code that set it, the message suggested a method named after Flow's response writer, and it claimed that the trace led to the caller. Now the locator is reported at the call and the report names the replacement API. Reports whose stack does not reach code outside Flow now say so. Components can give a fix next to the detail of a report.

Risks:

  • ⚠️ Behaviour change, strict mode only: the exception for a string locator is now thrown by the setShortcutListenOnElement call itself. Before, it was thrown while the response was written, and only if a shortcut used the component with listenOn.
  • ⚠️ Message change: the reporter no longer quotes the detail of reportStringJavaScript(String), so callers quote the JavaScript themselves. The only caller with a detail is in an unmerged components PR.
  • ⚠️ Internal API: adds ContentSecurityPolicyViolationReporter.reportStringJavaScript(String detail, String fix), in a class marked for internal use.
  • ✅ No change in the off mode or for locators set without a current service.

Context. The locator string was turned into a function only when the shortcut was set up before the client response. That kept the declared @JsStringFallback lookup working for components built outside a request, which is why that late path stays for them.

  • setShortcutListenOnElement(String, …) now creates the locator function at the call when string JavaScript is reported (warn/strict with a current service). Otherwise it keeps the string and creates the function when the shortcut is set up, as before.
  • The report for a string locator names Shortcuts.setShortcutListenOnElementLocator(…) with the declared function, on both paths.
  • The suggested method name falls back to call when the call site is a Flow class, such as UidlWriter.
  • The sentence "The stack trace leads from the API to the code that called it." is kept only when the stack has a frame of code outside Flow before Flow's request handling. Otherwise the message says that the JavaScript was sent while the response was written, so the trace does not reach the code that set it.
  • The Javadoc of ContentSecurityPolicyMode (WARN, STRICT) and ContentSecurityPolicyViolationException now says that a report whose stack reaches no application code does not lead to the code that set the JavaScript.
  • Added reportStringJavaScript(String detail, String fix). The detail follows "The JavaScript is in" without added quotes, and the fix follows as its own sentence.
  • The tests that check suggested names and message sentences now call the APIs from a helper outside the com.vaadin.flow packages, because the test class itself is in one. The existing detail tests now pass details that quote their JavaScript.

Working on behalf of @mcollovati

) (CP: 25.4)

Fixes #27614

**DX/docs** · flow-server · apps and add-ons that send string JavaScript
with the Content Security Policy mode set to `warn` or `strict`, in
particular through the deprecated
`Shortcuts.setShortcutListenOnElement(String, Component)`

With `warn` or `strict`, the string shortcut locator was reported only
while the response was written. The stack never reached the code that
set it, the message suggested a method named after Flow's response
writer, and it claimed that the trace led to the caller. Now the locator
is reported at the call and the report names the replacement API.
Reports whose stack does not reach code outside Flow now say so.
Components can give a fix next to the detail of a report.

**Risks:**
- ⚠️ Behaviour change, `strict` mode only: the exception for a string
locator is now thrown by the `setShortcutListenOnElement` call itself.
Before, it was thrown while the response was written, and only if a
shortcut used the component with `listenOn`.
- ⚠️ Message change: the reporter no longer quotes the detail of
`reportStringJavaScript(String)`, so callers quote the JavaScript
themselves. The only caller with a detail is in an unmerged components
PR.
- ⚠️ Internal API: adds
`ContentSecurityPolicyViolationReporter.reportStringJavaScript(String
detail, String fix)`, in a class marked for internal use.
- ✅ No change in the `off` mode or for locators set without a current
service.

**Context.** The locator string was turned into a function only when the
shortcut was set up before the client response. That kept the declared
`@JsStringFallback` lookup working for components built outside a
request, which is why that late path stays for them.

- `setShortcutListenOnElement(String, …)` now creates the locator
function at the call when string JavaScript is reported (`warn`/`strict`
with a current service). Otherwise it keeps the string and creates the
function when the shortcut is set up, as before.
- The report for a string locator names
`Shortcuts.setShortcutListenOnElementLocator(…)` with the declared
function, on both paths.
- The suggested method name falls back to `call` when the call site is a
Flow class, such as `UidlWriter`.
- The sentence "The stack trace leads from the API to the code that
called it." is kept only when the stack has a frame of code outside Flow
before Flow's request handling. Otherwise the message says that the
JavaScript was sent while the response was written, so the trace does
not reach the code that set it.
- The Javadoc of `ContentSecurityPolicyMode` (`WARN`, `STRICT`) and
`ContentSecurityPolicyViolationException` now says that a report whose
stack reaches no application code does not lead to the code that set the
JavaScript.
- Added `reportStringJavaScript(String detail, String fix)`. The detail
follows "The JavaScript is in" without added quotes, and the fix follows
as its own sentence.
- The tests that check suggested names and message sentences now call
the APIs from a helper outside the `com.vaadin.flow` packages, because
the test class itself is in one. The existing detail tests now pass
details that quote their JavaScript.

Working on behalf of @mcollovati

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

The report's stack trace is now captured at setShortcutListenOnElement, not when the shortcut delegate is attached

flowchart LR
    subgraph Before
        direction TB
        B1["Shortcuts.setShortcutListenOnElement(String)"] -->|"stores string locator"| B2["ShortcutRegistration.setupKeydownEventDelegateIfNeeded()"]
        B2 -->|"toLocatorFunction() at attach time"| B3["ContentSecurityPolicyViolationReporter.report()"]
        B3 -->|"stack stays inside com.vaadin.flow.*"| B4["message: stack trace leads to caller (wrong)"]
    end
    subgraph After
        direction TB
        A1["Shortcuts.setShortcutListenOnElement(String)"] -->|"toLocatorFunction() at the call (new)"| A2["ContentSecurityPolicyViolationReporter.report()"]:::changed
        A2 -->|"reachesCodeOutsideFlow(stack)"| A3{"caller frame outside com.vaadin.flow?"}:::changed
        A3 -->|"yes"| A4["message: stack trace leads to caller"]
        A3 -->|"no, e.g. no current service"| A5["message: sent while response was written (new)"]:::changed
    end
    Before ~~~ After
    classDef changed stroke:#c9a227,stroke-width:3px
Loading

Before, a string locator passed to Shortcuts.setShortcutListenOnElement(String, Component) was only turned into a JsFunction later, inside ShortcutRegistration.setupKeydownEventDelegateIfNeeded(), so the report's stack never left Flow's own attach machinery. Now toLocatorFunction() runs at the call itself when ContentSecurityPolicyViolationReporter.isEnabled(), and reachesCodeOutsideFlow() decides which of the two messages to append, per the PR description's stated behavior change for strict mode.

Diagram Bot draws the mechanism this pull request touches; it does not review the change. Verify it against the diff.

Generated by Diagram Bot for #27626 · agent · 54.4 AIC · ⊞ 9.2K · ◷

@github-actions github-actions Bot added the +0.0.1 label Dec 2, 2026
@vaadin-bot

Copy link
Copy Markdown
Collaborator Author

This PR is eligible for auto-merging policy, so it has been approved automatically. If there are pending conditions, auto merge (with 'squash' method) has been enabled for this PR [Message is sent from bot]

@vaadin-bot
vaadin-bot enabled auto-merge (squash) December 2, 2026 14:53
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Test Results

 1 656 files   - 30   1 747 suites   - 30   1h 48m 6s ⏱️ -27s
14 106 tests + 2  14 034 ✅ + 2  72 💤 ±0  0 ❌ ±0 
14 532 runs   - 61  14 460 ✅  - 61  72 💤 ±0  0 ❌ ±0 

Results for commit b868b67. ± Comparison against base commit 57c058b.

@vaadin-bot
vaadin-bot merged commit 564a018 into 25.4 Dec 2, 2026
58 checks passed
@vaadin-bot
vaadin-bot deleted the cherry-pick-27620-to-25.4-1796222615631 branch December 2, 2026 15:01
mcollovati pushed a commit to vaadin/docs that referenced this pull request Dec 2, 2026
## Summary

Adds a how-to guide under Building Apps > Security for moving an
application or an add-on to the strict, nonce-based Content Security
Policy mode (`contentSecurityPolicy=strict`). It starts from the `warn`
mode, which logs every call that wouldn't work under the policy, and
walks through fixing them.

The Spring Security paragraph describes the behavior of
vaadin/flow#27471 (also in 25.4); the reference section it links to is
updated for that behavior in #6808 / #6821, so merge one of those
together with this PR. The warn-mode messages and the `addSources()`
behavior the guide describes come from vaadin/flow#27620 (25.4:
vaadin/flow#27626), vaadin/flow-components#10901 and vaadin/flow#27618
(25.4: vaadin/flow#27623), which are merged after 25.4.0-rc1.

## What changed

- New page `building-apps/security/strict-content-security-policy.adoc`
(marked as a preview, like the strict mode itself):
- Find the calls with `warn`: what is logged, the stack traces that
don't reach the application code (Lit renderers, chart options), where
the message quotes the JavaScript to search for, compiler deprecation
warnings as a second list, editing the suggested declarations, strings
with changing ids that aren't flagged, the per-call-site limits, and
what `warn` can't find (browser-side blocks).
- A table that maps each string-based API (`executeJs(String)`,
`JsFunction.of()`, `addJsInitializer(String)`,
`addDynamicImport(String)`, `setFilter(String)`, `addEventData(String)`,
`LitRenderer.of(String)`, chart string functions,
`setShortcutListenOnElement(String)`, `executeJsWhenAttached()`, inline
`on…` handler attributes) to its replacement, with links to the
reference. Notes on `withArguments()`, and on dynamic imports of
server-served URLs that now fail the build.
- What already works unchanged (`callJsFunction()`, `@DomEvent` filters,
`@EventData`, built-in renderers), and the `@Uses` caveat for optimized
production builds.
- A `JsInvokers` method without `@JsExpression`, which calls the element
function of the same name, and a call that returns a value.
- Passing changing values as parameters, and declaring Lit renderer
templates with `CompiledLitTemplate`.
- Calling functions of frontend modules (such as connectors reached
through a global variable) with `@JsDefinitionImport`, or with
`JsModuleInvokers` and `Page.executeModuleJs(Class)` when later calls
depend on the first one. Turning a global-object connector into named
exports, and dropping its `@JsModule`.
  - A before/after of a Lit template, for `${fn}` → `${functions.fn}`.
- Add-ons the application can't change: how to check for a ready
version, why no warnings doesn't mean safe, `@JsStringFallback`, the
cases only the maintainers can fix, finding the add-on behind a bundle
error, and how an uncovered call fails under `strict`.
- Allowing external resources (including directives the default policy
lacks, such as `frame-src`, and the app's own directives), removing
other `Content-Security-Policy` headers together with the switch to
`strict`, the manual nonce/override approach, and Spring Security, where
the policy and its sources stay in the Spring Security configuration.
- Switching to `strict` and testing a production build with the browser
console, including blocks that only a web worker's console shows.
- A section for add-on maintainers: Java and frontend changes, keeping
connectors as plain modules, APIs that take a `JsFunction` instead of a
JavaScript string, and testing against a production build.
- The Security guides overview links to the new page.

## Test summary

- Checked every statement against the Flow and Flow Components sources
of the 25.4 branch, and the Spring Security behavior against
vaadin/flow#27471.
- The guide was followed step by step on a test application with its own
string JavaScript, an in-house add-on and three published add-ons; the
findings from that run are addressed.
- Vale (PR config) reports no errors or warnings on the changed pages.
- Checked that all cross-reference anchors exist on `main`.

Follow-ups:

- Link the `JsModuleInvokers` part to the reference section that #6805
adds, once it's merged.
- `CompiledLitTemplate` isn't documented on the Grid renderers reference
page yet. The guide shows a short example in the meantime.

Working on behalf of Mikhail

---------

Co-authored-by: Vaadin Bot <vaadin-bot@users.noreply.github.com>
Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants