Skip to content

Commit 6467359

Browse files
committed
Set CSP header by default
1 parent 0b94369 commit 6467359

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

optuna_dashboard/_app.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,30 @@ def index() -> BottleViewReturn:
103103
# Accept any following paths for client-side routing
104104
@app.get("/dashboard<:re:(/.*)?>")
105105
def dashboard() -> BottleViewReturn:
106-
return static_file("index.html", BASE_DIR, mimetype="text/html")
106+
if allow_unsafe:
107+
headers = {}
108+
else:
109+
# CSP header
110+
if llm_provider is not None:
111+
script_src_str = "script-src 'self' 'unsafe-inline' 'unsafe-eval'"
112+
else:
113+
# Parallel coordinate, which uses WebGL, requires unsafe-eval.
114+
script_src_str = "script-src 'self' 'unsafe-eval'"
115+
116+
csp_string = ";".join(
117+
[
118+
"default-src 'self'",
119+
"img-src 'self' data: blob:",
120+
"frame-src 'self'",
121+
"object-src 'none'",
122+
"connect-src 'self'",
123+
"style-src 'self' data: 'unsafe-inline'",
124+
script_src_str,
125+
]
126+
)
127+
headers = {"Content-Security-Policy": csp_string}
128+
129+
return static_file("index.html", BASE_DIR, mimetype="text/html", headers=headers)
107130

108131
@app.get("/api/meta")
109132
@json_api_view

0 commit comments

Comments
 (0)