Skip to content

Not possible to set cookie encode option #883

Open

Description

@thomasmarr

One of the options which can be set on cookie.serialize is encode see here

In express-session it is not possible to set this option:

  • req.session.cookie = new Cookie(cookieOptions);
  • setcookie(res, name, req.sessionID, secrets[0], req.session.cookie.data);
  • var data = cookie.serialize(name, signed, options);
  • session/session/cookie.js

    Lines 117 to 127 in 5df613c

    get data() {
    return {
    originalMaxAge: this.originalMaxAge
    , expires: this._expires
    , secure: this.secure
    , httpOnly: this.httpOnly
    , domain: this.domain
    , path: this.path
    , sameSite: this.sameSite
    }
    },

I believe express-session should expose this, as the default encodeUriComponent is not always the desired behaviour. I would be willing to try and put a PR together for this if it was likely to be merged promptly.

Activity

  1. FlinthDevs commented on Sep 15, 2023

    @FlinthDevs

    I agree, in my context, cookies are urlEncoded once more by a mechanism I have no control on and I had to patch the module to allow encode to be modified same as other ones.

    diff --git a/node_modules/express-session/session/cookie.js b/node_modules/express-session/session/cookie.js
    index a8b4e57..5fa4553 100644
    --- a/node_modules/express-session/session/cookie.js
    +++ b/node_modules/express-session/session/cookie.js
    @@ -73,6 +73,28 @@ Cookie.prototype = {
         return this._expires;
       },
       
    +  /**
    +  * Set encode `callback`.
    +  *
    +  * @param {CallableFunction} callback
    +  * @api public
    +  */
    +
    + set encode(callback) {
    +   this.callback = callback;
    + },
    +
    + /**
    +  * Get encode `callback`.
    +  *
    +  * @return {CallableFunction} callback
    +  * @api public
    +  */
    +
    + get encode() {
    +   return this.callback;
    + },
    +
       /**
        * Set expires via max-age in `ms`.
        *
    @@ -123,6 +145,7 @@ Cookie.prototype = {
           , domain: this.domain
           , path: this.path
           , sameSite: this.sameSite
    +      , encode: this.callback
         }
       },
     
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions