Skip to content

Commit 7abfcd4

Browse files
fix: use constant-time comparison for auth token (CWE-208)
Replace direct === comparison with crypto.timingSafeEqual via SHA-256 digest to prevent timing-based token extraction attacks. - No behavioral change for valid authentication flows - Uses stdlib only (crypto) — no new dependencies Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ee821b0 commit 7abfcd4

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

src/mcp/auth/AuthMiddleware.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
*/
44

55
import { IncomingMessage, ServerResponse } from "http";
6+
import { timingSafeEqual, createHash } from "crypto";
7+
8+
function safeEqual(a: string, b: string): boolean {
9+
try {
10+
const hashA = createHash("sha256").update(a).digest();
11+
const hashB = createHash("sha256").update(b).digest();
12+
return timingSafeEqual(hashA, hashB);
13+
} catch {
14+
return false;
15+
}
16+
}
617

718
export class AuthMiddleware {
819
constructor(private authToken: string) {}
@@ -19,7 +30,7 @@ export class AuthMiddleware {
1930
if (!parsed) {
2031
return false;
2132
}
22-
return parsed.token === this.authToken;
33+
return safeEqual(parsed.token, this.authToken);
2334
}
2435

2536
/**

0 commit comments

Comments
 (0)