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 | /** |
| 2 | * Runtime-neutral helpers for normalizing user-provided filesystem paths. |
| 3 | * |
| 4 | * In filesystem-less runtimes (Cloudflare Workers, etc.) the platform |
| 5 | * abstraction reports `supportsFileSystem: false` and `homedir()` returns |
| 6 | * `null`. We use that as the signal to drop unresolvable entries from the |
| 7 | * allowlist (fail-closed) rather than silently passing `~/foo` through as |
| 8 | * a literal directory name. |
| 9 | */ |
| 10 | |
| 11 | import { platform } from '#platform'; |
| 12 | |
| 13 | /** |
| 14 | * Expands a leading `~` to the user's home directory and resolves to an |
| 15 | * absolute path. Returns `undefined` when expansion is required but no |
| 16 | * home directory is available — that signals "unusable, drop me" to |
| 17 | * {@link expandHomeAndResolveMany}, which is safer than letting the literal |
| 18 | * `~/foo` survive as a CWD-relative path. |
| 19 | */ |
| 20 | export function expandHomeAndResolve(p: string | undefined): string | undefined { |
| 21 | if (!p) return p; |
| 22 | let expanded = p; |
| 23 | if (p === '~' || p.startsWith('~/') || p.startsWith('~\\')) { |
| 24 | const home = platform.homedir(); |
| 25 | if (!home) return undefined; |
| 26 | expanded = p === '~' ? home : platform.joinPath(home, p.slice(2)); |
| 27 | } |
| 28 | return platform.resolvePath(expanded); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Applies {@link expandHomeAndResolve} to each entry, dropping empty/invalid |
| 33 | * values. Preserves `undefined` and `false` (so downstream can distinguish |
| 34 | * "user did not configure" from "user explicitly disabled local uploads" |
| 35 | * from "user configured []"). |
| 36 | */ |
| 37 | export function expandHomeAndResolveMany( |
| 38 | list: string[] | false | undefined |
| 39 | ): string[] | false | undefined { |
| 40 | if (list === undefined) return undefined; |
| 41 | if (list === false) return false; |
| 42 | const out: string[] = []; |
| 43 | for (const raw of list) { |
| 44 | if (typeof raw !== 'string' || raw.trim() === '') continue; |
| 45 | const resolved = expandHomeAndResolve(raw); |
| 46 | if (resolved) out.push(resolved); |
| 47 | } |
| 48 | return out; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Absolute path of the default upload staging directory |
| 53 | * (`<home>/.composio/temp`), or `null` when a home directory is unavailable |
| 54 | * (e.g. Cloudflare Workers). |
| 55 | */ |
| 56 | export function getDefaultUploadDir(): string | null { |
| 57 | const home = platform.homedir(); |
| 58 | if (!home) return null; |
| 59 | return platform.resolvePath(platform.joinPath(home, '.composio', 'temp')); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Resolve the effective upload allowlist for a given user-provided value. |
| 64 | * |
| 65 | * - `undefined` → `[<default>]` when a home directory is available, else `[]` |
| 66 | * (fail-closed if we can't derive the default staging dir). |
| 67 | * - `false` → `[]` (explicit "reject all local paths"). URLs / File objects |
| 68 | * still work because they aren't path-checked. |
| 69 | * - `string[]` (including `[]`) replaces the default and is returned |
| 70 | * verbatim after `~` expansion / `path.resolve`. `[]` is treated as |
| 71 | * equivalent to `false`. |
| 72 | */ |
| 73 | export function resolveEffectiveUploadAllowlist(userDirs: string[] | false | undefined): string[] { |
| 74 | if (userDirs === undefined) { |
| 75 | const def = getDefaultUploadDir(); |
| 76 | return def ? [def] : []; |
| 77 | } |
| 78 | if (userDirs === false) return []; |
| 79 | const resolved = expandHomeAndResolveMany(userDirs); |
| 80 | return Array.isArray(resolved) ? resolved : []; |
| 81 | } |