1use std::path::{Path, PathBuf};
35use std::sync::Arc;
36
37use serde::{Deserialize, Serialize};
38
39pub const ENV_FILE: &str = "LEAN_CTX_WORKSPACE_FILE";
41pub const SCHEMA: &str = "lean-ctx.workspace/v1";
43const MAX_FILE_BYTES: u64 = 64 * 1024;
45pub const MAX_ROOTS: usize = 64;
47const MAX_ID_LEN: usize = 64;
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
52#[serde(rename_all = "snake_case")]
53pub enum Access {
54 ReadWrite,
55 ReadOnly,
56}
57
58impl Access {
59 pub fn as_str(self) -> &'static str {
60 match self {
61 Self::ReadWrite => "read_write",
62 Self::ReadOnly => "read_only",
63 }
64 }
65}
66
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68#[serde(deny_unknown_fields)]
69struct RootRecord {
70 path: String,
71 access: Access,
72}
73
74#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
75#[serde(deny_unknown_fields)]
76struct WorkspaceRecord {
77 schema: String,
78 id: String,
79 revision: u64,
80 primary: String,
81 #[serde(default)]
82 roots: Vec<RootRecord>,
83}
84
85#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct AttachedRoot {
88 pub path: PathBuf,
89 pub access: Access,
90}
91
92#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct Snapshot {
95 pub file: PathBuf,
96 pub id: String,
97 pub revision: u64,
98 pub primary: PathBuf,
100 pub roots: Vec<AttachedRoot>,
102 pub unavailable: Vec<String>,
105}
106
107impl Snapshot {
108 pub fn granting_roots(&self) -> impl Iterator<Item = (&Path, Access)> {
110 std::iter::once((self.primary.as_path(), Access::ReadWrite)).chain(
111 self.roots
112 .iter()
113 .map(|root| (root.path.as_path(), root.access)),
114 )
115 }
116
117 pub fn additional_root_strings(&self) -> Vec<String> {
119 self.roots
120 .iter()
121 .map(|root| root.path.to_string_lossy().into_owned())
122 .collect()
123 }
124
125 pub fn root_for(&self, canonical: &Path) -> Option<(&Path, Access)> {
129 self.granting_roots()
130 .filter(|(root, _)| canonical.starts_with(root))
131 .max_by_key(|(root, _)| root.components().count())
132 }
133
134 pub fn admits(&self, canonical: &Path) -> bool {
136 self.root_for(canonical).is_some()
137 }
138
139 pub fn admits_write(&self, canonical: &Path) -> bool {
141 self.root_for(canonical)
142 .is_some_and(|(_, access)| access == Access::ReadWrite)
143 }
144
145 pub fn project_root_for(&self, wanted: &str) -> String {
148 if let Ok(canonical) = crate::core::pathutil::canonicalize_secure(Path::new(wanted))
150 && self.admits(&canonical)
151 {
152 return canonical.to_string_lossy().into_owned();
153 }
154 self.primary.to_string_lossy().into_owned()
155 }
156
157 pub fn status_line(&self) -> String {
159 let mut line = format!(
160 "Workspace: {} rev {} (launcher-bound) — primary {}",
161 self.id,
162 self.revision,
163 self.primary.display()
164 );
165 for root in &self.roots {
166 line.push_str(&format!(
167 "; {} ({})",
168 root.path.display(),
169 root.access.as_str()
170 ));
171 }
172 if !self.unavailable.is_empty() {
173 line.push_str(&format!("; unavailable: {}", self.unavailable.join(", ")));
174 }
175 line
176 }
177
178 pub fn to_json(&self) -> serde_json::Value {
180 serde_json::json!({
181 "schema": SCHEMA,
182 "file": self.file,
183 "id": self.id,
184 "revision": self.revision,
185 "primary": self.primary,
186 "roots": self.roots.iter().map(|root| serde_json::json!({
187 "path": root.path,
188 "access": root.access.as_str(),
189 })).collect::<Vec<_>>(),
190 "unavailable": self.unavailable,
191 })
192 }
193}
194
195#[derive(Debug, Clone)]
197pub enum Authority {
198 Legacy,
200 Workspace(Arc<Snapshot>),
202 Unavailable(String),
204}
205
206impl Authority {
207 pub fn is_active(&self) -> bool {
208 !matches!(self, Self::Legacy)
209 }
210
211 pub fn snapshot(&self) -> Option<&Arc<Snapshot>> {
212 match self {
213 Self::Workspace(snapshot) => Some(snapshot),
214 _ => None,
215 }
216 }
217}
218
219tokio::task_local! {
220 static REQUEST: Authority;
222}
223
224pub fn configured_file() -> Option<Result<PathBuf, String>> {
228 let raw = std::env::var_os(ENV_FILE)?;
229 let path = PathBuf::from(&raw);
230 if raw.is_empty() {
231 return Some(Err(format!("{ENV_FILE} is set but empty")));
232 }
233 if !path.is_absolute() {
234 return Some(Err(format!(
235 "{ENV_FILE} must be an absolute path (got {})",
236 path.display()
237 )));
238 }
239 Some(Ok(path))
240}
241
242pub fn is_configured() -> bool {
245 REQUEST.try_with(Authority::is_active).unwrap_or(false) || std::env::var_os(ENV_FILE).is_some()
246}
247
248pub fn current() -> Authority {
251 REQUEST
252 .try_with(Clone::clone)
253 .unwrap_or_else(|_| load_current())
254}
255
256pub fn load_current() -> Authority {
258 match configured_file() {
259 None => Authority::Legacy,
260 Some(Err(reason)) => Authority::Unavailable(reason),
261 Some(Ok(path)) => match load_cached(&path) {
262 Ok(snapshot) => Authority::Workspace(snapshot),
263 Err(reason) => Authority::Unavailable(reason),
264 },
265 }
266}
267
268pub async fn scope<F: std::future::Future>(authority: Authority, request: F) -> F::Output {
270 REQUEST.scope(authority, request).await
271}
272
273pub fn sync_scope<R>(authority: Authority, operation: impl FnOnce() -> R) -> R {
276 REQUEST.sync_scope(authority, operation)
277}
278
279pub fn root_authorized(root: &Path) -> bool {
283 match current() {
284 Authority::Legacy => true,
285 Authority::Workspace(snapshot) => crate::core::pathutil::canonicalize_secure(root)
286 .is_ok_and(|canonical| snapshot.admits(&canonical)),
287 Authority::Unavailable(_) => false,
288 }
289}
290
291pub fn confine(raw: &str, project_root: &str, write: bool) -> Result<String, String> {
299 let workspace = current().is_active();
300 if !workspace && !write {
301 return Ok(raw.to_string());
302 }
303 let root = if project_root.is_empty() {
304 "."
305 } else {
306 project_root
307 };
308 let jailed = crate::core::pathjail::jail_path(Path::new(raw), Path::new(root))
309 .map_err(|error| error.to_string())?;
310 if write {
311 crate::core::pathjail::enforce_writable(&jailed)?;
312 }
313 Ok(jailed.to_string_lossy().into_owned())
314}
315
316pub fn project_dot(normalized: &str, project_root: Option<&str>) -> Result<String, String> {
320 match current() {
321 Authority::Legacy => Ok(normalized.to_string()),
322 Authority::Workspace(snapshot) => Ok(snapshot.project_root_for(project_root.unwrap_or(""))),
323 Authority::Unavailable(reason) => Err(unavailable_reason(&reason)),
324 }
325}
326
327pub fn unavailable_reason(reason: &str) -> String {
329 format!(
330 "workspace authority unavailable ({ENV_FILE}): {reason} — every path is denied until the \
331 Workspace file is valid; inspect it with `lean-ctx workspace diagnose`"
332 )
333}
334
335fn load_cached(path: &Path) -> Result<Arc<Snapshot>, String> {
341 load(path).map(Arc::new)
342}
343
344pub fn load(path: &Path) -> Result<Snapshot, String> {
346 let (file, meta) = open_checked(path)?;
347 parse_open(path, file, &meta)
348}
349
350fn parse_open(
351 path: &Path,
352 file: std::fs::File,
353 meta: &std::fs::Metadata,
354) -> Result<Snapshot, String> {
355 use std::io::Read as _;
356 if meta.len() > MAX_FILE_BYTES {
357 return Err(format!(
358 "{} is larger than {MAX_FILE_BYTES} bytes",
359 path.display()
360 ));
361 }
362 let mut text = String::new();
363 file.take(MAX_FILE_BYTES + 1)
364 .read_to_string(&mut text)
365 .map_err(|error| format!("cannot read {}: {error}", path.display()))?;
366 if text.len() as u64 > MAX_FILE_BYTES {
367 return Err(format!(
368 "{} is larger than {MAX_FILE_BYTES} bytes",
369 path.display()
370 ));
371 }
372 let record: WorkspaceRecord = serde_json::from_str(&text)
373 .map_err(|error| format!("{} is not a valid Workspace file: {error}", path.display()))?;
374 snapshot_from(path, &record)
375}
376
377fn snapshot_from(path: &Path, record: &WorkspaceRecord) -> Result<Snapshot, String> {
378 if record.schema != SCHEMA {
379 return Err(format!(
380 "unsupported Workspace schema '{}' (expected {SCHEMA})",
381 record.schema
382 ));
383 }
384 validate_id(&record.id)?;
385 if record.revision == 0 {
386 return Err("Workspace revision must be at least 1".to_string());
387 }
388 if record.roots.len() > MAX_ROOTS {
389 return Err(format!(
390 "a Workspace holds at most {MAX_ROOTS} additional roots"
391 ));
392 }
393 let primary = resolve_recorded_root(&record.primary)
394 .map_err(|reason| format!("primary root {}: {reason}", record.primary))?;
395 let mut roots: Vec<AttachedRoot> = Vec::new();
396 let mut unavailable = Vec::new();
397 let mut seen = vec![record.primary.clone()];
398 for entry in &record.roots {
399 if seen.contains(&entry.path) {
400 return Err(format!("root {} is listed twice", entry.path));
401 }
402 seen.push(entry.path.clone());
403 match resolve_recorded_root(&entry.path) {
404 Ok(path) => roots.push(AttachedRoot {
405 path,
406 access: entry.access,
407 }),
408 Err(RootError::Unavailable(_)) => unavailable.push(entry.path.clone()),
411 Err(RootError::Invalid(reason)) => {
412 return Err(format!("root {}: {reason}", entry.path));
413 }
414 }
415 }
416 let snapshot = Snapshot {
417 file: path.to_path_buf(),
418 id: record.id.clone(),
419 revision: record.revision,
420 primary,
421 roots,
422 unavailable,
423 };
424 let location = canonical_location(path)?;
427 if snapshot.admits(&location) {
428 return Err(format!(
429 "the Workspace file {} lies inside one of its own roots; keep it outside every root",
430 location.display()
431 ));
432 }
433 if own_dirs().iter().any(|dir| location.starts_with(dir)) {
436 return Err(format!(
437 "the Workspace file {} lies inside lean-ctx's own data, state, cache or config \
438 directory; keep it in a directory of the launcher",
439 location.display()
440 ));
441 }
442 Ok(snapshot)
443}
444
445enum RootError {
446 Invalid(String),
448 Unavailable(String),
450}
451
452impl std::fmt::Display for RootError {
453 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
454 match self {
455 Self::Invalid(reason) | Self::Unavailable(reason) => f.write_str(reason),
456 }
457 }
458}
459
460fn resolve_recorded_root(raw: &str) -> Result<PathBuf, RootError> {
463 let path = Path::new(raw);
464 check_root_shape(path).map_err(RootError::Invalid)?;
465 match crate::core::pathutil::canonicalize_secure(path) {
466 Ok(canonical) if canonical == path && canonical.is_dir() => Ok(canonical),
467 Ok(canonical) if canonical != path => Err(RootError::Unavailable(format!(
468 "now resolves to {} (not recorded)",
469 canonical.display()
470 ))),
471 Ok(_) => Err(RootError::Unavailable("is not a directory".to_string())),
472 Err(error) => Err(RootError::Unavailable(error.to_string())),
473 }
474}
475
476fn check_root_shape(path: &Path) -> Result<(), String> {
479 if !path.is_absolute() {
480 return Err("must be an absolute path".to_string());
481 }
482 if path.components().any(|c| {
483 matches!(
484 c,
485 std::path::Component::ParentDir | std::path::Component::CurDir
486 )
487 }) {
488 return Err("must not contain '.' or '..' components".to_string());
489 }
490 if path.to_string_lossy().as_bytes().contains(&0) {
491 return Err("contains a null byte".to_string());
492 }
493 if crate::core::pathutil::is_broad_or_unsafe_root(path) {
494 return Err("is a broad or unsafe directory (/, home, agent config)".to_string());
495 }
496 if let Some(home) = dirs::home_dir() {
497 let canonical_home =
498 crate::core::pathutil::canonicalize_secure(&home).unwrap_or(home.clone());
499 if home.starts_with(path) || canonical_home.starts_with(path) {
500 return Err("contains the home directory".to_string());
501 }
502 }
503 for own in own_dirs() {
504 if path.starts_with(&own) || own.starts_with(path) {
505 return Err("overlaps lean-ctx's own data directory".to_string());
506 }
507 }
508 Ok(())
509}
510
511fn own_dirs() -> Vec<PathBuf> {
513 [
514 crate::core::data_dir::lean_ctx_data_dir().ok(),
515 crate::core::paths::state_dir_read_only().ok(),
516 crate::core::paths::cache_dir_read_only().ok(),
517 crate::core::paths::config_dir_read_only().ok(),
518 ]
519 .into_iter()
520 .flatten()
521 .map(|dir| crate::core::pathutil::canonicalize_secure(&dir).unwrap_or(dir))
522 .collect()
523}
524
525fn validate_id(id: &str) -> Result<(), String> {
526 let valid = !id.is_empty()
527 && id.len() <= MAX_ID_LEN
528 && id
529 .chars()
530 .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
531 && !id.starts_with('.');
532 if valid {
533 Ok(())
534 } else {
535 Err(format!(
536 "Workspace id must be 1-{MAX_ID_LEN} characters of A-Z a-z 0-9 . _ - (not starting with '.')"
537 ))
538 }
539}
540
541#[cfg_attr(not(unix), allow(dead_code))]
546fn check_file_owner_mode(uid: u32, mode: u32, euid: u32) -> Result<(), String> {
547 if uid != euid {
548 return Err(format!(
549 "is owned by uid {uid}, not by this process's user (uid {euid})"
550 ));
551 }
552 if mode & 0o077 != 0 {
553 return Err(format!(
554 "has mode {:o}; it must not be accessible to group or others (chmod 600)",
555 mode & 0o7777
556 ));
557 }
558 Ok(())
559}
560
561#[cfg_attr(not(unix), allow(dead_code))]
565fn check_dir_owner_mode(uid: u32, mode: u32, euid: u32) -> Result<(), String> {
566 if uid != euid && uid != 0 {
567 return Err(format!(
568 "is owned by uid {uid}, neither this user (uid {euid}) nor root"
569 ));
570 }
571 if mode & 0o022 != 0 && mode & 0o1000 == 0 {
572 return Err(format!(
573 "has mode {:o}: writable by group or others without the sticky bit",
574 mode & 0o7777
575 ));
576 }
577 Ok(())
578}
579
580#[cfg(unix)]
581fn euid() -> u32 {
582 unsafe { libc::geteuid() }
584}
585
586fn canonical_location(path: &Path) -> Result<PathBuf, String> {
589 let name = path
590 .file_name()
591 .ok_or_else(|| format!("{} has no file name", path.display()))?;
592 let parent = path
593 .parent()
594 .ok_or_else(|| format!("{} has no parent directory", path.display()))?;
595 let parent = crate::core::pathutil::canonicalize_secure(parent)
596 .map_err(|error| format!("directory of {}: {error}", path.display()))?;
597 Ok(parent.join(name))
598}
599
600#[cfg(unix)]
601fn check_ancestors(file: &Path) -> Result<(), String> {
602 use std::os::unix::fs::MetadataExt as _;
603 let euid = euid();
604 let mut dir = file.parent();
605 while let Some(current) = dir {
606 let meta = std::fs::symlink_metadata(current)
607 .map_err(|error| format!("directory {}: {error}", current.display()))?;
608 check_dir_owner_mode(meta.uid(), meta.mode(), euid)
609 .map_err(|reason| format!("directory {} {reason}", current.display()))?;
610 dir = current.parent();
611 }
612 Ok(())
613}
614
615#[cfg(not(unix))]
618fn check_ancestors(_file: &Path) -> Result<(), String> {
619 Err(
620 "Workspace authority needs a Unix host: file ownership cannot be verified on this \
621 platform yet"
622 .to_string(),
623 )
624}
625
626fn open_checked(path: &Path) -> Result<(std::fs::File, std::fs::Metadata), String> {
629 let location = canonical_location(path)?;
630 check_ancestors(&location)?;
631 let mut options = std::fs::OpenOptions::new();
632 options.read(true);
633 #[cfg(unix)]
634 {
635 use std::os::unix::fs::OpenOptionsExt as _;
636 options.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC);
637 }
638 let link = std::fs::symlink_metadata(&location)
639 .map_err(|error| format!("cannot open {}: {error}", location.display()))?;
640 if link.file_type().is_symlink() {
641 return Err(format!(
642 "{} is a symlink; a Workspace file must not be",
643 location.display()
644 ));
645 }
646 let file = options
647 .open(&location)
648 .map_err(|error| format!("cannot open {}: {error}", location.display()))?;
649 let meta = file
650 .metadata()
651 .map_err(|error| format!("cannot inspect {}: {error}", location.display()))?;
652 if !meta.is_file() {
653 return Err(format!("{} is not a regular file", location.display()));
654 }
655 #[cfg(unix)]
656 {
657 use std::os::unix::fs::MetadataExt as _;
658 check_file_owner_mode(meta.uid(), meta.mode(), euid())
659 .map_err(|reason| format!("{} {reason}", location.display()))?;
660 }
661 Ok((file, meta))
662}
663
664struct MutationLock {
668 _file: crate::core::file_lock::FlockGuard,
669}
670
671fn lock_path(location: &Path) -> PathBuf {
672 let mut name = location.file_name().unwrap_or_default().to_os_string();
673 name.push(".lock");
674 location.with_file_name(name)
675}
676
677fn acquire_lock(location: &Path) -> Result<MutationLock, String> {
678 let path = lock_path(location);
679 let mut options = std::fs::OpenOptions::new();
680 options.read(true).write(true).create(true).truncate(false);
681 #[cfg(unix)]
682 {
683 use std::os::unix::fs::OpenOptionsExt as _;
684 options.mode(0o600);
685 options.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC);
686 }
687 let file = options
688 .open(&path)
689 .map_err(|error| format!("cannot open lock {}: {error}", path.display()))?;
690 #[cfg(unix)]
691 {
692 use std::os::unix::fs::MetadataExt as _;
693 let meta = file
694 .metadata()
695 .map_err(|error| format!("cannot inspect lock {}: {error}", path.display()))?;
696 check_file_owner_mode(meta.uid(), meta.mode(), euid())
697 .map_err(|reason| format!("lock {} {reason}", path.display()))?;
698 }
699 let file = crate::core::file_lock::FlockGuard::exclusive(file)
700 .map_err(|error| format!("cannot lock {}: {error}", path.display()))?;
701 Ok(MutationLock { _file: file })
702}
703
704fn write_record(location: &Path, record: &WorkspaceRecord) -> Result<(), String> {
705 let mut bytes = serde_json::to_vec_pretty(record).map_err(|error| error.to_string())?;
706 bytes.push(b'\n');
707 #[cfg(unix)]
708 let permissions = {
709 use std::os::unix::fs::PermissionsExt as _;
710 Some(std::fs::Permissions::from_mode(0o600))
711 };
712 #[cfg(not(unix))]
713 let permissions: Option<std::fs::Permissions> = None;
714 crate::core::atomic_fs::try_atomic_write(location, &bytes, permissions.as_ref())
715 .map_err(|error| format!("cannot write {}: {error}", location.display()))?;
716 if let Some(parent) = location.parent() {
717 crate::core::atomic_fs::sync_directory(parent)
718 .map_err(|error| format!("cannot sync {}: {error}", parent.display()))?;
719 }
720 Ok(())
721}
722
723fn read_record(location: &Path) -> Result<WorkspaceRecord, String> {
724 use std::io::Read as _;
725 let (file, meta) = open_checked(location)?;
726 if meta.len() > MAX_FILE_BYTES {
727 return Err(format!(
728 "{} is larger than {MAX_FILE_BYTES} bytes",
729 location.display()
730 ));
731 }
732 let mut text = String::new();
733 file.take(MAX_FILE_BYTES + 1)
734 .read_to_string(&mut text)
735 .map_err(|error| format!("cannot read {}: {error}", location.display()))?;
736 let record: WorkspaceRecord = serde_json::from_str(&text).map_err(|error| {
737 format!(
738 "{} is not a valid Workspace file: {error}",
739 location.display()
740 )
741 })?;
742 snapshot_from(location, &record)?;
744 Ok(record)
745}
746
747fn canonical_new_root(raw: &Path) -> Result<String, String> {
749 let absolute = if raw.is_absolute() {
750 raw.to_path_buf()
751 } else {
752 std::env::current_dir()
753 .map_err(|error| error.to_string())?
754 .join(raw)
755 };
756 let canonical = crate::core::pathutil::canonicalize_secure(&absolute)
757 .map_err(|error| format!("{}: {error}", absolute.display()))?;
758 if !canonical.is_dir() {
759 return Err(format!("{} is not a directory", canonical.display()));
760 }
761 check_root_shape(&canonical).map_err(|reason| format!("{}: {reason}", canonical.display()))?;
762 Ok(canonical.to_string_lossy().into_owned())
763}
764
765fn ensure_absolute_file(path: &Path) -> Result<(), String> {
766 if path.is_absolute() {
767 Ok(())
768 } else {
769 Err(format!(
770 "the Workspace file must be an absolute path (got {})",
771 path.display()
772 ))
773 }
774}
775
776pub fn create(file: &Path, id: &str, primary: &Path) -> Result<Snapshot, String> {
778 ensure_absolute_file(file)?;
779 validate_id(id)?;
780 let primary = canonical_new_root(primary)?;
781 let location = canonical_location(file)?;
782 check_ancestors(&location)?;
783 let _lock = acquire_lock(&location)?;
784 if std::fs::symlink_metadata(&location).is_ok() {
785 return Err(format!("{} already exists", location.display()));
786 }
787 let record = WorkspaceRecord {
788 schema: SCHEMA.to_string(),
789 id: id.to_string(),
790 revision: 1,
791 primary,
792 roots: Vec::new(),
793 };
794 snapshot_from(&location, &record)?;
795 write_record(&location, &record)?;
796 load(&location)
797}
798
799pub fn add_root(
801 file: &Path,
802 root: &Path,
803 access: Access,
804 expect_revision: u64,
805) -> Result<Snapshot, String> {
806 mutate(file, expect_revision, |record| {
807 let root = canonical_new_root(root)?;
808 if root == record.primary || record.roots.iter().any(|entry| entry.path == root) {
809 return Err(format!("{root} is already attached"));
810 }
811 if record.roots.len() >= MAX_ROOTS {
812 return Err(format!(
813 "a Workspace holds at most {MAX_ROOTS} additional roots"
814 ));
815 }
816 record.roots.push(RootRecord { path: root, access });
817 Ok(())
818 })
819}
820
821pub fn remove_root(file: &Path, root: &Path, expect_revision: u64) -> Result<Snapshot, String> {
825 mutate(file, expect_revision, |record| {
826 let wanted = crate::core::pathutil::canonicalize_secure(root)
827 .map(|p| p.to_string_lossy().into_owned())
828 .unwrap_or_else(|_| root.to_string_lossy().into_owned());
829 let raw = root.to_string_lossy().into_owned();
830 if wanted == record.primary || raw == record.primary {
831 return Err("the primary root cannot be detached".to_string());
832 }
833 let before = record.roots.len();
834 record
835 .roots
836 .retain(|entry| entry.path != wanted && entry.path != raw);
837 if record.roots.len() == before {
838 return Err(format!("{raw} is not attached"));
839 }
840 Ok(())
841 })
842}
843
844fn mutate(
845 file: &Path,
846 expect_revision: u64,
847 change: impl FnOnce(&mut WorkspaceRecord) -> Result<(), String>,
848) -> Result<Snapshot, String> {
849 ensure_absolute_file(file)?;
850 let location = canonical_location(file)?;
851 let _lock = acquire_lock(&location)?;
852 let mut record = read_record(&location)?;
853 if record.revision != expect_revision {
854 return Err(format!(
855 "revision conflict: the Workspace is at revision {}, not {expect_revision} — \
856 re-read it with `lean-ctx workspace show` and retry",
857 record.revision
858 ));
859 }
860 change(&mut record)?;
861 record.revision = record
862 .revision
863 .checked_add(1)
864 .ok_or_else(|| "revision overflow".to_string())?;
865 snapshot_from(&location, &record)?;
866 write_record(&location, &record)?;
867 load(&location)
868}
869
870pub fn diagnose_lines() -> Vec<String> {
874 match configured_file() {
875 None => vec![format!(
876 "no Workspace bound ({ENV_FILE} unset): legacy root rules apply"
877 )],
878 Some(Err(reason)) => vec![unavailable_reason(&reason)],
879 Some(Ok(path)) => match load(&path) {
880 Ok(snapshot) => {
881 let mut lines = vec![snapshot.status_line()];
882 lines.push(
883 "client roots, extra_roots/allow_paths, the home jail scope and \
884 ctx_multi_repo roots grant nothing while bound"
885 .to_string(),
886 );
887 lines
888 }
889 Err(reason) => vec![unavailable_reason(&reason)],
890 },
891 }
892}
893
894#[cfg(all(test, unix))]
897#[path = "workspace_authority_tests.rs"]
898mod enforcement_tests;
899
900#[cfg(all(test, unix))]
901mod tests {
902 use super::*;
903
904 fn private_dir() -> tempfile::TempDir {
905 let dir = tempfile::tempdir().unwrap();
906 #[cfg(unix)]
907 {
908 use std::os::unix::fs::PermissionsExt as _;
909 std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
910 }
911 dir
912 }
913
914 fn project(base: &Path, name: &str) -> PathBuf {
915 let path = base.join(name);
916 std::fs::create_dir_all(&path).unwrap();
917 crate::core::pathutil::canonicalize_secure(&path).unwrap()
918 }
919
920 #[test]
921 fn create_add_remove_with_cas() {
922 let store = private_dir();
923 let code = tempfile::tempdir().unwrap();
924 let primary = project(code.path(), "app");
925 let lib = project(code.path(), "lib");
926 let file = store.path().join("ws.json");
927
928 let created = create(&file, "team-a", &primary).unwrap();
929 assert_eq!(created.revision, 1);
930 assert_eq!(created.primary, primary);
931 assert!(created.roots.is_empty());
932
933 let added = add_root(&file, &lib, Access::ReadOnly, 1).unwrap();
934 assert_eq!(added.revision, 2);
935 assert_eq!(added.roots[0].path, lib);
936 assert!(added.admits(&lib.join("src/x.rs")));
937 assert!(!added.admits_write(&lib.join("src/x.rs")));
938 assert!(added.admits_write(&primary.join("main.rs")));
939
940 let stale = add_root(&file, &lib, Access::ReadWrite, 1).unwrap_err();
942 assert!(stale.contains("revision conflict"), "{stale}");
943
944 let removed = remove_root(&file, &lib, 2).unwrap();
945 assert_eq!(removed.revision, 3);
946 assert!(!removed.admits(&lib.join("src/x.rs")));
947 assert!(
948 remove_root(&file, &primary, 3)
949 .unwrap_err()
950 .contains("primary")
951 );
952 assert!(
953 create(&file, "team-a", &primary)
954 .unwrap_err()
955 .contains("exists")
956 );
957 }
958
959 #[test]
960 fn nested_read_only_root_stays_read_only() {
961 let store = private_dir();
962 let code = tempfile::tempdir().unwrap();
963 let primary = project(code.path(), "app");
964 let vendored = project(&primary, "vendor");
965 let file = store.path().join("ws.json");
966 create(&file, "w", &primary).unwrap();
967 let snapshot = add_root(&file, &vendored, Access::ReadOnly, 1).unwrap();
968 assert!(snapshot.admits_write(&primary.join("a.rs")));
969 assert!(!snapshot.admits_write(&vendored.join("b.rs")));
970 }
971
972 #[test]
973 fn the_file_must_lie_outside_its_roots() {
974 let code = private_dir();
975 let primary = project(code.path(), "app");
976 let inside = primary.join("ws.json");
977 assert!(
978 create(&inside, "w", &primary)
979 .unwrap_err()
980 .contains("own roots")
981 );
982 assert!(!inside.exists());
983
984 let store = private_dir();
985 let file = store.path().join("ws.json");
986 create(&file, "w", &primary).unwrap();
987 let store_root = crate::core::pathutil::canonicalize_secure(store.path()).unwrap();
988 assert!(
989 add_root(&file, &store_root, Access::ReadOnly, 1)
990 .unwrap_err()
991 .contains("own roots")
992 );
993 assert_eq!(load(&file).unwrap().revision, 1);
994 }
995
996 #[test]
997 fn broad_and_own_roots_are_refused() {
998 let _home = crate::core::data_dir::test_env_lock();
999 let store = private_dir();
1000 let file = store.path().join("ws.json");
1001 assert!(create(&file, "w", Path::new("/")).is_err());
1002 if let Some(home) = dirs::home_dir() {
1003 assert!(create(&file, "w", &home).is_err());
1004 }
1005 assert!(create(&file, "../x", store.path()).is_err(), "bad id");
1006 assert!(!file.exists());
1007 }
1008
1009 #[test]
1010 fn malformed_and_unknown_fields_fail_closed() {
1011 let store = private_dir();
1012 let code = tempfile::tempdir().unwrap();
1013 let primary = project(code.path(), "app");
1014 let file = store.path().join("ws.json");
1015 let write = |text: String| {
1016 std::fs::write(&file, text).unwrap();
1017 #[cfg(unix)]
1018 {
1019 use std::os::unix::fs::PermissionsExt as _;
1020 std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o600)).unwrap();
1021 }
1022 };
1023 write("{".to_string());
1024 assert!(load(&file).is_err());
1025 write(format!(
1026 r#"{{"schema":"{SCHEMA}","id":"w","revision":1,"primary":{primary:?},"roots":[],"grant":"all"}}"#
1027 ));
1028 assert!(load(&file).unwrap_err().contains("unknown field"));
1029 write(format!(
1030 r#"{{"schema":"{SCHEMA}","id":"w","revision":0,"primary":{primary:?}}}"#
1031 ));
1032 assert!(load(&file).unwrap_err().contains("revision"));
1033 write(format!(
1035 r#"{{"schema":"{SCHEMA}","id":"w","revision":1,"primary":"{}/../app"}}"#,
1036 primary.display()
1037 ));
1038 assert!(load(&file).is_err());
1039 }
1040
1041 #[cfg(unix)]
1042 #[test]
1043 fn insecure_storage_is_refused() {
1044 use std::os::unix::fs::PermissionsExt as _;
1045 let store = private_dir();
1046 let code = tempfile::tempdir().unwrap();
1047 let primary = project(code.path(), "app");
1048 let file = store.path().join("ws.json");
1049 create(&file, "w", &primary).unwrap();
1050 assert!(load(&file).is_ok());
1051
1052 std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o644)).unwrap();
1053 assert!(load(&file).unwrap_err().contains("group or others"));
1054 std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o600)).unwrap();
1055
1056 let link = store.path().join("link.json");
1058 std::os::unix::fs::symlink(&file, &link).unwrap();
1059 assert!(load(&link).unwrap_err().contains("symlink"));
1060
1061 std::fs::set_permissions(store.path(), std::fs::Permissions::from_mode(0o777)).unwrap();
1063 assert!(load(&file).unwrap_err().contains("sticky"));
1064 std::fs::set_permissions(store.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
1065 assert!(load(&file).is_ok());
1066 }
1067
1068 #[test]
1072 fn foreign_owners_are_refused() {
1073 assert!(check_file_owner_mode(1000, 0o100600, 1000).is_ok());
1074 assert!(
1075 check_file_owner_mode(1001, 0o100600, 1000)
1076 .unwrap_err()
1077 .contains("uid 1001")
1078 );
1079 assert!(
1080 check_file_owner_mode(0, 0o100600, 1000).is_err(),
1081 "root-owned file is foreign"
1082 );
1083 assert!(check_file_owner_mode(1000, 0o100640, 1000).is_err());
1084 assert!(
1085 check_dir_owner_mode(0, 0o41777, 1000).is_ok(),
1086 "sticky /tmp"
1087 );
1088 assert!(check_dir_owner_mode(0, 0o40755, 1000).is_ok());
1089 assert!(check_dir_owner_mode(1000, 0o40700, 1000).is_ok());
1090 assert!(check_dir_owner_mode(1001, 0o40700, 1000).is_err());
1091 assert!(check_dir_owner_mode(1000, 0o40775, 1000).is_err());
1092 }
1093
1094 #[cfg(unix)]
1095 #[test]
1096 fn a_root_replaced_by_a_symlink_stops_granting() {
1097 let store = private_dir();
1098 let code = tempfile::tempdir().unwrap();
1099 let primary = project(code.path(), "app");
1100 let lib = project(code.path(), "lib");
1101 let elsewhere = project(code.path(), "secret");
1102 let file = store.path().join("ws.json");
1103 create(&file, "w", &primary).unwrap();
1104 add_root(&file, &lib, Access::ReadWrite, 1).unwrap();
1105
1106 std::fs::remove_dir(&lib).unwrap();
1107 std::os::unix::fs::symlink(&elsewhere, &lib).unwrap();
1108 let snapshot = load(&file).unwrap();
1109 assert!(snapshot.roots.is_empty());
1110 assert_eq!(
1111 snapshot.unavailable,
1112 vec![lib.to_string_lossy().into_owned()]
1113 );
1114 assert!(!snapshot.admits(&elsewhere.join("key")));
1115 assert_eq!(remove_root(&file, &lib, 2).unwrap().revision, 3);
1117 }
1118
1119 #[test]
1120 fn concurrent_writers_serialize_and_one_wins() {
1121 let store = private_dir();
1122 let code = tempfile::tempdir().unwrap();
1123 let primary = project(code.path(), "app");
1124 let roots: Vec<PathBuf> = (0..8)
1125 .map(|i| project(code.path(), &format!("r{i}")))
1126 .collect();
1127 let file = store.path().join("ws.json");
1128 create(&file, "w", &primary).unwrap();
1129 let wins: Vec<bool> = std::thread::scope(|scope| {
1130 let handles: Vec<_> = roots
1131 .iter()
1132 .map(|root| {
1133 let file = file.clone();
1134 scope.spawn(move || add_root(&file, root, Access::ReadWrite, 1).is_ok())
1135 })
1136 .collect();
1137 handles.into_iter().map(|h| h.join().unwrap()).collect()
1138 });
1139 assert_eq!(
1140 wins.iter().filter(|w| **w).count(),
1141 1,
1142 "exactly one CAS succeeds"
1143 );
1144 let snapshot = load(&file).unwrap();
1145 assert_eq!(snapshot.revision, 2);
1146 assert_eq!(snapshot.roots.len(), 1);
1147 }
1148
1149 #[test]
1150 fn request_scope_pins_one_revision() {
1151 let snapshot = Arc::new(Snapshot {
1152 file: PathBuf::from("/w.json"),
1153 id: "w".into(),
1154 revision: 7,
1155 primary: PathBuf::from("/work/app"),
1156 roots: Vec::new(),
1157 unavailable: Vec::new(),
1158 });
1159 let seen = sync_scope(Authority::Workspace(Arc::clone(&snapshot)), || {
1160 current().snapshot().map(|s| s.revision)
1161 });
1162 assert_eq!(seen, Some(7));
1163 assert_eq!(snapshot.project_root_for("/elsewhere"), "/work/app");
1164 }
1165}