Files
Kelx/utils/session-store-guard.js
2026-11-30 16:32:37 +00:00

106 lines
4.1 KiB
JavaScript

"use strict";
const SESSION_STORE_WRITE_SKIPPED_SYMBOL = Symbol.for("adpanel:storeWriteSkipped");
const SESSION_STORE_WRITE_SKIPPED_READ_ERROR = "tombstone-read-error";
function createSessionStoreTombstoneGuard({ isSessionSidRevoked, isRevokedSidTombstoneCachedLocally, getSessionDisplayId, logger = console } = {}) {
for (const dependency of [isSessionSidRevoked, isRevokedSidTombstoneCachedLocally, getSessionDisplayId]) {
if (typeof dependency !== "function") throw new TypeError("Session revocation dependencies are required");
}
return function wrapSessionStoreWithTombstoneGuard(store) {
if (!store || typeof store !== "object") return;
const guardMethod = (methodName) => {
const original = store[methodName];
if (typeof original !== "function") return;
store[methodName] = function tombstoneGuardedSessionWrite(...args) {
const sid = String(args[0] ?? "").trim();
const callback = typeof args[args.length - 1] === "function" ? args[args.length - 1] : null;
if (!sid) return original.apply(this, args);
let settled = false;
let reported = false;
const finish = (err, result) => {
if (settled) return;
settled = true;
if (callback) callback(err, result);
};
const discardWrite = () => new Promise((resolve) => {
// adpanel:tombstone-skip-signal - a READ-ERROR skip must be
// visible to the login binder without failing generic store saves.
if (methodName === "set" && !isRevokedSidTombstoneCachedLocally(sid) && args[1] && typeof args[1] === "object") {
try {
Object.defineProperty(args[1], SESSION_STORE_WRITE_SKIPPED_SYMBOL, {
value: SESSION_STORE_WRITE_SKIPPED_READ_ERROR,
enumerable: false,
});
} catch { }
}
let destroyed = false;
const done = (destroyErr) => {
if (destroyed) return;
destroyed = true;
if (destroyErr) logger.warn(`[security] Tombstoned session ${getSessionDisplayId(sid)} re-destroy errored:`, destroyErr?.message || destroyErr);
resolve();
};
try {
const maybeDestroyPromise = store.destroy.call(this, sid, done);
if (maybeDestroyPromise && typeof maybeDestroyPromise.then === "function") {
maybeDestroyPromise.then(() => done()).catch(done);
}
} catch (destroyErr) {
done(destroyErr);
}
});
// adpanel:session-write-revocation-race - a write admitted before
// logout can finish after destroy; revoke it again before completion.
const completeWrite = async (result) => {
if (await isSessionSidRevoked(sid)) {
await discardWrite();
finish(null);
return undefined;
}
finish(null, result);
return result;
};
if (!callback) {
return isSessionSidRevoked(sid).then(async (revoked) => {
if (revoked) {
await discardWrite();
return undefined;
}
const result = await original.apply(this, args);
return completeWrite(result);
});
}
isSessionSidRevoked(sid).then(async (revoked) => {
if (revoked) {
await discardWrite();
finish(null);
return;
}
args[args.length - 1] = function tombstoneGuardedStoreCallback(err, result) {
if (reported) return;
reported = true;
if (err) return finish(err);
completeWrite(result).catch((completionErr) => finish(completionErr));
};
try {
const maybePromise = original.apply(this, args);
if (maybePromise && typeof maybePromise.catch === "function") {
maybePromise.catch(() => { });
}
} catch (err) {
finish(err);
}
}).catch((err) => finish(err));
};
};
guardMethod("set");
guardMethod("touch");
};
}
module.exports = { createSessionStoreTombstoneGuard };