security: constant-time admin-token compare on four routes; legacy token off by default
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
inference-admin committed 2026-11-19 22:09:38 +00:00
1 parent 1f1d555125
commit 649392d2e8
1 file changed
+13 -5
+13 -5
View File
@@ -255,7 +255,7 @@ WEBHOOK_TOKEN = os.environ.get("WEBHOOK_TOKEN", "")
# Callers must read ADMIN_TOKEN and fail loudly; never fall back to WEBHOOK_TOKEN, # Callers must read ADMIN_TOKEN and fail loudly; never fall back to WEBHOOK_TOKEN,
# which 403s and would turn a missing variable into a mid-run failure. # which 403s and would turn a missing variable into a mid-run failure.
ADMIN_TOKEN = os.environ.get("ADMIN_TOKEN", "") ADMIN_TOKEN = os.environ.get("ADMIN_TOKEN", "")
ADMIN_TOKEN_LEGACY = os.environ.get("ADMIN_TOKEN_LEGACY", "1") != "0" ADMIN_TOKEN_LEGACY = os.environ.get("ADMIN_TOKEN_LEGACY", "0") != "0"
OC_GRAPHQL_URL = "https://opencollective.com/api/graphql/v2" OC_GRAPHQL_URL = "https://opencollective.com/api/graphql/v2"
OC_COLLECTIVE_SLUG = os.environ.get("OC_COLLECTIVE_SLUG", "inference-cooperative") OC_COLLECTIVE_SLUG = os.environ.get("OC_COLLECTIVE_SLUG", "inference-cooperative")
@@ -4721,7 +4721,9 @@ async def resend_guest_setup_email(email: str) -> dict:
@app.post("/admin/guest-invites/resend") @app.post("/admin/guest-invites/resend")
async def admin_guest_resend(request: Request): async def admin_guest_resend(request: Request):
"""Body: {"email": "guest@..."} — one fresh setup link.""" """Body: {"email": "guest@..."} — one fresh setup link."""
if not ADMIN_TOKEN or request.headers.get("x-admin-token") != ADMIN_TOKEN: if not ADMIN_TOKEN or not secrets.compare_digest(
request.headers.get("x-admin-token", ""), ADMIN_TOKEN
):
raise HTTPException(401, "Invalid admin token") raise HTTPException(401, "Invalid admin token")
body = await request.json() if request.headers.get("content-type", "").startswith("application/json") else {} body = await request.json() if request.headers.get("content-type", "").startswith("application/json") else {}
email = (body.get("email") or "").strip().lower() email = (body.get("email") or "").strip().lower()
@@ -4733,7 +4735,9 @@ async def admin_guest_resend(request: Request):
@app.post("/admin/guest-invites/resend-stale") @app.post("/admin/guest-invites/resend-stale")
async def admin_guest_resend_stale(request: Request): async def admin_guest_resend_stale(request: Request):
"""Resend every unaccepted guest invite older than `days` (default 7).""" """Resend every unaccepted guest invite older than `days` (default 7)."""
if not ADMIN_TOKEN or request.headers.get("x-admin-token") != ADMIN_TOKEN: if not ADMIN_TOKEN or not secrets.compare_digest(
request.headers.get("x-admin-token", ""), ADMIN_TOKEN
):
raise HTTPException(401, "Invalid admin token") raise HTTPException(401, "Invalid admin token")
body = await request.json() if request.headers.get("content-type", "").startswith("application/json") else {} body = await request.json() if request.headers.get("content-type", "").startswith("application/json") else {}
days = int(body.get("days") or 7) days = int(body.get("days") or 7)
@@ -5095,7 +5099,9 @@ def _alias_map(conn) -> dict:
@app.get("/admin/aliases") @app.get("/admin/aliases")
async def admin_aliases_list(request: Request): async def admin_aliases_list(request: Request):
if not ADMIN_TOKEN or request.headers.get("x-admin-token") != ADMIN_TOKEN: if not ADMIN_TOKEN or not secrets.compare_digest(
request.headers.get("x-admin-token", ""), ADMIN_TOKEN
):
raise HTTPException(401, "Invalid admin token") raise HTTPException(401, "Invalid admin token")
conn = get_db() conn = get_db()
try: try:
@@ -5110,7 +5116,9 @@ async def admin_aliases_list(request: Request):
@app.post("/admin/aliases") @app.post("/admin/aliases")
async def admin_aliases_add(request: Request): async def admin_aliases_add(request: Request):
"""Body: {"slug": "incognito-…", "email": "member@…"}. Idempotent on slug.""" """Body: {"slug": "incognito-…", "email": "member@…"}. Idempotent on slug."""
if not ADMIN_TOKEN or request.headers.get("x-admin-token") != ADMIN_TOKEN: if not ADMIN_TOKEN or not secrets.compare_digest(
request.headers.get("x-admin-token", ""), ADMIN_TOKEN
):
raise HTTPException(401, "Invalid admin token") raise HTTPException(401, "Invalid admin token")
body = await request.json() if request.headers.get("content-type", "").startswith("application/json") else {} body = await request.json() if request.headers.get("content-type", "").startswith("application/json") else {}
slug = (body.get("slug") or "").strip().lower() slug = (body.get("slug") or "").strip().lower()