Skip to content

Commit c4c68a9

Browse files
committed
fix(desktop): open cursor:// and vscode:// file links from chat
Editor file deep links such as cursor://file/<absolute path>:<line> in a chat message did nothing when clicked. The shared Markdown sanitizer and the web URL transform both dropped the href, and the desktop external-URL gate only let http(s) and remote-editor links through, so shell.openExternal was never reached. Allow `<scheme>://file/<absolute path>[:line[:col]]` for the editors that already model a remote scheme and use VS Code's file:line:col addressing (cursor, vscode, vscode-insiders, vscodium). The host must be exactly `file`, with no userinfo or port. Zed, JetBrains and every other scheme stay blocked.
1 parent b5a226e commit c4c68a9

9 files changed

Lines changed: 181 additions & 3 deletions

File tree

‎apps/desktop/src/electron/ElectronShell.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,66 @@ describe("ElectronShell", () => {
5454
}).pipe(Effect.provide(ElectronShell.layer)),
5555
);
5656

57+
it.effect("opens local editor file deep links", () =>
58+
Effect.gen(function* () {
59+
openExternalMock.mockResolvedValue(undefined);
60+
61+
const urls = [
62+
"cursor://file/Users/me/repo/file.md:186",
63+
"vscode://file/Users/me/repo/file.md:3:7",
64+
"vscode-insiders://file/Users/me/repo/file.md",
65+
"vscodium://file/C:/Users/me/repo/file.md:3",
66+
];
67+
const electronShell = yield* ElectronShell.ElectronShell;
68+
const results = yield* Effect.all(urls.map((url) => electronShell.openExternal(url)));
69+
70+
assert.deepEqual(results, [true, true, true, true]);
71+
assert.deepEqual(
72+
openExternalMock.mock.calls,
73+
urls.map((url) => [url]),
74+
);
75+
}).pipe(Effect.provide(ElectronShell.layer)),
76+
);
77+
78+
it.effect("normalizes the scheme case of local editor file deep links", () =>
79+
Effect.gen(function* () {
80+
openExternalMock.mockResolvedValue(undefined);
81+
82+
const electronShell = yield* ElectronShell.ElectronShell;
83+
const result = yield* electronShell.openExternal("VSCode://file/Users/me/repo/file.md:3");
84+
85+
assert.equal(result, true);
86+
assert.deepEqual(openExternalMock.mock.calls, [["vscode://file/Users/me/repo/file.md:3"]]);
87+
}).pipe(Effect.provide(ElectronShell.layer)),
88+
);
89+
90+
it.effect("does not open editor URLs that are not absolute local file links", () =>
91+
Effect.gen(function* () {
92+
openExternalMock.mockResolvedValue(undefined);
93+
94+
const electronShell = yield* ElectronShell.ElectronShell;
95+
const results = yield* Effect.all(
96+
[
97+
"cursor://user@file/Users/me/repo/file.md",
98+
"cursor://:secret@file/Users/me/repo/file.md",
99+
"cursor://file:8080/Users/me/repo/file.md",
100+
"cursor://anysphere.cursor-deeplink/prompt?text=hi",
101+
"vscode://vscode.git/clone?url=https%3A%2F%2Fexample.com%2Frepo.git",
102+
"cursor://file",
103+
"cursor://file/",
104+
"cursor:file/Users/me/repo/file.md",
105+
"foo://file/Users/me/repo/file.md",
106+
"zed://file/Users/me/repo/file.md",
107+
"jetbrains://file/Users/me/repo/file.md",
108+
"javascript:alert(1)",
109+
].map((url) => electronShell.openExternal(url)),
110+
);
111+
112+
assert.deepEqual(results, Array(12).fill(false));
113+
assert.equal(openExternalMock.mock.calls.length, 0);
114+
}).pipe(Effect.provide(ElectronShell.layer)),
115+
);
116+
57117
it.effect("does not open editor URLs that mix up link shapes", () =>
58118
Effect.gen(function* () {
59119
openExternalMock.mockResolvedValue(undefined);

‎apps/desktop/src/electron/ElectronShell.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MAC_PERMISSION_SETTINGS_URLS } from "../permissions/MacPermission.ts";
22
import {
3+
isLocalEditorFileUrl,
34
REMOTE_CAPABLE_EDITOR_IDS,
45
remoteSchemeForEditor,
56
type SystemSettingsPane,
@@ -12,8 +13,9 @@ import * as Option from "effect/Option";
1213
import * as Electron from "electron";
1314

1415
// Remote open-in-editor deep links (`vscode://vscode-remote/ssh-remote+…`,
15-
// `zed://ssh/<host>/<path>`, `jetbrains://gateway/ssh/environment?h=…`) must
16-
// reach the OS handler; every other non-web scheme stays blocked.
16+
// `zed://ssh/<host>/<path>`, `jetbrains://gateway/ssh/environment?h=…`) and
17+
// local file links (`cursor://file/<absolute path>:<line>`) must reach the OS
18+
// handler; every other non-web scheme stays blocked.
1719
const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
1820
const REMOTE_EDITOR_PROTOCOLS = new Set(
1921
REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
@@ -46,7 +48,9 @@ export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
4648

4749
try {
4850
const url = new URL(rawUrl);
49-
return SAFE_WEB_PROTOCOLS.has(url.protocol) || isRemoteEditorUrl(url)
51+
return SAFE_WEB_PROTOCOLS.has(url.protocol) ||
52+
isRemoteEditorUrl(url) ||
53+
isLocalEditorFileUrl(url)
5054
? Option.some(url.href)
5155
: Option.none();
5256
} catch {

‎apps/web/src/components/ChatMarkdown.browser.test.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,31 @@ describe("ChatMarkdown headings", () => {
249249
});
250250
});
251251

252+
describe("ChatMarkdown editor deep links", () => {
253+
const hrefs = (container: HTMLElement) =>
254+
[...container.querySelectorAll("a")].map((link) => link.getAttribute("href"));
255+
256+
it("keeps local editor file links", async () => {
257+
setup();
258+
await using screen = await renderText(
259+
"[file.md line 3](cursor://file/Users/me/repo/file.md:3) and [same](vscode://file/Users/me/repo/file.md:3:7)",
260+
);
261+
expect(hrefs(screen.container)).toEqual([
262+
"cursor://file/Users/me/repo/file.md:3",
263+
"vscode://file/Users/me/repo/file.md:3:7",
264+
]);
265+
});
266+
267+
it("still drops other editor links and unknown schemes", async () => {
268+
setup();
269+
await using screen = await renderText(
270+
"[prompt](cursor://anysphere.cursor-deeplink/prompt?text=hi) [user](cursor://me@file/Users/me/file.md) [unknown](foo://file/Users/me/file.md)",
271+
);
272+
expect(screen.container.innerHTML).not.toContain("://");
273+
expect(hrefs(screen.container).filter(Boolean)).toEqual([]);
274+
});
275+
});
276+
252277
describe("ChatMarkdown Windows file links", () => {
253278
const environmentId = EnvironmentId.make("env-windows");
254279
const renderWindows = (text: string, parseRawHtml: boolean) =>

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ import { useRenderedAppearance } from "../hooks/useTheme";
129129
import { useClientSettings } from "../hooks/useSettings";
130130
import { chatMarkdownClipboardPayload } from "../markdown-clipboard";
131131
import {
132+
isLocalEditorFileHref,
132133
resolveInlineCodeFileLinkMeta,
133134
resolveMarkdownFileLinkMeta,
134135
rewriteMarkdownFileUriHref,
@@ -643,6 +644,7 @@ function useChatMarkdownState({
643644
if (parseComposerContextHref(href)) return href;
644645
if (parseThreadLinkHref(href)) return href;
645646
if (isWindowsDrivePathHref(href)) return href;
647+
if (isLocalEditorFileHref(href)) return href;
646648
return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
647649
};
648650
const handleCopy = (event: ReactClipboardEvent<HTMLElement>) => {

‎apps/web/src/markdown-links.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22

33
import {
4+
isLocalEditorFileHref,
45
resolveInlineCodeFileLinkMeta,
56
resolveMarkdownFileLinkMeta,
67
rewriteMarkdownFileUriHref,
@@ -11,6 +12,37 @@ import {
1112
resolveMarkdownFileHostPath,
1213
} from "./markdownFileHostPath";
1314

15+
describe("isLocalEditorFileHref", () => {
16+
it("accepts absolute file deep links of known editors", () => {
17+
for (const href of [
18+
"cursor://file/Users/me/repo/file.md:186",
19+
"vscode://file/Users/me/repo/file.md:3:7",
20+
"vscode-insiders://file/Users/me/repo/file.md",
21+
"VSCODE://file/C:/Users/me/repo/file.md:3",
22+
]) {
23+
expect(isLocalEditorFileHref(href), href).toBe(true);
24+
}
25+
});
26+
27+
it("rejects other hosts, userinfo, relative paths, and unknown schemes", () => {
28+
for (const href of [
29+
"cursor://anysphere.cursor-deeplink/prompt?text=hi",
30+
"vscode://user@file/Users/me/repo/file.md",
31+
"vscode://file:8080/Users/me/repo/file.md",
32+
"vscode://vscode-remote/ssh-remote+example.com/home/user/project",
33+
"cursor://file",
34+
"cursor:file/Users/me/repo/file.md",
35+
"foo://file/Users/me/repo/file.md",
36+
"zed://file/Users/me/repo/file.md",
37+
"javascript:alert(1)",
38+
"./file.md",
39+
"",
40+
]) {
41+
expect(isLocalEditorFileHref(href), href).toBe(false);
42+
}
43+
});
44+
});
45+
1446
describe("rewriteMarkdownFileUriHref", () => {
1547
it("preserves encoded octets so file paths are decoded only once later", () => {
1648
expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(

‎apps/web/src/markdown-links.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isLocalEditorFileUrl } from "@t3tools/contracts";
12
import { fileBasename, isAbsolutePath, workspaceRelativeFilePath } from "@t3tools/shared/path";
23
import {
34
inlineCodeFilePathCandidate,
@@ -39,6 +40,14 @@ export function rewriteMarkdownFileUriHref(href: string | undefined): string | n
3940
return target ? `${target.path}${target.hash}` : null;
4041
}
4142

43+
export function isLocalEditorFileHref(href: string): boolean {
44+
try {
45+
return isLocalEditorFileUrl(new URL(href));
46+
} catch {
47+
return false;
48+
}
49+
}
50+
4251
export function resolveInlineCodeFileLinkMeta(
4352
codeText: string,
4453
cwd?: string,

‎packages/contracts/src/editor.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
177177
return editor === undefined ? undefined : remoteSchemeOf(editor);
178178
};
179179

180+
// `goto` editors share VS Code's `file:line:col` addressing, so their remote schemes also open
181+
// a local file as `<scheme>://file/<absolute path>[:line[:col]]`. Zed and JetBrains differ.
182+
export const LOCAL_FILE_LINK_SCHEMES: ReadonlyArray<string> = EDITORS.flatMap((editor) =>
183+
editor.launchStyle === "goto" && "remoteScheme" in editor ? [editor.remoteScheme] : [],
184+
);
185+
186+
export const isLocalEditorFileUrl = (url: URL): boolean =>
187+
LOCAL_FILE_LINK_SCHEMES.includes(url.protocol.slice(0, -1)) &&
188+
url.host === "file" &&
189+
url.username.length === 0 &&
190+
url.password.length === 0 &&
191+
url.pathname.length > 1;
192+
180193
export const buildRemoteOpenUrl = (input: {
181194
readonly editor: EditorId;
182195
readonly host: string;

‎packages/shared/src/markdownPipeline.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,36 @@ it("preserves in-app thread links through the shared Markdown sanitizer", () =>
2828
expect(JSON.stringify(tree)).not.toContain("javascript:");
2929
});
3030

31+
it("keeps local editor file links but not other schemes through the shared Markdown sanitizer", () => {
32+
const processor = unified()
33+
.use(remarkParse)
34+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
35+
.use(remarkRehype, { allowDangerousHtml: true })
36+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
37+
const tree = processor.runSync(
38+
processor.parse(
39+
[
40+
"[cursor](cursor://file/Users/me/repo/file.md:3)",
41+
"[code](vscode://file/Users/me/repo/file.md:3:7)",
42+
"[insiders](vscode-insiders://file/Users/me/repo/file.md)",
43+
"[unknown](foo://file/Users/me/repo/file.md)",
44+
"[zed](zed://file/Users/me/repo/file.md)",
45+
].join(" "),
46+
),
47+
);
48+
type Node = { tagName?: string; properties?: { href?: string }; children?: Node[] };
49+
const hrefs = ((tree.children[0] as Node).children ?? [])
50+
.filter((node) => node.tagName === "a")
51+
.map((node) => node.properties?.href);
52+
expect(hrefs).toEqual([
53+
"cursor://file/Users/me/repo/file.md:3",
54+
"vscode://file/Users/me/repo/file.md:3:7",
55+
"vscode-insiders://file/Users/me/repo/file.md",
56+
undefined,
57+
undefined,
58+
]);
59+
});
60+
3161
it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
3262
const processor = unified()
3363
.use(remarkParse)

‎packages/shared/src/markdownPipeline.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LOCAL_FILE_LINK_SCHEMES } from "@t3tools/contracts";
12
import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";
23
import type { Processor } from "unified";
34
import { isWindowsAbsolutePath } from "./path.ts";
@@ -157,6 +158,8 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
157158
"t3-citation",
158159
"t3-context",
159160
THREAD_LINK_PROTOCOL,
161+
// Scheme only: the renderer's URL transform checks the rest of the link shape.
162+
...LOCAL_FILE_LINK_SCHEMES,
160163
],
161164
src: [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"],
162165
},

0 commit comments

Comments
 (0)