mirror of
https://github.com/BigBodyCobain/Shadowbroker.git
synced 2026-12-06 09:34:09 +01:00
Secure Messages' Create Address calls GET /api/wormhole/dm/invite, which was still on require_local_operator in both the router and main.py. On a fresh Docker install the browser reaches the backend through the proxy, so the button failed with "This needs operator access". It now takes the operator-client token like the other DM routes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
5.5 KiB
Python
149 lines
5.5 KiB
Python
"""The web UI's gate, DM and identity routes take the operator-client token.
|
|
|
|
A fresh install has no admin-key login, so every route the Infonet terminal,
|
|
gate view and DM panel call must accept the token the frontend proxy presents
|
|
(see test_operator_client_token.py). Admin tools stay behind the admin key.
|
|
"""
|
|
|
|
import hmac
|
|
import hashlib
|
|
|
|
import pytest
|
|
from fastapi import Depends, FastAPI, HTTPException
|
|
from fastapi.routing import APIRoute
|
|
from fastapi.testclient import TestClient
|
|
|
|
import auth
|
|
|
|
ADMIN_KEY = "test-admin-key-for-operator-client-routes"
|
|
OPERATOR_GUARDS = {auth.require_operator_client, auth.require_operator_client_or_admin}
|
|
|
|
UI_ROUTES = [
|
|
("POST", "/api/wormhole/join"),
|
|
("POST", "/api/wormhole/connect"),
|
|
("GET", "/api/wormhole/identity"),
|
|
("POST", "/api/wormhole/identity/bootstrap"),
|
|
("POST", "/api/wormhole/gate/enter"),
|
|
("POST", "/api/wormhole/gate/leave"),
|
|
("POST", "/api/wormhole/gate/message/compose"),
|
|
("POST", "/api/wormhole/gate/message/post"),
|
|
("POST", "/api/wormhole/gate/messages/decrypt"),
|
|
("POST", "/api/wormhole/gate/state/export"),
|
|
("GET", "/api/wormhole/gate/{gate_id}/key"),
|
|
("GET", "/api/wormhole/dm/contacts"),
|
|
("PUT", "/api/wormhole/dm/contact"),
|
|
("POST", "/api/wormhole/dm/encrypt"),
|
|
("POST", "/api/wormhole/dm/decrypt"),
|
|
("POST", "/api/wormhole/dm/register-key"),
|
|
("GET", "/api/wormhole/dm/invite"),
|
|
("POST", "/api/wormhole/dm/invite/import"),
|
|
]
|
|
|
|
ADMIN_ONLY_ROUTES = [
|
|
("POST", "/api/wormhole/restart"),
|
|
("POST", "/api/wormhole/dm/root-witnesses/import"),
|
|
("GET", "/api/wormhole/review-export"),
|
|
]
|
|
|
|
|
|
def _definitions(app, method, path):
|
|
"""Every registered copy of a route. Some routes are defined both in a
|
|
router and in main.py, and which copy FastAPI reaches first depends on
|
|
import order, so every copy must carry the right guard."""
|
|
from routers import wormhole
|
|
|
|
found = [
|
|
route
|
|
for route in list(app.router.routes) + list(wormhole.router.routes)
|
|
if isinstance(route, APIRoute) and route.path == path and method in route.methods
|
|
]
|
|
assert found, f"{method} {path} is not registered"
|
|
return found
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def app():
|
|
import main
|
|
|
|
return main.app
|
|
|
|
|
|
@pytest.mark.parametrize(("method", "path"), UI_ROUTES)
|
|
def test_ui_route_takes_the_operator_client_token(app, method, path):
|
|
for route in _definitions(app, method, path):
|
|
guards = {dep.call for dep in route.dependant.dependencies}
|
|
assert guards & OPERATOR_GUARDS, f"{method} {path} guarded by {guards}"
|
|
|
|
|
|
@pytest.mark.parametrize(("method", "path"), ADMIN_ONLY_ROUTES)
|
|
def test_admin_tools_still_need_the_admin_key(app, method, path):
|
|
for route in _definitions(app, method, path):
|
|
guards = {dep.call for dep in route.dependant.dependencies}
|
|
assert guards, f"{method} {path} has no guard"
|
|
assert not guards & OPERATOR_GUARDS, f"{method} {path} opened to the UI token"
|
|
|
|
|
|
def _token() -> str:
|
|
return hmac.new(ADMIN_KEY.encode(), b"shadowbroker-operator-client-v1", hashlib.sha256).hexdigest()
|
|
|
|
|
|
@pytest.fixture
|
|
def client(monkeypatch):
|
|
monkeypatch.setattr(auth, "_current_admin_key", lambda: ADMIN_KEY)
|
|
test_app = FastAPI()
|
|
|
|
@test_app.post("/ui", dependencies=[Depends(auth.require_operator_client_or_admin)])
|
|
def ui():
|
|
return {"ok": True}
|
|
|
|
@test_app.post("/admin", dependencies=[Depends(auth.require_local_operator)])
|
|
def admin():
|
|
return {"ok": True}
|
|
|
|
return TestClient(test_app, client=("203.0.113.9", 50000))
|
|
|
|
|
|
def test_token_proves_origin_for_a_ui_on_another_port(client):
|
|
# The proxy checked the browser's origin before attaching the token, so a
|
|
# UI served on a port other than 3000 is not "untrusted".
|
|
headers = {auth.OPERATOR_CLIENT_HEADER: _token(), "Origin": "http://localhost:3301"}
|
|
assert client.post("/ui", headers=headers).status_code == 200
|
|
# An admin-unlocked UI on that port works too.
|
|
assert client.post("/admin", headers={**headers, "X-Admin-Key": ADMIN_KEY}).status_code == 200
|
|
|
|
|
|
def test_foreign_origin_without_token_is_refused(client):
|
|
headers = {"Origin": "https://attacker.example", "X-Admin-Key": ADMIN_KEY}
|
|
assert client.post("/admin", headers=headers).status_code == 403
|
|
|
|
|
|
def test_or_admin_guard_refuses_without_token_or_key(client):
|
|
assert client.post("/ui").status_code == 403
|
|
assert client.post("/ui", headers={auth.OPERATOR_CLIENT_HEADER: "0" * 64}).status_code == 403
|
|
|
|
|
|
def test_token_does_not_unlock_admin_routes(client):
|
|
resp = client.post("/admin", headers={auth.OPERATOR_CLIENT_HEADER: _token()})
|
|
assert resp.status_code == 403
|
|
|
|
|
|
def test_or_admin_guard_falls_back_to_require_admin(monkeypatch, client):
|
|
calls = []
|
|
|
|
def fake_require_admin(request):
|
|
calls.append(True)
|
|
raise HTTPException(status_code=403, detail="Forbidden — insufficient scope")
|
|
|
|
monkeypatch.setattr(auth, "require_admin", fake_require_admin)
|
|
assert client.post("/ui").status_code == 403
|
|
assert calls, "scoped-token callers must still reach require_admin"
|
|
|
|
|
|
def test_lan_mode_ignores_the_token(monkeypatch, client):
|
|
# Other devices can reach a LAN-mode dashboard and send any Host header,
|
|
# so there every browser signs in with the admin key.
|
|
monkeypatch.setenv("SHADOWBROKER_LAN_MODE", "true")
|
|
monkeypatch.setattr(auth, "require_admin", lambda request: (_ for _ in ()).throw(
|
|
HTTPException(status_code=403, detail="Forbidden")))
|
|
assert client.post("/ui", headers={auth.OPERATOR_CLIENT_HEADER: _token()}).status_code == 403
|