fix: harden license server and web gui security

This commit is contained in:
Your Name committed 2026-06-28 09:24:22 +08:00
1 parent 7c6189fd5e
commit ee3424cd9e
9 files changed
+357 -11

No files matched your search

+21
View File
@@ -2,12 +2,33 @@ import json
from http.server import BaseHTTPRequestHandler
SECURITY_HEADERS = (
("X-Content-Type-Options", "nosniff"),
("Referrer-Policy", "no-referrer"),
("X-Frame-Options", "DENY"),
)
def send_security_headers(handler: BaseHTTPRequestHandler) -> None:
for key, value in SECURITY_HEADERS:
handler.send_header(key, value)
def send_html_security_headers(handler: BaseHTTPRequestHandler) -> None:
send_security_headers(handler)
handler.send_header(
"Content-Security-Policy",
"default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'",
)
def send_json(handler: BaseHTTPRequestHandler, obj, status: int = 200) -> None:
data = json.dumps(obj, ensure_ascii=False).encode("utf-8")
handler.send_response(status)
handler.send_header("Content-Type", "application/json; charset=utf-8")
handler.send_header("Cache-Control", "no-store")
handler.send_header("Content-Length", str(len(data)))
send_security_headers(handler)
handler.end_headers()
try:
handler.wfile.write(data)
+4
View File
@@ -21,6 +21,7 @@ from gui.web_gui_pkg import (
write_config,
proc_runner_ops as _proc_ops,
)
from gui.web_gui_pkg.request_parsing import send_html_security_headers, send_security_headers
BASE_DIR = (
path_config.PROJECT_ROOT
@@ -111,6 +112,7 @@ def _send_html(handler, html: str, status: int = 200) -> None:
handler.send_header("Content-Type", "text/html; charset=utf-8")
handler.send_header("Content-Length", str(len(data)))
handler.send_header("Cache-Control", "no-store")
send_html_security_headers(handler)
handler.end_headers()
handler.wfile.write(data)
@@ -355,6 +357,7 @@ def handle_get(handler, u, runner, csrf_token: str) -> None:
handler.send_header("Content-Type", "image/png")
handler.send_header("Content-Length", str(len(_EMPTY_PNG_BYTES)))
handler.send_header("Cache-Control", "no-store")
send_security_headers(handler)
handler.end_headers()
handler.wfile.write(_EMPTY_PNG_BYTES)
return
@@ -372,6 +375,7 @@ def handle_get(handler, u, runner, csrf_token: str) -> None:
handler.send_header("Content-Type", ctype)
handler.send_header("Content-Length", str(len(data)))
handler.send_header("Cache-Control", "no-store")
send_security_headers(handler)
handler.end_headers()
handler.wfile.write(data)
return
+7
View File
@@ -3,6 +3,8 @@ from http import HTTPStatus
from http.server import BaseHTTPRequestHandler
from typing import Callable
from gui.web_gui_pkg.request_parsing import send_html_security_headers, send_security_headers
def serve_static(
handler: BaseHTTPRequestHandler,
@@ -60,6 +62,7 @@ def serve_static(
handler.send_response(HTTPStatus.NOT_MODIFIED)
handler.send_header("ETag", etag)
handler.send_header("Cache-Control", cache_control)
send_security_headers(handler)
handler.end_headers()
return
except Exception:
@@ -68,6 +71,10 @@ def serve_static(
handler.send_header("Content-Type", ctype)
handler.send_header("Content-Length", str(len(data)))
handler.send_header("Cache-Control", cache_control)
if ext == ".html":
send_html_security_headers(handler)
else:
send_security_headers(handler)
if etag:
handler.send_header("ETag", etag)
handler.end_headers()