Session Injection
Getting attacker-controlled data into a user's trusted session, either because the server writes unvalidated input into it or because a client-side session cookie is unsigned or its signing key has…
Session Injection is the act of getting attacker-controlled data into a user's session state so it is later trusted by the application. It happens two ways: the server writes unvalidated user input into the session, or the attacker tampers with client-side session state (a cookie) that is not properly signed or whose signing/encryption key has leaked.
Impact
- Privilege escalation by setting a role, admin flag, or another user's ID
- Authentication bypass by forging
user_idin a client-side session - Remote code execution when a leaked key is combined with unsafe deserialization
Prevention
- Never assign request parameters directly into the session; set only values derived from the authenticated user (e.g.
session[:user_id] = user.idafter verifying credentials) - Keep authorization attributes (role, permissions) out of the session and read them from the user record server-side
- Sign and encrypt cookie-stored sessions, and treat
secret_key_baseas a secret: never commit it, rotate it if exposed - Use a safe session serializer (JSON, not Marshal) so a forged cookie cannot trigger deserialization code paths