Vulnerability
Session hijacking is the broad category covering attacks where an attacker obtains a valid session identifier belonging to another user.
Unlike predictable session IDs or session fixation, the attacker does not need to guess or manufacture the credential. The session already exists and is valid — the attacker's goal is simply to steal it.
Victim
→ valid authenticated session
↓
Session ID exposed or stolen
↓
Attacker obtains same credential
↓
Attacker sends it to application
↓
Server recognizes victim's session
↓
Attacker acts as the victim
Several common attack vectors can expose a session credential.
Network Interception:
If a session cookie is transmitted over an unencrypted HTTP connection, anyone able to observe that traffic may be able to capture it.
Browser
→ HTTP request
→ Cookie: session=abc123
↓
Network observer
→ captures session ID
The defense is to serve the application over HTTPS and mark session cookies with the Secure attribute:
Set-Cookie: session=abc123; Secure
Secure instructs the browser to send the cookie only over HTTPS connections.
Cross-Site Scripting:
XSS can expose a session identifier when attacker-controlled JavaScript runs in the application's origin.
Without HttpOnly:
document.cookie
may expose the session cookie to the injected script.
The attacker could potentially send that value elsewhere:
Injected JavaScript
↓
Reads accessible session cookie
↓
Attacker receives session ID
↓
Session hijacking
The HttpOnly attribute prevents JavaScript from directly reading the cookie:
Set-Cookie: session=abc123; HttpOnly
The distinction is important:
HttpOnly
→ prevents JavaScript from reading the cookie
HttpOnly
≠
prevents XSS
HttpOnly
≠
prevents malicious JavaScript from performing actions
An XSS vulnerability still needs to be fixed. HttpOnly simply limits one particularly valuable thing the injected script may be able to steal.
Session IDs in URLs
Session identifiers should never be placed in URLs:
https://example.com/account?session=abc123
URLs can be recorded in multiple places:
Browser history
Server access logs
Proxy logs
Analytics systems
Shared links
Referer headers
Session credentials should instead be carried through appropriately configured cookies or, where the architecture requires it, protected authorization headers.
CSRF and Ambient Authority:
CSRF does not normally steal the session identifier. Instead, it abuses the fact that the victim's browser may automatically attach that credential to a request.
Attacker-controlled page
↓
Triggers request to target application
↓
Browser may attach matching cookies
↓
Server sees authenticated request
The request may therefore execute with the victim's authority even though the victim never intended to perform that action.
The SameSite cookie attribute helps control when cookies are included in cross-site contexts:
Set-Cookie: session=abc123; SameSite=Lax
or, where the application's functionality permits:
Set-Cookie: session=abc123; SameSite=Strict
These settings help reduce CSRF exposure, though applications may still require additional CSRF protections depending on their architecture.
Defense in Depth
A properly configured session cookie might look like:
Set-Cookie: session=a9f3e7c1b2d84f...;
Secure;
HttpOnly;
SameSite=Lax;
Path=/
Combined with:
Cryptographically secure session IDs
+
Session regeneration after login
+
HTTPS everywhere
+
Secure / HttpOnly / appropriate SameSite cookies
+
No session IDs in URLs
+
Real server-side session invalidation
These controls address different failure points. No single flag makes session management secure on its own.