Skip to content

Commit 74db91d

Browse files
committed
CSP: adjust configuration to use CSS files from CDN on prod.
Addressed to #226
1 parent 43a4b46 commit 74db91d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ class ContentSecurityPolicyHeaderWriter implements HeaderWriter {
5454
// CheckStyle: ignore LineLength for next 1 line
5555
private static final String REPORT_URI = "report-uri https://mystamps.report-uri.io/r/default/csp/reportOnly";
5656

57-
// - 'self' is required for our own CSS files
5857
// - 'https://cdn.rawgit.com' is required by languages.min.css (TODO: GH #246)
59-
private static final String STYLE_SRC = "style-src 'self' https://cdn.rawgit.com";
58+
private static final String STYLE_SRC = "style-src https://cdn.rawgit.com";
59+
60+
// - 'self' is required for our own CSS files
61+
private static final String STYLES_SELF = " 'self'";
62+
63+
// - 'https://stamps.filezz.ru' is required for our own CSS files
64+
// - 'https://maxcdn.bootstrapcdn.com' is required for bootstrap.min.js
65+
private static final String STYLES_CDN =
66+
" https://stamps.filezz.ru https://maxcdn.bootstrapcdn.com";
6067

6168
// - 'sha256-Dpm...' is required for 'box-shadow: none; border: 0px;' inline CSS
6269
// that are using on /series/add and /series/{id} pages.
@@ -118,6 +125,7 @@ public void writeHeaders(HttpServletRequest request, HttpServletResponse respons
118125
response.setHeader("Content-Security-Policy-Report-Only", constructDirectives(uri));
119126
}
120127

128+
@SuppressWarnings("PMD.NPathComplexity")
121129
private String constructDirectives(String uri) {
122130
boolean onCollectionInfoPage = uri.startsWith(COLLECTION_INFO_PAGE_PATTERN);
123131

@@ -127,7 +135,8 @@ private String constructDirectives(String uri) {
127135
.append(IMG_SRC).append(SEPARATOR)
128136
.append(FONT_SRC).append(SEPARATOR)
129137
.append(REPORT_URI).append(SEPARATOR)
130-
.append(STYLE_SRC);
138+
.append(STYLE_SRC)
139+
.append(useSingleHost ? STYLES_SELF : STYLES_CDN);
131140

132141
if (onCollectionInfoPage) {
133142
sb.append(STYLE_COLLECTION_INFO);

0 commit comments

Comments
 (0)