Skip to content
Myr

Don't mangle the options object in res.cookie

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Make a copy of the cookie options before mutating it to pass to cookie.serialize. This prevents unexpected things from happening when we try to use the same options object multiple times. Also add a test to verify that the options object does not change after a request is made.

authored by Greg Methvin · committed by Greg Methvin · parent 8d21f1e · Browse files · 2 files+25 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+25 added−3 removed

  • tests:+20 added−0 removed
  • other:+5 added−3 removed

2 files+25 added−3 removed

modified:lib/response.js+5 added-3 removed
571 unmodified lines
572 * @api public573 */574575res.cookie = function(name, val, options){576 options = options || {};576 options = utils.merge({}, options);577 var secret = this.req.secret;578 var signed = options.signed;579 if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');580 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);581 if (signed) val = 's:' + sign(val, secret);582 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);582 if ('maxAge' in options) {583 options.expires = new Date(Date.now() + options.maxAge);584 options.maxAge /= 1000;585 }586 if (null == options.path) options.path = '/';584 options.maxAge /= 1000;587 this.set('Set-Cookie', cookie.serialize(name, String(val), options));588 return this;589};590
132 unmodified lines
modified:test/res.cookie.js+20 added