Express SessionID differs from SessionID in Cookie

express, node.js, redis, session, socket.io

Solution

Look at this piece of code from session middleware ( line 267 ):

var val = 's:' + signature.sign(req.sessionID, secret);

where `signature.sign` function is a concatenation ( pseudo-code ):

req.sessionID + "." + hash(secret)

where `hash` is a custom function ( see this for more details ).

This means that it is just a signing convention for cookies ( to make it more secure ). You can retrieve your sid by calling:

var signature = require( "cookie-signature" ),
    prefix = "s:";

var real_sid = sid.replace( prefix, "" );
real_sid = signature.unsign( real_sid, SESSION_SECRET );

Problem

I have a corresponding problem by using the given example. In the example, the session is used in the WebSocket by reloading it first: ``` socket.on('set value', function (val) { sess.reload(function () { sess.value = val; sess.touch().save(); }); }); ``` Trying to use it in my own application, i get the following exception: ``` sess.reload(function () { ^ TypeError: Object #<Object> has no method 'reload' ``` I think the problem is, that no one defines the variable sess as session: ``` io.listen(app).set('authorization', function (data, accept) { if (!data.headers.cookie) return accept('No cookie transmitted.', false); data.cookie = parseCookie(data.headers.cookie); data.sessionID = data.cookie['express.sid']; store.load(data.sessionID, function (err, session) { if (err || !session) return accept('Error', false); data.session = session; return accept(null, true); }); }) ``` Maybe some has a short fix for this? Problem solved: I tried to use this example: https://github.com/DanielBaulig/sioe-demo/blob/master/app.js with Express 3.0 and Redis. Therefore i created a Redis Store (connect-redis) instead of a MemoryStore: ``` app.use(express.session({cookie: {expires: new Date(Date.now() + 30*60*60*24*1000)}, secret: SESSION_SECRET, key: SESSION_KEY, store: new RedisStore({host:'localhost', port:'6379', client: dbRedis})})); ``` Since the parseCookie-Method in connect moved i used ``` parseCookie = require('cookie').parse ``` instead of ``` connect.utils.parseCookie ``` To access the session in the cookie i modified the example by using the following: ``` sio.set('authorization', function (data, accept) { if (!data.headers.cookie) return accept('No cookie transmitted.', false); data.cookie = parseCookie(data.headers.cookie); log.info('Cookie: $s', JSON.stringify(data.cookie)); data.sessionID = data.cookie['letter.sid']; log.info('SessionId: %s', data.sessionID); dbRedis.get(data.sessionID, function (err, session) { if (err || !session) return accept('Error ' + session, false); data.session = session; return accept(null, true); }); }) ``` Now my Problem is, that i cant load the session from Redis because the Session IDs differ Printing the Session ID on a page (req.sessionID) i get: n+57bnkLr+iXkMLbStWdFzK5 But in Redis the following ID is Stored: ``` [2012-12-03T22:14:56.632Z] INFO: Postbox/78964 on capns-mba.local: Cookie: $s {"SQLiteManager_currentLangue":"4","connect.sid":"s:xvYdDm5C0MEIg53EG8JgqBnM.Tx8+PMKa570zk6qt9vmCjRz2p/LP/COyyqGSm+VKxww","letter.sid":"s:n+57bnkLr+iXkMLbStWdFzK5.XPHh1xXrK9D4cPfJ7HcHO11PKk8FXLg6fIRGaWb/+jI"} [2012-12-03T22:14:56.632Z] INFO: Postbox/78964 on capns-mba.local: SessionId: s:n+57bnkLr+iXkMLbStWdFzK5.XPHh1xXrK9D4cPfJ7HcHO11PKk8FXLg6fIRGaWb/+jI ``` Obviously the req.sessionID is part of the SessionID saved in the cookie/redis - but why? And which is the correct sessionID?

Original source