Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding verification that sess.cookie is set #904

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ defineMethod(Session.prototype, 'reload', function reload(fn) {
store.get(this.id, function(err, sess){
if (err) return fn(err);
if (!sess) return fn(new Error('failed to load session'));
store.createSession(req, sess);
fn();
try {
store.createSession(req, sess);
} catch (e) {
err = e
}
fn(err);
});
return this;
});
Expand Down
8 changes: 7 additions & 1 deletion session/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ Store.prototype.load = function(sid, fn){
if (err) return fn(err);
if (!sess) return fn();
var req = { sessionID: sid, sessionStore: self };
fn(null, self.createSession(req, sess))
try {
sess = self.createSession(req, sess)
} catch (e) {
err = e
sess = null
}
fn(err, sess)
});
};

Expand Down
63 changes: 63 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,35 @@ describe('session()', function(){
})
})

describe('when session is corrupt', function () {
it('should return an error', function (done) {
var store = new session.MemoryStore()
var server = createServer({ store: store }, function (req, res) {
req.session.count = req.session.count || 0
req.session.count++
res.end('hits: ' + req.session.count)
})

store.get = function returnCorruptSession(sid, callback) {
callback(undefined, {});
}

request(server)
.get('/')
.expect(200, 'hits: 1', function (err, res) {
if (err) return done(err)
store.load(sid(res), function (err, sess) {
assert.ok(err);
assert.ok(err.message.match(/Cannot read prop/));
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(500, /Cannot read prop/, done)
})
})
})
})

describe('when session expired in store', function () {
it('should create a new session', function (done) {
var count = 0
Expand Down Expand Up @@ -1665,6 +1694,40 @@ describe('session()', function(){
})
})

it('should error when the session is corrupt', function (done) {
var store = new session.MemoryStore()
var server = createServer({ store: store }, function (req, res) {
if (req.url === '/') {
req.session.active = true
res.end('session created')
return
}

store.clear(function (err) {
if (err) return done(err)

store.get = function returnCorruptSession(sid, callback) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store.get needs to be overwritten here instead of on line 1719 because otherwise the error is caught (and surfaced gracefully) in the wrong spot and .reload() never executes.

callback(undefined, {});
}

req.session.reload(function (err) {
res.statusCode = err ? 500 : 200
res.end(err ? err.message : '')
})
})
})

request(server)
.get('/')
.expect(200, 'session created', function (err, res) {
if (err) return done(err)
request(server)
.get('/foo')
.set('Cookie', cookie(res))
.expect(500, /Cannot read prop/, done)
})
})

it('should not override an overriden `reload` in case of errors', function (done) {
var store = new session.MemoryStore()
var server = createServer({ store: store, resave: false }, function (req, res) {
Expand Down