@@ -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