-
Notifications
You must be signed in to change notification settings - Fork 430
feat(HTTPReceiver): add invalidRequestSignatureHandler callback #2827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mvanhorn
wants to merge
9
commits into
slackapi:main
Choose a base branch
from
mvanhorn:osc/2156-invalid-sig-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3ba7a2b
feat(HTTPReceiver): add invalidRequestSignatureHandler callback
mvanhorn 4aa2f01
test(HTTPReceiver): add tests for invalidRequestSignatureHandler
mvanhorn c2bdaf9
Merge branch 'main' into osc/2156-invalid-sig-handler
zimeg 5c78be9
Merge branch 'main' into osc/2156-invalid-sig-handler
zimeg 137fe26
Merge branch 'main' into osc/2156-invalid-sig-handler
zimeg 6ceb696
chore: changeset
zimeg 3c1f24c
refactor(HTTPReceiver): address review feedback on invalid-signature …
mvanhorn 436ae8d
Merge branch 'main' into osc/2156-invalid-sig-handler
zimeg 468f0bd
refactor(HTTPReceiver): drop logger from invalidRequestSignatureHandl…
mvanhorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,21 @@ | ||
| --- | ||
| "@slack/bolt": minor | ||
| --- | ||
|
|
||
| feat(HTTPReceiver): add invalidRequestSignatureHandler callback | ||
|
|
||
| Details of a failed request can be parsed and logged with the customized `invalidRequestSignatureHandler` callback for the `HTTPReceiver` receiver: | ||
|
|
||
| ```javascript | ||
| import { App, HTTPReceiver } from "@slack/bolt"; | ||
|
|
||
| const app = new App({ | ||
| token: process.env.SLACK_BOT_TOKEN, | ||
| receiver: new HTTPReceiver({ | ||
| signingSecret: "unexpectedvalue", | ||
| invalidRequestSignatureHandler: (args) => { | ||
| app.logger.warn(args); | ||
| }, | ||
| }), | ||
| }); | ||
| ``` |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -34,6 +34,12 @@ import type { ParamsIncomingMessage } from './ParamsIncomingMessage'; | |
| import { type CustomRoute, type ReceiverRoutes, buildReceiverRoutes } from './custom-routes'; | ||
| import { verifyRedirectOpts } from './verify-redirect-opts'; | ||
|
|
||
| export interface HTTPReceiverInvalidRequestSignatureHandlerArgs { | ||
| rawBody: string; | ||
| signature: string; | ||
| ts: number; | ||
| } | ||
|
|
||
| // Option keys for tls.createServer() and tls.createSecureContext(), exclusive of those for http.createServer() | ||
| const httpsOptionKeys = [ | ||
| 'ALPNProtocols', | ||
|
|
@@ -81,6 +87,16 @@ export interface HTTPReceiverOptions { | |
| logLevel?: LogLevel; | ||
| processBeforeResponse?: boolean; | ||
| signatureVerification?: boolean; | ||
| /** | ||
| * Called when an incoming request fails signature verification. Override to | ||
| * emit custom telemetry, return a specific response body, or suppress the | ||
| * default warn log. The receiver still returns `401 Unauthorized` to the | ||
| * client regardless of what the handler does. | ||
| * | ||
| * Defaults to a handler that logs a warning with the received | ||
| * `x-slack-signature` and `x-slack-request-timestamp` values. | ||
| */ | ||
| invalidRequestSignatureHandler?: (args: HTTPReceiverInvalidRequestSignatureHandlerArgs) => void; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📚 suggestion: While it's not shared in other options right now we might add Let's also surface this in documentation here: 🔗 https://docs.slack.dev/tools/bolt-js/reference#receiver-options |
||
| clientId?: string; | ||
| clientSecret?: string; | ||
| stateSecret?: InstallProviderOptions['stateSecret']; // required when using default stateStore | ||
|
|
@@ -137,6 +153,8 @@ export default class HTTPReceiver implements Receiver { | |
|
|
||
| private signatureVerification: boolean; | ||
|
|
||
| private invalidRequestSignatureHandler: (args: HTTPReceiverInvalidRequestSignatureHandlerArgs) => void; | ||
|
|
||
| private app?: App; | ||
|
|
||
| public requestListener: RequestListener; | ||
|
|
@@ -178,6 +196,7 @@ export default class HTTPReceiver implements Receiver { | |
| logLevel = LogLevel.INFO, | ||
| processBeforeResponse = false, | ||
| signatureVerification = true, | ||
| invalidRequestSignatureHandler, | ||
| clientId = undefined, | ||
| clientSecret = undefined, | ||
| stateSecret = undefined, | ||
|
|
@@ -195,6 +214,8 @@ export default class HTTPReceiver implements Receiver { | |
| this.signingSecret = signingSecret; | ||
| this.processBeforeResponse = processBeforeResponse; | ||
| this.signatureVerification = signatureVerification; | ||
| this.invalidRequestSignatureHandler = | ||
| invalidRequestSignatureHandler ?? this.defaultInvalidRequestSignatureHandler.bind(this); | ||
| this.logger = | ||
| logger ?? | ||
| (() => { | ||
|
|
@@ -447,7 +468,13 @@ export default class HTTPReceiver implements Receiver { | |
| } catch (err) { | ||
| const e = err as Error; | ||
| if (this.signatureVerification) { | ||
| this.logger.warn(`Failed to parse and verify the request data: ${e.message}`); | ||
| const requestWithRawBody = req as IncomingMessage & { rawBody?: string }; | ||
| const rawBody = typeof requestWithRawBody.rawBody === 'string' ? requestWithRawBody.rawBody : ''; | ||
| this.invalidRequestSignatureHandler({ | ||
| rawBody, | ||
| signature: (req.headers['x-slack-signature'] as string) ?? '', | ||
| ts: Number(req.headers['x-slack-request-timestamp']) || 0, | ||
| }); | ||
| } else { | ||
| this.logger.warn(`Failed to parse the request body: ${e.message}`); | ||
| } | ||
|
|
@@ -565,4 +592,12 @@ export default class HTTPReceiver implements Receiver { | |
| installer.handleCallback(req, res, installCallbackOptions).catch(errorHandler); | ||
| } | ||
| } | ||
|
|
||
| private defaultInvalidRequestSignatureHandler(args: HTTPReceiverInvalidRequestSignatureHandlerArgs): void { | ||
| const { signature, ts } = args; | ||
|
|
||
| this.logger.warn( | ||
| `Invalid request signature detected (X-Slack-Signature: ${signature}, X-Slack-Request-Timestamp: ${ts})`, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐣 note: Here are findings of the adjacent implementation:
bolt-js/src/receivers/AwsLambdaReceiver.ts
Lines 51 to 57 in a8b7880
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👁️🗨️ thought: I'd be curious to use default values here and perhaps matching the adjacent interface name, but I'm less confident about the second point...