Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2798537
fix(spring): keep Spring Security headers on responses forwarded to V…
totally-not-ai[bot] Nov 22, 2026
c14993c
fix(spring): ignore a failed close after a forward like the container…
totally-not-ai[bot] Nov 22, 2026
8ae7c06
fix(spring): log the ignored close failures of a forwarded response
totally-not-ai[bot] Nov 22, 2026
172eefb
Merge branch 'main' into fix/spring-security-headers-on-forwarded-res…
Artur- Nov 23, 2026
9208412
Merge branch 'main' into fix/spring-security-headers-on-forwarded-res…
totally-not-ai[bot] Nov 26, 2026
46c7528
fix: only fall back to the writer when the stream is unavailable
totally-not-ai[bot] Nov 28, 2026
793285a
test: cover failures to get the forwarded response's stream or writer
totally-not-ai[bot] Nov 28, 2026
d01ccd8
Merge branch 'main' into fix/spring-security-headers-on-forwarded-res…
caalador Nov 30, 2026
020ee36
feat: use Spring Security nonce for strict Content Security Policy
heruan Nov 27, 2026
e820ed1
test: pin that Spring Security decides where its policy applies
heruan Nov 27, 2026
d4abf55
Merge remote-tracking branch 'origin/main' into feat/csp-spring-secur…
heruan Nov 30, 2026
edcadf7
docs: describe provided nonce without naming Spring Security
heruan Nov 30, 2026
71a788e
test: remove test method duplicated by merge
heruan Nov 30, 2026
2f53b4e
feat: provide strict Content Security Policy for Spring Security
heruan Nov 30, 2026
497e95f
refactor: configure strict Content Security Policy through VaadinSecu…
heruan Nov 30, 2026
2d7db03
fix: keep disabled headers off and use provided nonce in every mode
heruan Dec 1, 2026
af17290
refactor: look up Content Security Policy nonce provider for each page
heruan Dec 1, 2026
b6e3f83
Merge remote-tracking branch 'origin/main' into feat/csp-spring-secur…
heruan Dec 1, 2026
5921900
feat: send report-only Content Security Policy in csp warn mode
totally-not-ai[bot] Dec 2, 2026
4001800
feat: warn once about existing report-only header in csp warn mode
totally-not-ai[bot] Dec 2, 2026
f772279
Merge remote-tracking branch 'origin/main' into feat/csp-warn-report-…
totally-not-ai[bot] Dec 2, 2026
30ebc1c
docs: say that the app asks for report-only policy in Spring Security
totally-not-ai[bot] Dec 2, 2026
15c13e1
Merge branch 'main' into feat/csp-warn-report-only
mcollovati Dec 2, 2026
8049a50
Merge remote-tracking branch 'origin/main' into feat/csp-warn-report-…
totally-not-ai[bot] Dec 2, 2026
07aa51f
feat: send report-only policy of csp warn mode in development mode too
totally-not-ai[bot] Dec 2, 2026
559492b
docs: say that warn mode sends no policy when nonce is provided
totally-not-ai[bot] Dec 2, 2026
c54d628
test: cover provided nonce in warn mode in development mode
totally-not-ai[bot] Dec 2, 2026
e18c190
docs: say that report-to needs a Reporting-Endpoints header
totally-not-ai[bot] Dec 2, 2026
40a11e2
docs: name report-uri as the reporting directive example
totally-not-ai[bot] Dec 2, 2026
aff99d3
Merge main into feat/csp-warn-report-only
totally-not-ai[bot] Dec 3, 2026
48a5f90
feat: send report-only policy in csp strict mode in development mode
totally-not-ai[bot] Dec 3, 2026
1a614f0
test: cover existing report-only header in strict development mode
totally-not-ai[bot] Dec 3, 2026
d4b53f1
refactor: build csp warning arguments only when warn is enabled
totally-not-ai[bot] Dec 3, 2026
c375bb7
fix: keep csp warning for later when warn logging is off
totally-not-ai[bot] Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

/**
* Creates the Content Security Policy that a Vaadin page needs in the
* {@link ContentSecurityPolicyMode#STRICT} mode.
* {@link ContentSecurityPolicyMode#STRICT} mode. The strict mode sends it as a
* report-only policy in development mode, and the
* {@link ContentSecurityPolicyMode#WARN} mode always does.
* <p>
* For internal use only. May be renamed or removed in a future release.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@
/**
* A mutable Content Security Policy, i.e. a set of directives and the sources
* of each directive, that is serialized as the value of a
* {@code Content-Security-Policy} HTTP header.
* {@code Content-Security-Policy} or
* {@code Content-Security-Policy-Report-Only} HTTP header.
* <p>
* Directive names are case-insensitive and kept in lower case. Directives are
* written to the header in the order they were first set. Sources are written
* as given, so keywords need their quotes, for example {@code "'self'"} or
* {@code "'none'"}.
* <p>
* In the {@link ContentSecurityPolicyMode#STRICT} mode, Vaadin creates the
* policy for each index HTML response and exposes it through
* In the {@link ContentSecurityPolicyMode#STRICT} and
* {@link ContentSecurityPolicyMode#WARN} modes, Vaadin creates the policy for
* each index HTML response and exposes it through
* {@link com.vaadin.flow.server.communication.IndexHtmlResponse#getContentSecurityPolicy()},
* so that an
* {@link com.vaadin.flow.server.communication.IndexHtmlRequestListener} can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
* {@link com.vaadin.flow.dom.Element#executeJs(String, Object...)}, because the
* browser has to compile it, which a strict policy blocks. This applies to the
* calls that Vaadin itself makes as well, and in development mode as much as in
* production mode. The {@link #STRICT} mode also sends the Content Security
* Policy header in production mode.
* production mode. Both modes also send the Content Security Policy that a
* Vaadin page needs. The {@link #STRICT} mode sends it in production mode as a
* {@code Content-Security-Policy} header, which the browser enforces. In
* development mode, and in the {@link #WARN} mode, it is sent as a
* {@code Content-Security-Policy-Report-Only} header, which the browser only
* reports on.
*
* @since 25.4
*/
Expand All @@ -50,6 +54,31 @@ public enum ContentSecurityPolicyMode {
* and the warning says so. A warning is logged once per call site, or, when
* a component reports what the JavaScript belongs to, once per call site
* and reported part, such as a chart option.
* <p>
* Vaadin also sends the policy of the {@link #STRICT} mode, with a new
* nonce on the scripts of every index HTML response, but as a
* {@code Content-Security-Policy-Report-Only} header. The browser then
* reports in its console what the policy would block, such as inline
* scripts, {@code eval} in client-side code or resources from other
* origins, but still runs and loads it. A {@code report-uri} directive
* added through
* {@link com.vaadin.flow.server.communication.IndexHtmlResponse#getContentSecurityPolicy()}
* also sends the reports to a collector of the application. A
* {@code report-to} directive only names an endpoint group, which the
* application also declares in a {@code Reporting-Endpoints} response
* header. 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 that the development server and the development
* tools need, so the reports show up during development. No header is sent
* if the response already has a {@code Content-Security-Policy-Report-Only}
* header, which is logged as a warning once. An enforced
* {@code Content-Security-Policy} header that the application sends is left
* as it is.
* <p>
* When an integration with a security framework provides the nonce, as
* described for the {@link #STRICT} mode, Vaadin puts that nonce on the
* scripts and sends no policy of its own in this mode either. To get the
* reports, configure the policy of the security framework as report-only.
*/
WARN,
/**
Expand All @@ -64,8 +93,16 @@ public enum ContentSecurityPolicyMode {
* that nonce and the scripts they load. Applications adjust the policy
* through
* {@link com.vaadin.flow.server.communication.IndexHtmlResponse#getContentSecurityPolicy()}.
* No header is sent in development mode, because the development server and
* the development tools need {@code eval}.
* <p>
* In development mode, the same policy is sent as a
* {@code Content-Security-Policy-Report-Only} header, with the nonce on the
* scripts, because the development server and the development tools need
* {@code eval}. The browser then reports in its console what the policy
* would block in production mode, such as inline scripts, {@code eval} in
* client-side code or resources from other origins, without blocking it. As
* in the {@link #WARN} mode, no header is sent if the response already has
* a {@code Content-Security-Policy-Report-Only} header, which is logged as
* a warning once.
* <p>
* An integration with a security framework, such as the one for Spring
* Security in vaadin-spring, can provide the nonce instead. This is the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -102,11 +103,15 @@ public class IndexHtmlRequestHandler extends JavaScriptBootstrapHandler {
private static final String SCRIPT_INITIAL = "initial";
public static final String LIVE_RELOAD_PORT_ATTR = "livereload.port";
private static final String CONTENT_SECURITY_POLICY_HEADER = "Content-Security-Policy";
private static final String CONTENT_SECURITY_POLICY_REPORT_ONLY_HEADER = "Content-Security-Policy-Report-Only";

// Not static: a SecureRandom in a static field would be created at build
// time in native images that initialize classes at build time (Quarkus)
private final SecureRandom nonceRandom = new SecureRandom();

// The header is usually set for every page, so it is reported only once
private final AtomicBoolean reportOnlyHeaderPresenceLogged = new AtomicBoolean();

@Override
public boolean synchronizedHandleRequest(VaadinSession session,
VaadinRequest request, VaadinResponse response) throws IOException {
Expand Down Expand Up @@ -160,7 +165,9 @@ public boolean synchronizedHandleRequest(VaadinSession session,
if (providedNonce != null) {
indexHtmlResponse.setContentSecurityPolicy(providedNonce, null);
} else if (config
.getContentSecurityPolicyMode() == ContentSecurityPolicyMode.STRICT) {
.getContentSecurityPolicyMode() != ContentSecurityPolicyMode.OFF) {
// The warn mode creates the same policy as the strict mode, so
// that the policy it reports on is the one that will be enforced
String nonce = createContentSecurityPolicyNonce();
indexHtmlResponse.setContentSecurityPolicy(nonce,
StrictContentSecurityPolicy.create(nonce));
Expand Down Expand Up @@ -266,11 +273,14 @@ public boolean synchronizedHandleRequest(VaadinSession session,
indexDocument);

if (indexHtmlResponse.getContentSecurityPolicy() != null) {
if (config.isProductionMode()) {
// The dev server and dev tools need eval, so the policy of
// Vaadin is only sent in production mode
applyContentSecurityPolicy(indexHtmlResponse);
}
// The dev server and dev tools need eval, so the policy is only
// enforced in production mode. A report-only policy blocks
// nothing, so it is sent in development mode too, where the
// developer sees its reports.
boolean enforced = config.isProductionMode() && config
.getContentSecurityPolicyMode() == ContentSecurityPolicyMode.STRICT;
applyContentSecurityPolicy(indexHtmlResponse,
config.getContentSecurityPolicyMode(), enforced);
} else if (indexHtmlResponse.getContentSecurityPolicyNonce() != null) {
// A policy with the nonce is sent by its provider, whatever the
// mode, so the scripts need the nonce to run
Expand All @@ -294,10 +304,33 @@ private String createContentSecurityPolicyNonce() {
return Base64.getEncoder().withoutPadding().encodeToString(bytes);
}

private static void applyContentSecurityPolicy(
IndexHtmlResponse indexHtmlResponse) {
private void applyContentSecurityPolicy(IndexHtmlResponse indexHtmlResponse,
ContentSecurityPolicyMode mode, boolean enforced) {
VaadinResponse response = indexHtmlResponse.getVaadinResponse();
if (response.containsHeader(CONTENT_SECURITY_POLICY_HEADER)) {
if (!enforced) {
if (response.containsHeader(
CONTENT_SECURITY_POLICY_REPORT_ONLY_HEADER)) {
// A report-only policy must not break the page, and replacing
// the header would drop the reports the application asked for
if (getLogger().isWarnEnabled()
&& reportOnlyHeaderPresenceLogged.compareAndSet(false,
true)) {
getLogger().warn(
"The response already has a {} header, so Vaadin does "
+ "not send its report-only policy for the "
+ "'{}={}' setting. Remove the code that sets the "
+ "header to test the policy Vaadin enforces "
+ "in the strict mode in production, and "
+ "adjust it through "
+ "IndexHtmlResponse.getContentSecurityPolicy() "
+ "in an IndexHtmlRequestListener instead.",
CONTENT_SECURITY_POLICY_REPORT_ONLY_HEADER,
InitParameters.CONTENT_SECURITY_POLICY,
mode.name().toLowerCase(Locale.ROOT));
}
return;
}
} else if (response.containsHeader(CONTENT_SECURITY_POLICY_HEADER)) {
// Sending the page without Vaadin's policy would silently drop
// the protection the strict mode promises
throw new IllegalStateException(String.format(
Expand All @@ -314,7 +347,11 @@ private static void applyContentSecurityPolicy(
// The caller checks that there is one
ContentSecurityPolicy policy = Objects
.requireNonNull(indexHtmlResponse.getContentSecurityPolicy());
response.setHeader(CONTENT_SECURITY_POLICY_HEADER,
// The browser reports what the report-only policy would block, but
// still loads it
response.setHeader(
enforced ? CONTENT_SECURITY_POLICY_HEADER
: CONTENT_SECURITY_POLICY_REPORT_ONLY_HEADER,
policy.toHeaderValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,21 @@ public Optional<UI> getUI() {
* {@code script-src} directive of the Content Security Policy.
* <p>
* A new nonce is created for every response when the application runs with
* the {@link ContentSecurityPolicyMode#STRICT} mode. An integration with a
* the {@link ContentSecurityPolicyMode#STRICT} or
* {@link ContentSecurityPolicyMode#WARN} mode. An integration with a
* security framework, such as the one for Spring Security in
* {@code vaadin-spring}, can provide the nonce of the policy of that
* framework, which is then used instead, in every mode. Scripts added by an
* {@link IndexHtmlRequestListener} get the nonce automatically, but the
* value can also be used for other content that needs it. A nonce created
* by Vaadin is not put on the scripts in development mode.
* by Vaadin is not put on the scripts when Vaadin does not send its
* report-only policy because the response already has a
* {@code Content-Security-Policy-Report-Only} header, see
* {@link #getContentSecurityPolicy()}. No policy that is sent then uses the
* nonce.
*
* @return the nonce, or {@code null} if the mode is not
* {@link ContentSecurityPolicyMode#STRICT} and no nonce is provided
* @return the nonce, or {@code null} if the mode is
* {@link ContentSecurityPolicyMode#OFF} and no nonce is provided
* @since 25.4
*/
public @Nullable String getContentSecurityPolicyNonce() {
Expand All @@ -145,10 +150,14 @@ public Optional<UI> getUI() {
* Gets the Content Security Policy that is sent with this response.
* <p>
* The policy is created when the application runs with the
* {@link ContentSecurityPolicyMode#STRICT} mode. An
* {@link ContentSecurityPolicyMode#STRICT} mode, where it is sent as a
* {@code Content-Security-Policy} header in production mode, or with the
* {@link ContentSecurityPolicyMode#WARN} mode, where the same policy is
* sent as a {@code Content-Security-Policy-Report-Only} header, so that the
* browser reports what it would block without blocking it. An
* {@link IndexHtmlRequestListener} can add or override directives for
* resources of the application, such as {@code connect-src},
* {@code img-src}, {@code frame-src} or {@code report-to}.
* {@code img-src}, {@code frame-src} or {@code report-uri}.
* <p>
* Do not change {@code script-src}, and do not add {@code script-src-elem}
* or {@code script-src-attr}: {@code script-src} holds the nonce and
Expand All @@ -160,20 +169,28 @@ public Optional<UI> getUI() {
* document, which gets the nonce, or load it from a script that is already
* trusted.
* <p>
* The header is written after all listeners have run. It is not written in
* development mode, because the development server and the development
* tools need {@code eval}. In production mode, the request fails with an
* {@link IllegalStateException} if the response already has a
* The header is written after all listeners have run. The
* {@link ContentSecurityPolicyMode#STRICT} mode sends the policy as a
* {@code Content-Security-Policy-Report-Only} header in development mode,
* because the development server and the development tools need
* {@code eval}, and a report-only policy blocks nothing. In production mode
* and the {@link ContentSecurityPolicyMode#STRICT} mode, the request fails
* with an {@link IllegalStateException} if the response already has a
* {@code Content-Security-Policy} header when the page is written, for
* example from a servlet filter that runs before Vaadin.
* example from a servlet filter that runs before Vaadin. When the policy is
* sent as report-only, a {@code Content-Security-Policy-Report-Only} header
* that the response already has is kept, and the policy of Vaadin is not
* sent, which is logged as a warning once. A
* {@code Content-Security-Policy} header that the response already has is
* enforced next to the report-only policy.
* <p>
* There is no policy to adjust when the application sends its own with a
* nonce it shares with Vaadin, which an integration with a security
* framework can make possible. That policy is sent with the page as
* configured, and is adjusted where it is configured.
*
* @return the mutable policy, or {@code null} if the mode is not
* {@link ContentSecurityPolicyMode#STRICT} or if the policy is not
* @return the mutable policy, or {@code null} if the mode is
* {@link ContentSecurityPolicyMode#OFF} or if the policy is not
* sent by Vaadin
* @since 25.4
*/
Expand Down
Loading