Compare commits

...

2 Commits

Author SHA1 Message Date
CartSnitch Engineer Bot e151873bb3 Merge main into fix/restore-token-hash
Sync with upstream changes (frontend API route alignment) while
preserving the SHA-256 token hashing fix.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-01 10:22:52 +00:00
CartSnitch Engineer Bot 3f9c683522 fix(api): restore SHA-256 session token hashing (regression from PR #95)
Better-Auth v1.5.6+ stores tokens as SHA-256 hashes in the sessions
table. The raw cookie value must be hashed before querying so that
stored-hash == computed-hash, restoring auth on all data endpoints.

Also adopts SESSION_COOKIE_NAMES list from PR #95 so both pending PRs
(cookie fix and hash fix) can merge without conflict.

Fixes CAR-322. Regression from PR #95 (fix/secure-cookie-name).

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-01 08:39:20 +00:00
+5 -1
View File
@@ -5,6 +5,7 @@ Sessions are verified by querying the shared sessions table directly.
"""
from datetime import UTC, datetime
from hashlib import sha256
from uuid import UUID
from fastapi import Cookie, Depends, Header, HTTPException, Request, status
@@ -31,10 +32,13 @@ async def _validate_session_token(token: str, db: AsyncSession) -> UUID:
"""Validate a Better-Auth session token against the sessions table.
Returns the user_id (as UUID) if the session is valid and not expired.
Better-Auth v1.5.6+ stores tokens as SHA-256 hashes, so we hash the
incoming raw token before querying.
"""
hashed_token = sha256(token.encode("utf-8")).hexdigest()
result = await db.execute(
text("SELECT user_id, expires_at FROM sessions WHERE token = :token"),
{"token": token},
{"token": hashed_token},
)
row = result.first()