8
return filePath.slice(0, lastSep);
9
}
10
import { RemoteFileData, RemoteFileDataSchema } from '../types/ToolRouterSessionFilesMount.types';
11
-import { RemoteFileDownloadError, ValidationError } from '../errors';
11
+import {
12
+ ComposioBlockedInternalUrlError,
13
+ RemoteFileDownloadError,
14
+ ValidationError,
15
+} from '../errors';
16
+import { readResponseBodyWithLimit } from '../utils/readResponseBody';
17
18
/**
19
* Represents a file stored in a tool router session's file mount.
82
return platform.basename(this.mountRelativePath);
83
}
84
85
+ private downloadError(message: string, cause: unknown, response?: Response) {
86
+ return new RemoteFileDownloadError(message, {
87
+ statusCode: response?.status,
88
+ statusText: response?.statusText,
89
+ downloadUrl: this.downloadUrl,
90
+ mountRelativePath: this.mountRelativePath,
91
+ filename: this.filename,
92
+ cause,
93
+ });
94
+ }
95
+
96
+ private async downloadBytes(): Promise<{ content: Uint8Array; contentType: string }> {
97
+ let response: Response;
98
+ try {
99
+ response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
100
+ } catch (error) {
101
+ if (error instanceof ComposioBlockedInternalUrlError) {
102
+ throw error;
103
+ }
104
+ throw this.downloadError('Failed to download file', error);
105
+ }
106
+
107
+ if (!response.ok) {
108
+ await response.body?.cancel().catch(() => undefined);
109
+ throw this.downloadError(
110
+ `Failed to download file: ${response.status} ${response.statusText}`,
111
+ new Error(`HTTP ${response.status}: ${response.statusText}`),
112
+ response
113
+ );
114
+ }
115
+
116
+ try {
117
+ const content = response.body
118
+ ? await readResponseBodyWithLimit(response)
119
+ : new Uint8Array(await response.arrayBuffer());
120
+ return {
121
+ content,
122
+ contentType: response.headers?.get?.('content-type') ?? '',
123
+ };
124
+ } catch (error) {
125
+ await response.body?.cancel().catch(() => undefined);
126
+ throw this.downloadError('Failed to read downloaded file', error, response);
127
+ }
128
+ }
129
+
130
/**
131
* Fetches the file content as a buffer.
132
* @returns The file content as a Uint8Array
133
* @throws RemoteFileDownloadError if the fetch fails
134
*/
135
async buffer(): Promise<Uint8Array> {
86
- // SSRF guard: `downloadUrl` is set from an API response, so it is untrusted
87
- // input like every other response field, and its bytes are handed straight
88
- // back to the caller. See ssrfGuard.node.ts.
89
- const response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
90
- if (!response.ok) {
91
- throw new RemoteFileDownloadError(
92
- `Failed to download file: ${response.status} ${response.statusText}`,
93
- {
94
- statusCode: response.status,
95
- statusText: response.statusText,
96
- downloadUrl: this.downloadUrl,
97
- mountRelativePath: this.mountRelativePath,
98
- filename: this.filename,
99
- cause: new Error(`HTTP ${response.status}: ${response.statusText}`),
100
- }
101
- );
102
- }
103
- const arrayBuffer = await response.arrayBuffer();
104
- return new Uint8Array(arrayBuffer);
136
+ return (await this.downloadBytes()).content;
137
}
138
139
/**
152
* @throws RemoteFileDownloadError if the fetch fails
153
*/
154
async blob(): Promise<Blob> {
123
- // SSRF guard: see `buffer()`.
124
- const response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
125
- if (!response.ok) {
126
- throw new RemoteFileDownloadError(
127
- `Failed to download file: ${response.status} ${response.statusText}`,
128
- {
129
- statusCode: response.status,
130
- statusText: response.statusText,
131
- downloadUrl: this.downloadUrl,
132
- mountRelativePath: this.mountRelativePath,
133
- filename: this.filename,
134
- cause: new Error(`HTTP ${response.status}: ${response.statusText}`),
135
- }
136
- );
137
- }
138
- return response.blob();
155
+ const { content, contentType } = await this.downloadBytes();
156
+ return new Blob([content as BlobPart], { type: contentType });
157
}
158
159
/**