## 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>
This PR cherry-picks changes from the original PR #27620 to branch 25.4.