fix(auth): answer a malformed session cookie as no session

The session lookup turned every query error into a 503, so a cookie
holding a NUL byte reached the legacy id path, Postgres refused the
value, and an anonymous caller could produce server errors at will.

A cookie that cannot be a secret or a legacy id is now answered as no
session before the database is asked, and only an unreachable store
(connection errors, timeouts, an exhausted pool) is a 503. Any other
query error is answered like a value that matches nothing.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 09:48:23 +02:00
1 parent c300574a8e
commit 2f7f04ecab
4 files changed
+124 -22

No files matched your search

+24
View File
@@ -13,6 +13,8 @@
* unique-constraint code.
*/
import { classifyRestoreFailure } from "@/lib/export/restore-failure-cause";
/** Narrow a thrown Prisma error to a specific known-request code. */
function hasPrismaCode(err: unknown, code: string): boolean {
return (
@@ -37,3 +39,25 @@ export function isP2002(err: unknown): boolean {
export function isP2025(err: unknown): boolean {
return hasPrismaCode(err, "P2025");
}
/**
* Whether a query failed because the database could not be reached, as opposed
* to refusing what it was asked. The first is worth a retry and is a 503; the
* second is an answer about the input.
*
* Reads the same signals the restore classifier does (`connection` and
* `timeout`: a connection SQLSTATE, a server shutting down, a statement or
* idle timeout, Prisma's P1001 / P1002 / P1017 / P2028, a terminated
* connection), plus the two that only a pooled request meets: P2024, no
* connection free in the pool within its timeout, and P1008, an operation that
* timed out.
*/
export function isDatabaseUnavailable(err: unknown): boolean {
if (hasPrismaCode(err, "P2024") || hasPrismaCode(err, "P1008")) return true;
const { cause } = classifyRestoreFailure(err);
if (cause === "connection" || cause === "timeout") return true;
const message = err instanceof Error ? err.message : "";
return /ECONNREFUSED|ECONNRESET|ETIMEDOUT|Can't reach database server|timed out/i.test(
message,
);
}