Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

tasks: Do not run worktree hooks in restricted mode (#66284)

Piotr Osiewicz2eb2928c1507
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<WorktreeStore>,
project_id: u64,
task_store: Entity<TaskStore>,
- pending_local_settings:
- HashMap<PathTrust, BTreeMap<(WorktreeId, Arc<RelPath>), Option<String>>>,
+ pending_local_settings: HashMap<
+ PathTrust,
+ BTreeMap<(WorktreeId, Arc<RelPath>), HashMap<LocalSettingsKind, Option<String>>>,
+ >,
_trusted_worktrees_watcher: Option<Subscription>,
_user_settings_watcher: Option<Subscription>,
_editorconfig_watcher: Option<Subscription>,
@@ -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;
- 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,
- )
+ 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 {
- applied = false;
- self.pending_local_settings
- .entry(PathTrust::Worktree(worktree_id))
- .or_default()
- .insert((worktree_id, directory.clone()), file_content.clone());
+ 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) => {
+ 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<Self>) {
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<TaskTemplate>)> = {
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