Skip to content

Commit 55da2f6

Browse files
committed
fix parse bytes enforcement
1 parent 855dca5 commit 55da2f6

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

apps/sim/app/api/files/parse/route.test.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,27 @@ describe('File Parse API Route', () => {
328328
expect(data.results).toHaveLength(2)
329329
})
330330

331-
it('should cap remaining download size while processing multi-file parse results', async () => {
331+
it('should keep the multi-file download cap independent from the remaining parsed-output cap', async () => {
332332
inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({
333333
isValid: true,
334334
resolvedIP: '203.0.113.10',
335335
})
336-
inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValue(
337-
new Response('file content', {
338-
status: 200,
339-
headers: { 'content-type': 'text/plain' },
340-
})
341-
)
336+
inputValidationMockFns.mockSecureFetchWithPinnedIP
337+
.mockResolvedValueOnce(
338+
new Response('file content', {
339+
status: 200,
340+
headers: { 'content-type': 'text/plain' },
341+
})
342+
)
343+
.mockResolvedValueOnce(
344+
new Response('second file content', {
345+
status: 200,
346+
headers: {
347+
'content-length': String(20 * 1024 * 1024),
348+
'content-type': 'text/plain',
349+
},
350+
})
351+
)
342352

343353
const fourMbContent = 'a'.repeat(4 * 1024 * 1024)
344354
mockParseBuffer
@@ -364,17 +374,17 @@ describe('File Parse API Route', () => {
364374
1,
365375
'https://example.com/file1.txt',
366376
'203.0.113.10',
367-
expect.objectContaining({ maxResponseBytes: 5 * 1024 * 1024 })
377+
expect.objectContaining({ maxResponseBytes: 100 * 1024 * 1024 })
368378
)
369379
expect(inputValidationMockFns.mockSecureFetchWithPinnedIP).toHaveBeenNthCalledWith(
370380
2,
371381
'https://example.com/file2.txt',
372382
'203.0.113.10',
373-
expect.objectContaining({ maxResponseBytes: 1024 * 1024 })
383+
expect.objectContaining({ maxResponseBytes: 100 * 1024 * 1024 })
374384
)
375385
})
376386

377-
it('should preserve the remaining multi-file cap when an external URL reuses a workspace file', async () => {
387+
it('should preserve the full download cap when an external URL reuses a workspace file', async () => {
378388
inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({
379389
isValid: true,
380390
resolvedIP: '203.0.113.10',
@@ -411,7 +421,7 @@ describe('File Parse API Route', () => {
411421
expect(response.status).toBe(200)
412422
expect(data.results).toHaveLength(2)
413423
expect(storageServiceMockFns.mockDownloadFile).toHaveBeenCalledWith(
414-
expect.objectContaining({ key: 'workspace-file2.txt', maxBytes: 1024 * 1024 })
424+
expect.objectContaining({ key: 'workspace-file2.txt', maxBytes: 100 * 1024 * 1024 })
415425
)
416426
})
417427

apps/sim/app/api/files/parse/route.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
173173
executionContext,
174174
headers,
175175
request.signal,
176-
remainingOutputBytes,
176+
MAX_DOWNLOAD_SIZE_BYTES,
177177
remainingOutputBytes
178178
)
179179
if (result.metadata) {
@@ -1110,14 +1110,12 @@ async function handleGenericTextBuffer(
11101110
logger.warn('Specialized parser failed, falling back to generic parsing:', parserError)
11111111
}
11121112

1113-
if (maxParsedOutputBytes !== undefined) {
1114-
assertKnownSizeWithinLimit(fileBuffer.length, maxParsedOutputBytes, 'parsed file output')
1115-
}
11161113
const content = fileBuffer.toString('utf-8')
1114+
const limitedContent = assertParsedContentWithinLimit(content, maxParsedOutputBytes)
11171115

11181116
return {
11191117
success: true,
1120-
content,
1118+
content: limitedContent,
11211119
filePath: originalPath || filename,
11221120
metadata: {
11231121
fileType: fileType || getMimeTypeFromExtension(extension),

0 commit comments

Comments
 (0)