Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session Variables - Doc Request

Sessions' variables

In all web app you can get/set sessions' variables

PHP:

$foo = $_SESSION['myVar'];

.NET (MVC, in Controller):

using System.Web.Mvc;
// ...
var foo = Session["myVar"];

I am looking for some detailed informations on sessions' variables :

  • Their initial purpose (what problems did they aimed to address ?)
  • Common use cases

Storage

  • Where is it stored on the system ?

Hard drive, RAM, ...

  • Who is storing it?

Client / Server

  • I guess it's server-side, so what is managing it ?

Web Server (Apache, IIS, ...) / Web App

  • What is the lifetime of a session's variable ?

The session, right. So when do a session start, when does it end and how do the system know when it can get rid of these variables (GC mechanism) ?


Security

  • Known security flaws ?

PS: I would like to allow people here to build a good documentation about this concept. Feel free to edit the question if you think some questions should be added or edited.

like image 605
Apolo Avatar asked May 18 '16 13:05

Apolo


2 Answers

Purpose

Session Variables were created primarily to deal with the stateless behavior of the HTTP protocol. Because each page request was handled pretty much completely separately from each other page request, developers wanted ways to tie strings of requests together. The canonical example of this is a login page that authenticates the user and then changes the behavior of pages requested after login.

To help with this problem, many languages and/or frameworks provided the concept of a Session Variable which would let the developer store data that would be associated with a specific browser and would persist across separate requests from that same browser.

So, to take logins as an example, on the first request from a new browser, the Session Variable would be blank. Then the user would fill out authentication information and assuming it was correct, on the server side the code would set the Session Variable for that browser to contain some sort of identifier to say that his browser was authenticated. Then during subsequent requests the code could check that identifier in the Session Variable to do some specific code that required logging in.

Another common use case would be for a "wizard" workflow. You might have a multi-page form that you want the user to fill in over several separate requests. As the user fills out the form, you can add the values to the session until the user gets to the end of the form at which time you could save it in some more permanent storage.

Storage and Management

There are many ways to store Session Variables. Any sort of persistent storage that is persistent across requests will work. Probably the most basic way is to just create a separate file for each session. PHP does this by taking a session ID that it has stored as a cookie in a browser and then looks for a file with a named derived from the session ID.

You can also store Session Variables in databases, shared memory, or even in the cookie itself. Ruby on Rails stores Session Variables by encrypting the data and then setting the cookie to the encrypted data. So the session gets stored in the user's browser itself.

Most typically the Session Variable is associated with a cookie that is stored in web browser in some way. This cookie is usually managed automatically by the language or framework that the web server application is written in. The language or framework detects a new session and creates a new Session Variable that it provides to the web server application via some sort of API. The web server application can then use the API to store information in the Session Variable, to delete it, create a new one, etc... Usually the framework has some default value for the lifetime of the session, but usually this is adjustable via the API. I think the most typical default lifetime is the the lifetime of the browser process via a cookie that has a lifetime associated with the user's browser process.

Security

There are a lot of security issues around Session Variables because they are typically used to manage authorization and authentication in web applications.

For example, many applications set the session lifetime just using the lifetime associated with the cookie. Many login systems want to force the user to re-login after a specified time, but you can't trust the browser to expire the cookie when you tell it to. The browser could be buggy, could be written by a malicious person, or manipulated by the user herself to adjust the lifetime of the cookie. So if the Session Variable API you are using relies on the cookie lifetime, you may need to have a secondary mechanism that forces the Session Variable to expire even if the cookie doesn't.

Some other security issues involve storage. If you store A Session ID in a cookie and then use that Session ID as your file name to store the Session Variable in, a malicious browser can change the Session ID in the cookie to another ID and then requests from that browser would start using some other browser's session file.

Another issue is stolen session information. Through XSS or packet inspection, session information can be stolen from a users browser session and then used by a malicious user to access the other user's accounts. This sort of problem is typically mitigated by using SSL to protect the session in transit.

This page explains a lot of the security issues when using PHP's implementation of Session Variables. Ruby on Rails has a similar page that outlines the security issues with Session Variables for that platform.

like image 166
Peter Haight Avatar answered Sep 20 '22 20:09

Peter Haight


So, I will be taking this question on under two considerations: 1. I am answering under PHP guidelines. 2. I am assuming that a shared hosting service is used.

Storage With the use of shared hosting, the php.ini file holds this answer. The file is created, physically, at the path you specify through the "session.save_path" line within the php.ini file.

Source: php.net Manual

Who Stores Session The session is TECHNICALLY stored by the SERVER but at request, obviously, by the client. So, answer: SERVER. Source: session_start

Who Manages It If your session.save_path is set to go somewhere on a shared hosting server, then they control the GC that destroys it or ignores it until later. Actually, instances have happened for me where other clients within the shared hosting server had their session_gc.maxlifetime at a MUCH shorter amount than I did, therefore causing my session files to be destroyed in the amount of time that THEY set (other shared users). To get around this, edit your "session.save_path" to within your OWN file tree.

Lifetime As said previously, "session.gc_maxlifetime" controls this file's "expiration". Along with this, the "session.gc_probability" and "session.gc_divisor" should be considered, and set to "1" and "100", respectively. Google search this for further explanation.

Source: session.gc_maxlifetime

Security I'm going to let php.net handle this, but here's the link!

Source: Security

like image 20
mboyde Avatar answered Sep 20 '22 20:09

mboyde