express-flash icon indicating copy to clipboard operation
express-flash copied to clipboard

Random Cookies Appearing in Sessions table

Open haroot opened this issue 7 years ago • 4 comments

i believe this plugin is causing these cookies to pile up in my table:

{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"flash":{}}

haroot avatar Nov 21 '17 01:11 haroot

connect-session (dependency) modifies the session even if not necessary, so e.g. express-session saves the new session thinking its not uninitialized. https://github.com/jaredhanson/connect-flash/issues/33

From the saveUninitialized docs of https://github.com/expressjs/session:

The session is uninitialized when it is new but not modified.

This is a big issue, causing empty sessions to be created on page loads.

Upstream issue is here: https://github.com/RGBboy/express-flash/blob/master/lib/express-flash.js

res.locals.messages = req.flash();

manuel-di-iorio avatar Sep 04 '18 10:09 manuel-di-iorio

I have the same problem. Did anybody found a solution?

roberto-belardo avatar Oct 18 '18 08:10 roberto-belardo

@backslash451 This is the workaround, call this middleware before your routes:

export default (req, res, next) => {
  const _end = res.end;
  
  res.end = function fixExpressResponseEnd(...args) {
    // Fix for the connect-flash empty session
    // https://github.com/jaredhanson/connect-flash/issues/33
    if (req.session && !Object.keys(req.session.flash || {}).length) {
      delete req.session.flash;
    }

    _end.apply(this, args);
  };

  next();
};

manuel-di-iorio avatar Oct 18 '18 09:10 manuel-di-iorio

This is great. I'll think about using it, because I already found a different solution using a custom function to "enable" flash() middleware on all routes except for a special one used for healthcheck (this was the problem in the first place, a GET request every 1 sec used to check the status of the application, that created the "almost" empty session object).

roberto-belardo avatar Oct 18 '18 11:10 roberto-belardo