Skip to content

Commit 0bf34b5

Browse files
committed
Conditionally add upgrade-secure-requests CSP directive
1 parent 063f4ff commit 0bf34b5

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

server/configs/application.properties

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ management.server.port=@@shutdownPort@@
132132
#useLocalBuild# font-src 'self' data: ${FONT.SOURCES} ; /* Limit font source loading locations */\
133133
#useLocalBuild# script-src 'unsafe-eval' 'strict-dynamic' 'nonce-${REQUEST.SCRIPT.NONCE}' ; /* Limit scripts to those with nonces or transitive scripts */\
134134
#useLocalBuild# base-uri 'self' ; /* Limit the base tags to only source from current server */\
135+
#useLocalBuild# ${UPGRADE.SECURE.REQUESTS} /* Conditionally add upgrade-secure-requests directive if HTTPS is required */\
135136
#useLocalBuild# frame-ancestors 'self' ; /* Limit iframe content destinations (who can load this server's content into an iframe) */\
136137
#useLocalBuild# frame-src 'self' ${FRAME.SOURCES} ; /* Limit iframe content sources (from what servers can this server's iframe content be loaded) */\
137-
#useLocalBuild# report-uri /admin-contentSecurityPolicyReport.api?cspVersion=e11&${CSP.REPORT.PARAMS} ; /* Report any encountered CSP violations to the supplied URL */
138+
#useLocalBuild# report-uri /admin-contentSecurityPolicyReport.api?cspVersion=e12&${CSP.REPORT.PARAMS} ; /* Report any encountered CSP violations to the local server */
138139
## END OF CSP ENFORCE BLOCK (DO NOT CHANGE THIS TEXT)
139140

140141
## START OF CSP REPORT BLOCK (DO NOT CHANGE THIS TEXT)
@@ -147,9 +148,10 @@ csp.report=\
147148
font-src 'self' data: ${FONT.SOURCES} ; /* Limit font source loading locations */\
148149
script-src 'unsafe-eval' 'strict-dynamic' 'nonce-${REQUEST.SCRIPT.NONCE}' ; /* Limit scripts to those with nonces or transitive scripts */\
149150
base-uri 'self' ; /* Limit the base tags to only source from current server */\
151+
${UPGRADE.SECURE.REQUESTS} /* Conditionally add upgrade-secure-requests directive if HTTPS is required */\
150152
frame-ancestors 'self' ; /* Limit iframe content destinations (who can load this server's content into an iframe) */\
151153
frame-src 'self' ${FRAME.SOURCES} ; /* Limit iframe content sources (from what servers can this server's iframe content be loaded) */\
152-
report-uri /admin-contentSecurityPolicyReport.api?cspVersion=r11&${CSP.REPORT.PARAMS} ; /* Report any encountered CSP violations to the supplied URL */
154+
report-uri /admin-contentSecurityPolicyReport.api?cspVersion=r12&${CSP.REPORT.PARAMS} ; /* Report any encountered CSP violations to the local server */
153155
## END OF CSP REPORT BLOCK (DO NOT CHANGE THIS TEXT)
154156

155157
## Use a custom logging configuration

server/embedded/src/org/labkey/embedded/LabKeyServer.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ public static void main(String[] args)
6666

6767
SpringApplication application = new SpringApplication(LabKeyServer.class);
6868
application.addListeners(new ApplicationPidFileWriter("./labkey.pid"));
69+
// A strong Content Security Policy that reports violations to this server
70+
String strongCsp = """
71+
default-src 'self' ;
72+
connect-src 'self' ${CONNECTION.SOURCES} ;
73+
object-src 'none' ;
74+
style-src 'self' 'unsafe-inline' ${STYLE.SOURCES} ;
75+
img-src 'self' data: ${IMAGE.SOURCES} ;
76+
font-src 'self' data: ${FONT.SOURCES} ;
77+
script-src 'unsafe-eval' 'strict-dynamic' 'nonce-${REQUEST.SCRIPT.NONCE}' ;
78+
base-uri 'self' ;
79+
${UPGRADE.SECURE.REQUESTS}
80+
frame-ancestors 'self' ;
81+
frame-src 'self' ${FRAME.SOURCES} ;
82+
report-uri /admin-contentSecurityPolicyReport.api?cspVersion=r12&${CSP.REPORT.PARAMS}
83+
""";
6984
application.setDefaultProperties(Map.of(
7085
"server.tomcat.basedir", ".",
7186
"server.tomcat.accesslog.directory", logHome,
@@ -81,20 +96,7 @@ public static void main(String[] args)
8196
"server.error.include-stacktrace", "never",
8297
"server.error.include-message", "always",
8398

84-
// A strong report-only Content Security Policy that reports violations to this server
85-
"csp.report", """
86-
default-src 'self' ;
87-
connect-src 'self' ${CONNECTION.SOURCES} ;
88-
object-src 'none' ;
89-
style-src 'self' 'unsafe-inline' ${STYLE.SOURCES} ;
90-
img-src 'self' data: ${IMAGE.SOURCES} ;
91-
font-src 'self' data: ${FONT.SOURCES} ;
92-
script-src 'unsafe-eval' 'strict-dynamic' 'nonce-${REQUEST.SCRIPT.NONCE}' ;
93-
base-uri 'self' ;
94-
frame-ancestors 'self' ;
95-
frame-src 'self' ${FRAME.SOURCES} ;
96-
report-uri /admin-contentSecurityPolicyReport.api?cspVersion=r11&${CSP.REPORT.PARAMS}
97-
"""
99+
"csp.report", strongCsp
98100
));
99101
application.setBannerMode(Banner.Mode.OFF);
100102
application.run(args);

0 commit comments

Comments
 (0)