From d4cbbae82a73d1e277e771ae21b3610c701e95f1 Mon Sep 17 00:00:00 2001 From: shay Date: Mon, 11 May 2026 19:03:26 -0400 Subject: [PATCH] feat(api): add basic auth protection to /dev dashboard route --- generator/src/api.py | 13 +++++++++++++ generator/tests/test_api.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/generator/src/api.py b/generator/src/api.py index 8e60d29..db0dc25 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -414,6 +414,19 @@ def sanitize_settings(body: dict) -> dict: return out +@app.route("/dev") +@auth.require_basic_auth +def dev_index(): + """Gate the dashboard page itself so the browser pops the native + Basic-Auth prompt before any HTML loads. Without this, the SPA bundle + serves unauthenticated and the dashboard shell renders before the XHR + 401 lands — defeating the auth wall.""" + index = os.path.join(_STATIC_DIR, "index.html") + if os.path.isfile(index): + return send_from_directory(_STATIC_DIR, "index.html") + return jsonify({"error": "frontend not built"}), 503 + + @app.route("/", defaults={"path": ""}) @app.route("/") def spa(path: str): diff --git a/generator/tests/test_api.py b/generator/tests/test_api.py index 7c3f385..3c43837 100644 --- a/generator/tests/test_api.py +++ b/generator/tests/test_api.py @@ -624,3 +624,33 @@ def test_dev_latency_and_health(client, monkeypatch): for key in ("edge_cache_hit_rate", "fetcher_error_rate", "events_dropped_24h"): assert key in body + + +def test_dev_page_requires_basic_auth(client, monkeypatch): + """The /dev page itself must 401 without credentials so the browser pops + the native Basic-Auth prompt before any HTML loads. Without this gate + the SPA bundle serves unauthenticated and the dashboard shell renders + before the XHR 401 lands.""" + monkeypatch.setattr(cfg, "DEV_DASHBOARD_USER", "admin") + monkeypatch.setattr(cfg, "DEV_DASHBOARD_PASSWORD", "pw") + r = client.get("/dev") + assert r.status_code == 401 + assert r.headers.get("WWW-Authenticate", "").startswith("Basic") + + +def test_dev_page_503_when_dashboard_disabled(client, monkeypatch): + monkeypatch.setattr(cfg, "DEV_DASHBOARD_USER", "") + monkeypatch.setattr(cfg, "DEV_DASHBOARD_PASSWORD", "") + r = client.get("/dev") + assert r.status_code == 503 + + +def test_dev_page_serves_html_with_good_creds(client, monkeypatch, tmp_path): + """With valid Basic credentials, /dev should return whatever index.html + contains (or 503 if the frontend isn't built — both branches mean we + passed the auth gate).""" + monkeypatch.setattr(cfg, "DEV_DASHBOARD_USER", "admin") + monkeypatch.setattr(cfg, "DEV_DASHBOARD_PASSWORD", "pw") + r = client.get("/dev", headers=_basic()) + # 200 if the bundle is on disk, 503 otherwise — both prove auth passed. + assert r.status_code in (200, 503)