From 59d18fdf22ed7aa48a1a50a3a27a0ae01c47261c Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Fri, 20 Nov 2026 15:55:23 -0600 Subject: [PATCH] fix(web-api): reject files.uploadV2 when the file stream errors getFileDataAsStream only listened for "readable" and "end", so a stream that failed (for example fs.createReadStream on a missing path) emitted an unhandled "error" event. The process got an uncaught exception, or, when the stream already had an error listener, the upload promise never settled. Reject with WebAPIFileUploadReadFileDataError and keep the stream error as the cause. --- .changeset/web-api-upload-stream-error.md | 5 +++ packages/web-api/src/file-upload.test.ts | 53 ++++++++++++++++++++++- packages/web-api/src/file-upload.ts | 8 ++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 .changeset/web-api-upload-stream-error.md diff --git a/.changeset/web-api-upload-stream-error.md b/.changeset/web-api-upload-stream-error.md new file mode 100644 index 000000000..a50f4df7c --- /dev/null +++ b/.changeset/web-api-upload-stream-error.md @@ -0,0 +1,5 @@ +--- +"@slack/web-api": patch +--- + +fix: reject `files.uploadV2` with a `WebAPIFileUploadReadFileDataError` when the `file` stream errors, instead of an uncaught exception diff --git a/packages/web-api/src/file-upload.test.ts b/packages/web-api/src/file-upload.test.ts index f1b88cdb3..50659a873 100644 --- a/packages/web-api/src/file-upload.test.ts +++ b/packages/web-api/src/file-upload.test.ts @@ -1,8 +1,14 @@ import assert from 'node:assert/strict'; import { createReadStream, statSync, unlinkSync, writeFileSync } from 'node:fs'; +import { Readable } from 'node:stream'; import { afterEach, beforeEach, describe, it } from 'node:test'; import sinon from 'sinon'; -import { ErrorCode, SlackError, WebAPIFileUploadInvalidArgumentsError } from './errors'; +import { + ErrorCode, + SlackError, + WebAPIFileUploadInvalidArgumentsError, + WebAPIFileUploadReadFileDataError, +} from './errors'; import { buildChannelsWarning, buildLegacyFileTypeWarning, @@ -274,6 +280,18 @@ describe('file-upload', () => { // should return file data from the stream assert.strictEqual(Buffer.isBuffer(res), true); }); + it('rejects when the file ReadStream cannot be read', async () => { + const fileUpload = { + file: createReadStream('./test/fixtures/does-not-exist.txt'), + filename: 'Test File', + }; + await assert.rejects(getFileData(fileUpload), (err: unknown) => { + assert.ok(err instanceof WebAPIFileUploadReadFileDataError); + assert.strictEqual(err.code, ErrorCode.FileUploadReadFileDataError); + assert.strictEqual((err.cause as NodeJS.ErrnoException).code, 'ENOENT'); + return true; + }); + }); it('handles content as string', async () => { const validFileUpload = { content: 'Happiness', @@ -307,6 +325,39 @@ describe('file-upload', () => { assert.strictEqual(err.message, 'No data in supplied file'); } }); + it('rejects if the stream errors before any data is read', async () => { + await assert.rejects( + getFileDataAsStream(createReadStream('./test/fixtures/does-not-exist.txt')), + (err: unknown) => { + assert.ok(err instanceof WebAPIFileUploadReadFileDataError); + assert.ok(err instanceof SlackError); + assert.strictEqual(err.code, ErrorCode.FileUploadReadFileDataError); + assert.match(err.message, /^Unable to read file data from the supplied stream: ENOENT/); + assert.strictEqual((err.cause as NodeJS.ErrnoException).code, 'ENOENT'); + return true; + }, + ); + }); + it('rejects if the stream errors after some data is read', async () => { + const failure = new Error('connection reset'); + let reads = 0; + const readable = new Readable({ + read() { + reads += 1; + if (reads === 1) { + this.push('partial data'); + } else { + this.destroy(failure); + } + }, + }); + await assert.rejects(getFileDataAsStream(readable), (err: unknown) => { + assert.ok(err instanceof WebAPIFileUploadReadFileDataError); + assert.strictEqual(err.message, 'Unable to read file data from the supplied stream: connection reset'); + assert.strictEqual(err.cause, failure); + return true; + }); + }); it('ensures complete file is uploaded', async () => { try { // create a large file diff --git a/packages/web-api/src/file-upload.ts b/packages/web-api/src/file-upload.ts index 09d54a624..0b250a802 100644 --- a/packages/web-api/src/file-upload.ts +++ b/packages/web-api/src/file-upload.ts @@ -209,6 +209,14 @@ export async function getFileDataAsStream(readable: Readable): Promise { reject(Error('No data in supplied file')); } }); + // without this listener a failing stream is an uncaught exception and the promise never settles + readable.on('error', (err: Error) => { + reject( + new WebAPIFileUploadReadFileDataError(`Unable to read file data from the supplied stream: ${err.message}`, { + cause: err, + }), + ); + }); }); }