Skip to content

Commit 212b787

Browse files
committed
Properly merge extra_csp_directives with built-in directives
1 parent 84c0dd0 commit 212b787

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/qwc2_viewer.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,15 @@ def qwc2_index(self, identity, params, request_url):
210210

211211
# Inject CSP header and modify script tags
212212
nonce = secrets.token_urlsafe()
213-
csp = "; ".join([
214-
"script-src 'nonce-%s' 'strict-dynamic' 'wasm-unsafe-eval'" % nonce,
213+
csp = {
214+
"script-src": "'nonce-%s' 'strict-dynamic'" % nonce,
215215
# "style-src 'nonce-%s'" % nonce # TODO
216-
])
217-
if self.extra_csp_directives:
218-
csp += "; " + self.extra_csp_directives
216+
}
217+
for extra_csp in filter(bool, self.extra_csp_directives.split(";")):
218+
parts = extra_csp.strip().split(" ", 1)
219+
csp[parts[0]] = (csp.get(parts[0], "") + " " + parts[1]).strip()
220+
221+
csp = "; ".join(list(map(lambda t: " ".join(t), csp.items()))) + ";"
219222
viewer_index = viewer_index.replace('<head>', '<head>\n<meta http-equiv="Content-Security-Policy" content="%s">' % csp)
220223
viewer_index = viewer_index.replace('<script ', '<script nonce="%s" ' % nonce)
221224
viewer_index = viewer_index.replace('<script>', '<script nonce="%s">' % nonce)

0 commit comments

Comments
 (0)