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
40 changes: 40 additions & 0 deletions apps/sim/lib/internal/slack/file-download.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { assertSlackFileHost, slackFileDownloadUrl } from '@/lib/internal/slack/file-download'

const PRIVATE = 'https://files.slack.com/files-pri/T1-F1/report.pdf'
const DOWNLOAD = 'https://files.slack.com/files-pri/T1-F1/download/report.pdf'

describe('Slack file download URL', () => {
it('prefers the download URL, which serves bytes where the private URL may redirect', () => {
expect(
slackFileDownloadUrl({ url_private: PRIVATE, url_private_download: DOWNLOAD })?.href
).toBe(DOWNLOAD)
expect(slackFileDownloadUrl({ url_private: PRIVATE })?.href).toBe(PRIVATE)
})
it('never returns a URL for a file hosted outside Slack', () => {
for (const file of [
{ is_external: true, url_private: PRIVATE },
{ mode: 'external', url_private: PRIVATE },
{ filetype: 'remote', url_private: PRIVATE },
{ url_private: 'https://files.example.com/report.pdf' },
{ url_private: 'https://files.slack.com.example.com/report.pdf' },
{ url_private: 'http://files.slack.com/files-pri/T1-F1/report.pdf' },
{ url_private_download: 'https://files.example.com/report.pdf', url_private: PRIVATE },
{ url_private: 'not a url' },
{},
])
expect(slackFileDownloadUrl(file)).toBeUndefined()
})
it('keeps a redirect on the Slack file host', () => {
expect(() => assertSlackFileHost(DOWNLOAD)).not.toThrow()
for (const target of [
'https://slack-files.com/files-pri-safe/T1-F1/report.pdf',
'https://team.slack.com/?redir=%2Ffiles-pri%2FT1-F1',
'http://files.slack.com/files-pri/T1-F1/report.pdf',
])
expect(() => assertSlackFileHost(target)).toThrow()
})
})
33 changes: 33 additions & 0 deletions apps/sim/lib/internal/slack/file-download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/** The only host a Slack token is sent to when downloading a file's contents. */
const SLACK_FILE_ORIGIN = 'https://files.slack.com'

/**
* Where to download a Slack-hosted file's bytes with a Slack token, or undefined when Slack does
* not host it. `url_private_download` comes first: Slack answers a PDF's `url_private` with a
* redirect to a sandboxed viewer on another domain, while the download URL serves the bytes from
* its file host. A remote file's URL is whatever the app that shared it chose, so a file marked
* 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
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 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.
*/
export function isSlackFileHost(target: string): boolean {
return URL.parse(target)?.origin === SLACK_FILE_ORIGIN
}

/** Redirect guard for a Slack file download; see {@link isSlackFileHost}. */
export function assertSlackFileHost(target: string): void {
if (!isSlackFileHost(target))
throw new Error('Slack redirected the file download away from its file host')
}
30 changes: 29 additions & 1 deletion apps/sim/lib/internal/slack/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('Slack operations', () => {
name: 'report.pdf',
mimetype: 'application/pdf',
url_private: 'https://files.slack.com/report.pdf',
url_private_download: 'https://files.slack.com/download/report.pdf',
},
})
)
Expand All @@ -66,15 +67,23 @@ describe('Slack operations', () => {
)

expect(mockSecureFetchWithPinnedIP).toHaveBeenCalledWith(
'https://files.slack.com/report.pdf',
'https://files.slack.com/download/report.pdf',
'93.184.216.34',
{
headers: { Authorization: 'Bearer token' },
profile: 'contentFetch',
maxResponseBytes: MAX_FILE_SIZE,
signal: controller.signal,
assertRedirectTarget: expect.any(Function),
}
)
const guard = mockSecureFetchWithPinnedIP.mock.lastCall?.[2]?.assertRedirectTarget
expect(() =>
guard?.('https://files.slack.com/files-pri/T1-F1/download/report.pdf')
).not.toThrow()
expect(() => guard?.('https://slack-files.com/files-pri-safe/T1-F1/report.pdf')).toThrow(
expect.objectContaining({ status: 400 })
)
assert(isInternalToolFileResult(result))
expect(result.files).toEqual([
{ name: 'report.pdf', mimeType: 'application/pdf', buffer: Buffer.from('pdf') },
Expand All @@ -92,6 +101,25 @@ describe('Slack operations', () => {
expect(result.present([storedFile])).toEqual({ success: true, output: { file: storedFile } })
})

it('never sends the token to a file hosted outside Slack', async () => {
vi.mocked(global.fetch).mockResolvedValueOnce(
slackResponse({
ok: true,
file: {
name: 'plan.pdf',
is_external: true,
url_private: 'https://files.example.com/plan.pdf',
},
})
)

await expect(executeSlackDownload({ accessToken: 'token', fileId: 'F1' })).rejects.toThrow(
'File has no Slack-hosted download URL'
)
expect(mockValidateUrlWithDNS).not.toHaveBeenCalled()
expect(mockSecureFetchWithPinnedIP).not.toHaveBeenCalled()
})

it('stops following history cursors at the page ceiling whatever maxPages asks for', async () => {
vi.mocked(global.fetch).mockImplementation(async () =>
slackResponse({
Expand Down
12 changes: 8 additions & 4 deletions apps/sim/lib/internal/slack/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
slackString,
} from '@/lib/internal/slack/client'
import { SlackOperationError } from '@/lib/internal/slack/errors'
import { isSlackFileHost, slackFileDownloadUrl } from '@/lib/internal/slack/file-download'
import { forEachSlackAttachmentFile } from '@/lib/internal/slack/file-input'
import {
createInternalToolFileResult,
Expand Down Expand Up @@ -447,17 +448,20 @@ export async function executeSlackDownload(input: SlackDownloadBody, signal?: Ab
const file = slackObject(data, 'file') ?? {}
const name = input.fileName || slackString(file, 'name') || 'download'
const mimeType = slackString(file, 'mimetype') || 'application/octet-stream'
const urlPrivate = slackString(file, 'url_private')
if (!urlPrivate) failure(400, 'File does not have a download URL')
const downloadUrl = urlPrivate
const validation = await validateUrlWithDNS(downloadUrl, 'urlPrivate', 'contentFetch')
const downloadUrl = slackFileDownloadUrl(file)?.href
if (!downloadUrl) failure(400, 'File has no Slack-hosted download URL')
const validation = await validateUrlWithDNS(downloadUrl, 'downloadUrl', 'contentFetch')
signal?.throwIfAborted()
if (!validation.isValid) failure(400, validation.error || 'Invalid Slack file URL')
const response = await secureFetchWithPinnedIP(downloadUrl, validation.resolvedIP, {
profile: 'contentFetch',
headers: { Authorization: `Bearer ${input.accessToken}` },
maxResponseBytes: MAX_FILE_SIZE,
signal,
assertRedirectTarget: (target) => {
if (!isSlackFileHost(target))
failure(400, 'Slack redirected the file download away from its file host')
},
})
signal?.throwIfAborted()
if (!response.ok) failure(400, 'Failed to download file content')
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/lib/sim-search/live/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Each event must exist under the source's delegated token, be in an allowed calen

### Slack

Member mode only. Search uses the connected user's Slack real-time search grant and provider search syntax. Date bounds are sent as numeric provider timestamps; date ordering selects keyword retrieval and timestamp sort, because Slack semantic retrieval uses relevance ordering. Channels, messages, direct messages, and files are limited by that grant and provider scopes; there is no admin channel filter in member mode. Missing required search scopes produce a reconnect status. Thread/file reads use the same connected identity. A file read downloads the full text of a canvas (HTML), text snippet, PDF or DOCX from `files.slack.com` only, never a remote file's own URL (redirects are followed only within that host; a hop elsewhere, such as a sign-in page, keeps the preview), within the shared response-size limit; when Slack will not serve it, the read returns the `files.info` preview. Real-time Search returns at most 20 results a call and counts every page against the member's rate limit, so a search across every source reads one page; a Slack-scoped or native Slack search follows the cursor for up to three pages to fill its limit, and a later page that fails keeps the earlier pages and returns the unread cursor.
Member mode only. Search uses the connected user's Slack real-time search grant and provider search syntax. Date bounds are sent as numeric provider timestamps; date ordering selects keyword retrieval and timestamp sort, because Slack semantic retrieval uses relevance ordering. Channels, messages, direct messages, and files are limited by that grant and provider scopes; there is no admin channel filter in member mode. Missing required search scopes produce a reconnect status. Thread/file reads use the same connected identity. A file read downloads the full text of a canvas (HTML), text snippet, PDF or DOCX from `files.slack.com` only, using `url_private_download` when present (Slack redirects a PDF's `url_private` to a viewer on another domain), never a remote file's own URL (redirects are followed only within that host; a hop elsewhere, such as a sign-in page, keeps the preview), within the shared response-size limit; when Slack will not serve it, the read returns the `files.info` preview. Real-time Search returns at most 20 results a call and counts every page against the member's rate limit, so a search across every source reads one page; a Slack-scoped or native Slack search follows the cursor for up to three pages to fill its limit, and a later page that fails keeps the earlier pages and returns the unread cursor.

### GitHub

Expand Down
13 changes: 12 additions & 1 deletion apps/sim/lib/sim-search/live/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ describe('Slack file reads', () => {
expect.objectContaining({ service: 'slackFiles' })
)
})
it('downloads from the Slack download URL when the file has one', async () => {
const api = fileClient({
mimetype: 'text/plain',
url_private_download: 'https://files.slack.com/files-pri/T1-F123/download/plan',
})
expect((await readSlack(api, 'F123', undefined, 'file')).content).toBe('Full text')
expect(api.bytes).toHaveBeenCalledWith(
'/files-pri/T1-F123/download/plan',
{},
expect.objectContaining({ service: 'slackFiles' })
)
})
it('reads the full text of a text snippet', async () => {
const api = fileClient(
{ filetype: 'text', mimetype: 'text/plain' },
Expand All @@ -75,7 +87,6 @@ describe('Slack file reads', () => {
it('never sends the token to a file host other than Slack', async () => {
for (const file of [
{ mimetype: 'text/plain', url_private: 'https://files.example.com/plan.txt' },
{ mimetype: 'text/plain', url_private: 'http://files.slack.com/files-pri/T1-F123/plan' },
{ mimetype: 'text/plain', is_external: true },
]) {
const api = fileClient(file)
Expand Down
14 changes: 7 additions & 7 deletions apps/sim/lib/sim-search/live/slack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { slackFileDownloadUrl } from '@/lib/internal/slack/file-download'
import { recordSearchProviderSignal } from '@/lib/knowledge/search/diagnostics'
import { dateSortDirection, nativeDateBounds, nativeText } from '@/lib/sim-search/live/dates'
import {
Expand Down Expand Up @@ -389,20 +390,19 @@ 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. The token only goes to Slack's file host, never a remote file's own URL. 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.
* 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.
*/
async function slackFileText(
client: NativeClient,
file: Record<string, unknown>,
signal?: AbortSignal
): Promise<{ text: string } | { unavailable?: string }> {
const format = slackFileFormat(file)
if (!format || file.is_external === true || Number(file.size) > MAX_FILE_DOWNLOAD_BYTES[format])
return {}
const source = URL.parse(string(file.url_private))
if (source?.origin !== 'https://files.slack.com') return {}
if (!format || Number(file.size) > MAX_FILE_DOWNLOAD_BYTES[format]) return {}
const source = slackFileDownloadUrl(file)
if (!source) return {}
const timeout = AbortSignal.timeout(FILE_DOWNLOAD_TIMEOUT_MS)
const download = signal ? AbortSignal.any([signal, timeout]) : timeout
const request = { service: 'slackFiles', signal: download } as const
Expand Down
60 changes: 60 additions & 0 deletions apps/sim/lib/webhooks/providers/slack.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { createHmac } from 'node:crypto'
import { authOAuthUtilsMock, authOAuthUtilsMockFns } from '@sim/testing/mocks/auth-oauth-utils.mock'
import { queueTableRows } from '@sim/testing/mocks/database.mock'
import {
inputValidationMock,
inputValidationMockFns,
} from '@sim/testing/mocks/input-validation.mock'
import { schemaMock } from '@sim/testing/mocks/schema.mock'
import { describe, expect, it, vi } from 'vitest'
import { OutboundRoutingError } from '@/lib/core/network/routing'
import { assertSlackFileHost } from '@/lib/internal/slack/file-download'
import {
handleSlackChallenge,
resolveSlackEventKey,
Expand All @@ -12,6 +17,7 @@ import {
} from '@/lib/webhooks/providers/slack'

vi.mock('@/lib/oauth/credential-service', () => authOAuthUtilsMock)
vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)

const ctx = (body: unknown) => ({
webhook: {},
Expand Down Expand Up @@ -197,6 +203,60 @@ describe('slackHandler formatInput - native app bot token', () => {
})
})

describe('slackHandler formatInput - file downloads', () => {
async function formatWithFiles(files: Record<string, unknown>[]) {
const formatInput = slackHandler.formatInput
if (!formatInput) throw new Error('Slack handler has no formatInput')
return formatInput({
...ctx({ event: { type: 'message', channel: 'C1', user: 'U1', text: 'see file', files } }),
webhook: { providerConfig: { botToken: 'xoxb-test', includeFiles: true } },
query: {},
method: 'POST',
})
}

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',
name: 'report.pdf',
mimetype: 'application/pdf',
size: 4,
url_private: 'https://files.slack.com/files-pri/T1-F1/report.pdf',
url_private_download: 'https://files.slack.com/files-pri/T1-F1/download/report.pdf',
},
])

const [url, , options] = inputValidationMockFns.mockSecureFetchWithPinnedIP.mock.lastCall ?? []
expect(url).toBe('https://files.slack.com/files-pri/T1-F1/download/report.pdf')
expect(options).toMatchObject({ assertRedirectTarget: assertSlackFileHost })
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',
},
])

expect(inputValidationMockFns.mockSecureFetchWithPinnedIP).not.toHaveBeenCalled()
expect(eventOf(input).files).toEqual([])
})
})

describe('slackHandler formatInput - interactivity (block_actions)', () => {
it('carries the full view (state.values + private_metadata) through for a view_submission', async () => {
const { input } = await slackHandler.formatInput!(
Expand Down
Loading
Loading