Skip to content
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

Add an optional handler for invalid request signature error #2154

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/receivers/AwsLambdaReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,11 @@ describe('AwsLambdaReceiver', function () {
});

it('should detect invalid signature', async (): Promise<void> => {
const spy = sinon.spy();
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
invalidRequestSignatureHandler: spy,
});
const handler = awsReceiver.toHandler();
const timestamp = Math.floor(Date.now() / 1000);
Expand Down Expand Up @@ -504,6 +506,7 @@ describe('AwsLambdaReceiver', function () {
(_error, _result) => {},
);
assert.equal(response.statusCode, 401);
assert(spy.calledOnce);
});

it('should detect too old request timestamp', async (): Promise<void> => {
Expand Down
28 changes: 26 additions & 2 deletions src/receivers/AwsLambdaReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export interface AwsEvent {

export type AwsCallback = (error?: Error | string | null, result?: any) => void;

export interface ReceiverInvalidRequestSignatureHandlerArgs {
rawBody: string;
signature: string;
ts: number;
awsEvent: AwsEvent;
awsResponse: Promise<AwsResponse>;
}

export interface AwsResponse {
statusCode: number;
headers?: {
Expand Down Expand Up @@ -76,6 +84,7 @@ export interface AwsLambdaReceiverOptions {
* @default noop
*/
customPropertiesExtractor?: (request: AwsEvent) => StringIndexed;
invalidRequestSignatureHandler?: (args: ReceiverInvalidRequestSignatureHandlerArgs) => void;
}

/*
Expand All @@ -95,12 +104,15 @@ export default class AwsLambdaReceiver implements Receiver {

private customPropertiesExtractor: (request: AwsEvent) => StringIndexed;

private invalidRequestSignatureHandler: (args: ReceiverInvalidRequestSignatureHandlerArgs) => void;

public constructor({
signingSecret,
logger = undefined,
logLevel = LogLevel.INFO,
signatureVerification = true,
customPropertiesExtractor = (_) => ({}),
invalidRequestSignatureHandler,
}: AwsLambdaReceiverOptions) {
// Initialize instance variables, substituting defaults for each value
this.signingSecret = signingSecret;
Expand All @@ -112,6 +124,11 @@ export default class AwsLambdaReceiver implements Receiver {
return defaultLogger;
})();
this.customPropertiesExtractor = customPropertiesExtractor;
if (invalidRequestSignatureHandler) {
this.invalidRequestSignatureHandler = invalidRequestSignatureHandler;
} else {
this.invalidRequestSignatureHandler = this.defaultInvalidRequestSignatureHandler;
}
}

public init(app: App): void {
Expand Down Expand Up @@ -171,8 +188,9 @@ export default class AwsLambdaReceiver implements Receiver {
const signature = this.getHeaderValue(awsEvent.headers, 'X-Slack-Signature') as string;
const ts = Number(this.getHeaderValue(awsEvent.headers, 'X-Slack-Request-Timestamp'));
if (!this.isValidRequestSignature(this.signingSecret, rawBody, signature, ts)) {
this.logger.info(`Invalid request signature detected (X-Slack-Signature: ${signature}, X-Slack-Request-Timestamp: ${ts})`);
return Promise.resolve({ statusCode: 401, body: '' });
const awsResponse = Promise.resolve({ statusCode: 401, body: '' });
this.invalidRequestSignatureHandler({ rawBody, signature, ts, awsEvent, awsResponse });
return awsResponse;
}
}

Expand Down Expand Up @@ -313,4 +331,10 @@ export default class AwsLambdaReceiver implements Receiver {
const caseInsensitiveKey = Object.keys(headers).find((it) => key.toLowerCase() === it.toLowerCase());
return caseInsensitiveKey !== undefined ? headers[caseInsensitiveKey] : undefined;
}

private defaultInvalidRequestSignatureHandler(args: ReceiverInvalidRequestSignatureHandlerArgs): void {
const { signature, ts } = args;

this.logger.info(`Invalid request signature detected (X-Slack-Signature: ${signature}, X-Slack-Request-Timestamp: ${ts})`);
}
}