From 2eb2928c15073b30d525b516fd3f2f79bf703307 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Thu, 29 Oct 2026 11:58:53 +0000 Subject: [PATCH] tasks: Do not run worktree hooks in restricted mode (#66284) ## Summary What problem does this solve, how, and why does this change matter? Use "Closes #123" for each issue resolved. Include artifacts produced when testing: before/after screenshots, GIFs, videos, benchmarks. ## Testing Manually test your changes; report results, mention platforms if relevant. How can reviewers test this easily? Include setup and steps. ## Self-review - [ ] I've reviewed my diff for quality, security, reliability, and performance. - [ ] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [ ] Tests cover the new or changed behavior. Release Notes: - Worktree task hooks no longer run when a given workspace is in restricted mode. --- crates/project/src/project_settings.rs | 128 +++++++++--------- .../tests/integration/project_tests.rs | 94 +++++++++++++ crates/workspace/src/tasks.rs | 14 +- docs/src/worktree-trust.md | 2 + 4 files changed, 171 insertions(+), 67 deletions(-) diff --git a/crates/project/src/project_settings.rs b/crates/project/src/project_settings.rs index 189ac52bbd7..51f9ec0a8cf 100644 --- a/crates/project/src/project_settings.rs +++ b/crates/project/src/project_settings.rs @@ -848,8 +848,10 @@ pub struct SettingsObserver { worktree_store: Entity, project_id: u64, task_store: Entity, - pending_local_settings: - HashMap), Option>>, + pending_local_settings: HashMap< + PathTrust, + BTreeMap<(WorktreeId, Arc), HashMap>>, + >, _trusted_worktrees_watcher: Option, _user_settings_watcher: Option, _editorconfig_watcher: Option, @@ -892,34 +894,27 @@ impl SettingsObserver { for ((worktree_id, directory_path), settings_contents) in pending_local_settings { - let path = - LocalSettingsPath::InWorktree(directory_path.clone()); - apply_local_settings( - worktree_id, - path.clone(), - LocalSettingsKind::Settings, - &settings_contents, + let Some(worktree) = settings_observer + .worktree_store + .read(cx) + .worktree_for_id(worktree_id, cx) + else { + continue; + }; + settings_observer.update_settings( + worktree, + settings_contents.into_iter().map(|(kind, content)| { + ( + LocalSettingsPath::InWorktree( + directory_path.clone(), + ), + kind, + content, + ) + }), + false, cx, ); - if let Some(downstream_client) = - &settings_observer.downstream_client - { - downstream_client - .send(proto::UpdateWorktreeSettings { - project_id: settings_observer.project_id, - worktree_id: worktree_id.to_proto(), - path: path.to_proto(), - content: settings_contents, - kind: Some( - local_settings_kind_to_proto( - LocalSettingsKind::Settings, - ) - .into(), - ), - outside_worktree: Some(false), - }) - .log_err(); - } } } } @@ -1377,32 +1372,37 @@ impl SettingsObserver { OnceCell::new() }; for (directory_path, kind, file_content) in settings_contents { - let mut applied = true; + if kind != LocalSettingsKind::Editorconfig + && !*can_trust_worktree.get_or_init(|| { + if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) { + trusted_worktrees.update(cx, |trusted_worktrees, cx| { + trusted_worktrees.can_trust(&self.worktree_store, worktree_id, cx) + }) + } else { + true + } + }) + { + if let LocalSettingsPath::InWorktree(directory) = directory_path { + self.pending_local_settings + .entry(PathTrust::Worktree(worktree_id)) + .or_default() + .entry((worktree_id, directory)) + .or_default() + .insert(kind, file_content); + } + continue; + } + match (&directory_path, kind) { (LocalSettingsPath::InWorktree(directory), LocalSettingsKind::Settings) => { - if *can_trust_worktree.get_or_init(|| { - if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) { - trusted_worktrees.update(cx, |trusted_worktrees, cx| { - trusted_worktrees.can_trust(&self.worktree_store, worktree_id, cx) - }) - } else { - true - } - }) { - apply_local_settings( - worktree_id, - LocalSettingsPath::InWorktree(directory.clone()), - kind, - &file_content, - cx, - ) - } else { - applied = false; - self.pending_local_settings - .entry(PathTrust::Worktree(worktree_id)) - .or_default() - .insert((worktree_id, directory.clone()), file_content.clone()); - } + apply_local_settings( + worktree_id, + LocalSettingsPath::InWorktree(directory.clone()), + kind, + &file_content, + cx, + ) } (LocalSettingsPath::InWorktree(directory), LocalSettingsKind::Tasks) => { let result = task_store.update(cx, |task_store, cx| { @@ -1477,19 +1477,17 @@ impl SettingsObserver { } }; - if applied { - if let Some(downstream_client) = &self.downstream_client { - downstream_client - .send(proto::UpdateWorktreeSettings { - project_id: self.project_id, - worktree_id: remote_worktree_id.to_proto(), - path: directory_path.to_proto(), - content: file_content.clone(), - kind: Some(local_settings_kind_to_proto(kind).into()), - outside_worktree: Some(directory_path.is_outside_worktree()), - }) - .log_err(); - } + if let Some(downstream_client) = &self.downstream_client { + downstream_client + .send(proto::UpdateWorktreeSettings { + project_id: self.project_id, + worktree_id: remote_worktree_id.to_proto(), + path: directory_path.to_proto(), + content: file_content.clone(), + kind: Some(local_settings_kind_to_proto(kind).into()), + outside_worktree: Some(directory_path.is_outside_worktree()), + }) + .log_err(); } } } diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rs index f1cbd4ce336..64de3521afd 100644 --- a/crates/project/tests/integration/project_tests.rs +++ b/crates/project/tests/integration/project_tests.rs @@ -1353,6 +1353,100 @@ async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext) ); } +#[gpui::test] +async fn test_project_tasks_and_debug_scenarios_require_trust(cx: &mut gpui::TestAppContext) { + use project::trusted_worktrees::{self, DbTrustedPaths, PathTrust, TrustedWorktrees}; + + init_test(cx); + TaskStore::init(None); + cx.update(|cx| trusted_worktrees::init(DbTrustedPaths::default(), cx)); + + let fs = FakeFs::new(cx.executor()); + fs.insert_tree( + path!("/dir"), + json!({ + ".zed": { + "settings.json": r#"{ "tab_size": 8 }"#, + "tasks.json": r#"[{ "label": "initial", "command": "echo initial", "hooks": ["create_worktree"] }]"#, + "debug.json": r#"[{ "label": "debug", "adapter": "Debugpy", "request": "launch" }]"#, + }, + "file.rs": "" + }), + ) + .await; + + let project = Project::test_with_worktree_trust(fs.clone(), [path!("/dir").as_ref()], cx).await; + cx.run_until_parked(); + let worktree_store = project.read_with(cx, |project, _| project.worktree_store()); + let worktree_id = worktree_store.read_with(cx, |store, cx| { + store.worktrees().next().unwrap().read(cx).id() + }); + let inventory = project.read_with(cx, |project, cx| { + project + .task_store() + .read(cx) + .task_inventory() + .unwrap() + .clone() + }); + let contexts = TaskContexts { + active_worktree_context: Some((worktree_id, TaskContext::default())), + ..Default::default() + }; + let hooks = HashSet::from_iter([task::TaskHook::CreateWorktree]); + + assert!(inventory.read_with(cx, |inventory, _| { + inventory + .templates_with_hooks(&hooks, worktree_id) + .is_empty() + })); + let (_, scenarios) = inventory + .update(cx, |inventory, cx| { + inventory.list_debug_scenarios(&contexts, Vec::new(), Vec::new(), false, cx) + }) + .await; + assert!(scenarios.is_empty()); + + fs.save( + path!("/dir/.zed/tasks.json").as_ref(), + &r#"[{ "label": "updated", "command": "echo updated", "hooks": ["create_worktree"] }]"# + .into(), + Default::default(), + ) + .await + .unwrap(); + cx.run_until_parked(); + assert!(inventory.read_with(cx, |inventory, _| { + inventory + .templates_with_hooks(&hooks, worktree_id) + .is_empty() + })); + + let trusted_worktrees = cx.update(|cx| TrustedWorktrees::try_get_global(cx).unwrap()); + trusted_worktrees.update(cx, |store, cx| { + store.trust( + &worktree_store, + HashSet::from_iter([PathTrust::Worktree(worktree_id)]), + cx, + ); + }); + cx.run_until_parked(); + + let templates = inventory.read_with(cx, |inventory, _| { + inventory.templates_with_hooks(&hooks, worktree_id) + }); + assert_eq!(templates.len(), 1); + assert_eq!(templates[0].1.label, "updated"); + assert_eq!(templates[0].1.command, "echo updated"); + let (_, scenarios) = inventory + .update(cx, |inventory, cx| { + inventory.list_debug_scenarios(&contexts, Vec::new(), Vec::new(), false, cx) + }) + .await; + assert_eq!(scenarios.len(), 1); + assert_eq!(scenarios[0].1.label, "debug"); +} + #[gpui::test] async fn test_invalid_local_tasks_shows_toast_with_doc_link(cx: &mut gpui::TestAppContext) { init_test(cx); diff --git a/crates/workspace/src/tasks.rs b/crates/workspace/src/tasks.rs index 7bc6420c4c7..85ed9b661ef 100644 --- a/crates/workspace/src/tasks.rs +++ b/crates/workspace/src/tasks.rs @@ -4,7 +4,7 @@ use anyhow::Result; use collections::HashSet; use gpui::{AppContext, AsyncWindowContext, Context, Entity, Task, TaskExt, WeakEntity}; use language::Buffer; -use project::{TaskSourceKind, WorktreeId}; +use project::{TaskSourceKind, WorktreeId, trusted_worktrees::TrustedWorktrees}; use remote::ConnectionState; use task::{ DebugScenario, ResolvedTask, SaveStrategy, SharedTaskContext, SpawnInTerminal, TaskContext, @@ -235,6 +235,16 @@ impl Workspace { pub fn run_create_worktree_tasks(&mut self, window: &mut Window, cx: &mut Context) { let project = self.project().clone(); let hooks = HashSet::from_iter([TaskHook::CreateWorktree]); + let worktree_store = project.read(cx).worktree_store(); + let mut worktrees: Vec<_> = project.read(cx).worktrees(cx).collect(); + if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) { + worktrees.retain(|worktree| { + let worktree_id = worktree.read(cx).id(); + trusted_worktrees.update(cx, |trusted_worktrees, cx| { + trusted_worktrees.can_trust(&worktree_store, worktree_id, cx) + }) + }); + } let worktree_tasks: Vec<(WorktreeId, TaskContext, Vec)> = { let project = project.read(cx); @@ -246,7 +256,7 @@ impl Workspace { let git_store = project.git_store().read(cx); let mut worktree_tasks = Vec::new(); - for worktree in project.worktrees(cx) { + for worktree in worktrees { let worktree = worktree.read(cx); let worktree_id = worktree.id(); let worktree_abs_path = worktree.abs_path(); diff --git a/docs/src/worktree-trust.md b/docs/src/worktree-trust.md index 4c190143c5e..8a4834e24b6 100644 --- a/docs/src/worktree-trust.md +++ b/docs/src/worktree-trust.md @@ -27,6 +27,8 @@ This feature works locally and on SSH and WSL remote hosts. Zed tracks trust inf Restricted Mode prevents: - Project settings (`.zed/settings.json`) from being parsed and applied +- Project tasks (`.zed/tasks.json`, `.vscode/tasks.json`) and debug configurations (`.zed/debug.json`, `.vscode/launch.json`) from being applied +- Automatic task hooks from running, including hooks defined in global tasks - Language servers from being installed and spawned - MCP servers from being installed and spawned