Skip to content

fix: keep fallback sources when addSources creates a directive (#27618) (CP: 25.4) - #27623

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

vaadin-bot merged 1 commit into
25.4from
cherry-pick-27618-to-25.4-1796217230210

Conversation

@vaadin-bot

Copy link
Copy Markdown
Collaborator

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


Fixes #27617

behavior deviation · flow-server · apps that add sources to a Content Security Policy directive that is not set yet

Background — CSP fallback. A fetch directive that is not in the policy falls back to another one in the browser: frame-src uses child-src, then default-src; worker-src uses child-src, script-src, then default-src. So with only default-src 'self', frames from the app's own server are allowed.

Adding a source to a directive the policy did not have, such as an external map in frame-src, also blocked the frames, media or manifest from the app's own server, because the new directive had only the added source. The new directive now starts with the sources it used to fall back to, so adding a source only allows more. The workaround was to add 'self' by hand.

Risks:

  • ⚠️ Behaviour change: addSources on a fetch directive that is not set now also keeps the fallback sources. The API is new in 25.4 and not yet in a final release. setDirective still sets exactly the given sources.

  • ✅ No public API, security, memory, serialization, threading or performance concerns.

  • ContentSecurityPolicy.addSources now creates a missing fetch directive with the sources of the first directive in its fallback list that is set (CSP Level 3 fallback list), without 'none'.

    • The sources are copied when the directive is created; later changes to the fallback do not follow.
    • Directives that do not fall back (form-action, base-uri, …) and directives with no fallback set start empty as before.
  • Javadoc of addSources explains the fallback and when a new directive still restricts.

  • Javadoc of addSources and IndexHtmlResponse.getContentSecurityPolicy() warns that a new script-src-elem or script-src-attr copies the nonce and 'strict-dynamic' from script-src, so host sources added to it are ignored by the browser; applications should not add those directives in strict mode.

  • Tests cover the fallback chains, 'none' (also as the first set fallback), no fallback, non-fetch directives, and that later fallback changes are not copied.

Chose this over only documenting the old behaviour in the Javadoc, since a method named "add" that can block what was allowed before is easy to misuse.

… (CP: 25.4)

Fixes #27617

**behavior deviation** · flow-server · apps that add sources to a
Content Security Policy directive that is not set yet

**Background — CSP fallback.** A fetch directive that is not in the
policy falls back to another one in the browser: `frame-src` uses
`child-src`, then `default-src`; `worker-src` uses `child-src`,
`script-src`, then `default-src`. So with only `default-src 'self'`,
frames from the app's own server are allowed.

Adding a source to a directive the policy did not have, such as an
external map in `frame-src`, also blocked the frames, media or manifest
from the app's own server, because the new directive had only the added
source. The new directive now starts with the sources it used to fall
back to, so adding a source only allows more. The workaround was to add
`'self'` by hand.

**Risks:**
- ⚠️ Behaviour change: `addSources` on a fetch directive that is not set
now also keeps the fallback sources. The API is new in 25.4 and not yet
in a final release. `setDirective` still sets exactly the given sources.
- ✅ No public API, security, memory, serialization, threading or
performance concerns.

- `ContentSecurityPolicy.addSources` now creates a missing fetch
directive with the sources of the first directive in its fallback list
that is set (CSP Level 3 fallback list), without `'none'`.
- The sources are copied when the directive is created; later changes to
the fallback do not follow.
- Directives that do not fall back (`form-action`, `base-uri`, …) and
directives with no fallback set start empty as before.
- Javadoc of `addSources` explains the fallback and when a new directive
still restricts.
- Javadoc of `addSources` and
`IndexHtmlResponse.getContentSecurityPolicy()` warns that a new
`script-src-elem` or `script-src-attr` copies the nonce and
`'strict-dynamic'` from `script-src`, so host sources added to it are
ignored by the browser; applications should not add those directives in
strict mode.
- Tests cover the fallback chains, `'none'` (also as the first set
fallback), no fallback, non-fetch directives, and that later fallback
changes are not copied.

Chose this over only documenting the old behaviour in the Javadoc, since
a method named "add" that can block what was allowed before is easy to
misuse.

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
@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 13:26
@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 39m 34s ⏱️ - 2m 47s
14 101 tests + 7  14 029 ✅ + 7  72 💤 ±0  0 ❌ ±0 
14 527 runs   - 56  14 455 ✅  - 56  72 💤 ±0  0 ❌ ±0 

Results for commit 53adfb9. ± Comparison against base commit aaf53cb.

@vaadin-bot
vaadin-bot merged commit 57c058b into 25.4 Dec 2, 2026
58 checks passed
@vaadin-bot
vaadin-bot deleted the cherry-pick-27618-to-25.4-1796217230210 branch December 2, 2026 13:33
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