Repository navigation
feat: send report-only Content Security Policy in warn and strict dev mode - #27612
Conversation
…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.
Type of change
How to test
API changesNo signatures change. |
…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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
📝 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
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
|
One more point about development mode, this time for With this PR,
In development mode, Proposal: in development mode, Details:
|
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.
|
@mcollovati Done as proposed. In development mode,
|
|
Fixed the Sonar finding. The warning about an existing |
|
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:
The only reports from Vaadin itself are the ones of #27627: the usage statistics 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 |
|



Part of #26696
Follow-up to #27471
new feature · flow-server, vaadin-spring · apps that use
contentSecurityPolicy=warn, orstrictin development modeBackground — report-only policy. A
Content-Security-Policy-Report-Onlyheader makes the browser check the page against a policy and report every violation, in the console and to areport-uri/report-tocollector, 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
warnmode, in development and production mode, and in thestrictmode in development mode. The browser then shows what the enforced policy would block, such as inline scripts,evalin client code and resources from other origins, which the server-side checks cannot see.strictin production mode enforces the same policy, as before.Risks:
warnmode, and instrictmode in development mode, now get aContent-Security-Policy-Report-Onlyheader and a nonce on their scripts. The browser console shows the violations; nothing is blocked.evalof the web components' development mode detector, and, with Vite, the inlineonerrorreload 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.warnmode,IndexHtmlResponse.getContentSecurityPolicy()andgetContentSecurityPolicyNonce()now return a value instead ofnull.offmode, instrictmode 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 instrictproduction mode, because the dev server and the dev tools needeval; a report-only policy blocks nothing, so it is sent in development mode.IndexHtmlRequestHandlercreates the nonce and the policy inwarnmode too, and sends the policy asContent-Security-Policy-Report-Onlyexcept instrictproduction mode, where it sendsContent-Security-PolicyContent-Security-Policyheader of the app is left alone when the policy is report-only;strictproduction mode still fails the request for itContentSecurityPolicyMode,IndexHtmlResponse,ContentSecurityPolicyandVaadinSecurityConfigurer.contentSecurityPolicy()warnin production and development mode andstrictin development mode; the test that no policy is sent now only coversoff, sincewarnandstrictdevelopment mode send oneWorking on behalf of @mcollovati