Open History to see the last commit
1const DIRECT_IMAGE_SOURCE_PATTERN = /^(?:https?:|data:|blob:|\/\/)/i;
2const URI_SCHEME_PATTERN = /^[A-Za-z][A-Za-z0-9+.-]*:/;
3const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
4
5export type MarkdownImageSource =
6 | { readonly _tag: "Direct"; readonly uri: string }
7 | { readonly _tag: "WorkspaceFile"; readonly path: string }
8 | { readonly _tag: "Blocked" };
9
10function safeDecode(value: string): string {
11 try {
12 return decodeURIComponent(value);
13 } catch {
14 return value;
15 }
16}
17
18function normalizeSource(value: string): string {
19 const trimmed = value.trim();
20 return trimmed.startsWith("<") && trimmed.endsWith(">") ? trimmed.slice(1, -1) : trimmed;
21}
22
23function normalizeWindowsDrivePath(value: string): string {
24 return /^\/[A-Za-z]:[\\/]/.test(value) ? value.slice(1) : value;
25}
26
27function parseFileUrl(value: string): string | null {
28 try {
29 const parsed = new URL(value);
30 if (parsed.protocol.toLowerCase() !== "file:") return null;
31
32 if (parsed.hostname.length > 0 && parsed.hostname.toLowerCase() !== "localhost") {
33 const pathname = safeDecode(parsed.pathname).replaceAll("/", "\\");
34 return `\\\\${safeDecode(parsed.hostname)}${pathname}`;
35 }
36
37 const pathname = safeDecode(parsed.pathname);
38 if (pathname.length === 0) return null;
39 return normalizeWindowsDrivePath(pathname);
40 } catch {
41 return null;
42 }
43}
44
45function stripSearchAndHash(value: string): string {
46 const searchIndex = value.indexOf("?");
47 const hashIndex = value.indexOf("#");
48 const end = [searchIndex, hashIndex]
49 .filter((index) => index >= 0)
50 .reduce((lowest, index) => Math.min(lowest, index), value.length);
51 return value.slice(0, end);
52}
53
54function joinWorkspacePath(workspaceRoot: string, relativePath: string): string {
55 const windows =
56 WINDOWS_DRIVE_PATH_PATTERN.test(workspaceRoot) || workspaceRoot.startsWith("\\\\");
57 const separator = windows ? "\\" : "/";
58 const root = workspaceRoot.replace(/[\\/]+$/, "");
59 const path = relativePath.replace(/[\\/]/g, separator).replace(/^[\\/]+/, "");
60 return `${root}${separator}${path}`;
61}
62
63/**
64 * Classifies a markdown image source by where its bytes must be loaded from.
65 * Filesystem paths belong to the environment host and must never reach a
66 * browser or native image component without first becoming a signed asset URL.
67 */
68export function classifyMarkdownImageSource(
69 value: string | null | undefined,
70 workspaceRoot?: string | null,
71): MarkdownImageSource {
72 if (value === null || value === undefined) return { _tag: "Blocked" };
73
74 const source = normalizeSource(value);
75 if (source.length === 0 || source.startsWith("#") || source.startsWith("?")) {
76 return { _tag: "Blocked" };
77 }
78 if (DIRECT_IMAGE_SOURCE_PATTERN.test(source)) {
79 return { _tag: "Direct", uri: source };
80 }
81
82 if (/^file:/i.test(source)) {
83 const path = parseFileUrl(source);
84 return path === null ? { _tag: "Blocked" } : { _tag: "WorkspaceFile", path };
85 }
86
87 const path = normalizeWindowsDrivePath(safeDecode(stripSearchAndHash(source)));
88 if (path.length === 0) return { _tag: "Blocked" };
89 if (path.startsWith("/") || WINDOWS_DRIVE_PATH_PATTERN.test(path) || path.startsWith("\\\\")) {
90 return { _tag: "WorkspaceFile", path };
91 }
92 if (URI_SCHEME_PATTERN.test(path) || path.startsWith("~/") || path.startsWith("~\\")) {
93 return { _tag: "Blocked" };
94 }
95 if (!workspaceRoot) return { _tag: "Blocked" };
96
97 return { _tag: "WorkspaceFile", path: joinWorkspacePath(workspaceRoot, path) };
98}