Skip to content

docs: describe strict Content Security Policy with Spring Security - #6808

Open
heruan wants to merge 5 commits into
mainfrom
docs/csp-spring-security-nonce
Open

heruan wants to merge 5 commits into
mainfrom
docs/csp-spring-security-nonce

Conversation

@heruan

@heruan heruan commented Dec 1, 2026

Copy link
Copy Markdown
Member

Part of vaadin/platform#9488
Depends on vaadin/flow#27471

This PR rewrites the "Spring Security" section of the Content Security Policy page for the behavior of vaadin/flow#27471: the policy is configured in Spring Security, and Vaadin uses its nonce and sends no policy of its own.

  • Replaced the description of the old behavior, where the bootstrap page carried only the policy of Vaadin and the directives of Spring Security were dropped
  • Added VaadinSecurityConfigurer.contentSecurityPolicy(): the default call, changing the policy, and setting the other Spring Security options for it
    • It replaces a policy set with HttpSecurity.headers(), and adds only the policy header when the headers are disabled
    • It belongs with the strict mode; with off or warn the page loads, but JavaScript sent as strings is blocked
  • Added that Spring Security sends the policy with every response of the filter chain, and in development mode too
  • Added "Policy Written in Spring Security": a policy with the {nonce} placeholder written with HttpSecurity.headers()
  • Added "When the Page Isn't Protected": a table of the configurations that leave the bootstrap page without a strict policy, and a note that an existing Spring Security policy is sent unchanged when strict is turned on
  • Changed the sentence before "Preview Limitations" that pointed to the old behavior, and split a long sentence on that line

Notes for the reviewer:

  • Do not merge before feat: use Spring Security nonce for strict Content Security Policy flow#27471. The API names follow that PR at 2d7db03818 and can still change in review.
  • The section has a since badge for 25.5, which is the version of Flow main today. It needs a change if the Flow PR is also released in another version.
  • The table comes from the cases tested in the Flow PR, see the review comments there.
  • The page does not list what is blocked in development mode under the policy (the reload handler of the Vite script and the usage statistics check). Both leave the application working.
  • Vale reports one error on the page, on a line this PR does not change.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Preview Deployment

This PR has been deployed for preview.

URL: https://docs-preview-pr-6808.fly.dev

Changed pages

Added content is highlighted in green; removed content is marked in red on each page.

Built from 57db756

@heruan
heruan marked this pull request as ready for review December 2, 2026 08:02

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed the changes — left 5 comments.

Finding
⚠️ The development-mode paragraph now contradicts the new Spring Security section
⚠️ The IndexHtmlRequestListener instructions above are now wrong for a Spring Security policy, and only the Spring Security section says so
🧩 The request matcher example silently produces the unprotected case that the section warns about
💬 The requirement to set the parameter to strict comes after all three examples
⚠️ The new behavior is stated without the version badge that the section it links to carries

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Around line 141 — could not attach to the exact diff line.

⚠️ The development-mode paragraph now contradicts the new Spring Security section

Line 141 still says: "In development mode, no Content-Security-Policy header is sent and no nonce is added to the page, because the development server and the development tools need eval."

The new section says the opposite for a Spring Security application (line 232: "Spring Security sends the policy in development mode too, and Vaadin puts the nonce on the scripts there as well"), and line 234 says the development tools and Copilot work under that policy. A reader of a Spring Boot application — the common case — meets the general claim first and its stated reason, and will not expect the policy in development mode.

Suggestion: qualify line 141, for example "Vaadin itself sends the policy only in production mode. In development mode, Vaadin sends no Content-Security-Policy header and adds no nonce to the page, unless Spring Security sends a policy, see <<#spring-security,Spring Security>>."

content-security-policy.adoc:141 · accuracy

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Qualified the sentence and linked to the section.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Around line 158 — could not attach to the exact diff line.

⚠️ The IndexHtmlRequestListener instructions above are now wrong for a Spring Security policy, and only the Spring Security section says so

Line 150 instructs: "To allow a resource that the default policy doesn't ... add an [interfacename]IndexHtmlRequestListener and add sources to the [classname]ContentSecurityPolicy of the response", and the examples carry the comment // null unless contentSecurityPolicy is strict.

With the change, the policy is also null in strict mode when Spring Security sends the policy, as line 218 states. A reader of a Spring Boot application who copies the listener from this section gets a listener that silently does nothing, and the comment tells them that cannot happen.

Suggestion: amend the comment to // null unless contentSecurityPolicy is strict and Vaadin sends the policy, and point from this section to <<#spring-security,Spring Security>>.

content-security-policy.adoc:158 · accuracy

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed the comment in the example and added a pointer to the section.

[source,java]
----
http.with(vaadin(), vaadin -> vaadin.contentSecurityPolicy(policy -> {
}, config -> config.requestMatchers("/app/**")));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧩 The request matcher example silently produces the unprotected case that the section warns about

The example restricts the policy to /app/**:

http.with(vaadin(), vaadin -> vaadin.contentSecurityPolicy(policy -> {
}, config -> config.requestMatchers("/app/**")));

The table below lists "A policy with a request matcher that doesn't match the request" as a case where the page is sent without any policy, and line 260 says nothing is logged. A reader with routes at / who copies this loses the policy on every bootstrap page with no sign of it.

Add one sentence to the example, such as: "The matcher must cover the routes of the application; a bootstrap page outside it is sent without a policy."

content-security-policy.adoc:225 · examples

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a sentence after the example.


Calling [methodname]`contentSecurityPolicy()` on [classname]`VaadinSecurityConfigurer` replaces a policy that the same filter chain configures with [methodname]`HttpSecurity.headers()`. If the application has disabled the headers of Spring Security for the filter chain, only the `Content-Security-Policy` header is added.

Use it together with the `strict` value of the `contentSecurityPolicy` parameter. With `off` or `warn`, the bootstrap page loads, but the browser blocks the JavaScript that the server sends as strings, such as the expression of [methodname]`executeJs()`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 The requirement to set the parameter to strict comes after all three examples

"Use it together with the strict value of the contentSecurityPolicy parameter. With off or warn, the bootstrap page loads, but the browser blocks the JavaScript that the server sends as strings" appears three examples after the configuration it applies to.

A reader who arrives through the link on line 177 and copies the first example without setting vaadin.contentSecurityPolicy=strict ships an application whose string JavaScript, such as executeJs(), is blocked in the browser. Move this requirement before the first example, or into the paragraph that introduces it.

content-security-policy.adoc:230 · clarity

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved before the first example.

Leave `script-src` unchanged: it holds the nonce and `'strict-dynamic'`, so replacing it blocks every script on the page, and host sources added to it are ignored by the browser. To load a script from another origin instead, add a `<script>` element to the document, which gets the nonce automatically.

Setting the `Content-Security-Policy` header another way, such as from a servlet filter that runs before Vaadin or the listener in <<#creating-random-nonce,Creating Random Nonce>>, isn't compatible with this: in production mode, the request fails if the header is already set when the page is written, so that the page is never sent without Vaadin's own policy. A policy configured in Spring Security doesn't fail the request. See <<#spring-security,Spring Security>> for how the two policies work together.
Setting the `Content-Security-Policy` header another way, such as from a servlet filter that runs before Vaadin or the listener in <<#creating-random-nonce,Creating Random Nonce>>, isn't compatible with this. In production mode, the request fails if the header is already set when the page is written. This way, the page is never sent without Vaadin's own policy. A policy configured in Spring Security is the exception: Vaadin then uses the nonce of that policy and sends no policy of its own. See <<#spring-security,Spring Security>>.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ The new behavior is stated without the version badge that the section it links to carries

"A policy configured in Spring Security is the exception: Vaadin then uses the nonce of that policy and sends no policy of its own."

The section this sentence links to is badged since:com.vaadin:vaadin@V25.5, and pom.xml pins 25.4.0-beta4. A reader on 25.4 takes this sentence as current behavior, while in 25.4 the bootstrap page carries only the policy of Vaadin. Badge the clause the same way, for example [since:com.vaadin:vaadin@V25.5]#A policy configured in Spring Security is the exception: ...#.

content-security-policy.adoc:177 · accuracy

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added the badge to the sentence.

@heruan heruan self-assigned this Dec 2, 2026
…ity-nonce

# Conflicts:
#	articles/flow/security/protections/content-security-policy.adoc

@mcollovati mcollovati left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The Flow PR is merged and cherry-picked to 25.4, so this can go in once the comments are addressed. A few pages outside this PR need an update for the Spring Security case:

Map page (articles/components/map/index.adoc, "Content Security Policy" section, added in #6807)

The section tells the reader to add the map service origins in an IndexHtmlRequestListener (line 621), with the comment // null unless contentSecurityPolicy is strict (line 629). When Spring Security sends the policy, getContentSecurityPolicy() returns null. The listener then silently does nothing, and the map shows no background in production. Line 641 ("Vaadin sends the policy only in production mode") doesn't hold either, since Spring Security sends the policy in development mode too.

Please add the Spring Security variant, with a link to <<{articles}/flow/security/protections/content-security-policy#spring-security,Spring Security>>:

http.with(vaadin(), vaadin -> vaadin.contentSecurityPolicy(policy -> policy
        .addSources("img-src", "https://maps.example.com")
        .addSources("img-src", "https://*.tile.openstreetmap.org")));

The comment in the listener example should also match the one on the CSP page: // null unless contentSecurityPolicy is strict and Vaadin sends the policy.

Security Configuration (articles/flow/security/spring/security-configuration.adoc)

This page lists the methods of VaadinSecurityConfigurer under "Configuration Methods", but not the new contentSecurityPolicy() overloads. The documentation bot's PR for the same Flow change, #6821, already has a "Content Security Policy Configuration" subsection for them, before "Request Matchers". Please take it over into this PR, and we close #6821 as a duplicate. Its changes to the CSP page overlap with this PR and would conflict with it. The subsection needs a few changes:

  • Add [role="since:com.vaadin:vaadin@V25.4"] above the heading, since nothing else on the page marks the methods as new.
  • Use [methodname] for HttpSecurity.headers(), like the rest of the page.
  • Also list HeadersConfigurer under "Applied Configurers", since the configurer applies it when contentSecurityPolicy() is called.

Configuration Properties (articles/flow/configuration/properties.adoc, line 141)

With strict, Vaadin sends a nonce-based Content-Security-Policy header in production mode

This isn't true when Spring Security sends the policy. Something like "…in production mode, unless Spring Security sends the policy" would do.

Set the parameter to `warn` to log every call in the application that wouldn't work under such a policy. Each log entry is a [classname]`ContentSecurityPolicyViolationException`, whose stack trace leads to the code that made the call.

Set the parameter to `strict` to have Vaadin send the policy. Vaadin sends it only in production mode. In development mode, no `Content-Security-Policy` header is sent and no nonce is added to the page, because the development server and the development tools need `eval`. An API that sends JavaScript to the browser as a string, such as [methodname]`executeJs()` with a string expression, throws a [classname]`ContentSecurityPolicyViolationException` in both production and development mode. However, the browser doesn't block anything the policy wouldn't allow, such as a request to an external API or an image from another origin, until the application runs as a production build. Test a production build of the application under the policy before you rely on it.
Set the parameter to `strict` to have Vaadin send the policy. Vaadin sends it only in production mode. In development mode, Vaadin sends no `Content-Security-Policy` header and adds no nonce to the page, unless Spring Security sends a policy. See <<#spring-security,Spring Security>>. An API that sends JavaScript to the browser as a string, such as [methodname]`executeJs()` with a string expression, throws a [classname]`ContentSecurityPolicyViolationException` in both production and development mode. However, the browser doesn't block anything the policy wouldn't allow, such as a request to an external API or an image from another origin, until the application runs as a production build. Test a production build of the application under the policy before you rely on it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In development mode, Vaadin sends no Content-Security-Policy header and adds no nonce to the page, unless Spring Security sends a policy.

The "unless" applies to both parts of the sentence, so it reads as if Vaadin sends a header in development mode when Spring Security has a policy. Vaadin never sends one in that case. It only puts the nonce of Spring Security on the scripts. Maybe split it:

Suggested change
Set the parameter to `strict` to have Vaadin send the policy. Vaadin sends it only in production mode. In development mode, Vaadin sends no `Content-Security-Policy` header and adds no nonce to the page, unless Spring Security sends a policy. See <<#spring-security,Spring Security>>. An API that sends JavaScript to the browser as a string, such as [methodname]`executeJs()` with a string expression, throws a [classname]`ContentSecurityPolicyViolationException` in both production and development mode. However, the browser doesn't block anything the policy wouldn't allow, such as a request to an external API or an image from another origin, until the application runs as a production build. Test a production build of the application under the policy before you rely on it.
Set the parameter to `strict` to have Vaadin send the policy. Vaadin sends it only in production mode. In development mode, Vaadin sends no `Content-Security-Policy` header. It adds no nonce to the page either, unless Spring Security sends a policy. See <<#spring-security,Spring Security>>. An API that sends JavaScript to the browser as a string, such as [methodname]`executeJs()` with a string expression, throws a [classname]`ContentSecurityPolicyViolationException` in both production and development mode. However, the browser doesn't block anything the policy wouldn't allow, such as a request to an external API or an image from another origin, until the application runs as a production build. Test a production build of the application under the policy before you rely on it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Applied.


An application that already has a policy in Spring Security keeps it unchanged when the `contentSecurityPolicy` parameter is set to `strict`. Check that the policy has the `{nonce}` placeholder in `script-src`, and neither `'unsafe-inline'` nor `'unsafe-eval'` there.

A filter chain without a policy isn't affected: Vaadin sends its own policy for the bootstrap pages that it serves. An application can have the policy of Vaadin on the Vaadin pages and another policy elsewhere. Use two filter chains for that, and configure a policy only in the one that doesn't serve Vaadin pages.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
A filter chain without a policy isn't affected: Vaadin sends its own policy for the bootstrap pages that it serves. An application can have the policy of Vaadin on the Vaadin pages and another policy elsewhere. Use two filter chains for that, and configure a policy only in the one that doesn't serve Vaadin pages.
A filter chain without a policy isn't affected: Vaadin sends its own policy for the bootstrap pages that it serves. An application can have the policy of Vaadin on the Vaadin pages and another policy elsewhere. Use two filter chains for that, and configure a policy only in the one that doesn't serve Vaadin pages. See <<{articles}/flow/security/spring/security-configuration#multiple-filter-chains,Multiple Filter Chains>> for how to set them up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Applied.

The feature ships in 25.4, so the badges say so. The Map, Security Configuration and Configuration Properties pages now cover the case where Spring Security sends the policy.
@heruan

heruan commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

All done in 0d14c2b.

  • Map page: added the Spring Security variant with the link, changed the comment in the listener example, and the production mode sentence now mentions Spring Security.
  • Security Configuration: took the subsection from docs: use Spring Security's nonce for strict Content Security Policy #6821 with your three changes. I also added a [[multiple-filter-chains]] anchor for the new link from the CSP page.
  • Configuration Properties: qualified the strict sentence.

#6821 can be closed as a duplicate.

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.

3 participants