Skip to main content
A presentation slide titled "Cookie and Session Security" with a large dark-blue curved shape on the right containing the word "Demo." The bottom-left shows a small "© Copyright KodeKloud" text.
This lesson demonstrates how to audit an Express application for cookie- and session-related weaknesses and provides concrete remediation patterns. The example focuses on JWT-based authentication (no server-side cookie sessions), highlights the security implications, and maps findings to prioritized fixes you can implement quickly. We ran an interactive analysis session (local assistant/REPL) to explore the repository and guide the audit. The session output looked like this:
A shortened sample of the structured audit prompt used to direct the analysis:
Large or reusable prompts used for these audits are stored in a repository to avoid retyping long instructions each time.

What the audit looked for

The audit checks focused on session and cookie hygiene, CSRF controls, and session storage practices. Key control areas included:
  • Session configuration (secure/httpOnly/sameSite, regeneration, timeouts)
  • Cookie security (flags, scope, no sensitive data in cookies, encryption)
  • CSRF protection (tokens, double-submit cookies, origin/referer checks)
  • Session storage (avoid default memory store, prefer Redis/DB, cleanup)
Detailed checklist mapping: The audit also produced a structured findings report with fields like Title, Severity, CWE, Evidence (file/line), Exploitability notes, and remediation snippets.

High-level summary of findings

  • The app uses JWT-based authentication (no server-side cookie sessions), so many cookie-specific checks were N/A. However, JWTs introduce other risks that require operational controls.
  • Key issues discovered:
    • No server-side token revocation (no blacklisting).
    • No refresh token pattern — access tokens are long-lived or not rotated safely.
    • JWT secret management is weak or not validated (risk of brute-force or leaked secrets).
    • No CSRF middleware detected (relevant if cookies are introduced later).
    • No explicit guidance for secure client-side token storage (storing JWTs in localStorage is risky).
Example diagnostic notes:
  • No cookie usage found — Secure/HttpOnly/SameSite checks were marked N/A.
  • No CSRF middleware in server.js (server.js:1-19).
  • No refresh token implementation and no logout endpoint that invalidates tokens.
  • JWTs remain valid until expiry — no revocation mechanism in place.
Final (example) risk score: 8.5 / 10 (high). Immediate priorities: rotate JWT secret and add token blacklisting.

Representative audit excerpts

The report explicitly noted that many cookie checks were skipped due to JWT-only usage, but it stressed that any future introduction of cookies must include Secure, HttpOnly, and SameSite flags and CSRF protections. It also flagged missing logout/revocation endpoints and insecure secret handling.

Remediation snippets and patterns

Below are concrete, copy-paste-friendly code patterns you can adapt to your codebase. Keep your app structure and error handling in mind when integrating these.
  1. Server-side session (cookie) configuration (only if you switch to cookie sessions)
  1. CSRF protection (when cookies are used)
  1. Strong JWT secret validation (startup-time checks)
  1. Implement refresh token pattern (access + refresh token)
  1. Token blacklisting (Redis-backed revocation) — middleware + logout
Logout handler to add token to blacklist with TTL equal to the remaining lifetime:
  1. Sliding session timeout / short-lived access tokens example
Avoid storing access or refresh tokens in localStorage or any storage accessible to JavaScript. Prefer HttpOnly cookies for refresh tokens and keep access tokens short-lived and in memory. Storing tokens in localStorage increases your attack surface for XSS.
  1. Guidance summary — secure token handling
  • Use HttpOnly, Secure, SameSite cookies for refresh tokens.
  • Keep access tokens short-lived (minutes) and refresh them via a secure refresh flow.
  • Store refresh tokens server-side (DB/Redis) or as HttpOnly cookies with rotation.
  • Validate JWT_SECRET at startup and rotate secrets on a regular schedule.

Proof-of-concept checks (curl examples)

Use these commands to reproduce the lack of revocation or logout behavior during testing:

Prioritization & remediation roadmap

Top fixes to reduce risk fast: Suggested timeline:
  • Immediate: JWT secret rotation and token blacklisting.
  • Week 1: Refresh tokens + secure cookie patterns and logout.
  • Week 2: CSRF controls and session store adoption.
  • Month 1: Session monitoring, adaptive timeouts, and device tracking.

Compliance impact

The audit mapped findings to common frameworks:
  • OWASP Top 10: A07:2021 (Identification and Authentication Failures) — applicable due to lack of revocation and long-lived tokens.
  • PCI DSS / SOC 2 / NIST: Insufficient session invalidation and access management controls may impact compliance posture.

Closing notes

  • JWT-only architectures reduce some cookie risks but require operational controls: revocation, refresh, and secure storage.
  • If you introduce cookies later, ensure Secure, HttpOnly, and SameSite flags and add CSRF protection.
  • Break audits into focused checks (sessions, cookies, CSRF, storage) to produce actionable, prioritized findings and reduce missed items.
Upcoming topics will cover file handling and business logic audits.

Watch Video