Skip to content

Commit 6278a01

Browse files
Bill LeoutsakosBill Leoutsakos
authored andcommitted
fix(quickbooks): enforce attachment upload bounds
1 parent ab39f4e commit 6278a01

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

apps/sim/app/api/tools/quickbooks/add-attachment/route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
7272
const files = processFilesToUserFiles([rawFile], requestId, logger)
7373
if (files.length !== 1) throw new Error('Exactly one valid file is required')
7474
const file = files[0]
75+
assertKnownSizeWithinLimit(file.size, MAX_FILE_SIZE, 'QuickBooks attachment file')
7576
const denied = await assertToolFileAccess(file.key, authResult.userId, requestId, logger)
7677
if (denied) return denied
7778
let downloaded: Awaited<ReturnType<typeof downloadServableFileFromStorage>>
7879
try {
79-
downloaded = await downloadServableFileFromStorage(file, requestId, logger)
80+
downloaded = await downloadServableFileFromStorage(file, requestId, logger, {
81+
maxBytes: MAX_FILE_SIZE,
82+
})
8083
} catch (error) {
8184
const notReady = docNotReadyResponse(error)
8285
if (notReady) return notReady

apps/sim/app/api/tools/quickbooks/documents.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ describe('QuickBooks document API routes', () => {
184184
expect.anything()
185185
)
186186
expect(mockDownloadServableFileFromStorage).toHaveBeenCalledTimes(1)
187+
expect(mockDownloadServableFileFromStorage).toHaveBeenCalledWith(
188+
attachmentFile,
189+
expect.any(String),
190+
expect.anything(),
191+
{ maxBytes: MAX_FILE_SIZE }
192+
)
187193
const formData = mockFetch.mock.calls[0][1].body as FormData
188194
expect(formData.get('file_metadata_01')).toBeInstanceOf(Blob)
189195
expect(formData.get('file_content_01')).toBeInstanceOf(Blob)
@@ -223,6 +229,22 @@ describe('QuickBooks document API routes', () => {
223229
)
224230
expect(mixed.status).toBe(400)
225231

232+
mockProcessFilesToUserFiles.mockReturnValueOnce([
233+
{ ...attachmentFile, size: MAX_FILE_SIZE + 1 },
234+
])
235+
const declaredOversized = await addAttachment(
236+
createMockRequest('POST', {
237+
...auth,
238+
attachmentKind: 'file',
239+
targetType: 'invoice',
240+
targetId: '1',
241+
file: attachmentFile,
242+
})
243+
)
244+
expect(declaredOversized.status).toBe(413)
245+
expect(mockAssertToolFileAccess).not.toHaveBeenCalled()
246+
expect(mockDownloadServableFileFromStorage).not.toHaveBeenCalled()
247+
226248
mockDownloadServableFileFromStorage.mockResolvedValueOnce({
227249
buffer: Buffer.alloc(0),
228250
contentType: 'application/pdf',

0 commit comments

Comments
 (0)