Skip to content
Draft
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
20 changes: 16 additions & 4 deletions lib/utils/razorpay-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,23 @@ function validateWebhookSignature (body, signature, secret) {

body = body.toString();

var expectedSignature = crypto.createHmac('sha256', secret)
.update(body)
.digest('hex');
// Compute HMAC as raw bytes (32 bytes for SHA-256)
var expectedBuffer = crypto.createHmac('sha256', secret)
.update(body)
.digest();

// Decode incoming hex signature to raw bytes
// Note: Buffer.from(hex) returns empty/partial Buffer on invalid hex (no throw),
// which is caught by the length check below
var signatureBuffer = Buffer.from(signature, 'hex');

// Both should be 32 bytes (SHA-256 output)
if (expectedBuffer.length !== signatureBuffer.length) {
return false;
}

return expectedSignature === signature;
// Constant-time comparison to prevent timing attacks
return crypto.timingSafeEqual(expectedBuffer, signatureBuffer);
}

function validatePaymentVerification(params={}, signature, secret){
Expand Down
Loading