Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Environment/Utility/
PathSecurity.rs

1//! # Path Security Utilities
2//!
3//! Functions for validating filesystem access and enforcing workspace trust.
4
5use std::path::{Path, PathBuf};
6
7use CommonLibrary::Error::CommonError::CommonError;
8
9use crate::{ApplicationState::State::ApplicationState::ApplicationState, dev_log};
10
11/// A critical security helper that checks if a given filesystem path is
12/// allowed for access.
13///
14/// The access model has two tiers:
15///
16/// 1. **Trusted system paths** - directories Land itself owns (user extensions,
17///    agent plugins, app-support storage, bundled extension roots). These are
18///    never "user content" and the extension scanner, VSIX installer, and
19///    global-storage probes must be able to read/write them regardless of which
20///    workspace folder is open. They bypass the workspace-folder check
21///    entirely.
22///
23/// 2. **Workspace content** - everything else is only reachable when the
24///    resolved path is a descendant of a currently registered, trusted
25///    workspace folder. That's the sandbox boundary that keeps extensions from
26///    rifling through `$HOME` via `vscode.workspace.fs`.
27///
28/// Without tier 1, the scanner's read of `~/.fiddee/extensions` is
29/// rejected as "Path is outside of the registered workspace folders", so
30/// user-installed VSIXes never reach the Extensions sidebar even though
31/// they are present on disk.
32pub fn IsPathAllowedForAccess(ApplicationState:&ApplicationState, PathToCheck:&Path) -> Result<(), CommonError> {
33	// Per-call verification line is one of the highest-volume tags
34	// (~15k hits per long session). The failure path below logs its own
35	// line; the success path is auditable from IPC-side request logs.
36	// Keep under `vfs-verbose` for deep debugging only.
37	dev_log!("vfs-verbose", "[EnvironmentSecurity] Verifying path: {}", PathToCheck.display());
38
39	// Defensive: empty path would slip through the trusted-system
40	// check (no allow-list segment matches) AND the workspace-
41	// descendant check (`Path::starts_with("")` returns true). Without
42	// this guard, an extension probing `vscode.workspace.fs.stat("")`
43	// would be authorised against ANY registered workspace folder.
44	// Reject up front so the caller falls through to its not-found
45	// handler.
46	if PathToCheck.as_os_str().is_empty() {
47		return Err(CommonError::FileSystemPermissionDenied {
48			Path:PathToCheck.to_path_buf(),
49			Reason:"Empty path: caller must supply an explicit filesystem path.".to_string(),
50		});
51	}
52
53	// Tier 1: trusted system paths bypass workspace gating. See
54	// `IsTrustedSystemPath` for the complete allow-list. Scanner reads,
55	// VSIX installs, agent-plugin probes, and per-extension global-storage
56	// stats hit this path on every boot.
57	if IsTrustedSystemPath(PathToCheck) {
58		return Ok(());
59	}
60
61	if !ApplicationState.Workspace.IsTrusted.load(std::sync::atomic::Ordering::Relaxed) {
62		return Err(CommonError::FileSystemPermissionDenied {
63			Path:PathToCheck.to_path_buf(),
64			Reason:"Workspace is not trusted. File access is denied.".to_string(),
65		});
66	}
67
68	let FoldersGuard = ApplicationState
69		.Workspace
70		.WorkspaceFolders
71		.lock()
72		.map_err(super::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
73
74	if FoldersGuard.is_empty() {
75		// Allow access if no folder is open, as operations are likely on user-chosen
76		// files. A stricter model could deny this.
77		return Ok(());
78	}
79
80	// Use canonical paths on both sides so that prefix-matching survives
81	// macOS's `/Volumes/<vol>/...` vs `/private/var/...` resolution and
82	// any symlinked submodule roots. Cocoon's URI strip yields the user-
83	// visible path (`/Volumes/<vol>/.../Land/Dependency/...`) while the
84	// workspace folder URL stays as built from `from_directory_path` -
85	// these can disagree on platforms where the resolved canonical path
86	// differs from the URI-derived one (encoded mount-point indirection,
87	// case-insensitive HFS+, etc.). Without this, a workspace with deep
88	// submodule trees rejects every read that walks past the first level
89	// even though the path is a literal descendant of the open folder.
90	let CanonicalPathToCheck =
91		crate::Cache::PathCanon::Canonicalize::Fn(PathToCheck).unwrap_or_else(|_| PathToCheck.to_path_buf());
92
93	let IsAllowed = FoldersGuard.iter().any(|Folder| {
94		let FolderPath = match Folder.URI.to_file_path() {
95			Ok(P) => P,
96			Err(_) => return false,
97		};
98		let CanonicalFolderPath =
99			crate::Cache::PathCanon::Canonicalize::Fn(&FolderPath).unwrap_or_else(|_| FolderPath.clone());
100		// Try both canonical-canonical AND raw-raw - either match wins.
101		PathToCheck.starts_with(&FolderPath)
102			|| PathToCheck.starts_with(&CanonicalFolderPath)
103			|| CanonicalPathToCheck.starts_with(&FolderPath)
104			|| CanonicalPathToCheck.starts_with(&CanonicalFolderPath)
105	});
106
107	if IsAllowed {
108		Ok(())
109	} else {
110		// Surface the comparison details so a workspace-mismatch bug
111		// (URL-to-path conversion, canonicalisation drift) is debuggable
112		// without rebuilding. Tag is `vfs` so it appears under the
113		// default `short` trace set.
114		let FolderPaths:Vec<String> = FoldersGuard
115			.iter()
116			.map(|F| {
117				F.URI
118					.to_file_path()
119					.map(|P| P.display().to_string())
120					.unwrap_or_else(|_| format!("<bad-uri:{}>", F.URI))
121			})
122			.collect();
123
124		dev_log!(
125			"vfs",
126			"[PathSecurity] reject path={} canonical={} folders=[{}]",
127			PathToCheck.display(),
128			CanonicalPathToCheck.display(),
129			FolderPaths.join(", ")
130		);
131
132		Err(CommonError::FileSystemPermissionDenied {
133			Path:PathToCheck.to_path_buf(),
134			Reason:"Path is outside of the registered workspace folders.".to_string(),
135		})
136	}
137}
138
139/// Return `true` when `PathToCheck` falls under a directory that Land itself
140/// manages and the sandbox should not gate.
141///
142/// Covered roots:
143///
144/// - `${Lodge}` (explicit override, if set).
145/// - `$HOME/.fiddee/**` - the canonical namespace for user-installed
146///   extensions, agent plugins, global storage, and any other FIDDEE-owned
147///   state that lives outside the VS Code-style profile tree. Resolved through
148///   the `Utilities::FiddeeRoot` atom.
149/// - `$HOME/.land/**` - legacy alias kept for forward-compat reads of
150///   pre-rename install trees so existing user data stays reachable until the
151///   next install migrates it.
152/// - The Mountain executable's own `extensions/`, `../Resources/extensions/`
153///   and `../Resources/app/extensions/` neighbours - built-in extension roots
154///   that ship inside the `.app` bundle.
155/// - `$APPDATA`-equivalents: Tauri's resolved app-data / app-config / app-local
156///   directories (via `$XDG_DATA_HOME`, `$XDG_CONFIG_HOME` if set; on macOS the
157///   `Library/Application Support/land.editor.*` tree).
158/// - `${TMPDIR}` + `/tmp`, `/private/tmp`, `/var/tmp` - scratch dirs language
159///   servers write their port-handoff / socket / lock files to. `TMPDIR` on
160///   macOS points at `/var/folders/.../T/` but extensions hardcode
161///   `/tmp/<tool>` directly.
162/// - Third-party tool state under `$HOME/{.gitkraken,.gk,.copilot,
163///   .config/git}` - probed by GitLens, copilot-chat, etc. Application state,
164///   not user content.
165///
166/// Anything outside this list still flows through the workspace-folder
167/// check. The set is intentionally narrow: it unblocks Land's *own*
168/// bookkeeping reads + cooperating neighbour-tool probes without
169/// handing extensions an unbounded filesystem.
170fn IsTrustedSystemPath(PathToCheck:&Path) -> bool {
171	// Canonicalising is best-effort - when the path doesn't exist yet
172	// (e.g. first-boot probes for `globalStorage/<extension>/state.json`)
173	// `canonicalize` returns Err and we compare against the raw path.
174	let Candidate =
175		crate::Cache::PathCanon::Canonicalize::Fn(PathToCheck).unwrap_or_else(|_| PathToCheck.to_path_buf());
176
177	if let Ok(Override) = std::env::var("Lodge") {
178		if !Override.is_empty() {
179			let OverridePath = PathBuf::from(&Override);
180			if Candidate.starts_with(&OverridePath) || PathToCheck.starts_with(&OverridePath) {
181				return true;
182			}
183		}
184	}
185
186	if let Ok(Home) = std::env::var("HOME") {
187		// Primary user-scope root post-rename. Resolved through the
188		// `FiddeeRoot` atom so any future rename touches a single file.
189		let FiddeeRoot = crate::IPC::WindServiceHandlers::Utilities::FiddeeRoot::FiddeeRoot();
190		if Candidate.starts_with(&FiddeeRoot) || PathToCheck.starts_with(&FiddeeRoot) {
191			return true;
192		}
193
194		// Legacy alias - pre-rename installs still hold extensions and
195		// recently-opened state under `~/.land`. Reads stay allow-listed
196		// so existing data remains visible until the user reinstalls or
197		// migrates to `~/.fiddee`.
198		let LandRoot = PathBuf::from(&Home).join(".land");
199		if Candidate.starts_with(&LandRoot) || PathToCheck.starts_with(&LandRoot) {
200			return true;
201		}
202
203		// macOS / Linux Application-Support trees that host Land's per-profile
204		// state. `land.editor.*` prefix matches every build profile variant.
205		let MacAppSupport = PathBuf::from(&Home).join("Library/Application Support");
206		if (Candidate.starts_with(&MacAppSupport) || PathToCheck.starts_with(&MacAppSupport))
207			&& ContainsLandEditorSegment(PathToCheck)
208		{
209			return true;
210		}
211
212		let XdgConfig = std::env::var("XDG_CONFIG_HOME")
213			.map(PathBuf::from)
214			.unwrap_or_else(|_| PathBuf::from(&Home).join(".config"));
215		if (Candidate.starts_with(&XdgConfig) || PathToCheck.starts_with(&XdgConfig))
216			&& ContainsLandEditorSegment(PathToCheck)
217		{
218			return true;
219		}
220
221		let XdgData = std::env::var("XDG_DATA_HOME")
222			.map(PathBuf::from)
223			.unwrap_or_else(|_| PathBuf::from(&Home).join(".local/share"));
224		if (Candidate.starts_with(&XdgData) || PathToCheck.starts_with(&XdgData))
225			&& ContainsLandEditorSegment(PathToCheck)
226		{
227			return true;
228		}
229	}
230
231	if let Ok(Exe) = std::env::current_exe() {
232		if let Some(ExeParent) = Exe.parent() {
233			let BundleRoots = [
234				ExeParent.join("extensions"),
235				ExeParent.join("../Resources/extensions"),
236				ExeParent.join("../Resources/app/extensions"),
237				// Sky's Static/Application/extensions root is reached via
238				// `../../../Sky/Target/Static/Application/extensions` in the
239				// debug profile - match the canonical `Sky/Target/Static/Application/extensions`
240				// segment regardless of how many `..` hops the scan path used.
241			];
242			for Root in BundleRoots {
243				let Normalised = crate::Cache::PathCanon::Canonicalize::Fn(&Root).unwrap_or(Root.clone());
244				if Candidate.starts_with(&Normalised) || PathToCheck.starts_with(&Root) {
245					return true;
246				}
247			}
248		}
249	}
250
251	// Sky / Dependency bundled extension trees. These are debug-profile
252	// layouts where the scanner reaches the bundle root via relative hops
253	// from the Mountain executable directory - canonicalising already
254	// resolves that, but we also fall back to a path-segment match so a
255	// missing file (first-boot probe) still clears the check.
256	if ContainsPathSegments(PathToCheck, &["Sky", "Target", "Static", "Application", "extensions"])
257		|| ContainsPathSegments(PathToCheck, &["Dependency", "Microsoft", "Dependency", "Editor", "extensions"])
258	{
259		return true;
260	}
261
262	// Sky's Target tree as a whole is build output Land controls (product.json,
263	// nls bundles, package.json, workbench bundle artifacts). gitlens reads
264	// `Sky/Target/product.json` to detect the host product; the workbench reads
265	// its own bundled metadata. None of these are user content - allowing the
266	// whole `Sky/Target/` subtree mirrors the bundled-extension carve-out
267	// above and keeps third-party probes from getting "outside workspace"
268	// rejections for files Land itself shipped.
269	if ContainsPathSegments(PathToCheck, &["Sky", "Target"])
270		|| ContainsPathSegments(PathToCheck, &["Output", "Target"])
271		|| ContainsPathSegments(PathToCheck, &["Dependency", "Microsoft", "Dependency", "Editor", "out"])
272		|| ContainsPathSegments(
273			PathToCheck,
274			&["Dependency", "Microsoft", "Dependency", "Editor", "product.json"],
275		) {
276		return true;
277	}
278
279	if let Ok(TempDir) = std::env::var("TMPDIR") {
280		let TempPath = PathBuf::from(&TempDir);
281		if !TempPath.as_os_str().is_empty() && (Candidate.starts_with(&TempPath) || PathToCheck.starts_with(&TempPath))
282		{
283			return true;
284		}
285	}
286
287	// Platform-conventional scratch roots that don't show up in `TMPDIR`
288	// on macOS/Linux. Language servers (ruby-lsp, solargraph, jdtls,
289	// pyright, …) write port-handoff / reporter / socket files under
290	// `/tmp/<tool>/` as a matter of course. `/var/folders/.../T/` IS
291	// covered by `TMPDIR` on macOS, but `/tmp` and `/private/tmp` are
292	// the ones extensions actually target. Guarding these under the
293	// system-trust tier is safe: extensions run inside Cocoon's Node
294	// host, which already has unconstrained process-level filesystem
295	// access - the sandbox only gates IPC round-trips through Mountain,
296	// not the extension's own `fs.writeFileSync`.
297	for Root in ["/tmp", "/private/tmp", "/var/tmp"] {
298		let RootPath = PathBuf::from(Root);
299		if Candidate.starts_with(&RootPath) || PathToCheck.starts_with(&RootPath) {
300			return true;
301		}
302	}
303
304	// Third-party tool state directories extensions commonly probe.
305	// GitLens stats `~/.gitkraken/workspaces/workspaces.json` to offer a
306	// "Open in GitKraken" menu; copilot-chat stats `~/.copilot/` for
307	// cached completions. These live outside Land's namespace but are
308	// not user-content either - they're application state from another
309	// tool, safe to read/stat.
310	if let Ok(Home) = std::env::var("HOME") {
311		for Suffix in [".gitkraken", ".gk", ".copilot", ".config/git"] {
312			let ToolRoot = PathBuf::from(&Home).join(Suffix);
313			if Candidate.starts_with(&ToolRoot) || PathToCheck.starts_with(&ToolRoot) {
314				return true;
315			}
316		}
317	}
318
319	// Read-only POSIX OS-info files. Many extensions (csharp, ruby-lsp,
320	// rust-analyzer, debug adapters, telemetry SDKs) probe these to
321	// branch on distro / kernel for spawning the correct binary. They
322	// are world-readable system files - the workspace-folder check
323	// rejects them as "outside workspace" but there's no plausible
324	// abuse vector. Match by full equality to keep the carve-out tight.
325	for SystemFile in [
326		"/etc/os-release",
327		"/etc/lsb-release",
328		"/etc/system-release",
329		"/etc/redhat-release",
330		"/etc/SuSE-release",
331		"/etc/debian_version",
332		"/etc/alpine-release",
333		"/etc/machine-id",
334		"/etc/timezone",
335		"/etc/localtime",
336		"/proc/version",
337		"/proc/cpuinfo",
338		"/proc/meminfo",
339		"/proc/self/status",
340		"/proc/self/cgroup",
341	] {
342		let SysPath = PathBuf::from(SystemFile);
343		if Candidate == SysPath || PathToCheck == SysPath {
344			return true;
345		}
346	}
347
348	false
349}
350
351/// True when `path` contains a directory segment whose name starts with
352/// `land.editor.`. Used to tighten the Application-Support / XDG checks so
353/// we only trust directories that Land itself provisioned, not every file
354/// under `$HOME/Library/Application Support`.
355fn ContainsLandEditorSegment(path:&Path) -> bool {
356	path.components().any(|Component| {
357		Component
358			.as_os_str()
359			.to_str()
360			.map(|Name| Name.starts_with("land.editor."))
361			.unwrap_or(false)
362	})
363}
364
365/// True when every element of `segments` appears in order as consecutive
366/// path components of `path`. Used to match Sky / Dependency extension
367/// roots regardless of which relative-path prefix the scanner used.
368fn ContainsPathSegments(path:&Path, segments:&[&str]) -> bool {
369	let Names:Vec<&str> = path.components().filter_map(|C| C.as_os_str().to_str()).collect();
370	if segments.is_empty() || Names.len() < segments.len() {
371		return false;
372	}
373	Names
374		.windows(segments.len())
375		.any(|Window| Window.iter().zip(segments.iter()).all(|(A, B)| A == B))
376}