Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(chat): hold remote images in replies until the owner asks
trohitg22fa923ef0d6
diff --git a/client/src/components/ui/ConsolePanel.tsx b/client/src/components/ui/ConsolePanel.tsxindex 9ee73c3c..a0184987 100644--- a/client/src/components/ui/ConsolePanel.tsx+++ b/client/src/components/ui/ConsolePanel.tsx@@ -35,6 +35,7 @@ import {SelectValue,} from '@/components/ui/select';import { cn } from '@/lib/utils';+import { CHAT_MARKDOWN_COMPONENTS } from '@/features/chat';import { resolveNodeDescription } from '../../lib/nodeSpec';import { useAppStore } from '../../store/useAppStore';import { usePanelResize } from '../../hooks/usePanelResize';@@ -440,7 +441,7 @@ const ConsolePanel: React.FC<ConsolePanelProps> = ({</pre>) : (<div className="chat-markdown text-sm leading-snug text-foreground">- <ReactMarkdown remarkPlugins={[remarkGfm, remarkBreaks]}>+ <ReactMarkdown remarkPlugins={[remarkGfm, remarkBreaks]} components={CHAT_MARKDOWN_COMPONENTS}>{msg.message}</ReactMarkdown></div>diff --git a/client/src/features/chat/__tests__/markdown.test.tsx b/client/src/features/chat/__tests__/markdown.test.tsxnew file mode 100644index 00000000..df9e659c--- /dev/null+++ b/client/src/features/chat/__tests__/markdown.test.tsx@@ -0,0 +1,62 @@+/**+ * Markdown in chat replies: images from outside the workspace wait for the+ * owner (a reply could otherwise leak data through an image URL the moment+ * it renders), workspace images load, and links open in a new tab.+ */++import { describe, expect, it } from 'vitest';+import { render, screen } from '@testing-library/react';+import userEvent from '@testing-library/user-event';+import ReactMarkdown from 'react-markdown';++import { CHAT_MARKDOWN_COMPONENTS, isWorkspaceImage } from '..';++function renderReply(text: string) {+ return render(<ReactMarkdown components={CHAT_MARKDOWN_COMPONENTS}>{text}</ReactMarkdown>);+}++describe('isWorkspaceImage', () => {+ const origin = window.location.origin;++ it.each([+ ['/api/workspace/wf-1/files/shot.png', true],+ [`${origin}/api/workspace/wf-1/files/shot.png`, true],+ ['https://elsewhere.example/api/workspace/wf-1/files/shot.png', false],+ ['https://tracker.example/pixel.png?d=secret', false],+ ['/api/other/thing.png', false],+ ['', false],+ [undefined, false],+ ])('%j -> %s', (src, expected) => {+ expect(isWorkspaceImage(src)).toBe(expected);+ });+});++describe('chat markdown', () => {+ it('loads a workspace image straight away', () => {+ renderReply('');+ const img = screen.getByRole('img', { name: 'chart' });+ expect(img).toHaveAttribute('src', '/api/workspace/wf-1/files/chart.png');+ expect(img).toHaveAttribute('referrerpolicy', 'no-referrer');+ });++ it('holds a remote image until the owner asks for it', async () => {+ const user = userEvent.setup();+ renderReply('');++ expect(screen.queryByRole('img')).toBeNull();+ const reveal = screen.getByRole('button', { name: 'Show image from tracker.example' });++ await user.click(reveal);++ const img = screen.getByRole('img', { name: 'x' });+ expect(img).toHaveAttribute('src', 'https://tracker.example/pixel.png?d=secret');+ expect(img).toHaveAttribute('referrerpolicy', 'no-referrer');+ });++ it('opens links in a new tab without an opener', () => {+ renderReply('[docs](https://example.com/docs)');+ const link = screen.getByRole('link', { name: 'docs' });+ expect(link).toHaveAttribute('target', '_blank');+ expect(link).toHaveAttribute('rel', 'noopener noreferrer');+ });+});diff --git a/client/src/features/chat/index.ts b/client/src/features/chat/index.tsnew file mode 100644index 00000000..eda51bdc--- /dev/null+++ b/client/src/features/chat/index.ts@@ -0,0 +1,9 @@+/**+ * The chat shared by Home's employee page and Dev's console Chat pane+ * (docs-internal/chat_protocol.md). Import from here, not from the folders+ * inside.+ */++export { CHAT_MARKDOWN_COMPONENTS } from './markdown/components';+export { SafeImage } from './markdown/SafeImage';+export { isWorkspaceImage } from './markdown/workspaceImage';diff --git a/client/src/features/chat/markdown/SafeImage.tsx b/client/src/features/chat/markdown/SafeImage.tsxnew file mode 100644index 00000000..cf5e117f--- /dev/null+++ b/client/src/features/chat/markdown/SafeImage.tsx@@ -0,0 +1,33 @@+/**+ * An image in an employee's reply. Replies are model output, and a reply+ * steered by something the employee read could embed an image whose URL+ * carries data out to another server the moment it loads. So only images+ * served by this app's own workspace route load by themselves; any other+ * address waits behind a button that names its host, and loads without a+ * referrer once the owner asks for it.+ */++import { useState, type ImgHTMLAttributes } from 'react';+import { ImageOff } from 'lucide-react';++import { Button } from '@/components/ui/button';++import { imageHost, isWorkspaceImage } from './workspaceImage';++type SafeImageProps = ImgHTMLAttributes<HTMLImageElement> & { node?: unknown };++export function SafeImage({ node: _node, src, alt, ...rest }: SafeImageProps) {+ const source = typeof src === 'string' ? src : undefined;+ const [allowed, setAllowed] = useState(() => isWorkspaceImage(source));++ if (!source) return null;+ if (allowed) {+ return <img {...rest} src={source} alt={alt ?? ''} loading="lazy" referrerPolicy="no-referrer" />;+ }+ return (+ <Button type="button" variant="chip" size="chip" onClick={() => setAllowed(true)} title={source}>+ <ImageOff aria-hidden className="size-3.5" />+ {`Show image from ${imageHost(source)}`}+ </Button>+ );+}diff --git a/client/src/features/chat/markdown/components.tsx b/client/src/features/chat/markdown/components.tsxnew file mode 100644index 00000000..27bc3c09--- /dev/null+++ b/client/src/features/chat/markdown/components.tsx@@ -0,0 +1,14 @@+/**+ * Element overrides for markdown in chat replies, shared by Home's thread and+ * Dev's chat pane: links open in a new tab, so following one never leaves the+ * conversation, and images go through SafeImage.+ */++import type { Components } from 'react-markdown';++import { SafeImage } from './SafeImage';++export const CHAT_MARKDOWN_COMPONENTS: Components = {+ a: ({ node: _node, ...props }) => <a {...props} target="_blank" rel="noopener noreferrer" />,+ img: SafeImage,+};diff --git a/client/src/features/chat/markdown/workspaceImage.ts b/client/src/features/chat/markdown/workspaceImage.tsnew file mode 100644index 00000000..b7bd7662--- /dev/null+++ b/client/src/features/chat/markdown/workspaceImage.ts@@ -0,0 +1,34 @@+/**+ * Which image addresses in a reply may load without asking: only this app's+ * own workspace file route, which serves nothing but images, audio and video+ * inline (services/media/preview.py on the server).+ */++import { buildApiUrl } from '@/config/api';++const WORKSPACE_PREFIX = '/api/workspace/';++function apiOrigin(): string {+ return new URL(buildApiUrl('/'), window.location.origin).origin;+}++/** True when `src` points at this app's workspace file route. */+export function isWorkspaceImage(src: string | undefined): boolean {+ if (!src) return false;+ try {+ const origin = apiOrigin();+ const url = new URL(src, origin);+ return url.origin === origin && url.pathname.startsWith(WORKSPACE_PREFIX);+ } catch {+ return false;+ }+}++/** The host to name on the "Show image from …" button. */+export function imageHost(src: string): string {+ try {+ return new URL(src, window.location.origin).host || src;+ } catch {+ return src;+ }+}diff --git a/client/src/features/home/employee/ThreadMarkdown.tsx b/client/src/features/home/employee/ThreadMarkdown.tsxindex a82def73..0d3ccb6b 100644--- a/client/src/features/home/employee/ThreadMarkdown.tsx+++ b/client/src/features/home/employee/ThreadMarkdown.tsx@@ -2,22 +2,20 @@* An employee's message in the thread, as markdown (lists, links, bold),* in the type and colour of the bubble around it. Its own chunk: the talk* thread loads it lazily, so the markdown stack stays out of Home's first- * load. Links open in a new tab, so following one never leaves the- * conversation.+ * load. Links open in a new tab, and images from outside the workspace wait+ * for the owner (CHAT_MARKDOWN_COMPONENTS).*/-import ReactMarkdown, { type Components } from 'react-markdown';+import ReactMarkdown from 'react-markdown';import remarkBreaks from 'remark-breaks';import remarkGfm from 'remark-gfm';-const COMPONENTS: Components = {- a: ({ node: _node, ...props }) => <a {...props} target="_blank" rel="noopener noreferrer" />,-};+import { CHAT_MARKDOWN_COMPONENTS } from '@/features/chat';export default function ThreadMarkdown({ text }: { text: string }) {return (<div className="chat-markdown min-w-0">- <ReactMarkdown remarkPlugins={[remarkGfm, remarkBreaks]} components={COMPONENTS}>+ <ReactMarkdown remarkPlugins={[remarkGfm, remarkBreaks]} components={CHAT_MARKDOWN_COMPONENTS}>{text}</ReactMarkdown></div>