07-session-management-and-identification.md

πŸͺ Module 07: Session Management & Client Identification

Core Focus: How Servers Differentiate Clients (2 Tabs on 1 Laptop vs 2 Laptops on 1 WiFi vs 2 Laptops on Different Networks), Layer 3/4 vs Layer 7 Identification, Stateful vs Stateless Sessions (Cookies vs JWT), Browser Storage Mechanisms, and Critical Security Defense (XSS, CSRF, Session Hijacking).


πŸ“‘ Table of Contents

  1. The Fundamental Problem of Statelessness in HTTP
  2. The 3 Core Interview Identification Scenarios
  3. Master Identification Summary Matrix
  4. Session Tracking Mechanisms: Stateful vs Stateless
  5. Client-Side Storage Comparison
  6. Security Threats & Mitigation Strategies
  7. Juspay & Payment Systems Real-World Context
  8. High-Yield Interview Questions & Verbatim Answers

1. The Fundamental Problem of Statelessness in HTTP

HTTP is by design a stateless protocol. Every request is executed independently with zero memory of previous requests:

Request 1: POST /login (User authenticates as Alice) -> Server returns 200 OK Request 2: GET /checkout -> Server: "Who are you?"

To maintain user identity across transactions, the Application Layer (Layer 7) establishes a Session via tokens or cookies.


2. The 3 Core Interview Identification Scenarios

This is a classic systems and networking interview question frequently asked to test candidates across the OSI stack.

THE 3 DIFFERENTIATION SCENARIOS β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ SCENARIO A β”‚ SCENARIO B β”‚ SCENARIO C β”‚ β”‚ Two Tabs on Same Laptop β”‚ Two Laptops on Same WiFi β”‚ Two Laptops on Diff Networks β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Tab 1 β”‚Tab 2 β”‚ β”‚ β”‚ PC1 β”‚ β”‚ PC2 β”‚ β”‚ β”‚Home β”‚ β”‚4G/5Gβ”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ [ 1 Laptop ] β”‚ [ 1 Router ] β”‚ [Router A] [Tower B] β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2.1 Scenario A: Two Tabs on the Same Laptop

What is Shared vs What is Distinct?

  • Public IP Address: Identical (e.g., 203.0.113.5).
  • Private IP Address: Identical (e.g., 192.168.1.10).
  • MAC Address: Identical.
  • User-Agent String: Identical (same browser version and OS).
  • HTTP Cookies: Identical & Shared! By browser architecture, all regular tabs within the same browser profile share the exact same cookie jar and localStorage.

How does the Server Differentiate Them?

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ DIFFERENTIATING TWO TABS IN THE SAME BROWSER β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ 1. `sessionStorage` (Client-Side Isolation): β”‚ β”‚ - `sessionStorage` is strictly scoped to the individual browser tab window. β”‚ β”‚ - If Tab 1 opens Order A and Tab 2 opens Order B, each stores its own unique ID. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ 2. Unique Tab / Window ID in Request Headers: β”‚ β”‚ - Single-Page Applications (React/Vue) generate a UUID on tab mount: β”‚ β”‚ `const tabId = crypto.randomUUID();` β”‚ β”‚ - Every API call attaches: `X-Tab-ID: 7a9f-4b12-...` β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ 3. URL Parameter / Route Scoping: β”‚ β”‚ - Scoped state directly in the URL path (e.g., `/orders/ord_101` vs `/orders/ord_102`).β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ 4. Separate WebSocket Connections: β”‚ β”‚ - Each tab opens its own distinct WebSocket connection holding a unique connection β”‚ β”‚ ID in server memory. β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2.2 Scenario B: Two Laptops on the Same Local WiFi / NAT

What is Shared vs What is Distinct?

  • Public IP Address: Identical! Because both laptops connect through the same home/office WiFi router, their traffic is translated by NAT/PAT to the router's single public WAN IP (203.0.113.5).
  • Private IP Address: Distinct on the LAN (192.168.1.10 vs 192.168.1.20), but completely hidden from the destination server.
  • Client Source Ports (Layer 4): Distinct! The NAT router assigns distinct source ports on its public IP (e.g., 203.0.113.5:40001 vs 203.0.113.5:40002).
  • Application Layer Cookies / JWTs (Layer 7): Completely Distinct! Each laptop runs its own separate browser with its own private storage.
NAT DIFFERENTIATION AT LAYER 4 & LAYER 7 Laptop A (Bob) [192.168.1.10] ──► [ NAT Router ] ──► [Src: 203.0.113.5:40001 | Cookie: sid_bob] ──► [ Server ] Laptop B (Alice) [192.168.1.20] ──► [ 203.0.113.5] ──► [Src: 203.0.113.5:40002 | Cookie: sid_ali] ──► [ Server ]

How does the Server Differentiate Them?

  1. At Transport Layer (Layer 4): Differentiated by unique NAT-allocated source port numbers.
  2. At Application Layer (Layer 7): Differentiated by unique Session IDs (Set-Cookie: session_id=...) or Authorization Bearer JWT Tokens.

2.3 Scenario C: Two Laptops on Different Networks

What is Shared vs What is Distinct?

  • Public IP Address (Layer 3): Completely Distinct! Laptop 1 has IP 49.36.12.8 (Home Broadband) and Laptop 2 has IP 157.48.91.22 (Mobile 5G).
  • Network Routing: Traffic routes through different ISPs and autonomous systems (BGP).
  • Application State: Distinct session tokens.

How does the Server Differentiate Them?

  • Easily differentiated at Layer 3 (Source IP Address), Layer 4 (Source Ports), and Layer 7 (Authentication Tokens).

3. Master Identification Summary Matrix

Metric / DimensionTwo Tabs (Same Laptop)Two Laptops (Same WiFi)Two Laptops (Diff Networks)
Public IP (Seen by Server)Same (203.0.113.5)Same (203.0.113.5)Different (203.0... vs 49.36...)
Private IP (LAN)Same (192.168.1.10)Different (.10 vs .20)Different (Different LANs)
TCP Port (from NAT)Can be shared (HTTP/2)Different (Separate NAT ports)Different
Cookie JarShared & IdenticalIsolated & IndependentIsolated & Independent
localStorageSharedIsolatedIsolated
sessionStorageIsolated per tabIsolatedIsolated
Primary Server MechanismCustom Tab ID / URL Route / sessionStorageSession Cookie / JWT + NAT PortSource Public IP + Session Cookie / JWT

4. Session Tracking Mechanisms: Stateful vs Stateless

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ STATEFUL vs STATELESS SESSIONS β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ STATEFUL SESSIONS (Session ID in Cookie + Central Redis Store): β”‚ β”‚ 1. User logs in -> Server creates session object in Redis: `{user_id: 101, role: "admin"}`β”‚ β”‚ 2. Server returns cookie: `Set-Cookie: session_id=sess_9981; HttpOnly; Secure` β”‚ β”‚ 3. Subsequent requests send `Cookie: session_id=sess_9981`. β”‚ β”‚ 4. Server queries Redis to fetch user profile on every request. β”‚ β”‚ - Pros: Instant revocation/logout possible by deleting key from Redis. β”‚ β”‚ - Cons: Redis storage overhead + extra network hop to DB/cache per request. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ STATELESS SESSIONS (JSON Web Token - JWT): β”‚ β”‚ 1. User logs in -> Server generates signed token: `Header.Payload.Signature`. β”‚ β”‚ 2. Server returns JWT to client (no server-side memory storage). β”‚ β”‚ 3. Client attaches JWT: `Authorization: Bearer <jwt_token>`. β”‚ β”‚ 4. Server verifies signature cryptographically using public/secret key. β”‚ β”‚ - Pros: Horizontally scalable across 1000s of servers without shared cache. β”‚ β”‚ - Cons: Cannot revoke easily before expiration without maintaining a blacklist. β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5. Client-Side Storage Comparison

5.1 Cookies vs LocalStorage vs SessionStorage vs IndexedDB

Storage TypeCapacityLifetimeSent to Server Automatically?Tab ScopeBest Used For
Cookie4 KBConfigurable via Expires / Max-AgeYes (Attached to every HTTP request)Shared across tabsAuth Session IDs, Tracking IDs
localStorage5–10 MBPersistent (until explicitly cleared)No (Client-side JS only)Shared across tabsUser UI theme (Dark mode), cached preferences
sessionStorage5 MBCleared when tab/window is closedNo (Client-side JS only)Isolated to single tabMulti-tab checkout state, multi-step forms
IndexedDBHundreds of MBPersistentNoShared across tabsOffline web app data, client databases

5.2 Essential Cookie Security Flags

Set-Cookie: session_id=abc123xyz; Secure; HttpOnly; SameSite=Strict; Path=/; Max-Age=86400
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Security Flag β”‚ Purpose & Attack Vector Mitigated β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **`HttpOnly`** β”‚ **Blocks JavaScript access** (`document.cookie` is β”‚ β”‚ β”‚ empty). Prevents token theft via **XSS (Cross-Site β”‚ β”‚ β”‚ Scripting)** attacks. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **`Secure`** β”‚ Cookie is **only transmitted over HTTPS**. Prevents β”‚ β”‚ β”‚ cleartext packet sniffing over public WiFi. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **`SameSite=Strict`** β”‚ Cookie is **never sent on cross-site requests** (even β”‚ β”‚ β”‚ following a link from an external site). 100% blocks β”‚ β”‚ β”‚ **CSRF (Cross-Site Request Forgery)** attacks. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **`SameSite=Lax`** β”‚ Default in modern Chrome. Sent on top-level navigation β”‚ β”‚ β”‚ (GET links), but blocked on cross-site POST/PUT. β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

6. Security Threats & Mitigation Strategies

WEB ATTACKS & DEFENSE MATRIX β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Attack Vector β”‚ Defense Strategy β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **XSS (Script Theft)**β”‚ `HttpOnly` flag on Cookies + Content Security Policy β”‚ β”‚ β”‚ (CSP) + Sanitizing all HTML/JSON outputs. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **CSRF (Forged Req)** β”‚ `SameSite=Strict/Lax` cookie attribute + Anti-CSRF β”‚ β”‚ β”‚ synchronizer tokens + Custom request headers. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **Session Hijacking** β”‚ TLS 1.3 encryption (`Secure` cookies) + Binding sessionβ”‚ β”‚ β”‚ to Device Fingerprint + Short TTL with Refresh Tokens. β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ **Session Fixation** β”‚ **Regenerate Session ID immediately upon login**. β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

7. Juspay & Payment Systems Real-World Context

PAYMENT CHECKOUT SESSION LIFECYCLE [ Merchant Web App ] ──► (1) POST /v1/session (amount=1000) ──► [ Juspay Backend ] β”‚ Generates short-lived Checkout Token (TTL = 15 min) β–Ό [ Merchant Web App ] ◄── (2) Returns `payment_session_token` β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ (3) Opens Juspay HyperSDK in Iframe / Redirect with token β”‚ (4) Completes UPI / 3DS OTP Authentication β”‚ (5) Session invalidated immediately upon final terminal state (SUCCESS / FAILED)
  1. Short-Lived Ephemeral Checkout Sessions:
    • Payment sessions at Juspay do not use standard long-lived cookies. They use ephemeral, cryptographically signed tokens with a strict 15-minute TTL.
  2. Multi-Tab Payment Protection:
    • If a customer opens two different payment checkouts in two browser tabs, Juspay's SDK uses sessionStorage and unique order_session_id tokens so actions in Tab 2 never overwrite or corrupt the payment state in Tab 1.
  3. Strict SameSite Handling for Bank 3DS Redirections:
    • When returning from an external Bank 3DS OTP page via a cross-site HTTP POST redirection, browsers running SameSite=Strict will withhold cookies. Payment gateways engineer redirect handlers to handle SameSite=None; Secure tokens or encode state inside the secure redirect URL.

8. High-Yield Interview Questions & Verbatim Answers

Q1: "How does a web server differentiate between two tabs open on the same laptop in the same browser?"

Spoken Answer:
*"At the network and transport layers, both tabs share the same Public IP, Private IP, and User-Agent, and they share the exact same cookie jar and localStorage within that browser profile.

To differentiate between them at the application layer:

  1. We use sessionStorage, which is strictly isolated per browser tab window.
  2. Single Page Applications generate a unique Tab UUID upon page load and attach it to every outgoing request header (X-Tab-ID: uuid).
  3. Distinct operations embed resource IDs directly into the URL path (e.g., /checkout/order_1 vs /checkout/order_2).
  4. If WebSockets are used, each tab establishes an independent TCP socket with its own unique connection identifier."*

Q2: "How does a web server differentiate between two different laptops connected to the same home WiFi network?"

Spoken Answer:
*"Both laptops share the exact same Public IP address seen by the server due to Network Address Translation (NAT/PAT) on their shared WiFi router.

The server differentiates them at two layers:

  1. Transport Layer (Layer 4): The NAT router assigns distinct, unique source port numbers on its public IP for each laptop's outgoing TCP connections (e.g., 203.0.113.5:40001 vs 203.0.113.5:40002).
  2. Application Layer (Layer 7): Each laptop runs its own isolated browser with independent local storage, holding unique Session Cookies (JSESSIONID) or Authorization Bearer JWTs issued upon login."*

Q3: "What is the purpose of the HttpOnly, Secure, and SameSite flags on cookies?"

Spoken Answer:
*"These flags provide defense-in-depth against primary web vulnerabilities:

  • HttpOnly: Prevents client-side JavaScript (document.cookie) from reading the cookie, mitigating token theft via XSS (Cross-Site Scripting).
  • Secure: Enforces that the browser only transmits the cookie over encrypted HTTPS connections, protecting against packet sniffing on unencrypted networks.
  • SameSite (Strict/Lax): Controls whether the cookie is attached to cross-site requests. Setting SameSite=Strict ensures the cookie is never sent when following third-party links or cross-origin requests, eliminating CSRF (Cross-Site Request Forgery) attacks."*

Q4: "What is the difference between Stateful (Session ID) and Stateless (JWT) session management?"

Spoken Answer:
*"In Stateful Session Management, the server generates a random Session ID stored in a cookie and retains the actual session data (user ID, permissions) in a central database or Redis cache. Every request requires querying the session store. The major advantage is instant revocation capability.

In Stateless Session Management (JWT), the server encodes user claims into a self-contained, digitally signed token (Header.Payload.Signature). The client stores the token and sends it in the Authorization: Bearer header. The server verifies the token signature cryptographically without querying a database. The advantage is effortless horizontal scalability, but the trade-off is difficulty in revoking tokens before their expiration."*