diff --git a/apps/sim/lib/internal/slack/file-download.ts b/apps/sim/lib/internal/slack/file-download.ts index 8dceb28a280..cd2888ac17f 100644 --- a/apps/sim/lib/internal/slack/file-download.ts +++ b/apps/sim/lib/internal/slack/file-download.ts @@ -9,8 +9,7 @@ const SLACK_FILE_ORIGIN = 'https://files.slack.com' * external, or any URL off Slack's file host, never receives a token. */ export function slackFileDownloadUrl(file: Record): URL | undefined { - if (file.is_external === true || file.mode === 'external' || file.filetype === 'remote') - return undefined + if (isExternalSlackFile(file)) return undefined const candidate = [file.url_private_download, file.url_private].find( (value): value is string => typeof value === 'string' && value.length > 0 ) @@ -18,6 +17,11 @@ export function slackFileDownloadUrl(file: Record): URL | undef return url?.origin === SLACK_FILE_ORIGIN ? url : undefined } +/** Whether a file was shared from another app, whose URL is that app's own host. */ +export function isExternalSlackFile(file: Record): boolean { + return file.is_external === true || file.mode === 'external' || file.filetype === 'remote' +} + /** * Whether a redirect stays on Slack's file host. A hop elsewhere, such as the workspace sign-in * page Slack sends a token it will not serve, must be refused rather than read as the file. diff --git a/apps/sim/lib/internal/slack/operations.test.ts b/apps/sim/lib/internal/slack/operations.test.ts index b50a739257f..976b3b9c815 100644 --- a/apps/sim/lib/internal/slack/operations.test.ts +++ b/apps/sim/lib/internal/slack/operations.test.ts @@ -108,7 +108,7 @@ describe('Slack operations', () => { file: { name: 'plan.pdf', is_external: true, - url_private: 'https://files.example.com/plan.pdf', + url_private: 'https://files.slack.com/files-pri/T1-F1/plan.pdf', }, }) ) diff --git a/apps/sim/lib/sim-search/live/slack.test.ts b/apps/sim/lib/sim-search/live/slack.test.ts index cf6041f3b8d..9eee86263b6 100644 --- a/apps/sim/lib/sim-search/live/slack.test.ts +++ b/apps/sim/lib/sim-search/live/slack.test.ts @@ -94,13 +94,46 @@ describe('Slack file reads', () => { expect(api.bytes).not.toHaveBeenCalled() } }) - it('keeps the preview, with the reason Slack gave, when Slack will not serve the file', async () => { + it('keeps the preview when Slack will not serve the file, without suggesting a reconnect', async () => { + const tooLarge = fileClient({ mimetype: 'application/pdf' }, async () => { + throw new NativeSearchError('unavailable', 'This file exceeds the 4 MiB live-read limit.') + }) + const large = await readSlack(tooLarge, 'F123', undefined, 'file') + expect(large.content).toContain( + 'Full text unavailable: This file exceeds the 4 MiB live-read limit.' + ) + expect(large.content).toContain('Short preview') const denied = fileClient({ mimetype: 'text/plain' }, async () => { - throw new NativeSearchError('reconnect', 'The provider denied access.', undefined, 403) + throw new NativeSearchError( + 'reconnect', + 'The provider denied access. Reconnect your account with search and read permissions.', + undefined, + 403 + ) }) const read = await readSlack(denied, 'F123', undefined, 'file') - expect(read.content).toContain('Full text unavailable: The provider denied access.') + expect(read.content).toContain( + "Full text unavailable: Slack did not serve this file's contents" + ) + expect(read.content).not.toContain('Reconnect') expect(read.content).toContain('Short preview') + const throttled = fileClient({ mimetype: 'text/plain' }, async () => { + throw new NativeSearchError( + 'rate_limited', + 'Provider rate limit reached. Try again later.', + 30, + 403 + ) + }) + expect((await readSlack(throttled, 'F123', undefined, 'file')).content).toContain( + 'Full text unavailable: Provider rate limit reached. Try again later.' + ) + const relink = fileClient({ mimetype: 'text/plain' }, async () => { + throw new NativeSearchError('reconnect', 'Reconnect this provider to restore its API host.') + }) + expect((await readSlack(relink, 'F123', undefined, 'file')).content).toContain( + 'Full text unavailable: Reconnect this provider to restore its API host.' + ) for (const failure of [ new Error('Too many redirects (max: 0)'), new DOMException('The operation timed out.', 'TimeoutError'), diff --git a/apps/sim/lib/sim-search/live/slack.ts b/apps/sim/lib/sim-search/live/slack.ts index 44a99ca314b..c8502dd83f6 100644 --- a/apps/sim/lib/sim-search/live/slack.ts +++ b/apps/sim/lib/sim-search/live/slack.ts @@ -392,7 +392,7 @@ function decodeFileText(bytes: Buffer, mimetype: string): string | undefined { * The full text of a file Slack hosts: canvases as HTML, snippets and text files as text, PDF and * DOCX extracted, from Slack's download URL when it has one. The token only goes to Slack's file * host. Short of the caller aborting, any failure (denial, redirect, size limit, unreadable bytes, - * slow download) leaves the read with its preview, plus the provider's reason when it gave one. + * slow download) leaves the read with its preview and a reason when one applies. */ async function slackFileText( client: NativeClient, @@ -416,7 +416,14 @@ async function slackFileText( return text && Buffer.byteLength(text, 'utf8') <= MAX_FILE_TEXT_BYTES ? { text } : {} } catch (error) { signal?.throwIfAborted() - return error instanceof NativeSearchError ? { unavailable: error.message } : {} + if (!(error instanceof NativeSearchError)) return {} + // files.info already accepted this token, so reconnecting would not change a refused file. + return { + unavailable: + error.status === 'reconnect' && (error.httpStatus === 401 || error.httpStatus === 403) + ? "Slack did not serve this file's contents to your account. Open the source." + : error.message, + } } } diff --git a/apps/sim/lib/webhooks/providers/slack.test.ts b/apps/sim/lib/webhooks/providers/slack.test.ts index 53154037766..934160ba1e5 100644 --- a/apps/sim/lib/webhooks/providers/slack.test.ts +++ b/apps/sim/lib/webhooks/providers/slack.test.ts @@ -6,7 +6,7 @@ import { inputValidationMockFns, } from '@sim/testing/mocks/input-validation.mock' import { schemaMock } from '@sim/testing/mocks/schema.mock' -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { OutboundRoutingError } from '@/lib/core/network/routing' import { assertSlackFileHost } from '@/lib/internal/slack/file-download' import { @@ -18,6 +18,8 @@ import { vi.mock('@/lib/oauth/credential-service', () => authOAuthUtilsMock) vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) +const { mockOutboundFetch } = vi.hoisted(() => ({ mockOutboundFetch: vi.fn() })) +vi.mock('@/lib/core/network/outbound-fetch.server', () => ({ outboundFetch: mockOutboundFetch })) const ctx = (body: unknown) => ({ webhook: {}, @@ -204,6 +206,17 @@ describe('slackHandler formatInput - native app bot token', () => { }) describe('slackHandler formatInput - file downloads', () => { + beforeEach(() => { + inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({ + isValid: true, + resolvedIP: '93.184.216.34', + originalHostname: 'files.slack.com', + }) + inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValue( + new Response(Buffer.from('%PDF'), { status: 200 }) + ) + }) + async function formatWithFiles(files: Record[]) { const formatInput = slackHandler.formatInput if (!formatInput) throw new Error('Slack handler has no formatInput') @@ -216,15 +229,6 @@ describe('slackHandler formatInput - file downloads', () => { } it('downloads from the Slack download URL and keeps redirects on the file host', async () => { - inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({ - isValid: true, - resolvedIP: '93.184.216.34', - originalHostname: 'files.slack.com', - }) - inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValue( - new Response(Buffer.from('%PDF'), { status: 200 }) - ) - const { input } = await formatWithFiles([ { id: 'F1', @@ -242,16 +246,45 @@ describe('slackHandler formatInput - file downloads', () => { expect(eventOf(input).files).toHaveLength(1) }) + it("looks up the download URL when the event carries only the file's private URL", async () => { + mockOutboundFetch.mockResolvedValueOnce( + Response.json({ + ok: true, + file: { + id: 'F3', + url_private: 'https://files.slack.com/files-pri/T1-F3/report.pdf', + url_private_download: 'https://files.slack.com/files-pri/T1-F3/download/report.pdf', + }, + }) + ) + + const { input } = await formatWithFiles([ + { + id: 'F3', + name: 'report.pdf', + mimetype: 'application/pdf', + size: 4, + url_private: 'https://files.slack.com/files-pri/T1-F3/report.pdf', + }, + ]) + + expect(inputValidationMockFns.mockSecureFetchWithPinnedIP.mock.lastCall?.[0]).toBe( + 'https://files.slack.com/files-pri/T1-F3/download/report.pdf' + ) + expect(eventOf(input).files).toHaveLength(1) + }) + it('never sends the bot token to a file hosted outside Slack', async () => { const { input } = await formatWithFiles([ { id: 'F2', name: 'plan.pdf', is_external: true, - url_private: 'https://files.example.com/plan.pdf', + url_private: 'https://files.slack.com/files-pri/T1-F2/plan.pdf', }, ]) + expect(mockOutboundFetch).not.toHaveBeenCalled() expect(inputValidationMockFns.mockSecureFetchWithPinnedIP).not.toHaveBeenCalled() expect(eventOf(input).files).toEqual([]) }) diff --git a/apps/sim/lib/webhooks/providers/slack.ts b/apps/sim/lib/webhooks/providers/slack.ts index d87c5fbf131..0c8299d9124 100644 --- a/apps/sim/lib/webhooks/providers/slack.ts +++ b/apps/sim/lib/webhooks/providers/slack.ts @@ -15,7 +15,11 @@ import { secureFetchWithPinnedIP, validateUrlWithDNS, } from '@/lib/core/security/input-validation.server' -import { assertSlackFileHost, slackFileDownloadUrl } from '@/lib/internal/slack/file-download' +import { + assertSlackFileHost, + isExternalSlackFile, + slackFileDownloadUrl, +} from '@/lib/internal/slack/file-download' import { getSlackBotCredential, refreshAccessTokenIfNeeded, @@ -322,7 +326,7 @@ async function downloadSlackFiles( for (const file of filesToProcess) { let f = file as Record - if (!f.url_private && !f.url_private_download && f.id) { + if (!f.url_private_download && f.id && !isExternalSlackFile(f)) { const resolved = await resolveSlackFileInfo(f.id as string, botToken) if (resolved) f = { ...f, ...resolved } }