Skip to content

feat: add new option for custom hash #1035

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

Open
wants to merge 2 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ app.use(session({
}))
```

##### hash

Function to call to generate a hash of session object for detect changes.

```js
app.use(session({
hash: function(sess) {
// serialize
const { cookie, ...sessWithoutCookie } = sess;
const str = JSON.stringify(sessWithoutCookie);
// hash
return crypto
.createHash('sha1')
.update(str, 'utf8')
.digest('hex')
},
secret: 'keyboard cat'
}))
```

##### name

The name of the session ID cookie to set in the response (and read from in the
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var defer = typeof setImmediate === 'function'
* @param {String|Array} [options.secret] Secret for signing session ID
* @param {Object} [options.store=MemoryStore] Session store
* @param {String} [options.unset]
* @param {Function} [options.hash] Hash method to detect changes in the session object
* @return {Function} middleware
* @public
*/
Expand Down Expand Up @@ -114,10 +115,17 @@ function session(options) {
// get the cookie signing secret
var secret = opts.secret

// get the hash method
var hash = opts.hash || hashSession

if (typeof generateId !== 'function') {
throw new TypeError('genid option must be a function');
}

if (typeof hash !== 'function') {
throw new TypeError('hash option must be a function');
}

if (resaveSession === undefined) {
deprecate('undefined resave option; provide resave option');
resaveSession = true;
Expand Down Expand Up @@ -601,7 +609,7 @@ function getcookie(req, name, secrets) {
* @private
*/

function hash(sess) {
function hashSession(sess) {
// serialize
var str = JSON.stringify(sess, function (key, val) {
// ignore sess.cookie property
Expand Down
36 changes: 36 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,42 @@ describe('session()', function(){
});
});

describe('hash option', function(){
it('should reject non-function values', function(){
assert.throws(session.bind(null, { hash: 'bogus!' }), /hash.*must/)
});

it('should provide default hash', function(done){
request(createServer())
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, done)
});

it('should allow custom function', function(done){
var counter = 0;
function hash() {
counter++;
var str = JSON.stringify(sess, function (key, val) {
if (this === sess && key === 'cookie') {
return
}
return val
})
return str;
}

var server = createServer({ hash: hash }, function (req, res) {
res.end('counter ' + counter)
});

request(server)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, 'counter 1', done)
});
});

describe('key option', function(){
it('should default to "connect.sid"', function(done){
request(createServer())
Expand Down