Skip to content

Commit ea9fbc1

Browse files
authored
fix(desktop): open cursor:// and vscode:// file links from chat (pingdotgg#23486)
1 parent 3da48c1 commit ea9fbc1

9 files changed

Lines changed: 227 additions & 2 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,67 @@ 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.forEach(urls, (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.forEach(
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+
],
110+
(url) => electronShell.openExternal(url),
111+
);
112+
113+
assert.deepEqual(results, Array(12).fill(false));
114+
assert.equal(openExternalMock.mock.calls.length, 0);
115+
}).pipe(Effect.provide(ElectronShell.layer)),
116+
);
117+
57118
it.effect("does not open editor URLs that mix up link shapes", () =>
58119
Effect.gen(function* () {
59120
openExternalMock.mockResolvedValue(undefined);

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

Lines changed: 5 additions & 2 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,
@@ -11,7 +12,7 @@ import * as Option from "effect/Option";
1112

1213
import * as Electron from "electron";
1314

14-
// Remote editor deep links must reach the OS handler.
15+
// Remote editor deep links and local editor file links must reach the OS handler.
1516
const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
1617
const REMOTE_EDITOR_PROTOCOLS = new Set(
1718
REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
@@ -44,7 +45,9 @@ export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
4445

4546
try {
4647
const url = new URL(rawUrl);
47-
return SAFE_WEB_PROTOCOLS.has(url.protocol) || isRemoteEditorUrl(url)
48+
return SAFE_WEB_PROTOCOLS.has(url.protocol) ||
49+
isRemoteEditorUrl(url) ||
50+
isLocalEditorFileUrl(url)
4851
? Option.some(url.href)
4952
: Option.none();
5053
} catch {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,31 @@ describe("ChatMarkdown inline code selection", () => {
433433
});
434434
});
435435

436+
describe("ChatMarkdown editor deep links", () => {
437+
const hrefs = (container: HTMLElement) =>
438+
[...container.querySelectorAll("a")].map((link) => link.getAttribute("href"));
439+
440+
it("keeps local editor file links", async () => {
441+
setup();
442+
await using screen = await renderText(
443+
"[file.md line 3](cursor://file/Users/me/repo/file.md:3) and [same](vscode://file/Users/me/repo/file.md:3:7)",
444+
);
445+
expect(hrefs(screen.container)).toEqual([
446+
"cursor://file/Users/me/repo/file.md:3",
447+
"vscode://file/Users/me/repo/file.md:3:7",
448+
]);
449+
});
450+
451+
it("still drops other editor links and unknown schemes", async () => {
452+
setup();
453+
await using screen = await renderText(
454+
"[prompt](cursor://anysphere.cursor-deeplink/prompt?text=hi) [user](cursor://me@file/Users/me/file.md) [unknown](foo://file/Users/me/file.md)",
455+
);
456+
expect(screen.container.innerHTML).not.toContain("://");
457+
expect(hrefs(screen.container).filter(Boolean)).toEqual([]);
458+
});
459+
});
460+
436461
describe("ChatMarkdown Windows file links", () => {
437462
const environmentId = EnvironmentId.make("env-windows");
438463
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
@@ -136,6 +136,7 @@ import { useRenderedAppearance } from "../hooks/useTheme";
136136
import { useClientSettings } from "../hooks/useSettings";
137137
import { chatMarkdownClipboardPayload } from "../markdown-clipboard";
138138
import {
139+
isLocalEditorFileHref,
139140
resolveInlineCodeFileLinkMeta,
140141
resolveMarkdownFileLinkMeta,
141142
rewriteMarkdownFileUriHref,
@@ -399,6 +400,7 @@ function markdownUrlTransform(href: string) {
399400
if (parseComposerContextHref(href)) return href;
400401
if (parseThreadLinkHref(href)) return href;
401402
if (isWindowsDrivePathHref(href)) return href;
403+
if (isLocalEditorFileHref(href)) return href;
402404
return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
403405
}
404406

‎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
@@ -174,6 +174,19 @@ export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
174174
return editor === undefined ? undefined : remoteSchemeOf(editor);
175175
};
176176

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

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,62 @@ it("builds the tree rehype-raw builds for markdown without HTML", () => {
204204
}
205205
});
206206

207+
it("keeps local editor file links but not other schemes through the shared Markdown sanitizer", () => {
208+
const processor = unified()
209+
.use(remarkParse)
210+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
211+
.use(remarkRehype, { allowDangerousHtml: true })
212+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
213+
const tree = processor.runSync(
214+
processor.parse(
215+
[
216+
"[cursor](cursor://file/Users/me/repo/file.md:3)",
217+
"[code](vscode://file/Users/me/repo/file.md:3:7)",
218+
"[insiders](vscode-insiders://file/Users/me/repo/file.md)",
219+
"[unknown](foo://file/Users/me/repo/file.md)",
220+
"[zed](zed://file/Users/me/repo/file.md)",
221+
].join(" "),
222+
),
223+
);
224+
type Node = { tagName?: string; properties?: { href?: string }; children?: Node[] };
225+
const hrefs = ((tree.children[0] as Node).children ?? [])
226+
.filter((node) => node.tagName === "a")
227+
.map((node) => node.properties?.href);
228+
expect(hrefs).toEqual([
229+
"cursor://file/Users/me/repo/file.md:3",
230+
"vscode://file/Users/me/repo/file.md:3:7",
231+
"vscode-insiders://file/Users/me/repo/file.md",
232+
undefined,
233+
undefined,
234+
]);
235+
});
236+
237+
it("keeps mixed-case local editor file links by lowercasing the scheme before sanitizing", () => {
238+
const processor = unified()
239+
.use(remarkParse)
240+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
241+
.use(remarkRehype, { allowDangerousHtml: true })
242+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
243+
const tree = processor.runSync(
244+
processor.parse(
245+
[
246+
"[upper](VSCODE://file/Users/me/repo/file.md:3:7)",
247+
"[mixed](Cursor://file/Users/me/repo/file.md:3)",
248+
"[other](FOO://file/Users/me/repo/file.md)",
249+
].join(" "),
250+
),
251+
);
252+
type Node = { tagName?: string; properties?: { href?: string }; children?: Node[] };
253+
const hrefs = ((tree.children[0] as Node).children ?? [])
254+
.filter((node) => node.tagName === "a")
255+
.map((node) => node.properties?.href);
256+
expect(hrefs).toEqual([
257+
"vscode://file/Users/me/repo/file.md:3:7",
258+
"cursor://file/Users/me/repo/file.md:3",
259+
undefined,
260+
]);
261+
});
262+
207263
it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
208264
const processor = unified()
209265
.use(remarkParse)

‎packages/shared/src/markdownPipeline.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isLocalEditorFileUrl, LOCAL_FILE_LINK_SCHEMES } from "@t3tools/contracts";
12
import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";
23
import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts";
34
import { type PluggableList, type Processor, type Transformer, unified } from "unified";
@@ -164,6 +165,26 @@ function rehypePreserveImageSourceMeta() {
164165

165166
const IMAGE_SRC_PROTOCOLS = [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"];
166167

168+
// The sanitizer compares schemes case-sensitively, so lowercase accepted editor links first.
169+
function rehypeNormalizeLocalEditorLinks() {
170+
return (tree: MarkdownImageHastNode) => {
171+
const visit = (node: MarkdownImageHastNode) => {
172+
const href = node.properties?.href;
173+
if (node.type === "element" && node.tagName === "a" && typeof href === "string") {
174+
try {
175+
const url = new URL(href);
176+
if (isLocalEditorFileUrl(url)) node.properties = { ...node.properties, href: url.href };
177+
} catch {
178+
// Not an absolute URL; the sanitizer decides.
179+
}
180+
}
181+
node.children?.forEach(visit);
182+
};
183+
184+
visit(tree);
185+
};
186+
}
187+
167188
const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
168189
...defaultSchema,
169190
attributes: {
@@ -195,6 +216,8 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
195216
"t3-citation",
196217
"t3-context",
197218
THREAD_LINK_PROTOCOL,
219+
// Scheme only: the renderer's URL transform checks the rest of the link shape.
220+
...LOCAL_FILE_LINK_SCHEMES,
198221
],
199222
src: IMAGE_SRC_PROTOCOLS,
200223
dataDarkSrc: IMAGE_SRC_PROTOCOLS,
@@ -301,6 +324,7 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
301324
rehypePreserveBareAnchorPlaceholders,
302325
rehypeRawWhenPresent,
303326
rehypePreserveImageSourceMeta,
327+
rehypeNormalizeLocalEditorLinks,
304328
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
305329
];
306330

0 commit comments

Comments
 (0)