diff --git a/flow-server/src/main/java/com/vaadin/flow/internal/StrictContentSecurityPolicy.java b/flow-server/src/main/java/com/vaadin/flow/internal/StrictContentSecurityPolicy.java index 7e7a1a8d9f6..b736f0f951b 100644 --- a/flow-server/src/main/java/com/vaadin/flow/internal/StrictContentSecurityPolicy.java +++ b/flow-server/src/main/java/com/vaadin/flow/internal/StrictContentSecurityPolicy.java @@ -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. *

* For internal use only. May be renamed or removed in a future release. */ diff --git a/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicy.java b/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicy.java index 7dbcfbf6574..b8a4cac15d8 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicy.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicy.java @@ -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. *

* 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'"}. *

- * 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 diff --git a/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicyMode.java b/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicyMode.java index 88eab8bff6e..3d4167d9d49 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicyMode.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/ContentSecurityPolicyMode.java @@ -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 */ @@ -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. + *

+ * 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. + *

+ * 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, /** @@ -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}. + *

+ * 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. *

* An integration with a security framework, such as the one for Spring * Security in vaadin-spring, can provide the nonce instead. This is the diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java index 42639b04029..0cd2482bf30 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java @@ -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; @@ -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 { @@ -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)); @@ -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 @@ -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( @@ -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()); } diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlResponse.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlResponse.java index 36abfd2444e..e37a69cf60c 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlResponse.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlResponse.java @@ -125,16 +125,21 @@ public Optional getUI() { * {@code script-src} directive of the Content Security Policy. *

* 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() { @@ -145,10 +150,14 @@ public Optional getUI() { * Gets the Content Security Policy that is sent with this response. *

* 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}. *

* 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 @@ -160,20 +169,28 @@ public Optional getUI() { * document, which gets the nonce, or load it from a script that is already * trusted. *

- * 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. *

* 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 */ diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java index 0cb26ecb433..f90b754ee90 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java @@ -30,6 +30,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -49,10 +50,14 @@ import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatcher; import org.mockito.InOrder; import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import tools.jackson.databind.node.ObjectNode; import com.vaadin.experimental.Feature; @@ -269,11 +274,27 @@ public void serveIndexHtml_listenerSetsCacheControl_overridesDefault() inOrder.verify(response).setHeader("Cache-Control", "max-age=60"); } - @Test - public void serveIndexHtml_strictCsp_sendsPolicyWithNonceOnEveryScript() + @ParameterizedTest + @CsvSource({ + "STRICT, true, Content-Security-Policy, " + + "Content-Security-Policy-Report-Only", + "WARN, true, Content-Security-Policy-Report-Only, " + + "Content-Security-Policy", + "WARN, false, Content-Security-Policy-Report-Only, " + + "Content-Security-Policy", + "STRICT, false, Content-Security-Policy-Report-Only, " + + "Content-Security-Policy" }) + public void serveIndexHtml_policySent_sendsPolicyWithNonceOnEveryScript( + ContentSecurityPolicyMode contentSecurityPolicyMode, + boolean productionMode, String headerName, String otherHeaderName) throws IOException { deploymentConfiguration - .setContentSecurityPolicyMode(ContentSecurityPolicyMode.STRICT); + .setContentSecurityPolicyMode(contentSecurityPolicyMode); + if (!productionMode) { + useDevelopmentMode(); + } + // A policy of the other kind is evaluated on its own by the browser + when(response.containsHeader(otherHeaderName)).thenReturn(true); service.addIndexHtmlRequestListener(evt -> { evt.getDocument().body().appendElement("script").attr("src", "listener.js"); @@ -288,7 +309,8 @@ public void serveIndexHtml_strictCsp_sendsPolicyWithNonceOnEveryScript() "https://api.example.com"); }); - String header = serveIndexHtmlAndCaptureCspHeader(); + String header = serveIndexHtmlAndCaptureCspHeader(headerName); + verify(response, never()).setHeader(eq(otherHeaderName), any()); Matcher matcher = Pattern.compile( "script-src 'nonce-([A-Za-z0-9+/]{22})' 'strict-dynamic'") .matcher(header); @@ -315,7 +337,8 @@ public void serveIndexHtml_strictCsp_sendsPolicyWithNonceOnEveryScript() .hasAttr("nonce")); responseOutput.reset(); - assertFalse(serveIndexHtmlAndCaptureCspHeader().contains(nonce), + assertFalse( + serveIndexHtmlAndCaptureCspHeader(headerName).contains(nonce), "Every response should get a new nonce"); } @@ -357,7 +380,8 @@ public void serveIndexHtml_strictCspAndNonceProvided_scriptsGetItAndNoPolicyIsSe } @ParameterizedTest - @CsvSource({ "OFF, true", "WARN, true", "STRICT, false", "OFF, false" }) + @CsvSource({ "OFF, true", "WARN, true", "STRICT, false", "OFF, false", + "WARN, false" }) public void serveIndexHtml_nonceProvided_scriptsGetItInEveryMode( ContentSecurityPolicyMode contentSecurityPolicyMode, boolean productionMode) throws Exception { @@ -385,6 +409,8 @@ public void serveIndexHtml_nonceProvided_scriptsGetItInEveryMode( // not know the mode of Vaadin verify(response, never()).setHeader(eq("Content-Security-Policy"), any()); + verify(response, never()) + .setHeader(eq("Content-Security-Policy-Report-Only"), any()); assertEquals("provided-nonce", responses.get(0).getContentSecurityPolicyNonce()); assertNull(responses.get(0).getContentSecurityPolicy()); @@ -408,7 +434,8 @@ public void serveIndexHtml_strictCspAndProviderWithoutNonce_policyOfVaadinIsSent .lookup(ContentSecurityPolicyNonceProvider.class)) .thenReturn(provider); - String header = serveIndexHtmlAndCaptureCspHeader(); + String header = serveIndexHtmlAndCaptureCspHeader( + "Content-Security-Policy"); Matcher matcher = Pattern.compile( "script-src 'nonce-([A-Za-z0-9+/]{22})' 'strict-dynamic'") @@ -466,49 +493,92 @@ public void serveIndexHtml_strictCspAndHeaderAlreadyPresent_throws() { } @ParameterizedTest - @CsvSource({ "OFF, true", "WARN, true", "STRICT, false" }) - public void serveIndexHtml_noStrictCspInProduction_noHeaderOrNonce( + @CsvSource({ "WARN, true", "WARN, false", "STRICT, false" }) + public void serveIndexHtml_reportOnlyPolicyAndHeaderAlreadyPresent_keepsItAndWarnsOnce( ContentSecurityPolicyMode contentSecurityPolicyMode, - boolean productionMode) throws Exception { + boolean productionMode) throws IOException { deploymentConfiguration .setContentSecurityPolicyMode(contentSecurityPolicyMode); + if (!productionMode) { + useDevelopmentMode(); + } + when(response.containsHeader("Content-Security-Policy-Report-Only")) + .thenReturn(true); + Logger logger = mock(Logger.class); + when(logger.isWarnEnabled()).thenReturn(true); + // Other classes log through the real loggers + try (MockedStatic loggerFactory = mockStatic( + LoggerFactory.class, Mockito.CALLS_REAL_METHODS)) { + loggerFactory + .when(() -> LoggerFactory + .getLogger(IndexHtmlRequestHandler.class)) + .thenReturn(logger); + + indexHtmlRequestHandler.synchronizedHandleRequest(session, + createVaadinRequest("/"), response); + responseOutput.reset(); + indexHtmlRequestHandler.synchronizedHandleRequest(session, + createVaadinRequest("/"), response); + } + + verify(logger).warn(anyString(), + eq("Content-Security-Policy-Report-Only"), + eq("contentSecurityPolicy"), + eq(contentSecurityPolicyMode.name().toLowerCase(Locale.ROOT))); + + verify(response, never()) + .setHeader(eq("Content-Security-Policy-Report-Only"), any()); + verify(response, never()).setHeader(eq("Content-Security-Policy"), + any()); + String html = responseOutput.toString(StandardCharsets.UTF_8); + assertTrue(html.contains(" responses = new ArrayList<>(); service.addIndexHtmlRequestListener(responses::add); indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response); - // In development mode, listeners still get the policy so that they - // work the same way in both modes assertEquals(1, responses.size()); - boolean strict = contentSecurityPolicyMode == ContentSecurityPolicyMode.STRICT; - assertEquals(strict, - responses.get(0).getContentSecurityPolicyNonce() != null); - assertEquals(strict, - responses.get(0).getContentSecurityPolicy() != null); + assertNull(responses.get(0).getContentSecurityPolicyNonce()); + assertNull(responses.get(0).getContentSecurityPolicy()); verify(response, never()).setHeader(eq("Content-Security-Policy"), any()); + verify(response, never()) + .setHeader(eq("Content-Security-Policy-Report-Only"), any()); assertFalse(responseOutput.toString(StandardCharsets.UTF_8) .contains("nonce=")); } - private String serveIndexHtmlAndCaptureCspHeader() throws IOException { + private void useDevelopmentMode() throws IOException { + File projectRootFolder = Files + .createTempDirectory(temporaryFolder, "temp").toFile(); + TestUtil.createIndexHtmlStub(projectRootFolder); + TestUtil.createStatsJsonStub(projectRootFolder); + deploymentConfiguration.setProductionMode(false); + deploymentConfiguration.setProjectFolder(projectRootFolder); + } + + private String serveIndexHtmlAndCaptureCspHeader(String headerName) + throws IOException { clearInvocations(response); indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response); ArgumentCaptor header = ArgumentCaptor.forClass(String.class); - verify(response).setHeader(eq("Content-Security-Policy"), - header.capture()); + verify(response).setHeader(eq(headerName), header.capture()); return header.getValue(); } diff --git a/vaadin-spring/src/main/java/com/vaadin/flow/spring/security/VaadinSecurityConfigurer.java b/vaadin-spring/src/main/java/com/vaadin/flow/spring/security/VaadinSecurityConfigurer.java index ee11dd5ddf8..876b4dc3b74 100644 --- a/vaadin-spring/src/main/java/com/vaadin/flow/spring/security/VaadinSecurityConfigurer.java +++ b/vaadin-spring/src/main/java/com/vaadin/flow/spring/security/VaadinSecurityConfigurer.java @@ -685,7 +685,10 @@ public VaadinSecurityConfigurer enableNavigationAccessControl( * policy is added and the default headers stay off. *

* Only use it when the strict mode is enabled: in the other modes Vaadin - * runs JavaScript in ways that the policy blocks. + * runs JavaScript in ways that the policy blocks. In the + * {@code vaadin.contentSecurityPolicy=warn} mode, send the policy as + * report-only instead, see + * {@link #contentSecurityPolicy(Consumer, Customizer)}. * * @return this configurer, for chaining * @see #contentSecurityPolicy(Consumer) @@ -731,7 +734,8 @@ public VaadinSecurityConfigurer contentSecurityPolicy( * Configures Spring Security to send the Content Security Policy that a * Vaadin page needs when the application runs with * {@code vaadin.contentSecurityPolicy=strict}, with the changes of the - * given customizers. + * given customizers. With {@code vaadin.contentSecurityPolicy=warn}, use + * the second customizer to send the policy as report-only. *

* The second customizer sets the other options of the Content Security * Policy of Spring Security, such as the requests the policy is sent for: @@ -744,6 +748,16 @@ public VaadinSecurityConfigurer contentSecurityPolicy( * It runs after the policy directives are set, so directives it sets * replace the policy. *

+ * The configurer does not read the Content Security Policy mode of Vaadin, + * so with {@code vaadin.contentSecurityPolicy=warn} the application asks + * for a report-only policy itself. The browser then reports what the policy + * would block in the strict mode without blocking it: + * + *

+     * http.with(vaadin(), vaadin -> vaadin.contentSecurityPolicy(policy -> {
+     * }, config -> config.reportOnly()));
+     * 
+ *

* These options can leave Vaadin pages without the protection of the strict * mode, and nothing reports it: a Vaadin page of a request that the request * matcher does not match is sent without any policy, and a report-only