khanhsec
VI
1 min readAuthenticationWeb

Where a session really ends

A cookie is only storage. The real boundary of a session is what the server is willing to trust — and it is easier to get wrong than it looks.

Bản tiếng Việt

Ask where a session lives and the answer sounds simple: in a cookie. But the real boundary of a session is not the cookie — it is what the server is willing to trust.

The three pieces

A working session needs an identifier, a place to keep state, and a way to prove the identifier was not edited in transit.

Lose the third piece and you lose all three. If the identifier is an unsigned string, the user can rewrite it.

A minimal check

import { createHash } from "node:crypto";
 
function tokenHash(token: string, pepper: string): string {
  return createHash("sha256").update(token + pepper).digest("hex");
}

The interesting part is not the algorithm. It is that the server stores only the hash — read the database and you still cannot use the token.

The boundaries

  • Identifiers must be random and long enough: 32 bytes is the floor.
  • State must be revocable: signing out has to kill the token immediately.
  • The signature must not depend on anything the client sends.

Get any one of those wrong and the rest of the system is decoration.

Comments

No comments yet.

Comments are moderated before they appear.

Get new posts by email

No ads, no tracking. Just new posts, unsubscribe any time.

Where a session really ends · khanhsec