harden(#4023): apply Opus security findings — verify-side name-pattern gate + require handler when auth enabled

Opus independent security review concurred SAFE and surfaced 2 LOW defense-in-depth
items, both applied: (1) verify_profile_cookie_value now validates the profile name
against _PROFILE_ID_RE itself (not only in get_profile_cookie) so a future second
caller can't return an unvalidated name; (2) build_profile_cookie raises when auth is
enabled and no handler is passed, so a future call site can't silently emit an
unsigned (session-unbound) profile cookie. +3 regression tests.
This commit is contained in:
nesquena-hermes
2026-06-12 07:37:48 +00:00
parent aef15ca559
commit 03799f8e4a
3 changed files with 52 additions and 0 deletions

View File

@@ -498,6 +498,12 @@ def verify_profile_cookie_value(cookie_value: str, session_cookie_value: str | N
token = _session_token_from_cookie_value(session_cookie_value)
if not profile_name or not token or not sig:
return None
# Defense-in-depth: validate the profile-name pattern here too, not only in
# get_profile_cookie(), so any future caller of this verifier can't return an
# unvalidated name. (#4023 Opus hardening.)
from api.profiles import _PROFILE_ID_RE
if profile_name != 'default' and not _PROFILE_ID_RE.fullmatch(profile_name):
return None
expected = hmac.new(
_signing_key(),
f"profile:{token}:{profile_name}".encode(),