"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(api): reject cross-site unsafe browser requests before loopback auth (#293)
Hinotobidafdb036fa69
diff --git a/agent/api_server.py b/agent/api_server.pyindex 7f14c51bc..dae0cd3be 100644--- a/agent/api_server.py+++ b/agent/api_server.py@@ -764,6 +764,9 @@ def _require_shutdown_authorization()+_SAFE_BROWSER_METHODS = {"GET", "HEAD", "OPTIONS"}++def _validate_api_auth(*,request: Request,@@ -772,6 +775,12 @@ def _validate_api_auth(allow_query: bool = False,) -> None:"""Validate configured auth, preserving loopback-only dev mode."""+ # CORS protects response reads, not blind side effects. Reject unsafe+ # browser-originated cross-site requests before honoring loopback dev-mode+ # trust, otherwise a malicious page can drive local POST/PUT/DELETE routes.+ if request.method.upper() not in _SAFE_BROWSER_METHODS:+ _reject_cross_site_browser_request(request)+# Loopback clients are always trusted, even when API_AUTH_KEY is set.# The key only gates non-local (LAN/remote) access.if _is_local_client(request):diff --git a/agent/tests/test_upload_api.py b/agent/tests/test_upload_api.pyindex 196b39664..680c9f5e8 100644--- a/agent/tests/test_upload_api.py+++ b/agent/tests/test_upload_api.py@@ -28,6 +28,47 @@ def _existing_uploads(uploads_dir: Path) -> list[Path]:return [p for p in uploads_dir.iterdir() if p.is_file()]+def test_cross_site_browser_upload_is_rejected_even_from_loopback(+ client: TestClient,+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setenv("API_AUTH_KEY", "secret")+ monkeypatch.setattr(api_server, "_API_KEY", "secret")++ response = client.post(+ "/upload",+ headers={+ "Host": "127.0.0.1:8899",+ "Origin": "https://evil.example",+ "Sec-Fetch-Site": "cross-site",+ },+ files={"file": ("poc.txt", b"x" * 1024, "text/plain")},+ )++ assert response.status_code == 403+ assert response.json()["detail"] == "Cross-site request denied"+ assert _existing_uploads(tmp_path) == []+++def test_loopback_upload_without_browser_origin_still_allowed_when_key_configured(+ client: TestClient,+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setenv("API_AUTH_KEY", "secret")+ monkeypatch.setattr(api_server, "_API_KEY", "secret")++ response = client.post(+ "/upload",+ headers={"Host": "127.0.0.1:8899"},+ files={"file": ("note.txt", b"safe", "text/plain")},+ )++ assert response.status_code == 200+ assert len(_existing_uploads(tmp_path)) == 1++def test_upload_under_limit_succeeds(client: TestClient, tmp_path: Path) -> None:payload = b"x" * (2 * 1024) # 2 KB, well under the 4 KB limitresponse = client.post(