Skip to content

Commit

Permalink
fix: closes #12185, fix cli user password reset
Browse files Browse the repository at this point in the history
refactor session get/destroy
  • Loading branch information
barisusakli committed Nov 29, 2023
1 parent a9ef58a commit 6790000
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
7 changes: 1 addition & 6 deletions src/api/users.js
@@ -1,6 +1,5 @@
'use strict';

const util = require('util');
const path = require('path');
const fs = require('fs').promises;

Expand Down Expand Up @@ -330,10 +329,6 @@ usersAPI.deleteToken = async (caller, { uid, token }) => {
return true;
};

const getSessionAsync = util.promisify((sid, callback) => {
db.sessionStore.get(sid, (err, sessionObj) => callback(err, sessionObj || null));
});

usersAPI.revokeSession = async (caller, { uid, uuid }) => {
// Only admins or global mods (besides the user themselves) can revoke sessions
if (parseInt(uid, 10) !== caller.uid && !await user.isAdminOrGlobalMod(caller.uid)) {
Expand All @@ -344,7 +339,7 @@ usersAPI.revokeSession = async (caller, { uid, uuid }) => {
let _id;
for (const sid of sids) {
/* eslint-disable no-await-in-loop */
const sessionObj = await getSessionAsync(sid);
const sessionObj = await db.sessionStoreGet(sid);
if (sessionObj && sessionObj.meta && sessionObj.meta.uuid === uuid) {
_id = sid;
break;
Expand Down
1 change: 1 addition & 0 deletions src/cli/user.js
Expand Up @@ -77,6 +77,7 @@ let winston;
async function init() {
db = require('../database');
await db.init();
await db.initSessionStore();

user = require('../user');
groups = require('../groups');
Expand Down
22 changes: 22 additions & 0 deletions src/database/index.js
Expand Up @@ -34,4 +34,26 @@ primaryDB.initSessionStore = async function () {
primaryDB.sessionStore = await sessionStoreDB.createSessionStore(sessionStoreConfig);
};

function promisifySessionStoreMethod(method, sid) {
return new Promise((resolve, reject) => {
if (!primaryDB.sessionStore) {
resolve(method === 'get' ? null : undefined);
return;
}

primaryDB.sessionStore[method](sid, (err, result) => {
if (err) reject(err);
else resolve(method === 'get' ? result || null : undefined);
});
});
}

primaryDB.sessionStoreGet = function (sid) {
return promisifySessionStoreMethod('get', sid);
};

primaryDB.sessionStoreDestroy = function (sid) {
return promisifySessionStoreMethod('destroy', sid);
};

module.exports = primaryDB;
8 changes: 2 additions & 6 deletions src/socket.io/index.js
Expand Up @@ -241,10 +241,6 @@ async function checkMaintenance(socket) {
throw new Error(`[[pages:maintenance.text, ${validator.escape(String(meta.config.title || 'NodeBB'))}]]`);
}

const getSessionAsync = util.promisify(
(sid, callback) => db.sessionStore.get(sid, (err, sessionObj) => callback(err, sessionObj || null))
);

async function validateSession(socket, errorMsg) {
const req = socket.request;
const { sessionId } = await plugins.hooks.fire('filter:sockets.sessionId', {
Expand All @@ -256,7 +252,7 @@ async function validateSession(socket, errorMsg) {
return;
}

const sessionData = await getSessionAsync(sessionId);
const sessionData = await db.sessionStoreGet(sessionId);
if (!sessionData) {
throw new Error(errorMsg);
}
Expand All @@ -282,7 +278,7 @@ async function authorize(request, callback) {
request: request,
});

const sessionData = await getSessionAsync(sessionId);
const sessionData = await db.sessionStoreGet(sessionId);
request.session = sessionData;
let uid = 0;
if (sessionData && sessionData.passport && sessionData.passport.user) {
Expand Down
18 changes: 5 additions & 13 deletions src/user/auth.js
Expand Up @@ -2,7 +2,6 @@

const winston = require('winston');
const validator = require('validator');
const util = require('util');
const _ = require('lodash');
const db = require('../database');
const meta = require('../meta');
Expand Down Expand Up @@ -62,17 +61,10 @@ module.exports = function (User) {
]);
};

const getSessionFromStore = util.promisify(
(sid, callback) => db.sessionStore.get(sid, (err, sessObj) => callback(err, sessObj || null))
);
const sessionStoreDestroy = util.promisify(
(sid, callback) => db.sessionStore.destroy(sid, err => callback(err))
);

User.auth.getSessions = async function (uid, curSessionId) {
await cleanExpiredSessions(uid);
const sids = await db.getSortedSetRevRange(`uid:${uid}:sessions`, 0, 19);
let sessions = await Promise.all(sids.map(sid => getSessionFromStore(sid)));
let sessions = await Promise.all(sids.map(sid => db.sessionStoreGet(sid)));
sessions = sessions.map((sessObj, idx) => {
if (sessObj && sessObj.meta) {
sessObj.meta.current = curSessionId === sids[idx];
Expand All @@ -93,7 +85,7 @@ module.exports = function (User) {
const expiredSids = [];
await Promise.all(Object.keys(uuidMapping).map(async (uuid) => {
const sid = uuidMapping[uuid];
const sessionObj = await getSessionFromStore(sid);
const sessionObj = await db.sessionStoreGet(sid);
const expired = !sessionObj || !sessionObj.hasOwnProperty('passport') ||
!sessionObj.passport.hasOwnProperty('user') ||
parseInt(sessionObj.passport.user, 10) !== parseInt(uid, 10);
Expand Down Expand Up @@ -128,13 +120,13 @@ module.exports = function (User) {

User.auth.revokeSession = async function (sessionId, uid) {
winston.verbose(`[user.auth] Revoking session ${sessionId} for user ${uid}`);
const sessionObj = await getSessionFromStore(sessionId);
const sessionObj = await db.sessionStoreGet(sessionId);
if (sessionObj && sessionObj.meta && sessionObj.meta.uuid) {
await db.deleteObjectField(`uid:${uid}:sessionUUID:sessionId`, sessionObj.meta.uuid);
}
await Promise.all([
db.sortedSetRemove(`uid:${uid}:sessions`, sessionId),
sessionStoreDestroy(sessionId),
db.sessionStoreDestroy(sessionId),
]);
};

Expand All @@ -159,7 +151,7 @@ module.exports = function (User) {

await Promise.all([
db.deleteAll(sessionKeys.concat(sessionUUIDKeys)),
...sids.map(sid => sessionStoreDestroy(sid)),
...sids.map(sid => db.sessionStoreDestroy(sid)),
]);
}, { batch: 1000 });
};
Expand Down

0 comments on commit 6790000

Please sign in to comment.