Skip to content

Commit ef08b40

Browse files
committed
fix: use constant-time comparison for HMAC verification (#522)
Replace non-constant-time === operator with crypto.timingSafeEqual() to prevent timing side-channel attacks on HMAC signature verification. Fixes #522
1 parent 73db72d commit ef08b40

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/signature-algorithms.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,17 @@ export class HmacSha1 implements SignatureAlgorithm {
143143
verifier.update(material);
144144
const res = verifier.digest("base64");
145145

146-
return res === signatureValue;
146+
// Use constant-time comparison to prevent timing attacks (CWE-208)
147+
// See: https://github.com/node-saml/xml-crypto/issues/522
148+
try {
149+
return crypto.timingSafeEqual(
150+
Buffer.from(res, "base64"),
151+
Buffer.from(signatureValue, "base64"),
152+
);
153+
} catch (e) {
154+
// timingSafeEqual throws if buffer lengths don't match
155+
return false;
156+
}
147157
},
148158
);
149159

0 commit comments

Comments
 (0)