Skip to content

Commit dcf4b69

Browse files
committed
fix: really enable use of CSP hashes for styles
It has turned out that hashes don't work alone and requires 'unsafe-hashes' directive. Here is the error from Chrome: Either the 'unsafe-inline' keyword, a hash ('sha256-tIs8OfjWm8MHgPJrHv7mM4wvA/FDFcra3Pd5icRMX+k='), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present. Part of #226
1 parent 448894a commit dcf4b69

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/main/java/ru/mystamps/web/support/spring/security/ContentSecurityPolicyHeaderWriter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,30 @@ protected String constructDirectives(String uri) {
179179
.append(REPORT_URI).append(host).append(SiteUrl.CSP_REPORTS_HANDLER).append(SEPARATOR)
180180
.append(STYLE_SRC).append(useSingleHost ? STYLES_SELF : STYLES_ALT);
181181

182+
boolean hasHashes = false;
182183
if (useCdn) {
183184
sb.append(' ').append(STYLES_CDN);
184185
}
185186

186187
if (onCollectionInfoPage) {
187188
sb.append(STYLE_COLLECTION_INFO);
189+
hasHashes = true;
188190

189191
} else if (uri.matches(ADD_IMAGE_PAGE_PATTERN)) {
190192
sb.append(STYLE_SERIES_ADD_IMAGE);
193+
hasHashes = true;
191194

192195
if (onAddSeriesPage) {
193196
sb.append(STYLE_SERIES_ADD_PAGE);
194197
}
195198

196199
} else if (onH2ConsolePage) {
197200
sb.append(STYLE_H2_CONSOLE);
201+
hasHashes = true;
202+
}
203+
204+
if (hasHashes) {
205+
sb.append(" 'unsafe-hashes'");
198206
}
199207

200208
sb.append(SEPARATOR)

src/test/java/ru/mystamps/web/support/spring/security/ContentSecurityPolicyHeaderWriterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public void onCollectionInfoPageWithLocalResources() {
154154
+ " 'self'"
155155
+ " https://www.gstatic.com"
156156
+ " 'sha256-/kXZODfqoc2myS1eI6wr0HH8lUt+vRhW8H/oL+YJcMg='"
157+
+ " 'unsafe-hashes'"
157158
)
158159
.contains(
159160
"script-src"
@@ -186,6 +187,7 @@ public void onCollectionInfoPageWithResourcesFromCdn() {
186187
+ " https://maxcdn.bootstrapcdn.com"
187188
+ " https://www.gstatic.com"
188189
+ " 'sha256-/kXZODfqoc2myS1eI6wr0HH8lUt+vRhW8H/oL+YJcMg='"
190+
+ " 'unsafe-hashes'"
189191
)
190192
.contains(
191193
"script-src"
@@ -220,6 +222,7 @@ public void onSeriesAddImagePageWithLocalResources() {
220222
+ " https://cdn.jsdelivr.net"
221223
+ " 'self'"
222224
+ " 'sha256-DpmxvnMJIlwkpmmAANZYNzmyfnX2PQCBDO4CB2BFjzU='"
225+
+ " 'unsafe-hashes'"
223226
)
224227
.contains("connect-src 'self'")
225228
// hope that all other directives are the same as on the index page
@@ -248,6 +251,7 @@ public void onSeriesAddImagePageWithResourcesFromCdn() {
248251
+ " https://stamps.filezz.ru"
249252
+ " https://maxcdn.bootstrapcdn.com"
250253
+ " 'sha256-DpmxvnMJIlwkpmmAANZYNzmyfnX2PQCBDO4CB2BFjzU='"
254+
+ " 'unsafe-hashes'"
251255
)
252256
.contains(
253257
"script-src"
@@ -283,6 +287,7 @@ public void onSeriesAddPageWithLocalResources() {
283287
+ " 'self'"
284288
+ " 'sha256-DpmxvnMJIlwkpmmAANZYNzmyfnX2PQCBDO4CB2BFjzU='"
285289
+ " https://cdnjs.cloudflare.com"
290+
+ " 'unsafe-hashes'"
286291
)
287292
.contains(
288293
"script-src"
@@ -316,6 +321,7 @@ public void onSeriesAddPageWithResourcesFromCdn() {
316321
+ " https://maxcdn.bootstrapcdn.com"
317322
+ " 'sha256-DpmxvnMJIlwkpmmAANZYNzmyfnX2PQCBDO4CB2BFjzU='"
318323
+ " https://cdnjs.cloudflare.com"
324+
+ " 'unsafe-hashes'"
319325
)
320326
.contains(
321327
"script-src"
@@ -357,6 +363,7 @@ public void onH2ConsoleWithLocalResources() {
357363
+ " 'sha256-yBhVF062O1IGu3ZngyEhh9l561VFLsJpdSxVtbwisRY='"
358364
+ " 'sha256-RZ7vfNSfdJtvDeBSz2SI5g3wroaD1A1SzsDb04Yw9V0='"
359365
+ " 'sha256-PGJ8tjuz2DXGgB1Sie9pW8BrxBGK6EQndbLEkXd44T8='"
366+
+ " 'unsafe-hashes'"
360367
)
361368
.contains("child-src 'self'")
362369
// hope that all other directives are the same as on the index page

0 commit comments

Comments
 (0)