|
| 1 | +use std::{collections::HashSet, fs, path::PathBuf}; |
| 2 | + |
| 3 | +use crate::{data_dir, workspace_exclude_file, workspace_trust_file}; |
| 4 | + |
| 5 | +pub struct WorkspaceTrust { |
| 6 | + trusted: HashSet<PathBuf>, |
| 7 | + excluded: Option<HashSet<PathBuf>>, |
| 8 | +} |
| 9 | + |
| 10 | +#[derive(Clone, Copy)] |
| 11 | +pub enum TrustStatus { |
| 12 | + Untrusted, |
| 13 | + Trusted, |
| 14 | +} |
| 15 | + |
| 16 | +impl WorkspaceTrust { |
| 17 | + /// Loads `WorkspaceTrust`. |
| 18 | + /// |
| 19 | + /// Should be used only when there is a need to change trust status |
| 20 | + /// of a particular workspace. |
| 21 | + /// |
| 22 | + /// For querying trust status of a workspace use `quick_query_workspace()` or |
| 23 | + /// `quick_query_workspace_with_explicit_untrust()` |
| 24 | + pub fn load(with_exclusion: bool) -> Self { |
| 25 | + let mut trusted = HashSet::new(); |
| 26 | + |
| 27 | + match fs::read_to_string(workspace_trust_file()) { |
| 28 | + Ok(workspace_trust_file) => { |
| 29 | + for line in workspace_trust_file.split('\n') { |
| 30 | + if !line.is_empty() { |
| 31 | + let path = PathBuf::from(line); |
| 32 | + trusted.insert(path); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + Err(e) => log::error!("workspace file couldn't be read: {:?}", e), |
| 37 | + }; |
| 38 | + |
| 39 | + let excluded = if with_exclusion { |
| 40 | + let mut untrusted = HashSet::new(); |
| 41 | + |
| 42 | + match fs::read_to_string(workspace_exclude_file()) { |
| 43 | + Ok(workspace_untrust_file) => { |
| 44 | + for line in workspace_untrust_file.split('\n') { |
| 45 | + if !line.is_empty() { |
| 46 | + let path = PathBuf::from(line); |
| 47 | + untrusted.insert(path); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + Err(e) => log::error!("workspace file couldn't be read: {:?}", e), |
| 52 | + }; |
| 53 | + |
| 54 | + Some(untrusted) |
| 55 | + } else { |
| 56 | + None |
| 57 | + }; |
| 58 | + WorkspaceTrust { trusted, excluded } |
| 59 | + } |
| 60 | + |
| 61 | + fn write_trust_to_file(&self) { |
| 62 | + let mut trust_text = String::new(); |
| 63 | + for workspace in self.trusted.iter() { |
| 64 | + if let Some(path_str) = workspace.to_str() { |
| 65 | + trust_text += &format!("{path_str}\n"); |
| 66 | + } |
| 67 | + } |
| 68 | + // let chains aren't supported in current MSRV |
| 69 | + if let Ok(false) = fs::exists(data_dir()) { |
| 70 | + if let Err(e) = fs::create_dir_all(data_dir()) { |
| 71 | + log::error!("Couldn't create helix's data directory: {:?}", e); |
| 72 | + }; |
| 73 | + } |
| 74 | + if let Err(e) = fs::write(workspace_trust_file(), trust_text) { |
| 75 | + log::error!("Error during write of workspace_trust file: {:?}", e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fn write_exclusion_to_file(&self) { |
| 80 | + if let Some(untrusted) = &self.excluded { |
| 81 | + let mut trust_text = String::new(); |
| 82 | + for workspace in untrusted.iter() { |
| 83 | + if let Some(path_str) = workspace.to_str() { |
| 84 | + trust_text += &format!("{path_str}\n"); |
| 85 | + } |
| 86 | + } |
| 87 | + // let chains aren't supported in current MSRV |
| 88 | + if let Ok(false) = fs::exists(data_dir()) { |
| 89 | + if let Err(e) = fs::create_dir_all(data_dir()) { |
| 90 | + log::error!("Couldn't create helix's data directory: {:?}", e); |
| 91 | + }; |
| 92 | + } |
| 93 | + if let Err(e) = fs::write(workspace_exclude_file(), trust_text) { |
| 94 | + log::error!("Error during write of workspace_trust file: {:?}", e); |
| 95 | + } |
| 96 | + } else { |
| 97 | + log::error!("Called write_untrust_to_file() when self.untrusted is None"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// Mark current workspace trusted |
| 102 | + pub fn trust_workspace(&mut self) { |
| 103 | + let workspace = crate::find_workspace().0; |
| 104 | + self.trusted.insert(workspace); |
| 105 | + self.write_trust_to_file(); |
| 106 | + } |
| 107 | + |
| 108 | + /// Remove trusted mark from current workspace |
| 109 | + pub fn untrust_workspace(&mut self) { |
| 110 | + let workspace = crate::find_workspace().0; |
| 111 | + self.trusted.remove(&workspace); |
| 112 | + self.write_trust_to_file(); |
| 113 | + } |
| 114 | + |
| 115 | + /// Mark current workspace excluded. |
| 116 | + /// |
| 117 | + /// Should be called only if `WorkspaceTrust` was created with `WorkspaceTrust::load(true)` |
| 118 | + pub fn exclude_workspace(&mut self) { |
| 119 | + let workspace = crate::find_workspace().0; |
| 120 | + self.trusted.remove(&workspace); |
| 121 | + if let Some(excluded) = &mut self.excluded { |
| 122 | + excluded.insert(workspace); |
| 123 | + self.write_exclusion_to_file(); |
| 124 | + } else { |
| 125 | + log::error!("Called untrust_workspace_permanent() when self.untrusted is None"); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +#[derive(Default, Clone, Copy, Debug)] |
| 131 | +pub enum TrustUntrustStatus { |
| 132 | + DenyAlways, |
| 133 | + #[default] |
| 134 | + DenyOnce, |
| 135 | + AllowAlways, |
| 136 | +} |
| 137 | + |
| 138 | +pub fn quick_query_workspace() -> TrustStatus { |
| 139 | + let workspace = crate::find_workspace().0; |
| 140 | + match fs::read_to_string(workspace_trust_file()) { |
| 141 | + Ok(workspace_trust_file) => { |
| 142 | + for line in workspace_trust_file.split('\n') { |
| 143 | + if PathBuf::from(line) == workspace { |
| 144 | + return TrustStatus::Trusted; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + Err(e) => log::error!("workspace file couldn't be read: {:?}", e), |
| 149 | + }; |
| 150 | + TrustStatus::Untrusted |
| 151 | +} |
| 152 | + |
| 153 | +pub fn quick_query_workspace_with_explicit_untrust() -> TrustUntrustStatus { |
| 154 | + let workspace = crate::find_workspace().0; |
| 155 | + match fs::read_to_string(workspace_trust_file()) { |
| 156 | + Ok(workspace_trust_file) => { |
| 157 | + for line in workspace_trust_file.split('\n') { |
| 158 | + if PathBuf::from(line) == workspace { |
| 159 | + return TrustUntrustStatus::AllowAlways; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + Err(e) => log::error!("workspace_trust file couldn't be read: {:?}", e), |
| 164 | + }; |
| 165 | + |
| 166 | + match fs::read_to_string(workspace_exclude_file()) { |
| 167 | + Ok(workspace_untrust_file) => { |
| 168 | + for line in workspace_untrust_file.split('\n') { |
| 169 | + if PathBuf::from(line) == workspace { |
| 170 | + return TrustUntrustStatus::DenyAlways; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + Err(e) => log::error!("workspace_untrust file couldn't be read: {:?}", e), |
| 175 | + }; |
| 176 | + TrustUntrustStatus::DenyOnce |
| 177 | +} |
0 commit comments