Skip to content

feat: send report-only Content Security Policy in warn and strict dev mode - #27612

Merged
mcollovati merged 34 commits into
mainfrom
feat/csp-warn-report-only
Dec 4, 2026
Merged

mcollovati merged 34 commits into
mainfrom
feat/csp-warn-report-only

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Part of #26696
Follow-up to #27471

new feature · flow-server, vaadin-spring · apps that use contentSecurityPolicy=warn, or strict in development mode

Background — report-only policy. A Content-Security-Policy-Report-Only header makes the browser check the page against a policy and report every violation, in the console and to a report-uri/report-to collector, without blocking anything.

Vaadin now sends its strict policy, with the nonce on its scripts, as report-only wherever it does not enforce it: in the warn mode, in development and production mode, and in the strict mode in development mode. The browser then shows what the enforced policy would block, such as inline scripts, eval in client code and resources from other origins, which the server-side checks cannot see. strict in production mode enforces the same policy, as before.

Risks:

  • ⚠️ Behaviour change: pages in warn mode, and in strict mode in development mode, now get a Content-Security-Policy-Report-Only header and a nonce on their scripts. The browser console shows the violations; nothing is blocked.
  • ⚠️ Behaviour change: in development mode, the console also shows two reports that do not come from the app: the eval of the web components' development mode detector, and, with Vite, the inline onerror reload handler that Flow puts on the dev mode script. Both are tracked in Development mode code of Vaadin violates the strict Content Security Policy #27627.
  • ⚠️ Behaviour change: in warn mode, IndexHtmlResponse.getContentSecurityPolicy() and getContentSecurityPolicyNonce() now return a value instead of null.
  • ✅ No change in off mode, in strict mode in production mode, or when a security framework provides the nonce.

Context. Follows the decisions of #27471: when a security framework such as Spring Security provides the nonce, Vaadin uses it in every mode and sends no policy of its own. With Spring Security, the app sends the policy as report-only through vaadin().contentSecurityPolicy(policy -> {}, config -> config.reportOnly()). No built-in endpoint for the reports is added. The policy is only enforced in strict production mode, because the dev server and the dev tools need eval; a report-only policy blocks nothing, so it is sent in development mode.

  • IndexHtmlRequestHandler creates the nonce and the policy in warn mode too, and sends the policy as Content-Security-Policy-Report-Only except in strict production mode, where it sends Content-Security-Policy
    • A report-only header that the response already has is kept, and a warning naming the mode is logged once per handler, so a report-only policy never fails the request
    • An enforced Content-Security-Policy header of the app is left alone when the policy is report-only; strict production mode still fails the request for it
  • Updated the Javadoc of ContentSecurityPolicyMode, IndexHtmlResponse, ContentSecurityPolicy and VaadinSecurityConfigurer.contentSecurityPolicy()
  • The tests of the sent policy now also cover warn in production and development mode and strict in development mode; the test that no policy is sent now only covers off, since warn and strict development mode send one

Working on behalf of @mcollovati

totally-not-ai Bot and others added 20 commits November 22, 2026 18:11
…aadin

Tomcat 11 by default ends a forwarded response on the innermost
response, so filters that write headers when the response is committed
through their wrapper, like Spring Security's HeaderWriterFilter, never
get to write them. With the root mapping every Vaadin response is
forwarded, so the bootstrap page lost those headers. Close the response
through the wrapper chain at the end of a forward, as the container did
before.

Fixes #27159
A close() failing with IllegalStateException made the code try the
writer, which then failed too and hid the real cause at trace level.
Get the stream or writer first, and close it separately.
A Vaadin page of a path the policy of Spring Security is not sent for gets no policy: Vaadin does not send its own in place of it.
VaadinSecurityConfigurer.getDefaultContentSecurityPolicy() returns the policy of the strict mode with the {nonce} placeholder of Spring Security, so an application does not have to copy the directives.
…rityConfigurer

Replaces the static getDefaultContentSecurityPolicy() with contentSecurityPolicy() options on the configurer, which follow the pattern of its other options. A second customizer sets the other options of the policy in Spring Security, such as the request matcher.
VaadinSecurityConfigurer.contentSecurityPolicy() no longer brings back the default headers of Spring Security when the application has disabled them. A nonce from a ContentSecurityPolicyNonceProvider is put on the scripts in every Content Security Policy mode, so a policy that uses it does not block the page when the mode is not strict. The provider lookup is cached in one volatile field.
Removes the cached provider from IndexHtmlRequestHandler. A volatile field for it is flagged by Sonar (java:S3077), and the lookup only runs once for each bootstrap page.
In production mode, the warn mode now sends the policy of the strict
mode as a Content-Security-Policy-Report-Only header, with the nonce on
the scripts of the page, so that the browser reports what the strict
mode would block. A report-only header that the response already has is
kept and logged as a warning. A nonce provided by a security framework
is still used in every mode, with no policy of Vaadin.
@totally-not-ai
totally-not-ai Bot requested a review from mcollovati December 2, 2026 07:08
@totally-not-ai

totally-not-ai Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Type of change

  • Feature

How to test

  1. Run an app in production mode with vaadin.contentSecurityPolicy=warn and no Spring Security policy.
  2. Open a page: the response has a Content-Security-Policy-Report-Only header with the strict policy, and every script of the page has its nonce.
  3. Add an inline onclick attribute or an eval call in client code: the browser console reports the violation, and the page still works.
  4. Add report-uri through IndexHtmlResponse.getContentSecurityPolicy() in an IndexHtmlRequestListener: the reports are posted there.
API changes

No signatures change. IndexHtmlResponse.getContentSecurityPolicy() and getContentSecurityPolicyNonce() now return a value in the warn mode too, as documented.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 662 files  ±0   1 753 suites  ±0   1h 48m 33s ⏱️ + 7m 50s
14 132 tests +6  14 060 ✅ +6  72 💤 ±0  0 ❌ ±0 
14 568 runs  +6  14 496 ✅ +6  72 💤 ±0  0 ❌ ±0 

Results for commit c375bb7. ± Comparison against base commit b7a51d5.

♻️ This comment has been updated with latest results.

Base automatically changed from feat/csp-spring-security-nonce to main December 2, 2026 09:30
…only

# Conflicts:
#	flow-server/src/main/java/com/vaadin/flow/internal/StrictContentSecurityPolicy.java
#	flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java
#	flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlResponse.java
#	flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java
#	flow-test-generic/src/main/java/com/vaadin/flow/testutil/ClassesSerializableTest.java
#	vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringSecurityAutoConfiguration.java
#	vaadin-spring/src/main/java/com/vaadin/flow/spring/security/SpringSecurityContentSecurityPolicyNonceProvider.java
#	vaadin-spring/src/main/java/com/vaadin/flow/spring/security/VaadinSecurityConfigurer.java

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changes — left 1 comment.

Finding
📝 The WARN javadoc says adding a report-to directive is enough to deliver reports to a collector

* {@link com.vaadin.flow.server.communication.IndexHtmlResponse#getContentSecurityPolicy()}
* also sends the reports to a collector of the application. Switching to
* the {@link #STRICT} mode then enforces the same policy. The header is
* sent in development mode too, since a report-only policy blocks nothing

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 The WARN javadoc says adding a report-to directive is enough to deliver reports to a collector

A report-to directive only names a reporting endpoint group; the browser needs a Reporting-Endpoints (or legacy Report-To) response header to resolve it. ContentSecurityPolicy serializes directives only, so following this sentence with report-to alone yields no reports and no hint why. report-uri does work standalone.

Either mention the extra response header the application has to set, or name only report-uri here.

ContentSecurityPolicyMode.java:68 · documentation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The WARN Javadoc now names report-uri as the directive that sends the reports on its own. It also says that report-to only names an endpoint group, which the application declares in a Reporting-Endpoints response header. The example in IndexHtmlResponse.getContentSecurityPolicy() now uses report-uri instead of report-to, for the same reason.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@mcollovati

Copy link
Copy Markdown
Contributor

One more point about development mode, this time for strict.

With this PR, warn sends its report-only policy in development mode too. But strict sends nothing in development mode: no enforced header, as before, and no report-only header either.

Mode Production Development
off nothing nothing
warn report-only header report-only header
strict enforced header nothing

In development mode, strict throws ContentSecurityPolicyViolationException for string JavaScript sent from the server, so those problems show up early. Problems on the client side, such as an inline handler, eval in an add-on, or a resource from another origin, do not show up, because the browser gets no policy. The developer first sees them in production, where the browser blocks them. This is the same reason we gave for warn in development mode.

Proposal: in development mode, strict sends the same policy as a Content-Security-Policy-Report-Only header, with the nonce on the scripts. It blocks nothing, so the development server and the development tools still work. Production mode does not change: the enforced header.

Details:

  • A header that the application already sets: in development mode, strict follows the rules of warn. It keeps an existing report-only header and logs the warning once. The IllegalStateException for an existing enforced header stays for production only, because the report-only policy does not replace it.
  • Javadoc: ContentSecurityPolicyMode.STRICT says "No header is sent in development mode, because the development server and the development tools need eval". This should change to say that a report-only header is sent.
  • A provided nonce (Spring Security, feat: use Spring Security nonce for strict Content Security Policy #27471): no change. Vaadin sends no policy of its own, and the security framework sends its policy in every mode.

The browser then reports the client-side violations that the enforced
policy would block in production, while the development server and the
development tools, which need eval, keep working.
Also name the mode in the warning about an existing report-only header,
since strict mode in development mode now logs it too.
@totally-not-ai totally-not-ai Bot changed the title feat: send report-only Content Security Policy in csp warn mode feat: send report-only Content Security Policy in warn and strict dev mode Dec 3, 2026
@totally-not-ai

totally-not-ai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

@mcollovati Done as proposed. In development mode, strict now sends the same policy as Content-Security-Policy-Report-Only, with the nonce on the scripts. Production mode still sends the enforced header.

  • An existing report-only header is kept in strict development mode, as in warn, and the warning is logged once. The warning now names the mode, for example contentSecurityPolicy=strict.
  • The IllegalStateException for an existing enforced header is thrown only in strict production mode.
  • A provided nonce is unchanged: Vaadin sends no policy of its own.
  • The STRICT Javadoc now describes the report-only header in development mode, and so do the class Javadoc, IndexHtmlResponse and the internal policy class.
  • Tests: the sent-policy test and the existing-header test now include strict in development mode. The no-policy test now covers only off.
  • PR title and description are updated.

mcollovati added a commit to mcollovati/skeleton-starter-flow-spring that referenced this pull request Dec 3, 2026
@totally-not-ai

totally-not-ai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the Sonar finding. The warning about an existing Content-Security-Policy-Report-Only header is now logged only when warn logging is enabled, so its arguments are not built otherwise. The level is checked before the log-once flag is set. If warn logging is off at first and turned on later, the warning is still logged once. Otherwise nothing changes: the header is kept, and the warning is logged once.

@mcollovati

Copy link
Copy Markdown
Contributor

Tested 1a614f0 (strict mode in development mode) with the reproducer (repro/csp-spring-security-nonce at 2e7b399, section "Report-only policy in the strict mode in development mode" of the README). Headless Chromium, with the dev bundle and with Vite. Everything works as expected.

In development mode:

  • strict, nothing else: the policy is sent as Content-Security-Policy-Report-Only, with Vaadin's nonce on the scripts. The browser reports and blocks nothing. The app, the inline script of a listener and the dev tools work.
  • strict + report-uri from a listener: the header has it, and the collector of the app gets the reports.
  • strict + an existing report-only header: the header of the app is kept. The warning is logged once and names contentSecurityPolicy=strict.
  • strict + an existing enforced header: both headers are sent, and there is no IllegalStateException.
  • Nonce from Spring Security (report-only and enforced): no change, Vaadin sends no policy of its own.
  • warn: no change.

The only reports from Vaadin itself are the ones of #27627: the usage statistics eval, and with Vite the onerror handler on vite-devmode.ts. #27629 fixes both.

I also checked an app that enforces the policy of Spring Security in development mode. The app and the dev tools still work. Two things are blocked, both fixed by #27629: the usage statistics code, and with Vite the reload when vite-devmode.ts fails to load.

@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@mcollovati
mcollovati added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit 5c4f2c7 Dec 4, 2026
57 checks passed
@mcollovati
mcollovati deleted the feat/csp-warn-report-only branch December 4, 2026 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Send the policy as Content-Security-Policy-Report-Only in csp warn mode

7 participants