Search code examples
djangocookiesdjango-authenticationdjango-sessions

How exactly does django validates its cookie?


I was reading up on cookie validation and came across the question of how exactly does Django validates its cookie?

If I remember correctly, Django stores session id in the cookie for later use. Does that mean that anyone who fakes the cookie will be able to use arbitrary session data?


Solution

  • The validation itself is damn simple: against the data in in the session backend. As you can see here, the data you receive in a cookie comes from your session, session_key attribute. Where it is being stored depends on your session backend, by default it's the database.

    1. It is impossible to "fake" a cookie. Unless someone stole your SECRET_KEY. More detailed info here.

    2. If someone steals a cookie from a client, the thief can use the client's session till it expires. You cannot prevent it. If you are aware of such a case, the client's password needs to be changed ASAP, as it will lead to invalidation of ther user's existing sessions (starting from Django 1.10).

    Upd: your question made me curious whether the session backend actually stores the value as is... Figures, it does. (I got also impressed there's pgAdmin for Windows) enter image description here