1
import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest';
2
import * as path from 'node:path';
3
+import * as fs from 'fs';
4
import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils.node';
5
import ComposioClient from '@composio/client';
6
import { ComposioSensitiveFilePathBlockedError } from '../../src/errors/FileModifierErrors';
66
headers: { 'content-type': contentType },
67
});
68
69
+/** A small, well-formed S3 download response (no `content-length` header). */
70
+const downloadedFileResponse = (body: Uint8Array = new Uint8Array(10)) =>
71
+ new Response(body, { status: 200 });
72
+
73
describe('fileUtils', () => {
74
let mockClient: ComposioClient;
75
319
320
describe('downloadFileFromS3', () => {
321
it('should generate filename with tool slug prefix', async () => {
317
- mockFetch.mockResolvedValue({
318
- ok: true,
319
- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
320
- });
322
+ mockFetch.mockResolvedValue(downloadedFileResponse());
323
324
const result = await downloadFileFromS3({
325
toolSlug: 'github',
331
});
332
333
it('should handle MIME types with + in downloadFileFromS3', async () => {
332
- mockFetch.mockResolvedValue({
333
- ok: true,
334
- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
335
- });
334
+ mockFetch.mockResolvedValue(downloadedFileResponse());
335
336
const result = await downloadFileFromS3({
337
toolSlug: 'api-tool',
363
// If a future change started reflecting the s3Url path or query into
364
// the local filename, an attacker-controlled CDN response could write
365
// outside the download dir or stomp on existing files.
367
- mockFetch.mockResolvedValue({
368
- ok: true,
369
- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
370
- });
366
+ mockFetch.mockResolvedValue(downloadedFileResponse());
367
368
const result = await downloadFileFromS3({
369
toolSlug: 'github',
386
// could in theory smuggle path components into the saved filename.
387
// `saveFile` defends against that by running the assembled filename
388
// through `path.basename` before joining with the download dir.
393
- mockFetch.mockResolvedValue({
394
- ok: true,
395
- arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
396
- });
389
+ mockFetch.mockResolvedValue(downloadedFileResponse());
390
391
const result = await downloadFileFromS3({
392
toolSlug: 'AAA/../etc/passwd',
402
expect(path.dirname(result.filePath as string)).not.toContain('etc');
403
expect(path.basename(result.filePath as string)).toBe('passwd_1640995200000abc12345.txt');
404
});
405
+
406
+ it('rejects a stream that exceeds the download limit and writes nothing', async () => {
407
+ // `s3Url` is an API-response field, so the body behind it is untrusted:
408
+ // without a cap, a dishonest (or absent) Content-Length lets the server
409
+ // stream unbounded bytes into the host process's heap and onto disk.
410
+ mockFetch.mockResolvedValue(downloadedFileResponse(new Uint8Array(2048)));
411
+
412
+ await expect(
413
+ downloadFileFromS3({
414
+ toolSlug: 'github',
415
+ s3Url: 'https://s3.example.com/huge.bin',
416
+ mimeType: 'text/plain',
417
+ maxDownloadBytes: 1024,
418
+ })
419
+ ).rejects.toThrow(/exceeds maximum allowed size/);
420
+
421
+ expect(fs.writeFileSync).not.toHaveBeenCalled();
422
+ });
423
+
424
+ it('downloads a body within the limit', async () => {
425
+ const body = new Uint8Array(512).fill(7);
426
+ mockFetch.mockResolvedValue(downloadedFileResponse(body));
427
+
428
+ const result = await downloadFileFromS3({
429
+ toolSlug: 'github',
430
+ s3Url: 'https://s3.example.com/small.bin',
431
+ mimeType: 'text/plain',
432
+ maxDownloadBytes: 1024,
433
+ });
434
+
435
+ expect(result.filePath).toBeDefined();
436
+ expect(fs.writeFileSync).toHaveBeenCalledWith(result.filePath, body);
437
+ });
438
});
439
440
describe('File object handling', () => {