Skip to content

Commit 090ed26

Browse files
committed
fix(slack): tolerate out-of-range event timestamps
1 parent 4947a65 commit 090ed26

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

packages/bots/slack/src/index.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { contractTestBot } from '@profullstack/sh1pt-core/testing';
22
import { request, type Server } from 'node:http';
33
import { describe, expect, it } from 'vitest';
4-
import bot, { slackSignature, type FetchLike } from './index.js';
4+
import bot, { slackSignature, slackTimestamp, type FetchLike } from './index.js';
55
import type { BotCtx, BotEvent, BotHandler } from '@profullstack/sh1pt-core';
66

77
contractTestBot(bot, { sampleConfig: {}, sampleChannel: 'C0123456789' });
@@ -75,6 +75,10 @@ function serverPort(server: Server): number {
7575
}
7676

7777
describe('Slack bot adapter', () => {
78+
it('falls back for out-of-range event timestamps', () => {
79+
expect(slackTimestamp('1e20')).toBe('1970-01-01T00:00:00.000Z');
80+
});
81+
7882
it('posts proactive messages with Slack Web API JSON and bearer auth', async () => {
7983
const { calls, fetcher } = captureFetch([{ ok: true, ts: '1700000000.123456' }]);
8084

packages/bots/slack/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,12 @@ function firstHeader(value: string | string[] | undefined): string | undefined {
451451
return Array.isArray(value) ? value[0] : value;
452452
}
453453

454-
function slackTimestamp(value: string | undefined): string {
454+
export function slackTimestamp(value: string | undefined): string {
455455
if (!value) return new Date().toISOString();
456456
const timestamp = Number(value);
457-
return Number.isFinite(timestamp) ? new Date(timestamp * 1000).toISOString() : new Date().toISOString();
457+
if (!Number.isFinite(timestamp)) return new Date().toISOString();
458+
const date = new Date(timestamp * 1000);
459+
return Number.isNaN(date.getTime()) ? new Date(0).toISOString() : date.toISOString();
458460
}
459461

460462
async function readBody(req: IncomingMessage): Promise<string> {

0 commit comments

Comments
 (0)