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

Update error msg on stale request #1503

Merged
merged 3 commits into from Jun 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/receivers/HTTPModuleFunctions.spec.ts
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
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
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