Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(conversations): classify chat links as external, handoff or blocked

oxoxDeva55e1f88a941
diff --git a/app/src/features/conversations/utils/format.test.ts b/app/src/features/conversations/utils/format.test.ts
new file mode 100644
index 0000000000..787ba91da8
--- /dev/null
+++ b/app/src/features/conversations/utils/format.test.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it } from 'vitest';
+
+import { classifyHref, isAllowedExternalHref, transformChatUrl } from './format';
+
+describe('classifyHref', () => {
+ it.each([
+ ['https://pay.example.com/o/1', 'external'],
+ ['http://example.com', 'external'],
+ ['mailto:[email protected]', 'external'],
+ ['phonepe://pay?id=1', 'handoff'],
+ ['upi://pay?pa=shop@bank&am=10', 'handoff'],
+ ['tez://upi/pay', 'handoff'],
+ ['workspace:docs/a.md', 'workspace'],
+ ['#citation-1', 'relative'],
+ ['javascript:alert(1)', 'blocked'],
+ ['JavaScript://%0Aalert(1)', 'blocked'],
+ ['data:text/html,hi', 'blocked'],
+ ['file:///etc/passwd', 'blocked'],
+ ['tauri://localhost', 'blocked'],
+ ['ohwidget://localhost/proxy', 'blocked'],
+ ['about:blank', 'blocked'],
+ ['upi:pay', 'blocked'],
+ ['https://', 'blocked'],
+ ['https://a.com/\nx', 'blocked'],
+ ['', 'blocked'],
+ ])('%s is %s', (href, expected) => {
+ expect(classifyHref(href)).toBe(expected);
+ });
+
+ it('keeps handoff links in markdown and blanks dangerous ones', () => {
+ expect(transformChatUrl('phonepe://pay?id=1')).toBe('phonepe://pay?id=1');
+ expect(transformChatUrl('javascript:alert(1)')).toBe('');
+ expect(transformChatUrl('#citation-2')).toBe('#citation-2');
+ });
+
+ it('only treats http(s) and mailto as openable', () => {
+ expect(isAllowedExternalHref('https://a.com')).toBe(true);
+ expect(isAllowedExternalHref('phonepe://pay')).toBe(false);
+ });
+});
diff --git a/app/src/features/conversations/utils/format.ts b/app/src/features/conversations/utils/format.ts
index 4510ffbbbf..858eb29d0c 100644
--- a/app/src/features/conversations/utils/format.ts
+++ b/app/src/features/conversations/utils/format.ts
@@ -1,3 +1,5 @@
+import { parseWorkspaceHref } from '../../../utils/workspaceLinks';
+
export function formatRelativeTime(dateStr: string): string {
const now = Date.now();
const then = new Date(dateStr).getTime();
@@ -12,12 +14,76 @@ export function formatRelativeTime(dateStr: string): string {
}
export function isAllowedExternalHref(rawHref: string): boolean {
- try {
- const url = new URL(rawHref);
- return url.protocol === 'http:' || url.protocol === 'https:' || url.protocol === 'mailto:';
- } catch {
- return false;
+ return classifyHref(rawHref) === 'external';
+}
+
+/**
+ * How a link in chat may be acted on:
+ * - `external`: `http(s)` / `mailto`, opened by the OS.
+ * - `handoff`: an app scheme such as `upi://` or `phonepe://`, meant for a
+ * phone; shown as a QR code and never opened on this machine.
+ * - `workspace`: a workspace file reference.
+ * - `relative`: an in-page fragment or relative reference.
+ * - `blocked`: anything that could run script or reach the app itself.
+ *
+ * The blocked list matches the core's (`mcp/ui/links.rs`).
+ */
+export type HrefClass = 'external' | 'handoff' | 'workspace' | 'relative' | 'blocked';
+
+const BLOCKED_SCHEMES = new Set([
+ 'javascript',
+ 'data',
+ 'vbscript',
+ 'file',
+ 'blob',
+ 'about',
+ 'tauri',
+ 'ipc',
+ 'asset',
+ 'ohwidget',
+ 'filesystem',
+ 'chrome',
+ 'chrome-extension',
+ 'view-source',
+ 'ws',
+ 'wss',
+ 'ftp',
+ 'openhuman',
+]);
+
+const SCHEME_RE = /^([a-z][a-z0-9+.-]{0,31}):/i;
+const MAX_HREF_LENGTH = 2048;
+
+export function classifyHref(rawHref: string | null | undefined): HrefClass {
+ const href = (rawHref ?? '').trim();
+ if (!href) return 'blocked';
+ if (parseWorkspaceHref(href)) return 'workspace';
+ // Control characters (including tab/newline that browsers strip) never pass.
+ // eslint-disable-next-line no-control-regex
+ if (/[\u0000-\u001f\u007f]/.test(href) || href.length > MAX_HREF_LENGTH) return 'blocked';
+ const match = SCHEME_RE.exec(href);
+ if (!match) return 'relative';
+ const scheme = match[1].toLowerCase();
+ if (BLOCKED_SCHEMES.has(scheme)) return 'blocked';
+ if (scheme === 'http' || scheme === 'https') {
+ try {
+ return new URL(href).hostname ? 'external' : 'blocked';
+ } catch {
+ return 'blocked';
+ }
}
+ if (scheme === 'mailto') return href.length > 'mailto:'.length ? 'external' : 'blocked';
+ return href.slice(match[0].length).startsWith('//') && href.length > match[0].length + 2
+ ? 'handoff'
+ : 'blocked';
+}
+
+/**
+ * The `urlTransform` both chat markdown surfaces share: keeps workspace,
+ * external, handoff and relative links; blanks everything else.
+ */
+export function transformChatUrl(url: string): string {
+ return classifyHref(url) === 'blocked' ? '' : url;
}
/**