Skip to content
← Writing

Session auth without a library, twice

From Kosher Kav

  • security
  • Cloudflare Workers
  • PHP

The decision

Two systems, two runtimes, the same call: for a small admin surface with a handful of accounts, hand-rolled session auth was less risk than the dependency that would replace it. That is not a general rule. It held here because the surface area was small enough to read in one sitting, the primitives the platform provided were the right ones, and every line was going to be audited anyway.

Workers: PBKDF2 over Web Crypto

The Kosher Kav dashboard runs on Cloudflare Workers, which has no Node crypto and, at the time, no bcrypt that would run inside the CPU budget. What it has is Web Crypto, and Web Crypto has PBKDF2-HMAC-SHA256 built in. Verification derives with the stored salt and iteration count and compares with a constant-time check.

async function derive(password: string, salt: Uint8Array, iterations: number) {
  const key = await crypto.subtle.importKey(
    "raw", new TextEncoder().encode(password), "PBKDF2", false, ["deriveBits"],
  );
  const bits = await crypto.subtle.deriveBits(
    { name: "PBKDF2", hash: "SHA-256", salt, iterations }, key, 256,
  );
  return new Uint8Array(bits);
}

export async function verify(password: string, stored: StoredHash) {
  const candidate = await derive(password, stored.salt, stored.iterations);
  return crypto.subtle.timingSafeEqual(candidate, stored.hash);
}

The iteration count is stored with the hash, so it can be raised later and old rows re-hashed on next login. The session itself is a random 256-bit id in an HttpOnly, Secure, SameSite=Lax cookie, looked up in D1. The whole module — hash, verify, create session, read session, revoke — is about 80 lines.

PHP: JWT in a cookie, CSRF by double submit

GlowSimcha's API is framework-free PHP 8.2 serving a React SPA on another origin. Sessions are HS256 JWTs in HttpOnly cookies, so JavaScript cannot read them — which means the SPA needs a second, readable token to prove a request came from it and not from a form on another site. The double-submit pattern: a random CSRF token in a non-HttpOnly cookie, echoed back in a header, compared on the server with hash_equals.

$cookie = $request->cookie('csrf');
$header = $request->header('x-csrf-token');
if ($cookie === null || $header === null || !hash_equals($cookie, $header)) {
    throw new HttpException(403, 'CSRF token mismatch');
}

Revocation is a token version on the user row: bump it and every outstanding JWT for that user fails verification on its next request. A per-device session registry lets a user see and sign out individual devices without touching the others.

What it cost, what it bought

It cost writing tests a library would have shipped with: wrong password, tampered token, expired token, replayed CSRF token, revoked version. It bought a security model that is explicit in the code rather than inherited from a package's defaults, and that a reviewer can read end to end in ten minutes. The day either system needs OAuth providers, MFA and a session dashboard for thousands of users, this gets replaced — and the tests will make that safe.