Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be stored in a session and what in a cookie?

I'm wondering, are there any guidelines or best practices on when to use sessions and cookies? What should and what should'nt be stored in them? Thanks!

like image 331
user133127 Avatar asked Dec 06 '09 13:12

user133127


People also ask

What do you store in a session cookie?

This cookie stores information such as the user's input and tracks the movements of the user within the website. There is no other information stored in the session cookie. Session cookies are set on a device's temporary memory when a browser session starts.

What is stored in session?

Session. Definition. Cookies are client-side files that are stored on a local computer and contain user information. Sessions are server-side files that store user information. Expiry.

What is the difference between session storage and cookies?

For most cases, we use the local Storage object if we want some data to be on the browser. If we want it on the server, then we use cookies, and the session storage is used when we want to destroy the data whenever that specific tab gets closed or the season is closed by the user.

What is stored in cookies?

Cookies are text files with small pieces of data — like a username and password — that are used to identify your computer as you use a computer network. Specific cookies known as HTTP cookies are used to identify specific users and improve your web browsing experience.


2 Answers

These documents are a good read on security problems with session cookies, and how to get around them.

  • Secure Session Management With Cookies for Web Applications
  • Hardened Stateless Session Cookies

In summary, you keep a secret key on the server. With this key you can calculate a secure hash over the secret key, a time stamp, and any data you want in the cookie. You include the secure hash, the time stamp and the data in the cookie.

When you receive a request you can validate that you get the signature expected. So nobody have tampered with the cookie contents.

like image 63
Christian Avatar answered Oct 31 '22 22:10

Christian


Only data that identify the session and non-security-sensitive user preferences.

A primary rule of writing secure apps is that a hostile party can easily modify data before returning it to you. Therefore you should not assume that any values submitted from a client are safe to use without validation. A standard technique is to hold data on the server and only exchange a key, constructed in a way that you can check for modification. (I.e. don't use the user ID or account number, as a hostile client could systematically manipulate such a value to try to retrieve data from other users or sessions.)

like image 43
joel.neely Avatar answered Oct 31 '22 23:10

joel.neely