Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Flask Sessions work?

Tags:

session

flask

I am very curious of how Flask sessions works, especially how it stores the information between server restarts (quote me if I am wrong). I understand that you have to set a unique app.secret_key so people cannot decrypt the session and modify the cookie in any way. Because the cookie for the session is just random generated letter and numbers, would this mean that the id is paired up with the id from the server side, and that the server stores the sessions? If that is so, how would Flask remember the sessions between restarts? If not, how does Flask know to decrypt the session cookie?

like image 284
EndenDragon Avatar asked Jul 18 '16 02:07

EndenDragon


People also ask

How do sessions work in Flask?

Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.

How many sessions are in a Flask?

In March 2022, The CW renewed the series for a ninth and final season. As of June 29, 2022, 171 episodes of The Flash have aired, concluding the eighth season.

How long do Flask sessions last?

Default session lifetime is 31 days, user need to specify the login refresh view in case of timeout. Above line will force user to re-login every 5 minutes.

What is the session object in Flask?

In the flask, a session object is used to track the session data which is a dictionary object that contains a key-value pair of the session variables and their associated values. ADVERTISEMENT. ADVERTISEMENT. The following syntax is used to set the session variable to a specific value on the server.

How to use session in flask?

To use session you must set the secret key first. The session object of the flask package is used to set and get session data. The session object works like a dictionary but it can also keep track modifications. When we use sessions the data is stored in the browser as a cookie. The cookie used to store session data is known session cookie.

Do you know what is flask?

But, do we know what is Flask? In short, Flask is a lightweight framework or in other words microframework that allows building web applications. Session in Flask has a concept very similar to that of a cookie, i.e. data containing identifier to recognize the computer on the network, except the fact that session data is stored in a server.

What is flask-session?

Flask-Session is an extension for Flask that support Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.

How is data stored in flask?

Flask uses the client-side approach. In order to store data across multiple requests, Flask utilizes cryptographically-signed cookies (stored on the web browser) to store the data for a session. This cookie is sent with each request to the Flask app on the server-side where it's decoded.


1 Answers

Flask generates the session cookie using its sister project, It's Dangerous. The project page has a great overview of how It's Dangerous works, but at a high level:

  • the data in your session (set by session["username"] = "EndenDragon") is serialized into a JSON string ({"username":"EndenDragon"})
  • that string is encoded using base64 encoding (eyJ1c2VybmFtZSI6IkVuZGVuRHJhZ29uIn0=). This makes it safe for use cases like an email verification link, where it might be appended at the end of the link.
  • the base64 encoded data has a "." appended to it. The timestamp when the session was created is base64 encoded and appended to it.
  • A cryptographic signature is generated for the session + timestamp, using your secret key. The signature to the session value after a "." as well.

The value is then sent to the browser as a Cookie in the response.

The values in the session can be read by end users (and over insecure connections). The server can verify cookies it receives hasn't been tampered with, without storing anything on its end. It just recomputes the signature from the session + timestamp part of the session value, and makes sure it matches the signature at the end of the session value.

The inclusion of the timestamp enables Flask to enforce the expiration date of permanent sessions on the server side, in addition to setting an expiration date on the client side.

Addendum

Users can easily read the values in the session by decoding the first part of the session value. Go to the "Storage" or "Application" tab in developer tools, look for the "session" cookie, copy the value up to the first period, and run btoa(<session-part>) in the Console.

like image 97
Max Shenfield Avatar answered Sep 21 '22 02:09

Max Shenfield