diff --git a/apps/sim/lib/sim-search/live/README.md b/apps/sim/lib/sim-search/live/README.md index 00f6ace7d53..19b052ffa8a 100644 --- a/apps/sim/lib/sim-search/live/README.md +++ b/apps/sim/lib/sim-search/live/README.md @@ -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, 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, 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 diff --git a/apps/sim/lib/sim-search/live/http.test.ts b/apps/sim/lib/sim-search/live/http.test.ts index 7a3ecd57472..f0f8bc8c974 100644 --- a/apps/sim/lib/sim-search/live/http.test.ts +++ b/apps/sim/lib/sim-search/live/http.test.ts @@ -6,7 +6,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) -import { createNativeClient } from '@/lib/sim-search/live/http' +import { createNativeClient, NativeSearchError } from '@/lib/sim-search/live/http' describe('native search network boundary', () => { beforeEach(() => { @@ -75,13 +75,32 @@ describe('native search network boundary', () => { }) ).rejects.toThrow('Invalid provider service') expect(inputValidationMockFns.mockSecureFetchWithValidation).not.toHaveBeenCalled() - await clientFor('https://slack.com').text('/files-pri/T1-F1/plan', undefined, { - service: 'slackFiles', + await clientFor('https://www.googleapis.com').json('/v4/spreadsheets/id', { service: 'sheets' }) + const [url, options] = inputValidationMockFns.mockSecureFetchWithValidation.mock.lastCall ?? [] + expect(url).toBe('https://sheets.googleapis.com/v4/spreadsheets/id') + expect(options).toMatchObject({ maxRedirects: 0 }) + expect(options).not.toHaveProperty('assertRedirectTarget') + }) + it('follows Slack file redirects only within its file host', async () => { + const client = createNativeClient({ + origin: 'https://slack.com', + accessToken: 'private', + signal: new AbortController().signal, }) - expect(inputValidationMockFns.mockSecureFetchWithValidation).toHaveBeenCalledWith( - 'https://files.slack.com/files-pri/T1-F1/plan', - expect.objectContaining({ maxRedirects: 0 }) - ) + await client.text('/files-pri/T1-F1/plan', undefined, { service: 'slackFiles' }) + const [url, options] = inputValidationMockFns.mockSecureFetchWithValidation.mock.lastCall ?? [] + expect(url).toBe('https://files.slack.com/files-pri/T1-F1/plan') + expect(options?.maxRedirects).toBeGreaterThan(0) + const assertTarget = options?.assertRedirectTarget + expect(() => + assertTarget('https://files.slack.com/files-pri/T1-F1/download/plan') + ).not.toThrow() + for (const target of [ + 'https://team.slack.com/?redir=%2Ffiles-pri%2FT1-F1', + 'https://files.slack.com.example.com/plan', + 'http://files.slack.com/files-pri/T1-F1/plan', + ]) + expect(() => assertTarget(target)).toThrow(NativeSearchError) }) it('keeps the HTTP status of a provider failure so callers can tell a missing item apart', async () => { const client = createNativeClient({ diff --git a/apps/sim/lib/sim-search/live/http.ts b/apps/sim/lib/sim-search/live/http.ts index be6331f4065..175de822e63 100644 --- a/apps/sim/lib/sim-search/live/http.ts +++ b/apps/sim/lib/sim-search/live/http.ts @@ -32,17 +32,32 @@ export const NATIVE_RESPONSE_MAX_BYTES = 4 * 1024 * 1024 /** * Sibling hosts a provider's token may reach, each only from its own provider origin: Google - * serves Sheets apart from Drive, and Slack serves file contents apart from its Web API. + * serves Sheets apart from Drive, and Slack serves file contents apart from its Web API. Slack's + * file host follows redirects only within itself: Slack sends a token it will not serve to a + * workspace sign-in page, which must be refused rather than read as the file. */ const NATIVE_SERVICES = { - sheets: { origin: 'https://www.googleapis.com', hostname: 'sheets.googleapis.com' }, - slackFiles: { origin: 'https://slack.com', hostname: 'files.slack.com' }, + sheets: { + origin: 'https://www.googleapis.com', + hostname: 'sheets.googleapis.com', + maxRedirects: 0, + }, + slackFiles: { origin: 'https://slack.com', hostname: 'files.slack.com', maxRedirects: 3 }, } as const +function assertSameHost(requested: URL, target: string) { + const next = new URL(target) + if (next.origin !== requested.origin) + throw new NativeSearchError( + 'unavailable', + `The provider redirected this file to ${next.hostname}, outside its file host. Open the source.` + ) +} + /** A sibling host a request may target instead of its client's provider origin. */ export type NativeService = keyof typeof NATIVE_SERVICES -/** Tokens only go to a code-selected provider origin; redirects never carry credentials. */ +/** Tokens only go to a code-selected provider origin; a redirect never carries them elsewhere. */ export function createNativeClient(input: { origin: string accessToken: string @@ -76,8 +91,8 @@ export function createNativeClient(input: { 'request_budget_exceeded' ) const url = new URL(input.origin) - if (options?.service) { - const service = NATIVE_SERVICES[options.service] + const service = options?.service ? NATIVE_SERVICES[options.service] : undefined + if (service) { if (url.origin !== service.origin) throw new Error('Invalid provider service') url.hostname = service.hostname } @@ -104,7 +119,10 @@ export function createNativeClient(input: { signal, timeout: 10_000, maxResponseBytes: NATIVE_RESPONSE_MAX_BYTES, - maxRedirects: 0, + maxRedirects: service?.maxRedirects ?? 0, + ...(service?.maxRedirects + ? { assertRedirectTarget: (target: string) => assertSameHost(url, target) } + : {}), acceptCompressed: true, connectionPool: input.pool, })