forked from slackapi/bolt-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix slackapi#192 ExpressReceiver support for rawBody for signature ve…
…rification
- Loading branch information
Showing
2 changed files
with
98 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { RequestHandler } from 'express'; | ||
import rawBody from 'raw-body'; | ||
import crypto from 'crypto'; | ||
import tsscmp from 'tsscmp'; | ||
import { ErrorCode, errorWithCode } from './errors'; | ||
|
||
export default class ExpressSignatureVerifier { | ||
|
||
public static create(signingSecret: string): RequestHandler { | ||
return async (req, _res, next) => { | ||
try { | ||
let stringBody: string; | ||
// On some environments like GCP (Google Cloud Platform), | ||
// req.body can be pre-parsed and be passed as req.rawBody here | ||
const preparsedRawBody: any = (req as any).rawBody; | ||
if (preparsedRawBody) { | ||
stringBody = preparsedRawBody.toString(); | ||
} else { | ||
stringBody = (await rawBody(req)).toString(); | ||
} | ||
|
||
const signature = req.headers['x-slack-signature'] as string; | ||
const ts = Number(req.headers['x-slack-request-timestamp']); | ||
if (!signature && !ts) { | ||
const error = errorWithCode( | ||
'Slack request signing verification failed. Some headers are missing.', | ||
ErrorCode.ExpressReceiverAuthenticityError, | ||
); | ||
return next(error); | ||
} | ||
|
||
// Divide current date to match Slack ts format | ||
// Subtract 5 minutes from current time | ||
const fiveMinutesAgo = Math.floor(Date.now() / 1000) - (60 * 5); | ||
|
||
if (ts < fiveMinutesAgo) { | ||
const error = errorWithCode( | ||
'Slack request signing verification failed. Timestamp is too old.', | ||
ErrorCode.ExpressReceiverAuthenticityError, | ||
); | ||
return next(error); | ||
} | ||
|
||
const hmac = crypto.createHmac('sha256', signingSecret); | ||
const [version, hash] = signature.split('='); | ||
hmac.update(`${version}:${ts}:${stringBody}`); | ||
|
||
if (!tsscmp(hash, hmac.digest('hex'))) { | ||
const error = errorWithCode( | ||
'Slack request signing verification failed. Signature mismatch.', | ||
ErrorCode.ExpressReceiverAuthenticityError, | ||
); | ||
return next(error); | ||
} | ||
|
||
// Verification passed, assign string body back to request and resume | ||
req.body = stringBody; | ||
|
||
next(); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
} | ||
|
||
} |