Skip to content

Commit 90aa07e

Browse files
committed
refactor(ContentSecurityPolicyHeaderWriter): return useSingleHost member back.
Should be in f616144 commit. Prerequisite to #1059
1 parent f5b9840 commit 90aa07e

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class ContentSecurityPolicyHeaderWriter implements HeaderWriter {
168168
+ 5;
169169

170170
private final boolean useCdn;
171+
private final boolean useSingleHost;
171172
private final boolean hasH2Console;
172173
private final String host;
173174

@@ -186,15 +187,13 @@ protected String constructDirectives(String uri) {
186187
StringBuilder sb = new StringBuilder(MIN_HEADER_LENGTH);
187188

188189
sb.append(DEFAULT_SRC).append(SEPARATOR)
189-
.append(IMG_SRC).append(useCdn ? IMG_SRC_ALT : IMG_SRC_SELF).append(SEPARATOR)
190+
.append(IMG_SRC).append(useSingleHost ? IMG_SRC_SELF : IMG_SRC_ALT).append(SEPARATOR)
190191
.append(FONT_SRC).append(useCdn ? FONT_SRC_CDN : FONT_SRC_SELF).append(SEPARATOR)
191192
.append(REPORT_URI).append(host).append(SiteUrl.CSP_REPORTS_HANDLER).append(SEPARATOR)
192-
.append(STYLE_SRC);
193+
.append(STYLE_SRC).append(useSingleHost ? STYLES_SELF : STYLES_ALT);
193194

194195
if (useCdn) {
195-
sb.append(STYLES_ALT).append(' ').append(STYLES_CDN);
196-
} else {
197-
sb.append(STYLES_SELF);
196+
sb.append(' ').append(STYLES_CDN);
198197
}
199198

200199
if (onCollectionInfoPage) {
@@ -212,12 +211,11 @@ protected String constructDirectives(String uri) {
212211
}
213212

214213
sb.append(SEPARATOR)
215-
.append(SCRIPT_SRC);
214+
.append(SCRIPT_SRC)
215+
.append(useSingleHost ? SCRIPTS_SELF : SCRIPTS_ALT);
216216

217217
if (useCdn) {
218-
sb.append(SCRIPTS_ALT).append(' ').append(SCRIPTS_CDN);
219-
} else {
220-
sb.append(SCRIPTS_SELF);
218+
sb.append(' ').append(SCRIPTS_CDN);
221219
}
222220

223221
if (onCollectionInfoPage) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@ public void configure(WebSecurity web) throws Exception {
8181
@SuppressWarnings({ "PMD.SignatureDeclareThrowsException", "checkstyle:linelength" })
8282
protected void configure(HttpSecurity http) throws Exception {
8383
boolean useCdn = environment.acceptsProfiles("prod");
84+
boolean useSingleHost = !environment.acceptsProfiles("prod");
8485
boolean hasH2Console = environment.acceptsProfiles("test");
8586

8687
// @todo #226 Introduce app.use-public-hostname property
8788
boolean usePublicHostname = environment.acceptsProfiles("prod");
8889
String hostname = usePublicHostname ? SiteUrl.PUBLIC_URL : SiteUrl.SITE;
8990

9091
ContentSecurityPolicyHeaderWriter cspWriter =
91-
new ContentSecurityPolicyHeaderWriter(useCdn, hasH2Console, hostname);
92+
new ContentSecurityPolicyHeaderWriter(useCdn, useSingleHost, hasH2Console, hostname);
9293

9394
http
9495
.authorizeRequests()

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class ContentSecurityPolicyHeaderWriterTest {
4848
@Test
4949
public void writeContentSecurityPolicyHeader() {
5050
ContentSecurityPolicyHeaderWriter writer =
51-
new ContentSecurityPolicyHeaderWriter(bool(), bool(), Random.host());
51+
new ContentSecurityPolicyHeaderWriter(bool(), bool(), bool(), Random.host());
5252

5353
HttpServletRequest request = new MockHttpServletRequest();
5454
HttpServletResponse response = new MockHttpServletResponse();
@@ -66,7 +66,7 @@ public void writeContentSecurityPolicyHeader() {
6666
@Test
6767
public void onIndexPageWithLocalResources() {
6868
ContentSecurityPolicyHeaderWriter writer =
69-
new ContentSecurityPolicyHeaderWriter(false, bool(), SiteUrl.SITE);
69+
new ContentSecurityPolicyHeaderWriter(false, true, bool(), SiteUrl.SITE);
7070
String[] directives = writer.constructDirectives("/").split(";");
7171

7272
assertThat(directives, hasItemInArray("default-src 'none'"));
@@ -92,7 +92,7 @@ public void onIndexPageWithLocalResources() {
9292
@Test
9393
public void onIndexPageWithResourcesFromCdn() {
9494
ContentSecurityPolicyHeaderWriter writer
95-
= new ContentSecurityPolicyHeaderWriter(true, bool(), SiteUrl.PUBLIC_URL);
95+
= new ContentSecurityPolicyHeaderWriter(true, false, bool(), SiteUrl.PUBLIC_URL);
9696
String[] directives = writer.constructDirectives("/").split(";");
9797

9898
assertThat(directives, hasItemInArray("default-src 'none'"));
@@ -136,7 +136,7 @@ public void onIndexPageWithResourcesFromCdn() {
136136
@Test
137137
public void onCollectionInfoPageWithLocalResources() {
138138
ContentSecurityPolicyHeaderWriter writer =
139-
new ContentSecurityPolicyHeaderWriter(false, bool(), Random.host());
139+
new ContentSecurityPolicyHeaderWriter(false, true, bool(), Random.host());
140140
String[] directives = writer.constructDirectives("/collection/user").split(";");
141141

142142
// test only the directives that differ from the index page
@@ -169,7 +169,7 @@ public void onCollectionInfoPageWithLocalResources() {
169169
@Test
170170
public void onCollectionInfoPageWithResourcesFromCdn() {
171171
ContentSecurityPolicyHeaderWriter writer =
172-
new ContentSecurityPolicyHeaderWriter(true, bool(), Random.host());
172+
new ContentSecurityPolicyHeaderWriter(true, false, bool(), Random.host());
173173
String[] directives = writer.constructDirectives("/collection/user").split(";");
174174

175175
// test only the directives that differ from the index page
@@ -205,7 +205,7 @@ public void onCollectionInfoPageWithResourcesFromCdn() {
205205
@Test
206206
public void onSeriesAddImagePageWithLocalResources() {
207207
ContentSecurityPolicyHeaderWriter writer =
208-
new ContentSecurityPolicyHeaderWriter(false, bool(), Random.host());
208+
new ContentSecurityPolicyHeaderWriter(false, true, bool(), Random.host());
209209

210210
for (String page : new String[]{"/series/11", "/series/12/ask", "/series/13/image"}) {
211211
String[] directives = writer.constructDirectives(page).split(";");
@@ -231,7 +231,7 @@ public void onSeriesAddImagePageWithLocalResources() {
231231
@Test
232232
public void onSeriesAddImagePageWithResourcesFromCdn() {
233233
ContentSecurityPolicyHeaderWriter writer =
234-
new ContentSecurityPolicyHeaderWriter(true, bool(), Random.host());
234+
new ContentSecurityPolicyHeaderWriter(true, false, bool(), Random.host());
235235

236236
for (String page : new String[]{"/series/11", "/series/12/ask", "/series/13/image"}) {
237237
String[] directives = writer.constructDirectives(page).split(";");
@@ -270,7 +270,7 @@ public void onSeriesAddImagePageWithResourcesFromCdn() {
270270
@Test
271271
public void onSeriesAddPageWithLocalResources() {
272272
ContentSecurityPolicyHeaderWriter writer =
273-
new ContentSecurityPolicyHeaderWriter(false, bool(), Random.host());
273+
new ContentSecurityPolicyHeaderWriter(false, true, bool(), Random.host());
274274
String[] directives = writer.constructDirectives("/series/add").split(";");
275275

276276
// test only the directives that differ from the index page
@@ -304,7 +304,7 @@ public void onSeriesAddPageWithLocalResources() {
304304
@Test
305305
public void onSeriesAddPageWithResourcesFromCdn() {
306306
ContentSecurityPolicyHeaderWriter writer =
307-
new ContentSecurityPolicyHeaderWriter(true, bool(), Random.host());
307+
new ContentSecurityPolicyHeaderWriter(true, false, bool(), Random.host());
308308
String[] directives = writer.constructDirectives("/series/add").split(";");
309309

310310
// test only the directives that differ from the index page
@@ -341,7 +341,7 @@ public void onSeriesAddPageWithResourcesFromCdn() {
341341
@Test
342342
public void onH2ConsoleWithLocalResources() {
343343
ContentSecurityPolicyHeaderWriter writer =
344-
new ContentSecurityPolicyHeaderWriter(false, true, Random.host());
344+
new ContentSecurityPolicyHeaderWriter(false, true, true, Random.host());
345345
String[] directives = writer.constructDirectives("/console/").split(";");
346346

347347
// test only the directives that are differ from the index page
@@ -376,7 +376,7 @@ public void onH2ConsoleWithLocalResources() {
376376
@Test
377377
public void onH2ConsoleWithResourcesFromCdn() {
378378
ContentSecurityPolicyHeaderWriter writer =
379-
new ContentSecurityPolicyHeaderWriter(true, false, Random.host());
379+
new ContentSecurityPolicyHeaderWriter(true, false, false, Random.host());
380380
String[] directives = writer.constructDirectives("/console/").split(";");
381381

382382
// "style-src" directive should be the same as for the index page

0 commit comments

Comments
 (0)