Skip to content

Commit

Permalink
Update error msg on stale request (#1503)
Browse files Browse the repository at this point in the history
* Update error msg on stale request
Co-authored-by: Kazuhiro Sera <[email protected]>
  • Loading branch information
srajiang authored Jun 24, 2022
1 parent e44a1ff commit 7eec438
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/receivers/HTTPModuleFunctions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('HTTPModuleFunctions', async () => {
try {
await func.parseAndVerifyHTTPRequest({ signingSecret }, req, res);
} catch (e) {
assert.equal((e as any).message, 'Failed to verify authenticity: stale');
assert.equal((e as any).message, 'Failed to verify authenticity: x-slack-request-timestamp must differ from system time by no more than 5 minutes or request is stale');
}
});
it('should detect an invalid signature', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/receivers/verify-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Request verification', async () => {
body: rawBody,
});
} catch (e) {
assert.equal((e as any).message, 'Failed to verify authenticity: stale');
assert.equal((e as any).message, 'Failed to verify authenticity: x-slack-request-timestamp must differ from system time by no more than 5 minutes or request is stale');
}
});
it('should detect an invalid signature', async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/receivers/verify-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ export function verifySlackRequest(options: SlackRequestVerificationOptions): vo

// Calculate time-dependent values
const nowMs = options.nowMilliseconds ?? Date.now();
const fiveMinutesAgoSec = Math.floor(nowMs / 1000) - 60 * 5;
const requestTimestampMaxDeltaMin = 5;
const fiveMinutesAgoSec = Math.floor(nowMs / 1000) - 60 * requestTimestampMaxDeltaMin;

// Enforce verification rules

// Rule 1: Check staleness
if (requestTimestampSec < fiveMinutesAgoSec) {
throw new Error(`${verifyErrorPrefix}: stale`);
throw new Error(`${verifyErrorPrefix}: x-slack-request-timestamp must differ from system time by no more than ${requestTimestampMaxDeltaMin
} minutes or request is stale`);
}

// Rule 2: Check signature
Expand Down

0 comments on commit 7eec438

Please sign in to comment.