Jump to content

cleanse data in sessions?


knobby2k

Recommended Posts

Hi guys,

 

quick and simple question, should you cleanse the data that you recover from a session.

 

i.e. i have my username in a session to ensure the user is logged in... so, on my page should i be cleansing the data with the various striptags, stripslashes, htmlspecialchars, etc... OR as long as I check the data matches what i expect to be entered at the time it is input by the user, will that data still be safe when i call the session?

 

I suppose what I am asking is can a malicious user spoof a session, so I call $_SESSION['username'] and it turn out to be $_SESSION['lots of damaging code']

 

Thanks

Link to comment
Share on other sites

You should call htmlspecialchars() on anything you want to echo that you don't expect to contain HTML code, unless you KNOW that variable can't contain malicious data (integers, floats, etc).

 

It's very hard for a user to change session data unless you allow them to.. for example

$_SESSION['someVar'] = $_GET['something'];

 

If you're on a shared host, and the host hasn't locked down the session files, it's possible for another user on the same machine to inject malicious code into a session. This is unavoidable from a programmers perspective, and the only way to protect from this RARE form of attack is to sanitize every session variable before use (htmlspecialchars for display, mysql_real_escape_string() for mysql). This is a little extreme though, as the attack can be hard to pull off.

 

Any time any form of data is supplied by an outside source, you should sanitize it before use.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.