Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/lib/server/adminToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { config } from "$lib/server/config";
import type { Session } from "$lib/types/Session";
import { logger } from "./logger";
import { v4 } from "uuid";
import { timingSafeEqual } from "crypto";

/** Constant-time string comparison to prevent timing attacks on admin token. */
function safeCompare(a: string, b: string): boolean {
if (a.length !== b.length) return false;
const bufA = Buffer.from(a);
const bufB = Buffer.from(b);
return timingSafeEqual(bufA, bufB);
}

class AdminTokenManager {
private token = config.ADMIN_TOKEN || v4();
Expand All @@ -19,7 +28,7 @@ class AdminTokenManager {

public checkToken(token: string, sessionId: Session["sessionId"]) {
if (!this.enabled) return false;
if (token === this.token) {
if (safeCompare(token, this.token)) {
logger.info(`[ADMIN] Token validated`);
this.adminSessions.push(sessionId);
this.token = config.ADMIN_TOKEN || v4();
Expand Down