Skip to content

The open alternative to Salesforce, designed for AI.

Stop a Slack content-type mismatch claiming a missing files:read scope (#26509)

Abdul Rahmane79b3365e835
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/download-slack-file.test.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/download-slack-file.test.ts
index 902cad5310e..b010d63b6f2 100644
--- a/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/download-slack-file.test.ts
+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/download-slack-file.test.ts
@@ -92,6 +92,23 @@ describe('downloadSlackFile', () => {
expect(result).toMatchObject({ success: false, reason: 'missing-scope' });
});
+ it('should name a mismatched content type a download failure, not a missing scope', async () => {
+ mockFetchResponse({
+ headers: new Headers({ 'content-type': 'application/octet-stream' }),
+ });
+
+ const result = await downloadPngFile();
+
+ expect(result).toMatchObject({
+ success: false,
+ reason: 'download-failed',
+ });
+ expect(result).toHaveProperty(
+ 'error',
+ expect.not.stringContaining('files:read'),
+ );
+ });
+
it('should name a transport failure as a download failure', async () => {
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('socket hang up')));
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/download-slack-file.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/download-slack-file.ts
index 4d5920cfe91..51c1f4f3c2e 100644
--- a/packages/twenty-apps/public/slack/src/logic-functions/utils/download-slack-file.ts
+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/download-slack-file.ts
@@ -15,6 +15,11 @@ const TOO_LARGE_ERROR = `file is over the ${SLACK_ASSISTANT_MAX_ATTACHMENT_SIZE_
const AUTH_REFUSAL_STATUSES = new Set([401, 403]);
+const SIGN_IN_PAGE_CONTENT_TYPE = 'text/html';
+
+const FILES_READ_SCOPE_MISSING_HINT =
+ 'which usually means the bot token predates the files:read scope';
+
const parseContentLengthBytes = (
headerValue: string | null,
): number | undefined => {
@@ -99,7 +104,7 @@ export const downloadSlackFile = async ({
if (AUTH_REFUSAL_STATUSES.has(response.status)) {
return {
success: false,
- error: `Slack refused the download with status ${response.status}, which usually means the bot token predates the files:read scope`,
+ error: `Slack refused the download with status ${response.status}, ${FILES_READ_SCOPE_MISSING_HINT}`,
reason: 'missing-scope',
};
}
@@ -114,10 +119,18 @@ export const downloadSlackFile = async ({
const responseContentType = response.headers.get('content-type') ?? '';
if (!responseContentType.startsWith(mimeType)) {
+ if (responseContentType.startsWith(SIGN_IN_PAGE_CONTENT_TYPE)) {
+ return {
+ success: false,
+ error: `Slack returned its sign-in page instead of ${mimeType}, ${FILES_READ_SCOPE_MISSING_HINT}`,
+ reason: 'missing-scope',
+ };
+ }
+
return {
success: false,
- error: `expected ${mimeType} but Slack returned ${isNonEmptyString(responseContentType) ? responseContentType : 'no content type'}, which usually means the bot token predates the files:read scope`,
- reason: 'missing-scope',
+ error: `expected ${mimeType} but Slack returned ${isNonEmptyString(responseContentType) ? responseContentType : 'no content type'}`,
+ reason: 'download-failed',
};
}