SessionStore: Propagate database errors to express-session

Send a 500 HTTP status code to the client if the session entry could
not be fetched from the database. This is useful in case the database
is busy and can't respond to the query in time. In this case we want
to abort the client connection as soon as possible.

Co-authored-by: Richard Hansen <rhansen@rhansen.org>
This commit is contained in:
webzwo0i 2021-07-05 06:07:40 +02:00 committed by Richard Hansen
parent 7572040836
commit 694d3f630e

View file

@ -17,18 +17,12 @@ const logger = log4js.getLogger('SessionStore');
module.exports = class SessionStore extends Store {
get(sid, fn) {
logger.debug(`GET ${sid}`);
DB.db.get(`sessionstorage:${sid}`, (err, sess) => {
if (sess) {
sess.cookie.expires = ('string' === typeof sess.cookie.expires
? new Date(sess.cookie.expires) : sess.cookie.expires);
if (!sess.cookie.expires || new Date() < sess.cookie.expires) {
fn(null, sess);
} else {
this.destroy(sid, fn);
}
} else {
fn();
}
DB.db.get(`sessionstorage:${sid}`, (err, s) => {
if (err != null) return fn(err);
if (!s) return fn(null);
if (typeof s.cookie.expires === 'string') s.cookie.expires = new Date(s.cookie.expires);
if (s.cookie.expires && new Date() >= s.cookie.expires) return this.destroy(sid, fn);
fn(null, s);
});
}