From dbf5cfe0a3a7ab5969580a9e044bb369eef020e8 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 12 Nov 2025 11:10:30 -0600 Subject: [PATCH] Make `curl` virtual command use configured corsProxy instead of hardcoded --- src/components/Shakespeare/ChatPane.tsx | 4 ++-- src/components/Terminal.tsx | 6 ++++-- src/lib/commands/curl.test.ts | 3 ++- src/lib/commands/curl.ts | 9 ++++++--- src/lib/tools/ShellTool.test.ts | 3 ++- src/lib/tools/ShellTool.ts | 6 ++++-- 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 3b65c84b..72fe5752 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -166,7 +166,7 @@ export const ChatPane = forwardRef(({ nostr_read_nips_index: new NostrReadNipsIndexTool(), nostr_generate_kind: new NostrGenerateKindTool(), nostr_publish_events: new NostrPublishEventsTool(), - shell: new ShellTool(fs, cwd, git, projectsPath, user?.signer), + shell: new ShellTool(fs, cwd, git, projectsPath, config.corsProxy, user?.signer), read_console_messages: new ReadConsoleMessagesTool(), skill: new SkillTool(fs, pluginsPath), }; @@ -180,7 +180,7 @@ export const ChatPane = forwardRef(({ } return baseTools; - }, [fs, git, cwd, user, projectId, projectsPath, tmpPath, pluginsPath, handleFileChanged]); + }, [fs, git, cwd, user, projectId, projectsPath, tmpPath, pluginsPath, config.corsProxy, handleFileChanged]); // Convert tools to OpenAI format const tools = useMemo(() => { diff --git a/src/components/Terminal.tsx b/src/components/Terminal.tsx index 91d48e8e..2fccd501 100644 --- a/src/components/Terminal.tsx +++ b/src/components/Terminal.tsx @@ -8,6 +8,7 @@ import { useFS } from '@/hooks/useFS'; import { useFSPaths } from '@/hooks/useFSPaths'; import { useGit } from '@/hooks/useGit'; import { useCurrentUser } from '@/hooks/useCurrentUser'; +import { useAppContext } from '@/hooks/useAppContext'; import { ansiToHtml, containsAnsiCodes } from '@/lib/ansiToHtml'; interface TerminalProps { @@ -37,10 +38,11 @@ export function Terminal({ projectId, className }: TerminalProps) { const { projectsPath } = useFSPaths(); const { git } = useGit(); const { user } = useCurrentUser(); + const { config } = useAppContext(); // Initialize ShellTool useEffect(() => { - shellToolRef.current = new ShellTool(fs, `${projectsPath}/${projectId}`, git, projectsPath, user?.signer); + shellToolRef.current = new ShellTool(fs, `${projectsPath}/${projectId}`, git, projectsPath, config.corsProxy, user?.signer); // Add welcome message setLines([{ @@ -49,7 +51,7 @@ export function Terminal({ projectId, className }: TerminalProps) { content: `Welcome to Shakespeare Terminal\nType 'help' to see available commands.`, timestamp: new Date() }]); - }, [projectId, fs, git, projectsPath, user?.signer]); + }, [projectId, fs, git, projectsPath, config.corsProxy, user?.signer]); // Auto-scroll to bottom when new lines are added or execution state changes useEffect(() => { diff --git a/src/lib/commands/curl.test.ts b/src/lib/commands/curl.test.ts index 1fd1a84e..64735b53 100644 --- a/src/lib/commands/curl.test.ts +++ b/src/lib/commands/curl.test.ts @@ -23,9 +23,10 @@ const mockFS: JSRuntimeFS = { describe('CurlCommand', () => { let curlCommand: CurlCommand; + const testCorsProxy = 'https://proxy.shakespeare.diy/?url={href}'; beforeEach(() => { - curlCommand = new CurlCommand(mockFS); + curlCommand = new CurlCommand(mockFS, testCorsProxy); vi.clearAllMocks(); }); diff --git a/src/lib/commands/curl.ts b/src/lib/commands/curl.ts index c4d175cd..ba4cc5f0 100644 --- a/src/lib/commands/curl.ts +++ b/src/lib/commands/curl.ts @@ -3,6 +3,7 @@ import type { JSRuntimeFS } from "../JSRuntime"; import type { ShellCommand, ShellCommandResult } from "./ShellCommand"; import { createSuccessResult, createErrorResult } from "./ShellCommand"; import { isAbsolutePath, validateWritePath } from "../security"; +import { proxyUrl } from "../proxyUrl"; /** * Parsed curl options @@ -35,9 +36,11 @@ export class CurlCommand implements ShellCommand { usage = 'curl [options] '; private fs: JSRuntimeFS; + private corsProxy: string; - constructor(fs: JSRuntimeFS) { + constructor(fs: JSRuntimeFS, corsProxy: string) { this.fs = fs; + this.corsProxy = corsProxy; } async execute(args: string[], cwd: string, _input?: string): Promise { @@ -61,9 +64,9 @@ export class CurlCommand implements ShellCommand { return createErrorResult(`${this.name}: unsupported protocol: ${url.protocol}`); } - // Proxy the URL through CorsProxy + // Proxy the URL through configured CorsProxy const originalUrl = url.toString(); - const proxiedUrl = `https://proxy.shakespeare.diy/?url=${encodeURIComponent(originalUrl)}`; + const proxiedUrl = proxyUrl(this.corsProxy, originalUrl); // Prepare request const requestInit: RequestInit = { diff --git a/src/lib/tools/ShellTool.test.ts b/src/lib/tools/ShellTool.test.ts index 35f84542..3eca52e6 100644 --- a/src/lib/tools/ShellTool.test.ts +++ b/src/lib/tools/ShellTool.test.ts @@ -35,12 +35,13 @@ describe('ShellTool', () => { let mockNostr: NPool; let shellTool: ShellTool; const testCwd = '/test/dir'; + const testCorsProxy = 'https://proxy.shakespeare.diy/?url={href}'; beforeEach(() => { mockFS = createMockFS(); mockNostr = createMockNostr(); mockGit = new Git({ fs: mockFS, nostr: mockNostr }); - shellTool = new ShellTool(mockFS, testCwd, mockGit); + shellTool = new ShellTool(mockFS, testCwd, mockGit, '/projects', testCorsProxy); }); it('should have correct tool properties', () => { diff --git a/src/lib/tools/ShellTool.ts b/src/lib/tools/ShellTool.ts index 68151f7c..a312fb1b 100644 --- a/src/lib/tools/ShellTool.ts +++ b/src/lib/tools/ShellTool.ts @@ -53,6 +53,7 @@ export class ShellTool implements Tool { private commands: Map; private projectsPath: string; private signer?: NostrSigner; + private corsProxy: string; readonly description = "Execute shell commands like cat, ls, cd, pwd, rm, cp, mv, echo, head, tail, grep, find, wc, touch, mkdir, sort, uniq, cut, tr, sed, diff, which, whoami, date, env, clear, git, curl, unzip, hexdump. Supports compound commands with &&, ||, ;, and | operators, and output redirection with > and >> operators"; @@ -62,11 +63,12 @@ export class ShellTool implements Tool { ), }); - constructor(fs: JSRuntimeFS, cwd: string, git: Git, projectsPath: string = '/projects', signer?: NostrSigner) { + constructor(fs: JSRuntimeFS, cwd: string, git: Git, projectsPath: string = '/projects', corsProxy: string, signer?: NostrSigner) { this.fs = fs; this.cwd = cwd; this.git = git; this.projectsPath = projectsPath; + this.corsProxy = corsProxy; this.signer = signer; this.commands = new Map(); @@ -75,7 +77,7 @@ export class ShellTool implements Tool { this.registerCommand(new CdCommand(fs)); this.registerCommand(new ClearCommand()); this.registerCommand(new CpCommand(fs)); - this.registerCommand(new CurlCommand(fs)); + this.registerCommand(new CurlCommand(fs, this.corsProxy)); this.registerCommand(new CutCommand(fs)); this.registerCommand(new DateCommand()); this.registerCommand(new DiffCommand(fs));