Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

gui: sanitize markdown tool-result links

Pranav Pcf5cb19fb849
diff --git a/gui/src/components/chat/ChatActivityRow.tsx b/gui/src/components/chat/ChatActivityRow.tsx
index b7809e61..7ab62b77 100644
--- a/gui/src/components/chat/ChatActivityRow.tsx
+++ b/gui/src/components/chat/ChatActivityRow.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef, useState, type ComponentType } from "react";
+import { useEffect, useRef, useState, type ReactNode } from "react";
import {
Activity,
Bot,
@@ -52,47 +52,48 @@ const activityTextClassName = cn(
CHAT_MUTED_TEXT_CLASS,
);
-function unknownToolIcon(name: string): ComponentType<{ className?: string }> {
+function renderUnknownToolIcon(name: string, className: string): ReactNode {
switch (classifyUnknownToolName(name)) {
case "web":
- return Globe;
+ return <Globe className={className} />;
case "github":
- return GitPullRequest;
+ return <GitPullRequest className={className} />;
default:
- return Activity;
+ return <Activity className={className} />;
}
}
-function operationIcon(
+function renderOperationIcon(
operation: ActivityOperation,
-): ComponentType<{ className?: string }> {
+ className: string,
+): ReactNode {
switch (operation.detail.kind) {
case "file_read":
- return FileText;
+ return <FileText className={className} />;
case "file_update":
- return Pencil;
+ return <Pencil className={className} />;
case "shell":
- return Terminal;
+ return <Terminal className={className} />;
case "search":
case "codebase_search":
- return Search;
+ return <Search className={className} />;
case "fetch":
- return Globe;
+ return <Globe className={className} />;
case "followup":
- return MessageCircle;
+ return <MessageCircle className={className} />;
case "plan":
- return Map;
+ return <Map className={className} />;
case "skill":
- return Sparkles;
+ return <Sparkles className={className} />;
case "task":
- return Bot;
+ return <Bot className={className} />;
case "todo_read":
case "todo_write":
- return ListTodo;
+ return <ListTodo className={className} />;
case "unknown":
- return unknownToolIcon(operation.name);
+ return renderUnknownToolIcon(operation.name, className);
default:
- return Activity;
+ return <Activity className={className} />;
}
}
@@ -125,11 +126,14 @@ function ActivityOperationRow({
const isExpandable = result != null;
const label = formatOperationLabel(operation, workspacePath);
const isCommand = operation.detail.kind === "shell";
- const Icon = operationIcon(operation);
+ const icon = renderOperationIcon(
+ operation,
+ "size-3.5 shrink-0 text-muted-foreground/60",
+ );
const content = (
<>
- <Icon className="size-3.5 shrink-0 text-muted-foreground/60" />
+ {icon}
<span className="min-w-0">
{isCommand ? (
<code className="rounded-md bg-muted px-1.5 py-0.5 font-mono text-xs text-foreground">
diff --git a/gui/src/components/chat/markdown/MarkdownRenderer.test.tsx b/gui/src/components/chat/markdown/MarkdownRenderer.test.tsx
index b8da1c9b..dbb2116e 100644
--- a/gui/src/components/chat/markdown/MarkdownRenderer.test.tsx
+++ b/gui/src/components/chat/markdown/MarkdownRenderer.test.tsx
@@ -15,3 +15,37 @@ test("autolinks a bare URL in plain text into a clickable control", () => {
expect(html).toContain("<button");
expect(html).toContain("https://react.dev/learn");
});
+
+test("renders unsafe markdown link schemes as plain text", () => {
+ const html = renderToStaticMarkup(
+ <MarkdownRenderer text="[click me](javascript:alert(1)) and [payload](data:text/html,test)" />,
+ );
+
+ expect(html).toContain("click me");
+ expect(html).toContain("payload");
+ expect(html).not.toContain("<a");
+ expect(html).not.toContain("href=");
+ expect(html).not.toContain("javascript:");
+ expect(html).not.toContain("data:text/html");
+});
+
+test("allows explicit https and mailto markdown links", () => {
+ const html = renderToStaticMarkup(
+ <MarkdownRenderer text="[docs](https://react.dev/learn) [mail](mailto:[email protected])" />,
+ );
+
+ expect(html).toContain('href="https://react.dev/learn"');
+ expect(html).toContain('href="mailto:[email protected]"');
+});
+
+test("allows markdown links to workspace file paths", () => {
+ const html = renderToStaticMarkup(
+ <MarkdownRenderer
+ text="[source](src/app.ts) [absolute](/Users/example/project/src/app.ts)"
+ workspacePath="/Users/example/project"
+ />,
+ );
+
+ expect(html).toContain('href="src/app.ts"');
+ expect(html).toContain('href="/Users/example/project/src/app.ts"');
+});
diff --git a/gui/src/components/chat/markdown/MarkdownRenderer.tsx b/gui/src/components/chat/markdown/MarkdownRenderer.tsx
index 29cfefb1..c0252d92 100644
--- a/gui/src/components/chat/markdown/MarkdownRenderer.tsx
+++ b/gui/src/components/chat/markdown/MarkdownRenderer.tsx
@@ -7,7 +7,7 @@ import { CHAT_BODY_TEXT_CLASS } from "../constants/chatStyles";
import {
isExternalHttpUrl,
resolveChatFilePathTarget,
- rewriteChatFileUriHref,
+ sanitizeChatMarkdownHref,
} from "../utils/chatLinks";
import { openFilePathFromChat, openUrlFromChat } from "../utils/chatOpen";
import { CodeBlock } from "./CodeBlock";
@@ -196,15 +196,11 @@ function LinkNode({
node: Extract<MdInline, { type: "link" }>;
workspacePath?: string | null;
}) {
- const href = rewriteChatFileUriHref(node.href) ?? node.href;
+ const href = sanitizeChatMarkdownHref(node.href, workspacePath);
if (!href) {
return (
- <span className="text-[color:var(--accent)]">
- <ChatInlineChildren workspacePath={workspacePath}>
- {renderInline(node.children, workspacePath)}
- </ChatInlineChildren>
- </span>
+ <span>{renderInline(node.children, workspacePath)}</span>
);
}
diff --git a/gui/src/components/chat/utils/chatLinks.ts b/gui/src/components/chat/utils/chatLinks.ts
index d0699904..6d3d1df3 100644
--- a/gui/src/components/chat/utils/chatLinks.ts
+++ b/gui/src/components/chat/utils/chatLinks.ts
@@ -53,6 +53,10 @@ export function isExternalHttpUrl(href: string): boolean {
return /^https?:\/\//iu.test(href);
}
+export function isMailtoUrl(href: string): boolean {
+ return /^mailto:/iu.test(href);
+}
+
function trimLinkCandidate(candidate: string): string {
let value = candidate.replace(TRAILING_PUNCTUATION_PATTERN, "");
@@ -141,6 +145,32 @@ export function rewriteChatFileUriHref(href: string | undefined): string | null
return `${target.path}${target.hash}`;
}
+export function sanitizeChatMarkdownHref(
+ href: string | undefined,
+ workspacePath?: string | null,
+): string | null {
+ if (href == null) {
+ return null;
+ }
+
+ const trimmed = href.trim();
+ if (trimmed.length === 0) {
+ return null;
+ }
+
+ const rewrittenFileHref = rewriteChatFileUriHref(trimmed);
+ const candidate = rewrittenFileHref ?? trimmed;
+ if (isExternalHttpUrl(candidate) || isMailtoUrl(candidate)) {
+ return candidate;
+ }
+
+ if (resolveChatFilePathTarget(candidate, workspacePath) != null) {
+ return candidate;
+ }
+
+ return null;
+}
+
function looksLikePosixFilesystemPath(path: string): boolean {
if (!path.startsWith("/")) {
return false;