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