In order to minimise the window of opportunity to steal a session, automatically rotate it onto a new session on a frequent basis. This makes a session cookie older than the automatic rollover time less likely to grant access and more likely to be detected. Should a stolen session cookie get rotated while the attacker is using it, the user will be notificed that their session has been taken the next time they open the app if the user re-visits the website before the session is discarded.
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
import {
|
|
readUsers, readSessions, readSubscriptions,
|
|
writeUsers, writeSessions, writeSubscriptions,
|
|
} from "~/server/database";
|
|
import { broadcastEvent, cancelAccountStreams } from "~/server/streams";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const serverSession = await requireServerSessionWithUser(event);
|
|
let users = await readUsers();
|
|
|
|
// Remove sessions for this user
|
|
const removedSessionIds = new Set<number>();
|
|
let sessions = await readSessions();
|
|
sessions = sessions.filter(session => {
|
|
if (session.accountId === serverSession.accountId) {
|
|
removedSessionIds.add(session.id);
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
cancelAccountStreams(serverSession.accountId);
|
|
await writeSessions(sessions);
|
|
await deleteCookie(event, "session");
|
|
|
|
// Remove subscriptions for this user
|
|
let subscriptions = await readSubscriptions();
|
|
subscriptions = subscriptions.filter(
|
|
subscription => !removedSessionIds.has(subscription.sessionId)
|
|
);
|
|
await writeSubscriptions(subscriptions);
|
|
|
|
// Remove the user
|
|
const account = users.find(user => user.id === serverSession.accountId)!;
|
|
const now = new Date().toISOString();
|
|
account.deleted = true;
|
|
account.updatedAt = now;
|
|
await writeUsers(users);
|
|
await broadcastEvent({
|
|
type: "user-update",
|
|
data: {
|
|
id: account.id,
|
|
updatedAt: now,
|
|
deleted: true,
|
|
}
|
|
});
|
|
|
|
// Update Schedule counts.
|
|
await updateScheduleInterestedCounts(users);
|
|
})
|