Skip to content

Commit

Permalink
Keep req.session.save non-enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 8, 2014
1 parent cd6b087 commit 5bc59f1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Keep `req.session.save` non-enumerable
* Prevent session prototype methods from being overwritten

1.8.0 / 2014-09-07
Expand Down
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,19 @@ function session(options){
// wrap session methods
function wrapmethods(sess) {
var _save = sess.save;
sess.save = function save() {

function save() {
debug('saving %s', this.id);
savedHash = hash(this);
_save.apply(this, arguments);
};
}

Object.defineProperty(sess, 'save', {
configurable: true,
enumerable: false,
value: save,
writable: true
});
}

// check if session has been modified
Expand Down
14 changes: 13 additions & 1 deletion test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ describe('session()', function(){
})
})

it('should only have session data enumerable (and cookie)', function (done) {
var server = createServer(null, function (req, res) {
req.session.test1 = 1
req.session.test2 = 'b'
res.end(Object.keys(req.session).sort().join(','))
})

request(server)
.get('/')
.expect(200, 'cookie,test1,test2', done)
})

describe('when response ended', function () {
it('should have saved session', function (done) {
var saved = false
Expand Down Expand Up @@ -483,7 +495,7 @@ describe('session()', function(){
should(sid(res)).not.equal(val)
done()
})
}, 10)
}, 15)
})
})

Expand Down

0 comments on commit 5bc59f1

Please sign in to comment.