Skip to content

Commit 4a417ad

Browse files
fix(webhook): avoid timingSafeEqual range error by checking buffer lengths
1 parent 7a1689c commit 4a417ad

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

functions/slack/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const formatSlackMessage = (query, response) => {
9090
*
9191
* This function follows the official Slack verification process:
9292
* https://api.slack.com/authentication/verifying-requests-from-slack
93-
*
93+
*
9494
* @param {object} req Cloud Function request object.
9595
* @param {string} req.headers Headers Slack SDK uses to authenticate request.
9696
* @param {string} req.rawBody Raw body of webhook request to check signature against.
@@ -118,13 +118,18 @@ const verifyWebhook = req => {
118118
hmac.update(basestring, 'utf-8');
119119
const digest = `v0=${hmac.digest('hex')}`;
120120

121+
// Convert digest and signature to Buffers for secure comparison
122+
const digestBuf = Buffer.from(digest, 'utf-8');
123+
const sigBuf = Buffer.from(requestSignature, 'utf-8');
124+
125+
if (digestBuf.length !== sigBuf.length) {
126+
const error = new Error('Invalid Slack signature (length mismatch)');
127+
error.code = 401;
128+
throw error;
129+
}
130+
121131
// Perform a constant-time comparison to prevent timing attacks
122-
if (
123-
!crypto.timingSafeEqual(
124-
Buffer.from(digest, 'utf-8'),
125-
Buffer.from(requestSignature, 'utf8')
126-
)
127-
) {
132+
if (!crypto.timingSafeEqual(digestBuf, sigBuf)) {
128133
const error = new Error('Invalid Slack signature');
129134
error.code = 401;
130135
throw error;

0 commit comments

Comments
 (0)