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

Fix #971 AwsLambdaReceiver fails to parse event.body if isBase64Encoded is true #972

Merged
merged 4 commits into from
Jun 29, 2021
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
70 changes: 70 additions & 0 deletions src/receivers/AwsLambdaReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,74 @@ describe('AwsLambdaReceiver', function () {
);
assert.equal(response2.statusCode, 200);
});

it('should accept an event containing a base64 encoded body', async () => {
const awsReceiver = new AwsLambdaReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
});
const handler = awsReceiver.toHandler();
const timestamp = Math.floor(Date.now() / 1000);
const body = JSON.stringify({
token: 'fixed-value',
team_id: 'T111',
enterprise_id: 'E111',
api_app_id: 'A111',
event: {
client_msg_id: '977a7fa8-c9b3-4b51-a0b6-3b6c647e2165',
type: 'app_mention',
text: '<@U222> test',
user: 'W111',
ts: '1612879521.002100',
team: 'T111',
channel: 'C111',
event_ts: '1612879521.002100',
},
type: 'event_callback',
event_id: 'Ev111',
event_time: 1612879521,
authorizations: [
{
enterprise_id: 'E111',
team_id: 'T111',
user_id: 'W111',
is_bot: true,
is_enterprise_install: false,
},
],
is_ext_shared_channel: false,
event_context: '1-app_mention-T111-C111',
});
const signature = crypto.createHmac('sha256', 'my-secret').update(`v0:${timestamp}:${body}`).digest('hex');
const awsEvent = {
resource: '/slack/events',
path: '/slack/events',
httpMethod: 'POST',
headers: {
Accept: 'application/json,*/*',
'Content-Type': 'application/json',
Host: 'xxx.execute-api.ap-northeast-1.amazonaws.com',
'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)',
'X-Slack-Request-Timestamp': `${timestamp}`,
'X-Slack-Signature': `v0=${signature}`,
},
multiValueHeaders: {},
queryStringParameters: null,
multiValueQueryStringParameters: null,
pathParameters: null,
stageVariables: null,
requestContext: {},
body: Buffer.from(body).toString('base64'),
isBase64Encoded: true,
};
const response1 = await handler(
awsEvent,
{},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(_error, _result) => {},
);
assert.equal(response1.statusCode, 404);
});
});
});

Expand Down Expand Up @@ -362,9 +430,11 @@ function withNoopWebClient(): Override {
'@slack/web-api': {
WebClient: class {
public token?: string;

constructor(token?: string, _options?: WebClientOptions) {
this.token = token;
}

public auth = {
test: sinon.fake.resolves({
enterprise_id: 'E111',
Expand Down
13 changes: 12 additions & 1 deletion src/receivers/AwsLambdaReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class AwsLambdaReceiver implements Receiver {
): Promise<AwsResponse> => {
this.logger.debug(`AWS event: ${JSON.stringify(awsEvent, null, 2)}`);

const rawBody: string = typeof awsEvent.body === 'undefined' || awsEvent.body == null ? '' : awsEvent.body;
const rawBody = this.getRawBody(awsEvent);

const body: any = this.parseRequestBody(
rawBody,
Expand Down Expand Up @@ -197,6 +197,17 @@ export default class AwsLambdaReceiver implements Receiver {
};
}

// eslint-disable-next-line class-methods-use-this
private getRawBody(awsEvent: AwsEvent): string {
if (typeof awsEvent.body === 'undefined' || awsEvent.body == null) {
return '';
}
if (awsEvent.isBase64Encoded) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return Buffer.from(awsEvent.body, 'base64').toString('ascii');
}
return awsEvent.body;
}

// eslint-disable-next-line class-methods-use-this
private parseRequestBody(stringBody: string, contentType: string | undefined, logger: Logger): any {
if (contentType === 'application/x-www-form-urlencoded') {
Expand Down