( signingSecret: string, requestSignature: string, requestTimestamp: string, body: string )
| 13 | |
| 14 | // Mock the verification function for testing |
| 15 | function verifySlackSignature( |
| 16 | signingSecret: string, |
| 17 | requestSignature: string, |
| 18 | requestTimestamp: string, |
| 19 | body: string |
| 20 | ): boolean { |
| 21 | // Check timestamp is recent (within 5 minutes) |
| 22 | const timestamp = parseInt(requestTimestamp, 10); |
| 23 | const now = Math.floor(Date.now() / 1000); |
| 24 | if (Math.abs(now - timestamp) > 60 * 5) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | // Create signature base string |
| 29 | const sigBasestring = `v0:${requestTimestamp}:${body}`; |
| 30 | |
| 31 | // Create HMAC signature |
| 32 | const mySignature = 'v0=' + crypto |
| 33 | .createHmac('sha256', signingSecret) |
| 34 | .update(sigBasestring) |
| 35 | .digest('hex'); |
| 36 | |
| 37 | // Compare signatures using timing-safe comparison |
| 38 | try { |
| 39 | return crypto.timingSafeEqual( |
| 40 | Buffer.from(mySignature, 'utf8'), |
| 41 | Buffer.from(requestSignature, 'utf8') |
| 42 | ); |
| 43 | } catch { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | describe("Slack Signature Verification", () => { |
| 49 | const testSecret = "test_signing_secret_abc123"; |
no test coverage detected