-
Notifications
You must be signed in to change notification settings - Fork 52.6k
fix: allow dashboard host headers for trusted proxies #28954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eco-botfleet-audit
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
eco-botfleet-audit:audit/dashboard-allowed-host-trusted-proxy-450
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+116
−19
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,23 +159,10 @@ def _require_token(request: Request) -> None: | |
| }) | ||
|
|
||
|
|
||
| def _is_accepted_host(host_header: str, bound_host: str) -> bool: | ||
| """True if the Host header targets the interface we bound to. | ||
|
|
||
| Accepts: | ||
| - Exact bound host (with or without port suffix) | ||
| - Loopback aliases when bound to loopback | ||
| - Any host when bound to 0.0.0.0 (explicit opt-in to non-loopback, | ||
| no protection possible at this layer) | ||
| """ | ||
| def _host_header_name(host_header: str) -> str: | ||
| """Return the normalized hostname portion of a Host header.""" | ||
| if not host_header: | ||
| return False | ||
| # Strip port suffix. IPv6 addresses use bracket notation: | ||
| # [::1] — no port | ||
| # [::1]:9119 — with port | ||
| # Plain hosts/v4: | ||
| # localhost:9119 | ||
| # 127.0.0.1:9119 | ||
| return "" | ||
| h = host_header.strip() | ||
| if h.startswith("["): | ||
| # IPv6 bracketed — port (if any) follows "]:" | ||
|
|
@@ -186,14 +173,64 @@ def _is_accepted_host(host_header: str, bound_host: str) -> bool: | |
| host_only = h.strip("[]") | ||
| else: | ||
| host_only = h.rsplit(":", 1)[0] if ":" in h else h | ||
| host_only = host_only.lower() | ||
| return host_only.strip().lower().rstrip(".") | ||
|
|
||
|
|
||
| def _normalize_allowed_hosts(allowed_hosts: Optional[List[str]] = None) -> Tuple[str, ...]: | ||
| """Normalize configured extra dashboard Host values. | ||
|
|
||
| Values are exact hostnames (optionally with a port suffix). Schemes, | ||
| paths, blanks and duplicates are ignored so config/env/CLI inputs remain | ||
| operator-friendly without widening the trust boundary. | ||
| """ | ||
| normalized: List[str] = [] | ||
| seen = set() | ||
| for value in allowed_hosts or []: | ||
| raw = str(value or "").strip() | ||
| if not raw or "*" in raw: | ||
| continue | ||
| if "://" in raw: | ||
| parsed = urllib.parse.urlparse(raw) | ||
| raw = parsed.netloc or parsed.path | ||
| else: | ||
| raw = raw.split("/", 1)[0] | ||
| host = _host_header_name(raw) | ||
| if host and host not in seen: | ||
| seen.add(host) | ||
| normalized.append(host) | ||
| return tuple(normalized) | ||
|
|
||
|
|
||
| def _is_accepted_host( | ||
| host_header: str, | ||
| bound_host: str, | ||
| allowed_hosts: Optional[List[str]] = None, | ||
| ) -> bool: | ||
| """True if the Host header targets the interface we bound to. | ||
|
|
||
| Accepts: | ||
| - Exact bound host (with or without port suffix) | ||
| - Loopback aliases when bound to loopback | ||
| - Any host when bound to 0.0.0.0 (explicit opt-in to non-loopback, | ||
| no protection possible at this layer) | ||
| - Extra exact hosts supplied by --allowed-host / dashboard.allowed_hosts | ||
| for loopback reverse-proxy deployments. | ||
| """ | ||
| host_only = _host_header_name(host_header) | ||
| if not host_only: | ||
| return False | ||
|
|
||
| # 0.0.0.0 bind means operator explicitly opted into all-interfaces | ||
| # (requires --insecure per web_server.start_server). No Host-layer | ||
| # defence can protect that mode; rely on operator network controls. | ||
| if bound_host in {"0.0.0.0", "::"}: | ||
| return True | ||
|
|
||
| # Explicit reverse-proxy names are exact matches only; wildcards are not | ||
| # supported because they would weaken the DNS-rebinding defence. | ||
| if host_only in _normalize_allowed_hosts(allowed_hosts): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allowlist check needs to live inside the loopback-bind branch. As written, a server explicitly bound to a non-loopback hostname will also accept any configured proxy hostname, weakening the exact-match rule for that mode. |
||
| return True | ||
|
|
||
| # Loopback bind: accept the loopback names | ||
| bound_lc = bound_host.lower() | ||
| if bound_lc in _LOOPBACK_HOST_VALUES: | ||
|
|
@@ -220,7 +257,8 @@ async def host_header_middleware(request: Request, call_next): | |
| bound_host = getattr(app.state, "bound_host", None) | ||
| if bound_host: | ||
| host_header = request.headers.get("host", "") | ||
| if not _is_accepted_host(host_header, bound_host): | ||
| allowed_hosts = getattr(app.state, "allowed_hosts", ()) | ||
| if not _is_accepted_host(host_header, bound_host, allowed_hosts): | ||
| return JSONResponse( | ||
| status_code=400, | ||
| content={ | ||
|
|
@@ -4518,6 +4556,7 @@ def start_server( | |
| allow_public: bool = False, | ||
| *, | ||
| embedded_chat: bool = False, | ||
| allowed_hosts: Optional[List[str]] = None, | ||
| ): | ||
| """Start the web UI server.""" | ||
| import uvicorn | ||
|
|
@@ -4544,6 +4583,7 @@ def start_server( | |
| # PTY child uses to publish events to the dashboard sidebar. | ||
| app.state.bound_host = host | ||
| app.state.bound_port = port | ||
| app.state.allowed_hosts = _normalize_allowed_hosts(allowed_hosts) | ||
|
|
||
| if open_browser: | ||
| import webbrowser | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid adding a public HERMES_* env var for this non-secret behavior; AGENTS.md says behavioral settings should go through config.yaml rather than new user-facing environment variables.