Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { ComposioBlockedInternalUrlError } from '../errors/SsrfErrors'; |
| 2 | |
| 3 | export interface SsrfSafeFetchOptions { |
| 4 | /** Maximum number of redirects to follow. Defaults to 5. */ |
| 5 | maxRedirects?: number; |
| 6 | /** |
| 7 | * Refuse a request when a configured dispatcher or environment proxy means |
| 8 | * the connection cannot be pinned to the address validated by the guard. |
| 9 | */ |
| 10 | requirePinnedConnection?: boolean; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Edge runtimes do not expose the DNS resolution APIs needed to prove that a |
| 15 | * hostname is publicly routable before connecting. Fail closed instead of |
| 16 | * falling back to an unguarded fetch. |
| 17 | */ |
| 18 | export const ssrfSafeFetch = async ( |
| 19 | rawUrl: string, |
| 20 | _init: RequestInit = {}, |
| 21 | _optionsOrMaxRedirects: SsrfSafeFetchOptions | number = {} |
| 22 | ): Promise<Response> => { |
| 23 | throw new ComposioBlockedInternalUrlError( |
| 24 | 'URL file uploads are not supported in edge runtimes because the destination cannot be safely validated', |
| 25 | { |
| 26 | url: rawUrl, |
| 27 | possibleFixes: [ |
| 28 | 'Fetch the public URL yourself and pass a File or ArrayBuffer instead', |
| 29 | 'Run the URL upload in a Node.js or Bun runtime', |
| 30 | ], |
| 31 | } |
| 32 | ); |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Unguarded `fetch`, for the call sites that must keep working here. |
| 37 | * |
| 38 | * Failing closed is the right default for a URL the caller chose to upload, but |
| 39 | * applying it to Tool Router session file transfers would take a working |
| 40 | * feature away from Workers instead of closing a hole reachable there. The |
| 41 | * guard exists to stop a URL from reaching private address space the SDK's host |
| 42 | * can see; an edge worker has no such vantage point — it cannot resolve DNS to |
| 43 | * check, and its `fetch` does not originate inside the caller's network. The |
| 44 | * Node build applies the full guard. |
| 45 | */ |
| 46 | export const ssrfSafeFetchWhereSupported = async ( |
| 47 | rawUrl: string, |
| 48 | init: RequestInit = {} |
| 49 | ): Promise<Response> => fetch(rawUrl, init); |