It’s funny, in a way, that cms34 has been around for nearly five years now, and it’s only just become a real issue that we were not maintaining sessions between SSL and non-SSL pages. This is somewhat understandable: practically speaking, the only time it really matters to carry over the session between the two is when a user is logged in. (This might not be the case with all web applications, but for us, at least, there’s rarely enough happening in the session when a user is not logged in for it to matter.)
As it happens, not that many cms34 sites use SSL; not that many cms34 sites use the user login capability on the front end. And very few use both. But we’ve had a couple of new sites come online lately that do use both, and it’s become a bit of an issue.
The issue was exacerbated by the fact that I recently modified the Users controller to require SSL on the login page, if the site has an SSL certificate. Consequently there were issues with trying to access login-restricted, but non-SSL pages… redirect loops and other such fun.
What’s the problem?
The problem is simple: for obvious security reasons, sessions and cookies cannot be shared directly between two different domains. It’s possible (although less secure) to share them between both SSL and non-SSL on the same domain, and it’s also relatively easy to set them up to work between different subdomains. But if your SSL pages use a different domain name than the non-SSL pages, even if they’re on the same server, there’s no way to get them to automatically use the same session.
The solution (though still not ideal, as it can introduce the risk of session hijacking), as you’ll find in lots of places, is to pass the session ID as a query string variable. Then you can use that to restore the same session ID, even if it’s on another domain — as long as it’s on the same physical server.
Some improvements
There are two key improvements I made to the basic “pass the session ID in the query string” scenario.
First, when the session is created I am writing the user’s IP address (using $_SERVER['REMOTE_ADDR']
) as a session variable. Then, when I am attempting to restore the session with the ID passed as a query string variable, I read the session file on the server first, and make sure the IP address in the file matches still matches the user’s current IP address. Only then do I restore the session.
Second, and this is an aesthetic issue almost as much as a security one, once the session has been re-established, and before any response has been sent, I strip the session ID out of the requested URL and redirect to that new URL. It’s all invisible to the user, and the session ID never actually appears in the browser’s address bar.
A look at the code
There’s a lot going on in the cms34 code, much of which is highly specific to this application. But in short the keys to making this work happen in two places:
UsersController::login()
I have a login()
action in UsersController
that handles all of the special functionality that needs to happen when a user logs in. The actual login itself happens “automagically” via AuthComponent
, but Auth doesn’t know everything I need to have happen when a user logs in, so after Auth does its work, my login()
action takes it from there.
Honestly not a lot needs to happen here to make this work. Just two things: you have to write the user’s IP address to the session as I noted above, and you have to pass the session ID in a query string variable on the redirect that happens when login is successful. My code looks a little something like this (note that I have an array in the session called Misc
that I use for… miscellaneous stuff like this):
class UsersController extends AppController {
var $name = 'Users'
// Other controller variables go here, of course.function login() {
// All of this should only run if AuthComponent has already logged the user in.
// Your session variable names may vary.
if ($this->Session->read('Auth.User')) {// Various session prep stuff happens here.
// Write IP address to session (used to verify user when restoring session).
$this->Session->write('Misc.remote_addr(',$_SERVER['REMOTE_ADDR']);// Some conditionals for special redirects come here but we'll skip that.
// Redirect user to home page, with session ID in query string.
// Make up a query string variable that makes sense for you.
$this->redirect('/?cms34sid=' . session_id());}
}
}
So far, so good. The rest of the excitement happens in…
AppController::beforeFilter()
Ah yes, the magical beforeFilter()
method. There’s a whole lot of stuff going on in AppController::beforeFilter()
in cms34, most of which is highly specific to our application. But this is where you will need to put your code to retrieve the session ID from the query string and restore the session… this function runs at the beginning of every page load on your site.
I’ve put this logic almost at the beginning of beforeFilter()
, because we really do want that session restored as soon as possible.
Here’s a look…
class AppController extends Controller {
function beforeFilter() {
// Additional code specific to your app will likely come before and after this.
// Only run if session ID query string variable is passed and different from the existing ID.
// Be sure to replace cms34sid with your actual query string variable name.
if (!empty($this->params['url']['cms34sid']) && $this->params['url']['cms34sid'] != session_id()) {// Verify session file exists.
// I am using CakeSession; your session files may be elsewhere.
$session_file = TMP.DS.'sessions'.DS.'sess_'.$this->params['url']['cms34sid'];if (file_exists($session_file)) {
$session_contents = file_get_contents($session_file);// Find user's IP address in session file data (to verify identity).
// The CakePHP session file stores a set of serialized arrays; we're reading raw serialized data.
// If you used a different session variable name than remote_addr, change it AND the 11 to its string length.
$session_match = 's:11:"remote_addr";s:'.strlen($_SERVER['REMOTE_ADDR']).':"'.$_SERVER['REMOTE_ADDR'] .'";';// User's IP address is in session file; so we can continue.
if (strpos($session_contents,$session_match) !== false) {// Set session ID to restore session
$this->Session->id($this->params['url']['cms34sid']);// Redirect to this same URL without session ID in query string
$current_url = rtrim(preg_replace('/cms34sid=[^&]+[&]?/','',current_url()),'?&');
$this->redirect($current_url);
}
}
}
}
}
A few final thoughts
I didn’t really discuss cookies at all here, but suffice to say there’s a cookie containing the session ID that gets written. If you’re only using cookies for the session ID (which is probably a good idea), then you don’t really need to do anything else with them. But if you’re writing certain cookies when a user logs in (like I do), you’ll need to write additional logic to restore them in AppController::beforeFilter()
. In my case, the relevant cookies are all duplicates of session data, but are intended for a few edge cases where I need to access that information through JavaScript or in .htaccess
files that are protecting login-restricted downloadable files — in other words, places where I can’t use PHP to look at session data.
You may also notice near the end of the code in the AppController::beforeFilter()
example above that I am calling a function called current_url()
. This is not a built-in part of PHP or CakePHP; it’s a simple little function I have in my config/functions.php
file. Here it is:
function current_url() {
return (!empty($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}