Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/sim/lib/internal/slack/file-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ 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<string, unknown>): 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
)
const url = candidate ? URL.parse(candidate) : null
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<string, unknown>): 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.
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/lib/internal/slack/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
})
)
Expand Down
39 changes: 36 additions & 3 deletions apps/sim/lib/sim-search/live/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
11 changes: 9 additions & 2 deletions apps/sim/lib/sim-search/live/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}
}

Expand Down
55 changes: 44 additions & 11 deletions apps/sim/lib/webhooks/providers/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: {},
Expand Down Expand Up @@ -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<string, unknown>[]) {
const formatInput = slackHandler.formatInput
if (!formatInput) throw new Error('Slack handler has no formatInput')
Expand All @@ -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',
Expand All @@ -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([])
})
Expand Down
8 changes: 6 additions & 2 deletions apps/sim/lib/webhooks/providers/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -322,7 +326,7 @@ async function downloadSlackFiles(

for (const file of filesToProcess) {
let f = file as Record<string, unknown>
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 }
}
Expand Down