T16.1: D21 session decode fails closed; PyJWT 2.15.1

decode_token treats any decode failure, and any cookie over 4096 chars,
as not signed in, and requires exp and sub. PyJWT 2.13.0 -> 2.15.1
(pip-audit: 13 advisories -> none).

Security review SEC-07: a crafted 1.6 KB cookie with no account gave
500 + stack trace; now 401.

New tests/security_check.py (no browser): D21 section 6/6. Against
da1325c with PyJWT 2.13.0 it fails 3 checks (crafted cookie 500,
token with no exp accepted, old pin) - the findings. The over-long
cookie and the two still-works checks pass on both.
Smoke test 27/27.
This commit is contained in:
2026-10-02 13:51:43 -07:00
parent 1c221a122e
commit 01ce7e941c
3 changed files with 162 additions and 4 deletions

View File

@@ -256,12 +256,29 @@ def maybe_refresh_token(claims: dict) -> Optional[str]:
return jwt.encode(payload, SECRET_KEY, algorithm=JWT_ALG)
# D21: a real session token is about 300 characters. Anything far larger is not
# one of ours, and refusing it before parsing keeps a crafted cookie from making
# the parser do work on our behalf.
_MAX_TOKEN_CHARS = 4096
def decode_token(token: str) -> Optional[dict]:
"""Return the token claims if the signature and expiry are valid, else None.
Session cookies only — a token of any other type is rejected."""
Session cookies only — a token of any other type is rejected.
D21: fails CLOSED on any error. This used to catch jwt.PyJWTError only, and
PyJWT 2.13.0 let a RecursionError out of header parsing, so a 1.6 KB cookie
from someone with no account produced a 500 and a stack trace instead of a
401 (security review SEC-07). Whatever goes wrong inside the decoder, the
answer is the same: not signed in."""
if not token or len(token) > _MAX_TOKEN_CHARS:
return None
try:
claims = jwt.decode(token, SECRET_KEY, algorithms=[JWT_ALG])
except jwt.PyJWTError:
# Every token this app issues carries exp and sub (create_token,
# maybe_refresh_token); one without them is not ours.
claims = jwt.decode(token, SECRET_KEY, algorithms=[JWT_ALG],
options={"require": ["exp", "sub"]})
except Exception: # noqa: BLE001 — see docstring: every failure is "not signed in"
return None
# No route issues a `typ`-carrying token anymore (that was the password-reset
# token, removed in T10.4), but a session token still must never validate as

View File

@@ -17,7 +17,9 @@ pymssql==2.3.13 # read-only lookups against the Micron asset DB (SQL
# MICRON_DB_URL to mssql+pyodbc://…?driver=ODBC+Driver+18+for+SQL+Server
pydantic==2.13.4
python-dotenv==1.2.2
PyJWT==2.13.0 # signed session tokens
PyJWT==2.15.1 # signed session tokens. D21 (2026-10-02): was 2.13.0, which
# pip-audit lists 13 advisories for (GHSA-8wjv-2p76-3863 let a
# crafted cookie raise past decode_token). 2.15.1: none known.
starlette==1.3.1 # pinned transitive (cookie / CORS handling — security-relevant)
Authlib==1.7.2 # Okta OIDC authorization-code flow (T10.1, wave 10 / D15)
httpx==0.28.1 # Authlib's OIDC client needs an HTTP client; explicit, not transitive