mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(api): add basic auth protection to /dev dashboard route
This commit is contained in:
@@ -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("/<path:path>")
|
||||
def spa(path: str):
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user