Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion src/lib/server/hosted-ui-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ const hostedUiSessions = new Map<
function constantTimeEquals(left: string, right: string): boolean {
const leftBuffer = Buffer.from(left);
const rightBuffer = Buffer.from(right);
return leftBuffer.length === rightBuffer.length && timingSafeEqual(leftBuffer, rightBuffer);
// Use the longer length to prevent timing leak on length comparison
const maxLen = Math.max(leftBuffer.length, rightBuffer.length);
const leftPadded = Buffer.alloc(maxLen, 0);
const rightPadded = Buffer.alloc(maxLen, 0);
leftBuffer.copy(leftPadded);
rightBuffer.copy(rightPadded);
return timingSafeEqual(leftPadded, rightPadded) && leftBuffer.length === rightBuffer.length;
}

function hashSessionId(sessionId: string): string {
Expand Down
13 changes: 7 additions & 6 deletions src/lib/server/mcp-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const rateLimitBuckets = new Map<string, number[]>();
function constantTimeEquals(left: string, right: string): boolean {
const leftBuffer = Buffer.from(left);
const rightBuffer = Buffer.from(right);

if (leftBuffer.length !== rightBuffer.length) {
return false;
}

return timingSafeEqual(leftBuffer, rightBuffer);
// Use the longer length to prevent timing leak on length comparison
const maxLen = Math.max(leftBuffer.length, rightBuffer.length);
const leftPadded = Buffer.alloc(maxLen, 0);
const rightPadded = Buffer.alloc(maxLen, 0);
leftBuffer.copy(leftPadded);
rightBuffer.copy(rightPadded);
return timingSafeEqual(leftPadded, rightPadded) && leftBuffer.length === rightBuffer.length;
}

interface ApiKeyExtractionOptions {
Expand Down