views:

127

answers:

6

I was creating a login system with PHP and I wondered: Why are sessions needed?

If I store a cookie with the userid and the sessionid doesn't it pose the exact same security risks to storing a cookie with userid and password hash (given that the password hash is strong enough)? Yeah, someone could potentially steal the cookie, but isn't it the same if they steal the sessionid cookie?

Could someone tell me what's the reason for using sessions in every (reasonably secure) login system?

+3  A: 

One of the benefits of a session is that you can generate a new one each time somebody logs in, and even periodically during a user's visit. If you just used userid and some hash of the password, then as soon as somebody stole your cookies they would be able to log in as you indefinitely. Sessions expire.

Nick
Good point. But if you clicked 'Remember me' (as most users eventually do) then wouldn't the risk be about the same?
If someone steals a cookie, then the next time the real user logs in, his session ID won't work anymore and so he'll be prompted for a login. Once he logs in with username/password, the crook's sessionID will no longer work.
supercat
'Remember me' buttons usually just mean to set the session cookie to NOT expire (e.g. become permanent) when the browser's closed. There's nothing magical about it.
Marc B
A: 

Cookies you can set to expire immediately, or a year from now if you wanted to. Sessions expire immediately when the browser is closed. They are generally more secure because of this. There really is no difference that matters in this particular scenario - except for the length of expiry, and the fact that one is stored 'physically' on the client machine, while the other is stored in memory.

Kolten
Cookies and sessions aren't really comparable - cookies are one way of implementing sessions (the other being in the query string of site URLs). Sessions can expire a year from now if you want them to.
Nick
As Nick mentioned, sessions can take longer to expire and cookies can be set to expire when the browser is closed (or even earlier), so duration doesn't seem much of a valid argument.@Nick I'm not trying to compare 2 ways of implementing sessions, I'm trying to compare cookie-based sessions to cookie-based, session-less authentication.
Since 99.9% of PHP session implementations rely on cookies, a cookie-based session system and a cookie-based sessionless system would have the exact same limitations when it comes to cookie information (expiry, etc). The difference is not where information is stored (both store the cookie on the client machine), it's where sensitive information is stored (With sessions, it's on the server side, and with session-less it's on the client side)...
ircmaxell
A: 

Because is more secure period.

With sessions the information is store in the server, therefore it would take another few steps if someone is to still any kind of information. With cookies anyone can get the indformation straight from the local machine without the need of server requests.

Pablo
This is not necessarily so, it is possible to implement your own session handlers that can store information encrypted in cookies.
Allain Lalonde
encripted decryptable data? interesting... but does it worth encrypting data in a cookie? when you can use sessions
Pablo
+1  A: 

Rather than creating a hash, why not encrypt the session id, so that you can decrypt it and see if it timed out.

If you also have it tied to an IP address then you can make it more secure.

Or, you can have the user just log in for every page they want to go to.

Once solution is to have a javascript app that actually gets all the information for the user, so, the username/password is stored in javascript. Then, for every page request, or interaction, pass either the username/password, or a token that was the result of some initial authentication, that is also encrypted as above.

Then you don't need a session, and if the user closes that tab, their information is gone, so there is no security risk.

Session ids are simpler than having javascript do everything, so, most people opt for sessions, but, you don't have to.

James Black
A: 

If I store a cookie with the userid and the sessionid doesn't it pose the exact same security risks to storing a cookie with userid and password hash (given that the password hash is strong enough)? Yeah, someone could potentially steal the cookie, but isn't it the same if they steal the sessionid cookie?

Yes, it's potentially a similar risk. Stealing a cookie may be argued not be as serious for several reasons:

  • Session cookies have a shorter lifetime.
  • Users frequently recycle username/passwords between sites, this can expose them more if their credentials are intercepted.
  • A session may actually not allow the user to do the same things as a username/password; for instance, the site may require the user to re-enter his credentials at some point.
  • The site may limit the session validity to one specific IP.

But yes, having a session cookie stolen may in some circumstances be as serious as having username/pass stolen. To have solid security against stolen credentials, you must encrypt (https) all the traffic between the users and the web server (even before they authenticate, otherwise an active attacker could change the action of the form submission where the user enters the credentials in order to make them not be posted through a secure channel).

Artefacto
i agree on most of the things you said, but i think using 'absolutely secure' is just too strong a word to use in context of security, just a thought! :)
ultrajohn
@ultra I rephrased it. I'll reserve the term "unconditionally secure" to one-time pads :p
Artefacto
@lucrezia You seriously have a problem with 1.? Users typically **never** change their password, unless forced to. 2. If you add salt you mitigate the recycling problem. However, if you use a user/pass hash to authenticate, that effectivily is a password surrogate that can be used to used to initiate sessions anytime (and therefore nulling the mitigating factor 1., 3. and 4.)
Artefacto
@artefacto, might be, but let's not forget, so they say, 'that security is only as strong as the weakest link', :)
ultrajohn
+1  A: 

You describe a design where the user credentials are validated on every page request and the login form is only used to store these credentials in the browser. I see several issues with such unusual approach:

  • You transmit sensitive information on every HTTP request.
  • In order to validate the credentials you possibly have to run a database query and you do it again and again and again.
  • You have to validate the credentials just to know who the request comes from.
  • Simultaneous connections with the same credentials will share data [see addendum].

It's more typical to validate the user once (possibly using an encrypted channel or hashed information) and remember it for a specified time.

Whatever, sessions and cookies are completely different tools. Cookies are a client-side storage while sessions are server-side. They only relate each other because the most typical session implementation uses cookies to store the session ID (since HTTP is a stateless protocol, you need such tricks to keep track of what request come from whom). The benefic of server-side storage is that you have full control on it:

  • You can use as much space as you need.
  • You can use whatever data format you need.
  • You can keep sensitive data or stuff you don't want to the user to see or mangle.
  • You can trust the information since you put it there.

Addendum

In a classical session system where you assign a random ID on session start a registered user can have many simultaneous session instances in different computers (or even in a single computer, e.g., one in Firefox and three more in Chrome incognito mode windows). If you only identify the visitor by his username, the user will only have one session: if he goes home for lunch he will find the session he left at work, if he shares his password with a friend he'll see what his friend is doing. This can be considered a bug or a feature and, of course, there're many tricks to prevent it. Just take it into account.

Álvaro G. Vicario
Thanks for the extensive reply.1. But it's encrypted :) 2. That's true for db-stored sessions as well, and many, many systems use that.3. You have to validate the sessionid to see where the request comes from too. Point being?4. I don't get that. Could you elaborate?
@lucrezia: what I mean in #3 is that you automatically know that all requests with the same session ID come from the same person (note I'm not saying *user*), even if he hasn't authenticated yet, no checks are required (unless you want to). If you can only identify a person by his user credentials you need to check that the user+pass match before you're able to figure out it's the same guy. And, of course, anonymous browser cannot benefit from session-relates stuff.I'll try to elaborate on #4.
Álvaro G. Vicario