deniz.in

Markets

Weather

Loading weather

· via dev.to (home feed)

Session cookies act as temporary passwords, dev.to security guide explains

A dev.to guide argues that after login the session cookie, not the password, authenticates every request — and shows how HttpOnly, Secure and unpredictable IDs protect it.

Session cookies act as temporary passwords, dev.to security guide explains

A developer writing on dev.to has published the first part of a practical security guide that reframes one of the most familiar pieces of web plumbing: the session cookie. The core argument is that once a user logs in, the password usually drops out of the picture entirely, and the session identifier stored in the browser is what actually authenticates every following request. In practice, the author writes, that identifier deserves the same care as a password, because it functions as one until it expires or is invalidated.

How sessions carry authentication

The post opens with the stateless nature of HTTP: two requests to the same host are, on their own, unrelated, so the server needs a mechanism to recognise that the visitor hitting the dashboard is the person who authenticated minutes earlier. In a traditional server-side setup, the flow runs from a credential check to the creation of server-held session state, a generated session ID, and a Set-Cookie header delivering that ID to the browser. From then on the browser presents the cookie automatically with each request, and the server looks up the associated record — typically holding the user ID, role and an authenticated flag. The password is never transmitted again.

Cookies and sessions are distinct

The author is careful to separate two terms that are often conflated. A cookie is browser-side storage, a piece of data attached to matching requests; a session is application state kept on the server. The session ID is the join between the two, and that is precisely why it is valuable: it is the only thing linking an incoming request to an authenticated account.

Possession is enough

According to the dev.to post, an attacker who obtains the session identifier can replay it in a request of their own. If the server sees a live authenticated session, the request looks legitimate, and the attacker never needs the victim's password at all. This bearer property is what makes the comparison to a temporary password apt.

Unpredictability is the baseline

Predictable identifiers — sequential numbers, or values assembled from a username and a timestamp — are described as bad designs, with the author noting that uniqueness is not the same as security. The recommendation is a cryptographically secure random generator, illustrated in Node.js with crypto.randomBytes(32) rendered as hex. The property that matters, the post stresses, is not that the value looks complicated but that an attacker cannot realistically guess the next valid one.

HttpOnly helps but does not repair XSS

Because JavaScript can read document.cookie, a cross-site scripting flaw can turn directly into cookie theft. Setting the HttpOnly flag stops scripts from reading the cookie while still allowing it to travel with normal requests. The author is explicit about the limitation: with malicious JavaScript already running in the victim's browser, an attacker can still perform authenticated actions — the example given changes the account's email address via a fetch call — because the browser attaches the cookie automatically. Protecting the cookie and eliminating XSS are separate problems, the post concludes.

Secure, with SameSite to follow

The Secure attribute confines the cookie to HTTPS connections, and the post recommends combining it with HttpOnly since each counters a different threat. The article, labelled Part 1, ends just as it turns to the SameSite attribute, indicating that cross-site request risks are due for a follow-up installment.

Why it matters

Hardening the login form — strong passwords, proper hashing, MFA, rate limiting — does nothing for the window after login if the token that replaces the password leaks. Stolen session cookies are a cheap and common route into accounts precisely because session management receives far less attention than authentication. The fixes described here are low-cost and well established: generate identifiers with a secure random source, mark authentication cookies HttpOnly and Secure, and treat the cookie stored in the browser as the live credential it really is.

  • #security
  • #cookies
  • #authentication
  • #web-development
  • #session-management

Related posts