-
Notifications
You must be signed in to change notification settings - Fork 52.3k
fix: harden dashboard security and runtime status #61305
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
AndreasG78
wants to merge
4
commits into
NousResearch:main
Choose a base branch
from
AndreasG78:fix/dashboard-security-runtime-a11y
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0400fb0
fix: harden dashboard security and runtime status
AndreasG78 47c682c
test: align dashboard CI regressions with hardened status APIs
AndreasG78 fe038b8
fix: continue autonomously after gateway restore
396c8aa
fix: map local Jarvis author for PR attribution
AndreasG78 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
|
|
||
| import logging | ||
| from typing import Awaitable, Callable | ||
| from urllib.parse import urlsplit | ||
|
|
||
| from fastapi import Request | ||
| from fastapi.responses import JSONResponse, RedirectResponse, Response | ||
|
|
@@ -252,6 +253,76 @@ def _safe_next_target(request: Request) -> str: | |
| return quote(target, safe="") | ||
|
|
||
|
|
||
| def _normalise_origin(scheme: str, host: str) -> str: | ||
| """Return a canonical ``scheme://host[:port]`` origin string.""" | ||
| scheme = (scheme or "").lower() | ||
| host = (host or "").strip().lower() | ||
| if not scheme or not host: | ||
| return "" | ||
| # Host may already include a port (or IPv6 bracket notation). Keep explicit | ||
| # non-default ports but strip default :80/:443 so equivalent origins match. | ||
| default = ":443" if scheme == "https" else ":80" if scheme == "http" else "" | ||
| if default and host.endswith(default): | ||
| host = host[: -len(default)] | ||
| return f"{scheme}://{host}" | ||
|
|
||
|
|
||
| def _request_origin(request: Request) -> str: | ||
| """Best-effort external origin for the dashboard request. | ||
|
|
||
| Honour common reverse-proxy headers because the auth gate is often mounted | ||
| behind TLS termination; fall back to the ASGI URL/Host shape used by tests | ||
| and direct Uvicorn binds. | ||
| """ | ||
| proto = request.headers.get("x-forwarded-proto", "").split(",", 1)[0].strip() | ||
| scheme = proto or request.url.scheme | ||
| host = request.headers.get("x-forwarded-host") or request.headers.get("host") or request.url.netloc | ||
| return _normalise_origin(scheme, host) | ||
|
|
||
|
|
||
| def _header_origin(value: str) -> str: | ||
| if not value or value == "null": | ||
| return "" | ||
| try: | ||
| parsed = urlsplit(value) | ||
| except ValueError: | ||
| return "" | ||
| if not parsed.scheme or not parsed.netloc: | ||
| return "" | ||
| return _normalise_origin(parsed.scheme, parsed.netloc) | ||
|
|
||
|
|
||
| def _csrf_origin_ok(request: Request) -> bool: | ||
| """Validate Fetch Metadata plus Origin/Referer for cookie-auth unsafe APIs. | ||
|
|
||
| Per OWASP CSRF guidance, privileged cookie-authenticated state changes must | ||
| not rely on SameSite alone. Browser unsafe requests should provide either a | ||
| same-origin ``Origin`` or, as fallback, a same-origin ``Referer``; Fetch | ||
| Metadata gives an additional early reject for explicit cross-site attempts. | ||
| Requests missing both headers are rejected fail-closed for cookie sessions. | ||
| Token-auth service routes bypass the cookie gate before this check. | ||
| """ | ||
| sec_fetch_site = request.headers.get("sec-fetch-site", "").strip().lower() | ||
| if sec_fetch_site == "cross-site": | ||
| return False | ||
|
|
||
| expected = _request_origin(request) | ||
| origin = request.headers.get("origin", "") | ||
| if origin: | ||
| return bool(expected) and _header_origin(origin) == expected | ||
| referer = request.headers.get("referer", "") | ||
| if referer: | ||
| return bool(expected) and _header_origin(referer) == expected | ||
| return False | ||
|
|
||
|
|
||
| def _csrf_forbidden() -> JSONResponse: | ||
| return JSONResponse( | ||
| {"detail": "Cross-site request blocked: invalid Origin or Referer"}, | ||
| status_code=403, | ||
| ) | ||
|
|
||
|
|
||
| async def gated_auth_middleware( | ||
| request: Request, | ||
| call_next: Callable[[Request], Awaitable[Response]], | ||
|
|
@@ -359,6 +430,8 @@ async def gated_auth_middleware( | |
| if refreshed is not None: | ||
| new_session, refreshing_provider = refreshed | ||
| request.state.session = new_session | ||
| if request.method.upper() in {"POST", "PUT", "PATCH", "DELETE"} and not _csrf_origin_ok(request): | ||
| return _csrf_forbidden() | ||
| response = await call_next(request) | ||
| # Persist the ROTATED tokens. Portal rotates the refresh token on | ||
| # every refresh and runs reuse-detection, so writing the new RT | ||
|
|
@@ -405,6 +478,8 @@ async def gated_auth_middleware( | |
| return response | ||
|
|
||
| request.state.session = session | ||
| if request.method.upper() in {"POST", "PUT", "PATCH", "DELETE"} and not _csrf_origin_ok(request): | ||
|
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 check does not cover |
||
| return _csrf_forbidden() | ||
| return await call_next(request) | ||
|
|
||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
This introduces a new
pending_self_reset.jsonstate-file protocol outside the PR summary. Current main has no producer for this file, and the changed tests exercise only restart-resume prompt wording. Please split this into a focused gateway change with an end-to-end producer/consumer test.