Skip to content

Commit be3741a

Browse files
authored
fix(slack): keep refused file reads from suggesting a reconnect and find the download URL for trigger files (#10095)
* fix(slack): keep refused file reads from suggesting a reconnect and find the download URL for trigger files * fix(slack): keep a throttled file download's retry reason
1 parent b525e27 commit be3741a

6 files changed

Lines changed: 102 additions & 21 deletions

File tree

‎apps/sim/lib/internal/slack/file-download.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ const SLACK_FILE_ORIGIN = 'https://files.slack.com'
99
* external, or any URL off Slack's file host, never receives a token.
1010
*/
1111
export function slackFileDownloadUrl(file: Record<string, unknown>): URL | undefined {
12-
if (file.is_external === true || file.mode === 'external' || file.filetype === 'remote')
13-
return undefined
12+
if (isExternalSlackFile(file)) return undefined
1413
const candidate = [file.url_private_download, file.url_private].find(
1514
(value): value is string => typeof value === 'string' && value.length > 0
1615
)
1716
const url = candidate ? URL.parse(candidate) : null
1817
return url?.origin === SLACK_FILE_ORIGIN ? url : undefined
1918
}
2019

20+
/** Whether a file was shared from another app, whose URL is that app's own host. */
21+
export function isExternalSlackFile(file: Record<string, unknown>): boolean {
22+
return file.is_external === true || file.mode === 'external' || file.filetype === 'remote'
23+
}
24+
2125
/**
2226
* Whether a redirect stays on Slack's file host. A hop elsewhere, such as the workspace sign-in
2327
* page Slack sends a token it will not serve, must be refused rather than read as the file.

‎apps/sim/lib/internal/slack/operations.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('Slack operations', () => {
108108
file: {
109109
name: 'plan.pdf',
110110
is_external: true,
111-
url_private: 'https://files.example.com/plan.pdf',
111+
url_private: 'https://files.slack.com/files-pri/T1-F1/plan.pdf',
112112
},
113113
})
114114
)

‎apps/sim/lib/sim-search/live/slack.test.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,46 @@ describe('Slack file reads', () => {
9494
expect(api.bytes).not.toHaveBeenCalled()
9595
}
9696
})
97-
it('keeps the preview, with the reason Slack gave, when Slack will not serve the file', async () => {
97+
it('keeps the preview when Slack will not serve the file, without suggesting a reconnect', async () => {
98+
const tooLarge = fileClient({ mimetype: 'application/pdf' }, async () => {
99+
throw new NativeSearchError('unavailable', 'This file exceeds the 4 MiB live-read limit.')
100+
})
101+
const large = await readSlack(tooLarge, 'F123', undefined, 'file')
102+
expect(large.content).toContain(
103+
'Full text unavailable: This file exceeds the 4 MiB live-read limit.'
104+
)
105+
expect(large.content).toContain('Short preview')
98106
const denied = fileClient({ mimetype: 'text/plain' }, async () => {
99-
throw new NativeSearchError('reconnect', 'The provider denied access.', undefined, 403)
107+
throw new NativeSearchError(
108+
'reconnect',
109+
'The provider denied access. Reconnect your account with search and read permissions.',
110+
undefined,
111+
403
112+
)
100113
})
101114
const read = await readSlack(denied, 'F123', undefined, 'file')
102-
expect(read.content).toContain('Full text unavailable: The provider denied access.')
115+
expect(read.content).toContain(
116+
"Full text unavailable: Slack did not serve this file's contents"
117+
)
118+
expect(read.content).not.toContain('Reconnect')
103119
expect(read.content).toContain('Short preview')
120+
const throttled = fileClient({ mimetype: 'text/plain' }, async () => {
121+
throw new NativeSearchError(
122+
'rate_limited',
123+
'Provider rate limit reached. Try again later.',
124+
30,
125+
403
126+
)
127+
})
128+
expect((await readSlack(throttled, 'F123', undefined, 'file')).content).toContain(
129+
'Full text unavailable: Provider rate limit reached. Try again later.'
130+
)
131+
const relink = fileClient({ mimetype: 'text/plain' }, async () => {
132+
throw new NativeSearchError('reconnect', 'Reconnect this provider to restore its API host.')
133+
})
134+
expect((await readSlack(relink, 'F123', undefined, 'file')).content).toContain(
135+
'Full text unavailable: Reconnect this provider to restore its API host.'
136+
)
104137
for (const failure of [
105138
new Error('Too many redirects (max: 0)'),
106139
new DOMException('The operation timed out.', 'TimeoutError'),

‎apps/sim/lib/sim-search/live/slack.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ function decodeFileText(bytes: Buffer, mimetype: string): string | undefined {
392392
* The full text of a file Slack hosts: canvases as HTML, snippets and text files as text, PDF and
393393
* DOCX extracted, from Slack's download URL when it has one. The token only goes to Slack's file
394394
* host. Short of the caller aborting, any failure (denial, redirect, size limit, unreadable bytes,
395-
* slow download) leaves the read with its preview, plus the provider's reason when it gave one.
395+
* slow download) leaves the read with its preview and a reason when one applies.
396396
*/
397397
async function slackFileText(
398398
client: NativeClient,
@@ -416,7 +416,14 @@ async function slackFileText(
416416
return text && Buffer.byteLength(text, 'utf8') <= MAX_FILE_TEXT_BYTES ? { text } : {}
417417
} catch (error) {
418418
signal?.throwIfAborted()
419-
return error instanceof NativeSearchError ? { unavailable: error.message } : {}
419+
if (!(error instanceof NativeSearchError)) return {}
420+
// files.info already accepted this token, so reconnecting would not change a refused file.
421+
return {
422+
unavailable:
423+
error.status === 'reconnect' && (error.httpStatus === 401 || error.httpStatus === 403)
424+
? "Slack did not serve this file's contents to your account. Open the source."
425+
: error.message,
426+
}
420427
}
421428
}
422429

‎apps/sim/lib/webhooks/providers/slack.test.ts‎

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
inputValidationMockFns,
77
} from '@sim/testing/mocks/input-validation.mock'
88
import { schemaMock } from '@sim/testing/mocks/schema.mock'
9-
import { describe, expect, it, vi } from 'vitest'
9+
import { beforeEach, describe, expect, it, vi } from 'vitest'
1010
import { OutboundRoutingError } from '@/lib/core/network/routing'
1111
import { assertSlackFileHost } from '@/lib/internal/slack/file-download'
1212
import {
@@ -18,6 +18,8 @@ import {
1818

1919
vi.mock('@/lib/oauth/credential-service', () => authOAuthUtilsMock)
2020
vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)
21+
const { mockOutboundFetch } = vi.hoisted(() => ({ mockOutboundFetch: vi.fn() }))
22+
vi.mock('@/lib/core/network/outbound-fetch.server', () => ({ outboundFetch: mockOutboundFetch }))
2123

2224
const ctx = (body: unknown) => ({
2325
webhook: {},
@@ -204,6 +206,17 @@ describe('slackHandler formatInput - native app bot token', () => {
204206
})
205207

206208
describe('slackHandler formatInput - file downloads', () => {
209+
beforeEach(() => {
210+
inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({
211+
isValid: true,
212+
resolvedIP: '93.184.216.34',
213+
originalHostname: 'files.slack.com',
214+
})
215+
inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValue(
216+
new Response(Buffer.from('%PDF'), { status: 200 })
217+
)
218+
})
219+
207220
async function formatWithFiles(files: Record<string, unknown>[]) {
208221
const formatInput = slackHandler.formatInput
209222
if (!formatInput) throw new Error('Slack handler has no formatInput')
@@ -216,15 +229,6 @@ describe('slackHandler formatInput - file downloads', () => {
216229
}
217230

218231
it('downloads from the Slack download URL and keeps redirects on the file host', async () => {
219-
inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({
220-
isValid: true,
221-
resolvedIP: '93.184.216.34',
222-
originalHostname: 'files.slack.com',
223-
})
224-
inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValue(
225-
new Response(Buffer.from('%PDF'), { status: 200 })
226-
)
227-
228232
const { input } = await formatWithFiles([
229233
{
230234
id: 'F1',
@@ -242,16 +246,45 @@ describe('slackHandler formatInput - file downloads', () => {
242246
expect(eventOf(input).files).toHaveLength(1)
243247
})
244248

249+
it("looks up the download URL when the event carries only the file's private URL", async () => {
250+
mockOutboundFetch.mockResolvedValueOnce(
251+
Response.json({
252+
ok: true,
253+
file: {
254+
id: 'F3',
255+
url_private: 'https://files.slack.com/files-pri/T1-F3/report.pdf',
256+
url_private_download: 'https://files.slack.com/files-pri/T1-F3/download/report.pdf',
257+
},
258+
})
259+
)
260+
261+
const { input } = await formatWithFiles([
262+
{
263+
id: 'F3',
264+
name: 'report.pdf',
265+
mimetype: 'application/pdf',
266+
size: 4,
267+
url_private: 'https://files.slack.com/files-pri/T1-F3/report.pdf',
268+
},
269+
])
270+
271+
expect(inputValidationMockFns.mockSecureFetchWithPinnedIP.mock.lastCall?.[0]).toBe(
272+
'https://files.slack.com/files-pri/T1-F3/download/report.pdf'
273+
)
274+
expect(eventOf(input).files).toHaveLength(1)
275+
})
276+
245277
it('never sends the bot token to a file hosted outside Slack', async () => {
246278
const { input } = await formatWithFiles([
247279
{
248280
id: 'F2',
249281
name: 'plan.pdf',
250282
is_external: true,
251-
url_private: 'https://files.example.com/plan.pdf',
283+
url_private: 'https://files.slack.com/files-pri/T1-F2/plan.pdf',
252284
},
253285
])
254286

287+
expect(mockOutboundFetch).not.toHaveBeenCalled()
255288
expect(inputValidationMockFns.mockSecureFetchWithPinnedIP).not.toHaveBeenCalled()
256289
expect(eventOf(input).files).toEqual([])
257290
})

‎apps/sim/lib/webhooks/providers/slack.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {
1515
secureFetchWithPinnedIP,
1616
validateUrlWithDNS,
1717
} from '@/lib/core/security/input-validation.server'
18-
import { assertSlackFileHost, slackFileDownloadUrl } from '@/lib/internal/slack/file-download'
18+
import {
19+
assertSlackFileHost,
20+
isExternalSlackFile,
21+
slackFileDownloadUrl,
22+
} from '@/lib/internal/slack/file-download'
1923
import {
2024
getSlackBotCredential,
2125
refreshAccessTokenIfNeeded,
@@ -322,7 +326,7 @@ async function downloadSlackFiles(
322326

323327
for (const file of filesToProcess) {
324328
let f = file as Record<string, unknown>
325-
if (!f.url_private && !f.url_private_download && f.id) {
329+
if (!f.url_private_download && f.id && !isExternalSlackFile(f)) {
326330
const resolved = await resolveSlackFileInfo(f.id as string, botToken)
327331
if (resolved) f = { ...f, ...resolved }
328332
}

0 commit comments

Comments
 (0)