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.
25 lines
688 B
TypeScript
25 lines
688 B
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
import { readUsers } from "~/server/database";
|
|
import { cancelSessionStreams } from "~/server/streams";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const session = await getServerSession(event, true);
|
|
if (session) {
|
|
const users = await readUsers();
|
|
const account = users.find(user => user.id === session.accountId);
|
|
if (account?.type === "anonymous") {
|
|
throw createError({
|
|
status: 409,
|
|
message: "Cannot log out of an anonymous account",
|
|
});
|
|
}
|
|
}
|
|
|
|
if (session) {
|
|
cancelSessionStreams(session.id);
|
|
}
|
|
await clearServerSession(event);
|
|
})
|