Release v0.51.317 — Release KG (Phase 3 light: align CSP enforcement with report policy, #3727) (#3786)
Some checks failed
Release & Docker / release (push) Has been cancelled
Some checks failed
Release & Docker / release (push) Has been cancelled
Phase-3-LOW backend security fix. #3727 (@rodboev, #1909): enforced CSP now honors the same connect-src as report-only (built from one shared template). Full suite 8205 passed, Codex SAFE (live-verified headers), Opus SHIP. Co-authored-by: rodboev <rodboev@users.noreply.github.com>
This commit is contained in:
63
server.py
63
server.py
@@ -134,58 +134,14 @@ from urllib.parse import urlparse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_CSP_CONNECT_BASE = (
|
||||
"'self' http://127.0.0.1:* http://localhost:* "
|
||||
"ws://127.0.0.1:* ws://localhost:*"
|
||||
)
|
||||
_CSP_EXTRA_CONNECT_RE = re.compile(
|
||||
r"^(?:https?|wss?)://(?:\*\.)?[A-Za-z0-9._~-]+(?::(?P<port>\d{1,5}|\*))?$"
|
||||
)
|
||||
|
||||
|
||||
def _valid_csp_extra_connect_source(source: str) -> bool:
|
||||
match = _CSP_EXTRA_CONNECT_RE.fullmatch(source)
|
||||
if not match:
|
||||
return False
|
||||
port = match.group("port")
|
||||
if not port or port == "*":
|
||||
return True
|
||||
try:
|
||||
return 1 <= int(port) <= 65535
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def _csp_extra_connect_src() -> str:
|
||||
raw = os.getenv("HERMES_WEBUI_CSP_CONNECT_EXTRA", "").strip()
|
||||
if not raw:
|
||||
return ""
|
||||
sources = raw.split()
|
||||
if not sources or any(not _valid_csp_extra_connect_source(src) for src in sources):
|
||||
logger.warning("Ignoring invalid HERMES_WEBUI_CSP_CONNECT_EXTRA value")
|
||||
return ""
|
||||
return " " + " ".join(sources)
|
||||
|
||||
|
||||
def _build_csp_report_only_policy() -> str:
|
||||
connect_src = _CSP_CONNECT_BASE + _csp_extra_connect_src()
|
||||
return (
|
||||
"default-src 'self'; "
|
||||
"base-uri 'self'; "
|
||||
"object-src 'none'; "
|
||||
"frame-ancestors 'self'; "
|
||||
"script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; "
|
||||
"style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; "
|
||||
"img-src 'self' data: blob:; "
|
||||
"font-src 'self' data:; "
|
||||
"media-src 'self' data: blob:; "
|
||||
f"connect-src {connect_src}; "
|
||||
"report-uri /api/csp-report; report-to csp-endpoint"
|
||||
)
|
||||
|
||||
from api.auth import check_auth
|
||||
from api.config import HOST, PORT, STATE_DIR, SESSION_DIR, DEFAULT_WORKSPACE
|
||||
from api.helpers import j, get_profile_cookie, _CLIENT_DISCONNECT_ERRORS
|
||||
from api.helpers import (
|
||||
j,
|
||||
get_profile_cookie,
|
||||
_build_csp_report_only_policy,
|
||||
_CLIENT_DISCONNECT_ERRORS,
|
||||
)
|
||||
from api.profiles import set_request_profile, clear_request_profile
|
||||
from api.routes import handle_delete, handle_get, handle_patch, handle_post, handle_put
|
||||
from api.startup import auto_install_agent_deps, fix_credential_permissions
|
||||
@@ -306,11 +262,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
_CSP_REPORT_TO = '{"group":"csp-endpoint","max_age":10886400,"endpoints":[{"url":"/api/csp-report"}]}'
|
||||
|
||||
@classmethod
|
||||
def csp_report_only_policy(cls) -> str:
|
||||
return _build_csp_report_only_policy()
|
||||
def csp_report_only_policy(cls, extra_connect_src=None) -> str:
|
||||
return _build_csp_report_only_policy(extra_connect_src)
|
||||
|
||||
def end_headers(self) -> None:
|
||||
self.send_header("Content-Security-Policy-Report-Only", self.csp_report_only_policy())
|
||||
extra_connect_src = getattr(self, "_csp_extra_connect_src", None)
|
||||
self.send_header("Content-Security-Policy-Report-Only", self.csp_report_only_policy(extra_connect_src))
|
||||
self.send_header("Report-To", self._CSP_REPORT_TO)
|
||||
super().end_headers()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user