Skip to content

Commit 6fe1d1c

Browse files
committed
feat(slack): add file attachment support to slack webhook trigger
1 parent c0b22a6 commit 6fe1d1c

File tree

2 files changed

+205
-71
lines changed

2 files changed

+205
-71
lines changed

apps/sim/lib/webhooks/utils.server.ts

Lines changed: 134 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,113 @@ export async function validateTwilioSignature(
527527
}
528528
}
529529

530+
const SLACK_FILE_HOSTS = new Set(['files.slack.com', 'files-pri.slack.com'])
531+
const SLACK_MAX_FILE_SIZE = 50 * 1024 * 1024 // 50 MB
532+
const SLACK_MAX_FILES = 10
533+
534+
/**
535+
* Downloads file attachments from Slack using the bot token.
536+
* Returns files in the format expected by WebhookAttachmentProcessor:
537+
* { name, data (base64 string), mimeType, size }
538+
*
539+
* Security:
540+
* - Validates each url_private against allowlisted Slack file hosts
541+
* - Uses validateUrlWithDNS + secureFetchWithPinnedIP to prevent SSRF
542+
* - Enforces per-file size limit and max file count
543+
*/
544+
async function downloadSlackFiles(
545+
rawFiles: any[],
546+
botToken: string
547+
): Promise<Array<{ name: string; data: string; mimeType: string; size: number }>> {
548+
const filesToProcess = rawFiles.slice(0, SLACK_MAX_FILES)
549+
const downloaded: Array<{ name: string; data: string; mimeType: string; size: number }> = []
550+
551+
for (const file of filesToProcess) {
552+
const urlPrivate = file.url_private as string | undefined
553+
if (!urlPrivate) {
554+
continue
555+
}
556+
557+
// Validate the URL points to a known Slack file host
558+
let parsedUrl: URL
559+
try {
560+
parsedUrl = new URL(urlPrivate)
561+
} catch {
562+
logger.warn('Slack file has invalid url_private, skipping', { fileId: file.id })
563+
continue
564+
}
565+
566+
if (!SLACK_FILE_HOSTS.has(parsedUrl.hostname)) {
567+
logger.warn('Slack file url_private points to unexpected host, skipping', {
568+
fileId: file.id,
569+
hostname: sanitizeUrlForLog(urlPrivate),
570+
})
571+
continue
572+
}
573+
574+
// Skip files that exceed the size limit
575+
const reportedSize = Number(file.size) || 0
576+
if (reportedSize > SLACK_MAX_FILE_SIZE) {
577+
logger.warn('Slack file exceeds size limit, skipping', {
578+
fileId: file.id,
579+
size: reportedSize,
580+
limit: SLACK_MAX_FILE_SIZE,
581+
})
582+
continue
583+
}
584+
585+
try {
586+
const urlValidation = await validateUrlWithDNS(urlPrivate, 'url_private')
587+
if (!urlValidation.isValid) {
588+
logger.warn('Slack file url_private failed DNS validation, skipping', {
589+
fileId: file.id,
590+
error: urlValidation.error,
591+
})
592+
continue
593+
}
594+
595+
const response = await secureFetchWithPinnedIP(urlPrivate, urlValidation.resolvedIP!, {
596+
headers: { Authorization: `Bearer ${botToken}` },
597+
})
598+
599+
if (!response.ok) {
600+
logger.warn('Failed to download Slack file, skipping', {
601+
fileId: file.id,
602+
status: response.status,
603+
})
604+
continue
605+
}
606+
607+
const arrayBuffer = await response.arrayBuffer()
608+
const buffer = Buffer.from(arrayBuffer)
609+
610+
// Verify the actual downloaded size doesn't exceed our limit
611+
if (buffer.length > SLACK_MAX_FILE_SIZE) {
612+
logger.warn('Downloaded Slack file exceeds size limit, skipping', {
613+
fileId: file.id,
614+
actualSize: buffer.length,
615+
limit: SLACK_MAX_FILE_SIZE,
616+
})
617+
continue
618+
}
619+
620+
downloaded.push({
621+
name: file.name || 'download',
622+
data: buffer.toString('base64'),
623+
mimeType: file.mimetype || 'application/octet-stream',
624+
size: buffer.length,
625+
})
626+
} catch (error) {
627+
logger.error('Error downloading Slack file, skipping', {
628+
fileId: file.id,
629+
error: error instanceof Error ? error.message : String(error),
630+
})
631+
}
632+
}
633+
634+
return downloaded
635+
}
636+
530637
/**
531638
* Format webhook input based on provider
532639
*/
@@ -788,42 +895,42 @@ export async function formatWebhookInput(
788895

789896
if (foundWebhook.provider === 'slack') {
790897
const event = body?.event
898+
const providerConfig = (foundWebhook.providerConfig as Record<string, any>) || {}
899+
const botToken = providerConfig.botToken as string | undefined
900+
const includeFiles = Boolean(providerConfig.includeFiles)
791901

792-
if (event && body?.type === 'event_callback') {
793-
return {
794-
event: {
795-
event_type: event.type || '',
796-
channel: event.channel || '',
797-
channel_name: '',
798-
user: event.user || '',
799-
user_name: '',
800-
text: event.text || '',
801-
timestamp: event.ts || event.event_ts || '',
802-
thread_ts: event.thread_ts || '',
803-
team_id: body.team_id || event.team || '',
804-
event_id: body.event_id || '',
805-
},
806-
}
902+
const rawEvent = event && body?.type === 'event_callback' ? event : body?.event
903+
904+
if (!rawEvent && body?.type !== 'event_callback') {
905+
logger.warn('Unknown Slack event type', {
906+
type: body?.type,
907+
hasEvent: !!body?.event,
908+
bodyKeys: Object.keys(body || {}),
909+
})
807910
}
808911

809-
logger.warn('Unknown Slack event type', {
810-
type: body?.type,
811-
hasEvent: !!body?.event,
812-
bodyKeys: Object.keys(body || {}),
813-
})
912+
const rawFiles: any[] = rawEvent?.files ?? []
913+
const hasFiles = rawFiles.length > 0
914+
915+
let files: any[] = []
916+
if (hasFiles && includeFiles && botToken) {
917+
files = await downloadSlackFiles(rawFiles, botToken)
918+
}
814919

815920
return {
816921
event: {
817-
event_type: body?.event?.type || body?.type || 'unknown',
818-
channel: body?.event?.channel || '',
922+
event_type: rawEvent?.type || body?.type || 'unknown',
923+
channel: rawEvent?.channel || '',
819924
channel_name: '',
820-
user: body?.event?.user || '',
925+
user: rawEvent?.user || '',
821926
user_name: '',
822-
text: body?.event?.text || '',
823-
timestamp: body?.event?.ts || '',
824-
thread_ts: body?.event?.thread_ts || '',
825-
team_id: body?.team_id || '',
927+
text: rawEvent?.text || '',
928+
timestamp: rawEvent?.ts || rawEvent?.event_ts || '',
929+
thread_ts: rawEvent?.thread_ts || '',
930+
team_id: body?.team_id || rawEvent?.team || '',
826931
event_id: body?.event_id || '',
932+
hasFiles,
933+
files,
827934
},
828935
}
829936
}

apps/sim/triggers/slack/webhook.ts

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ export const slackWebhookTrigger: TriggerConfig = {
3030
required: true,
3131
mode: 'trigger',
3232
},
33+
{
34+
id: 'botToken',
35+
title: 'Bot Token',
36+
type: 'short-input',
37+
placeholder: 'xoxb-...',
38+
description:
39+
'The bot token from your Slack app. Required for downloading files attached to messages.',
40+
password: true,
41+
required: false,
42+
mode: 'trigger',
43+
},
44+
{
45+
id: 'includeFiles',
46+
title: 'Include File Attachments',
47+
type: 'switch',
48+
defaultValue: false,
49+
description:
50+
'Download and include file attachments from messages. Requires a bot token with files:read scope.',
51+
required: false,
52+
mode: 'trigger',
53+
},
3354
{
3455
id: 'triggerSave',
3556
title: '',
@@ -46,9 +67,10 @@ export const slackWebhookTrigger: TriggerConfig = {
4667
'Go to <a href="https://api.slack.com/apps" target="_blank" rel="noopener noreferrer" class="text-muted-foreground underline transition-colors hover:text-muted-foreground/80">Slack Apps page</a>',
4768
'If you don\'t have an app:<br><ul class="mt-1 ml-5 list-disc"><li>Create an app from scratch</li><li>Give it a name and select your workspace</li></ul>',
4869
'Go to "Basic Information", find the "Signing Secret", and paste it in the field above.',
49-
'Go to "OAuth & Permissions" and add bot token scopes:<br><ul class="mt-1 ml-5 list-disc"><li><code>app_mentions:read</code> - For viewing messages that tag your bot with an @</li><li><code>chat:write</code> - To send messages to channels your bot is a part of</li></ul>',
70+
'Go to "OAuth & Permissions" and add bot token scopes:<br><ul class="mt-1 ml-5 list-disc"><li><code>app_mentions:read</code> - For viewing messages that tag your bot with an @</li><li><code>chat:write</code> - To send messages to channels your bot is a part of</li><li><code>files:read</code> - To access files and images shared in messages</li></ul>',
5071
'Go to "Event Subscriptions":<br><ul class="mt-1 ml-5 list-disc"><li>Enable events</li><li>Under "Subscribe to Bot Events", add <code>app_mention</code> to listen to messages that mention your bot</li><li>Paste the Webhook URL above into the "Request URL" field</li></ul>',
5172
'Go to "Install App" in the left sidebar and install the app into your desired Slack workspace and channel.',
73+
'Copy the "Bot User OAuth Token" (starts with <code>xoxb-</code>) and paste it in the Bot Token field above to enable file downloads.',
5274
'Save changes in both Slack and here.',
5375
]
5476
.map(
@@ -63,49 +85,54 @@ export const slackWebhookTrigger: TriggerConfig = {
6385

6486
outputs: {
6587
event: {
66-
type: 'object',
67-
description: 'Slack event data',
68-
properties: {
69-
event_type: {
70-
type: 'string',
71-
description: 'Type of Slack event (e.g., app_mention, message)',
72-
},
73-
channel: {
74-
type: 'string',
75-
description: 'Slack channel ID where the event occurred',
76-
},
77-
channel_name: {
78-
type: 'string',
79-
description: 'Human-readable channel name',
80-
},
81-
user: {
82-
type: 'string',
83-
description: 'User ID who triggered the event',
84-
},
85-
user_name: {
86-
type: 'string',
87-
description: 'Username who triggered the event',
88-
},
89-
text: {
90-
type: 'string',
91-
description: 'Message text content',
92-
},
93-
timestamp: {
94-
type: 'string',
95-
description: 'Message timestamp from the triggering event',
96-
},
97-
thread_ts: {
98-
type: 'string',
99-
description: 'Parent thread timestamp (if message is in a thread)',
100-
},
101-
team_id: {
102-
type: 'string',
103-
description: 'Slack workspace/team ID',
104-
},
105-
event_id: {
106-
type: 'string',
107-
description: 'Unique event identifier',
108-
},
88+
event_type: {
89+
type: 'string',
90+
description: 'Type of Slack event (e.g., app_mention, message)',
91+
},
92+
channel: {
93+
type: 'string',
94+
description: 'Slack channel ID where the event occurred',
95+
},
96+
channel_name: {
97+
type: 'string',
98+
description: 'Human-readable channel name',
99+
},
100+
user: {
101+
type: 'string',
102+
description: 'User ID who triggered the event',
103+
},
104+
user_name: {
105+
type: 'string',
106+
description: 'Username who triggered the event',
107+
},
108+
text: {
109+
type: 'string',
110+
description: 'Message text content',
111+
},
112+
timestamp: {
113+
type: 'string',
114+
description: 'Message timestamp from the triggering event',
115+
},
116+
thread_ts: {
117+
type: 'string',
118+
description: 'Parent thread timestamp (if message is in a thread)',
119+
},
120+
team_id: {
121+
type: 'string',
122+
description: 'Slack workspace/team ID',
123+
},
124+
event_id: {
125+
type: 'string',
126+
description: 'Unique event identifier',
127+
},
128+
hasFiles: {
129+
type: 'boolean',
130+
description: 'Whether the message has file attachments',
131+
},
132+
files: {
133+
type: 'file[]',
134+
description:
135+
'File attachments downloaded from the message (if includeFiles is enabled and bot token is provided)',
109136
},
110137
},
111138
},

0 commit comments

Comments
 (0)