Skip to content

Commit d0f0f23

Browse files
committed
fix(slack): report an off-host file redirect as a 400 from the download tool
1 parent 1c62611 commit d0f0f23

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ export function slackFileDownloadUrl(file: Record<string, unknown>): URL | undef
1919
}
2020

2121
/**
22-
* Redirect guard for a Slack file download: a hop off the file host, such as the workspace
23-
* sign-in page Slack sends a token it will not serve, is refused rather than read as the file.
22+
* Whether a redirect stays on Slack's file host. A hop elsewhere, such as the workspace sign-in
23+
* page Slack sends a token it will not serve, must be refused rather than read as the file.
2424
*/
25+
export function isSlackFileHost(target: string): boolean {
26+
return URL.parse(target)?.origin === SLACK_FILE_ORIGIN
27+
}
28+
29+
/** Redirect guard for a Slack file download; see {@link isSlackFileHost}. */
2530
export function assertSlackFileHost(target: string): void {
26-
if (URL.parse(target)?.origin !== SLACK_FILE_ORIGIN)
31+
if (!isSlackFileHost(target))
2732
throw new Error('Slack redirected the file download away from its file host')
2833
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ vi.mock('@/lib/internal/slack/file-input', () => ({
1818

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

21-
import { assertSlackFileHost } from '@/lib/internal/slack/file-download'
2221
import { executeSlackDownload } from '@/lib/internal/slack/operations'
2322
import { executeSlackGetChannelHistoryOperation } from '@/lib/internal/slack/operations/get-channel-history'
2423
import { MAX_FILE_SIZE } from '@/lib/uploads/utils/validation'
@@ -75,9 +74,16 @@ describe('Slack operations', () => {
7574
profile: 'contentFetch',
7675
maxResponseBytes: MAX_FILE_SIZE,
7776
signal: controller.signal,
78-
assertRedirectTarget: assertSlackFileHost,
77+
assertRedirectTarget: expect.any(Function),
7978
}
8079
)
80+
const guard = mockSecureFetchWithPinnedIP.mock.lastCall?.[2]?.assertRedirectTarget
81+
expect(() =>
82+
guard?.('https://files.slack.com/files-pri/T1-F1/download/report.pdf')
83+
).not.toThrow()
84+
expect(() => guard?.('https://slack-files.com/files-pri-safe/T1-F1/report.pdf')).toThrow(
85+
expect.objectContaining({ status: 400 })
86+
)
8187
assert(isInternalToolFileResult(result))
8288
expect(result.files).toEqual([
8389
{ name: 'report.pdf', mimeType: 'application/pdf', buffer: Buffer.from('pdf') },

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
slackString,
2626
} from '@/lib/internal/slack/client'
2727
import { SlackOperationError } from '@/lib/internal/slack/errors'
28-
import { assertSlackFileHost, slackFileDownloadUrl } from '@/lib/internal/slack/file-download'
28+
import { isSlackFileHost, slackFileDownloadUrl } from '@/lib/internal/slack/file-download'
2929
import { forEachSlackAttachmentFile } from '@/lib/internal/slack/file-input'
3030
import {
3131
createInternalToolFileResult,
@@ -458,7 +458,10 @@ export async function executeSlackDownload(input: SlackDownloadBody, signal?: Ab
458458
headers: { Authorization: `Bearer ${input.accessToken}` },
459459
maxResponseBytes: MAX_FILE_SIZE,
460460
signal,
461-
assertRedirectTarget: assertSlackFileHost,
461+
assertRedirectTarget: (target) => {
462+
if (!isSlackFileHost(target))
463+
failure(400, 'Slack redirected the file download away from its file host')
464+
},
462465
})
463466
signal?.throwIfAborted()
464467
if (!response.ok) failure(400, 'Failed to download file content')

0 commit comments

Comments
 (0)