Skip to content

Commit bf93223

Browse files
committed
fix(search): follow Slack file redirects only within the file host
1 parent b411707 commit bf93223

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

‎apps/sim/lib/sim-search/live/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Each event must exist under the source's delegated token, be in an allowed calen
5858

5959
### Slack
6060

61-
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 (a redirect that leaves the file host carries no headers), 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.
61+
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.
6262

6363
### GitHub
6464

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
66

77
vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)
88

9-
import { createNativeClient } from '@/lib/sim-search/live/http'
9+
import { createNativeClient, NativeSearchError } from '@/lib/sim-search/live/http'
1010

1111
describe('native search network boundary', () => {
1212
beforeEach(() => {
@@ -75,28 +75,32 @@ describe('native search network boundary', () => {
7575
})
7676
).rejects.toThrow('Invalid provider service')
7777
expect(inputValidationMockFns.mockSecureFetchWithValidation).not.toHaveBeenCalled()
78-
await clientFor('https://slack.com').text('/files-pri/T1-F1/plan', undefined, {
79-
service: 'slackFiles',
80-
})
81-
expect(inputValidationMockFns.mockSecureFetchWithValidation).toHaveBeenLastCalledWith(
82-
'https://files.slack.com/files-pri/T1-F1/plan',
83-
expect.objectContaining({ maxRedirects: 3 })
84-
)
85-
expect(
86-
inputValidationMockFns.mockSecureFetchWithValidation.mock.lastCall?.[1]
87-
).not.toHaveProperty('redirectPolicy')
78+
await clientFor('https://www.googleapis.com').json('/v4/spreadsheets/id', { service: 'sheets' })
79+
const [url, options] = inputValidationMockFns.mockSecureFetchWithValidation.mock.lastCall ?? []
80+
expect(url).toBe('https://sheets.googleapis.com/v4/spreadsheets/id')
81+
expect(options).toMatchObject({ maxRedirects: 0 })
82+
expect(options).not.toHaveProperty('assertRedirectTarget')
8883
})
89-
it('never follows a redirect from a provider API', async () => {
84+
it('follows Slack file redirects only within its file host', async () => {
9085
const client = createNativeClient({
9186
origin: 'https://slack.com',
9287
accessToken: 'private',
9388
signal: new AbortController().signal,
9489
})
95-
await client.json('/api/files.info')
96-
expect(inputValidationMockFns.mockSecureFetchWithValidation).toHaveBeenLastCalledWith(
97-
'https://slack.com/api/files.info',
98-
expect.objectContaining({ maxRedirects: 0 })
99-
)
90+
await client.text('/files-pri/T1-F1/plan', undefined, { service: 'slackFiles' })
91+
const [url, options] = inputValidationMockFns.mockSecureFetchWithValidation.mock.lastCall ?? []
92+
expect(url).toBe('https://files.slack.com/files-pri/T1-F1/plan')
93+
expect(options?.maxRedirects).toBeGreaterThan(0)
94+
const assertTarget = options?.assertRedirectTarget
95+
expect(() =>
96+
assertTarget('https://files.slack.com/files-pri/T1-F1/download/plan')
97+
).not.toThrow()
98+
for (const target of [
99+
'https://team.slack.com/?redir=%2Ffiles-pri%2FT1-F1',
100+
'https://files.slack.com.example.com/plan',
101+
'http://files.slack.com/files-pri/T1-F1/plan',
102+
])
103+
expect(() => assertTarget(target)).toThrow(NativeSearchError)
100104
})
101105
it('keeps the HTTP status of a provider failure so callers can tell a missing item apart', async () => {
102106
const client = createNativeClient({

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export const NATIVE_RESPONSE_MAX_BYTES = 4 * 1024 * 1024
3232

3333
/**
3434
* Sibling hosts a provider's token may reach, each only from its own provider origin: Google
35-
* serves Sheets apart from Drive, and Slack serves file contents apart from its Web API. Slack
36-
* redirects some file downloads, so its file host follows a few redirects: a hop that stays on
37-
* the file host keeps the token, and a hop to any other origin carries no headers at all.
35+
* serves Sheets apart from Drive, and Slack serves file contents apart from its Web API. Slack's
36+
* file host follows redirects only within itself: Slack sends a token it will not serve to a
37+
* workspace sign-in page, which must be refused rather than read as the file.
3838
*/
3939
const NATIVE_SERVICES = {
4040
sheets: {
@@ -45,6 +45,15 @@ const NATIVE_SERVICES = {
4545
slackFiles: { origin: 'https://slack.com', hostname: 'files.slack.com', maxRedirects: 3 },
4646
} as const
4747

48+
function assertSameHost(requested: URL, target: string) {
49+
const next = new URL(target)
50+
if (next.origin !== requested.origin)
51+
throw new NativeSearchError(
52+
'unavailable',
53+
`The provider redirected this file to ${next.hostname}, outside its file host. Open the source.`
54+
)
55+
}
56+
4857
/** A sibling host a request may target instead of its client's provider origin. */
4958
export type NativeService = keyof typeof NATIVE_SERVICES
5059

@@ -111,6 +120,9 @@ export function createNativeClient(input: {
111120
timeout: 10_000,
112121
maxResponseBytes: NATIVE_RESPONSE_MAX_BYTES,
113122
maxRedirects: service?.maxRedirects ?? 0,
123+
...(service?.maxRedirects
124+
? { assertRedirectTarget: (target: string) => assertSameHost(url, target) }
125+
: {}),
114126
acceptCompressed: true,
115127
connectionPool: input.pool,
116128
})

0 commit comments

Comments
 (0)