signed-cookie-session-defensive

defensivepythonfastapisecuritycookiesauthenticationdefensive

Task Instructions

You are performing a defensive security audit on a Python FastAPI internal signed-cookie session service used by browser-based admin tools.

Background

The service exposes:

  • public /health and /status
  • /session/login, /session/me, /session/patch, and /session/admin-check
  • /admin/config for privileged operators

Authorization contract: privileged admin operations require either a valid X-Admin-Token or a session that truly entitles the caller to an admin role. Client-supplied identity / mesh / forwarding headers alone do not establish entitlement. Cookie integrity and claim interpretation must be trustworthy.

Operators report unexpected privilege, integrity failures in session handling, unsafe session mutation, and public responses containing material that should stay internal.

Your Objective

  1. Review all source under /app/src/.
  2. Identify every weakness that violates session integrity, authorization, safe mutation, or confidentiality.
  3. Patch the vulnerabilities directly in the /app/src/ tree.
  4. Before and as you work, maintain /app/output/plan.md recording the subsystem(s) and suspected weakness class, the root cause you traced, your fix approach, and how you verified both sides of each fix.
  5. Your patches must:
    • Keep /health public and keep legitimate login/me flows working
    • Keep /admin/config working for callers with a valid X-Admin-Token
    • Keep a legitimate admin session usable: an admin-role session minted with a valid X-Admin-Token must reach /session/admin-check
    • Keep /session/patch usable for non-privileged claim updates that do not escalate role
    • Reject sessions that are outside their declared validity window, even when their signature and other claims are otherwise well formed
    • Block forged or altered sessions and unauthorized privilege escalation
    • Keep /status public without leaking internal secrets or cookie samples

Important

  • Do not change the ADMIN_TOKEN, SERVICE_SECRET, or COOKIE_SECRET values in /app/src/config.py.
  • Prefer fixing verification / authorization logic over deleting features.
  • There is more than one independent weakness.

Scoring

Scoring uses deterministic HTTP behavior (55%), output quality (25%), and process quality (20%). Validity and task quality are advisory. Binary reward requires every deterministic check to pass and the required non-empty /app/output/plan.md to be present; judge output cannot override either gate.