Debian Codegraph @

helix-term/src/handlers/workspace_trust.rs

raw symbols SCIP
1 use std::{
2 collections::HashSet,
3 path::PathBuf,
4 sync::{Arc, Mutex},
5 };
6
7 use helix_event::register_hook;
8 use helix_loader::workspace_trust::TrustStatus;
9 use helix_view::{events::DocumentDidOpen, handlers::Handlers, DocumentId};
10
11 use crate::{compositor::Compositor, job, ui};
12
13 const ID: &str = "workspace-trust-select";
14
15 pub(super) fn register_hooks(_handlers: &Handlers) {
16 // Tracks which workspaces have already been prompted (or auto-dismissed) during this session.
17 // Without this, every document opened in an untrusted workspace would re-dispatch the modal —
18 // `deny_once` writes `Untrusted` to the trust cache but `restricted_for_doc` returns `true`
19 // based purely on workspace state, so the cache alone is not enough to suppress re-prompts.
20 // Inserts return false when the path was already present, which we use to short-circuit.
21 let prompted: Arc<Mutex<HashSet<PathBuf>>> = Arc::default();
22 register_hook!(move |event: &mut DocumentDidOpen<'_>| {
23 let doc_id = event.doc;
24
25 let (workspace, servers_to_load) = {
26 let doc = doc!(event.editor, &doc_id);
27 let servers_to_load = doc
28 .language_config()
29 .map(|lang| !lang.language_servers.is_empty() || lang.debugger.is_some())
30 .unwrap_or(false);
31 (doc.workspace_root().to_path_buf(), servers_to_load)
32 };
33
34 // Stale: `.helix/` was edited since the user last ran `trust`. LSPs keep
35 // running (binaries are unchanged), but local config is dropped.
36 // Note: must use `status` (raw) not `query`: `query` collapses Stale to Untrusted via `demote_for_query`.
37 if event.editor.workspace_trust.status(&workspace) == TrustStatus::Stale {
38 event.editor.set_status(
39 "Workspace `.helix/` config changed since `:workspace-trust`. \
40 Local config not loaded. Run `:workspace-trust` to re-allow.",
41 );
42 return Ok(());
43 }
44
45 if !event
46 .editor
47 .workspace_trust
48 .restricted_for_doc(&workspace, servers_to_load)
49 {
50 return Ok(());
51 }
52
53 // Users who opt out of the modal still get the statusline `[⚠]` indicator and can act
54 // explicitly via `:workspace-trust`.
55 if !event.editor.workspace_trust.prompts_enabled() {
56 return Ok(());
57 }
58
59 // First time we've seen this workspace this session — prompt once.
60 if !prompted.lock().unwrap().insert(workspace.clone()) {
61 return Ok(());
62 }
63
64 // Cache "denied for this session" so future trust queries treat the workspace as untrusted.
65 event.editor.workspace_trust.deny_once(&workspace);
66 let workspace = workspace.clone();
67 job::dispatch_blocking(move |_editor, compositor| prompt(workspace, compositor));
68
69 Ok(())
70 });
71 }
72
73 fn prompt(workspace: PathBuf, compositor: &mut Compositor) {
74 let select = select(workspace);
75 compositor.replace_or_push(ID, select);
76 }
77
78 const TRUST_MESSAGE: &str = "Trust this workspace?
79
80 Trusted workspaces may load local Helix config files (`.helix/*`) and auto-start language servers. \
81 Both can execute arbitrary code. Only trust workspaces whose contents you have inspected.";
82
83 #[derive(Default, Clone, Copy, Debug)]
84 pub enum TrustChoice {
85 #[default]
86 Trust,
87 Never,
88 }
89
90 fn select(workspace: PathBuf) -> ui::Select<TrustChoice> {
91 ui::Select::new(
92 TRUST_MESSAGE,
93 [TrustChoice::Trust, TrustChoice::Never],
94 (),
95 move |editor, option, event| {
96 if event != ui::PromptEvent::Validate {
97 return;
98 }
99 match option {
100 TrustChoice::Trust => {
101 editor.workspace_trust.trust(&workspace);
102 let documents: Vec<DocumentId> = editor.documents.keys().cloned().collect();
103 for document_id in documents.iter() {
104 editor.launch_language_servers(*document_id);
105 }
106 let _ = editor
107 .config_events
108 .0
109 .send(helix_view::editor::ConfigEvent::Refresh);
110 }
111 TrustChoice::Never => {
112 editor.workspace_trust.exclude(&workspace);
113 // Drop any workspace overrides that snuck into the live editor config before
114 // the user excluded the workspace.
115 let _ = editor
116 .config_events
117 .0
118 .send(helix_view::editor::ConfigEvent::Refresh);
119 }
120 }
121 },
122 )
123 }
124
125 impl crate::ui::menu::Item for TrustChoice {
126 type Data = ();
127
128 fn format(&self, _data: &Self::Data) -> tui::widgets::Row<'_> {
129 match self {
130 TrustChoice::Trust => "Trust",
131 TrustChoice::Never => "Never",
132 }
133 .into()
134 }
135 }