| 1 | //! User-owned approval of the exact project hooks file, separate from folder trust. |
| 2 | use codewhale_config::ProjectHookAuthority; |
| 3 | use sha2::{Digest, Sha256}; |
| 4 | use std::path::Path; |
| 5 | |
| 6 | #[derive(Clone, Copy)] |
| 7 | pub(crate) struct ProjectFileKind { |
| 8 | label: &'static str, |
| 9 | file_name: &'static str, |
| 10 | receipt_key: &'static str, |
| 11 | review_hint: &'static str, |
| 12 | } |
| 13 | |
| 14 | pub(crate) const HOOKS_FILE: ProjectFileKind = ProjectFileKind { |
| 15 | label: "hooks", |
| 16 | file_name: "hooks.toml", |
| 17 | receipt_key: codewhale_runtime::config::HOOKS_RECEIPT_KEY, |
| 18 | review_hint: "/hooks review, then /hooks approve <digest>", |
| 19 | }; |
| 20 | |
| 21 | pub(crate) const MCP_FILE: ProjectFileKind = ProjectFileKind { |
| 22 | label: "MCP servers", |
| 23 | file_name: "mcp.json", |
| 24 | receipt_key: codewhale_runtime::config::MCP_RECEIPT_KEY, |
| 25 | review_hint: "/mcp review, then /mcp approve <digest>", |
| 26 | }; |
| 27 | |
| 28 | pub struct ReviewedProjectFile { |
| 29 | pub workspace: std::path::PathBuf, |
| 30 | pub digest: String, |
| 31 | pub contents: String, |
| 32 | } |
| 33 | |
| 34 | /// Read once for both the digest and parsing. Repository symlinks and special |
| 35 | /// files are not executable configuration, even if the workspace is trusted. |
| 36 | pub(crate) fn review_project_file( |
| 37 | workspace: &Path, |
| 38 | kind: ProjectFileKind, |
| 39 | ) -> Result<ReviewedProjectFile, String> { |
| 40 | let label = kind.label; |
| 41 | let workspace = workspace |
| 42 | .canonicalize() |
| 43 | .map_err(|_| format!("Cannot resolve {label} workspace"))?; |
| 44 | let mut path = workspace.clone(); |
| 45 | for component in [".codewhale", kind.file_name] { |
| 46 | path.push(component); |
| 47 | let metadata = std::fs::symlink_metadata(&path) |
| 48 | .map_err(|_| format!("Cannot read project {label} path"))?; |
| 49 | if metadata.file_type().is_symlink() { |
| 50 | return Err(format!("Project {label} path must not contain symlinks")); |
| 51 | } |
| 52 | if (component == ".codewhale" && !metadata.is_dir()) |
| 53 | || (component == kind.file_name && !metadata.is_file()) |
| 54 | { |
| 55 | return Err(format!( |
| 56 | "Project {label} must be a regular file in .codewhale" |
| 57 | )); |
| 58 | } |
| 59 | } |
| 60 | let contents = super::config::read_project_hooks_file(&path) |
| 61 | .map_err(|_| format!("Cannot read project {label} file (maximum 1 MiB)"))?; |
| 62 | let digest = Sha256::digest(contents.as_bytes()) |
| 63 | .iter() |
| 64 | .map(|byte| format!("{byte:02x}")) |
| 65 | .collect(); |
| 66 | Ok(ReviewedProjectFile { |
| 67 | workspace, |
| 68 | digest, |
| 69 | contents, |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | pub(crate) fn approved_project_file( |
| 74 | workspace: &Path, |
| 75 | kind: ProjectFileKind, |
| 76 | ) -> Result<ReviewedProjectFile, String> { |
| 77 | let label = kind.label; |
| 78 | if !codewhale_runtime::config::is_workspace_trusted(workspace) { |
| 79 | return Err(format!( |
| 80 | "Project {label} require workspace trust and separate approval" |
| 81 | )); |
| 82 | } |
| 83 | let reviewed = review_project_file(workspace, kind)?; |
| 84 | if codewhale_runtime::config::project_receipt_for_workspace( |
| 85 | &reviewed.workspace, |
| 86 | kind.receipt_key, |
| 87 | ) |
| 88 | .as_deref() |
| 89 | != Some(&reviewed.digest) |
| 90 | { |
| 91 | return Err(format!( |
| 92 | "Project {label} are unapproved or changed; use {}", |
| 93 | kind.review_hint |
| 94 | )); |
| 95 | } |
| 96 | Ok(reviewed) |
| 97 | } |
| 98 | |
| 99 | pub(crate) fn approve_project_file( |
| 100 | workspace: &Path, |
| 101 | kind: ProjectFileKind, |
| 102 | reviewed_digest: &str, |
| 103 | validate: impl FnOnce(&str) -> Result<(), String>, |
| 104 | ) -> Result<(), String> { |
| 105 | let label = kind.label; |
| 106 | if !codewhale_runtime::config::is_workspace_trusted(workspace) { |
| 107 | return Err(format!("Trust the workspace before approving its {label}")); |
| 108 | } |
| 109 | let reviewed = review_project_file(workspace, kind)?; |
| 110 | if reviewed_digest != reviewed.digest { |
| 111 | return Err(format!( |
| 112 | "The {label} file does not match the reviewed digest; review it again" |
| 113 | )); |
| 114 | } |
| 115 | validate(&reviewed.contents)?; |
| 116 | codewhale_runtime::config::save_workspace_project_receipt( |
| 117 | &reviewed.workspace, |
| 118 | kind.receipt_key, |
| 119 | reviewed_digest, |
| 120 | ) |
| 121 | .map_err(|_| format!("Could not save {label} approval in user config"))?; |
| 122 | Ok(()) |
| 123 | } |
| 124 | |
| 125 | pub fn review_project_hooks(workspace: &Path) -> Result<(ProjectHookAuthority, String), String> { |
| 126 | let reviewed = review_project_file(workspace, HOOKS_FILE)?; |
| 127 | Ok(( |
| 128 | ProjectHookAuthority { |
| 129 | workspace: reviewed.workspace, |
| 130 | digest: reviewed.digest, |
| 131 | }, |
| 132 | reviewed.contents, |
| 133 | )) |
| 134 | } |
| 135 | |
| 136 | pub fn approved_project_hooks(workspace: &Path) -> Result<(ProjectHookAuthority, String), String> { |
| 137 | let reviewed = approved_project_file(workspace, HOOKS_FILE)?; |
| 138 | Ok(( |
| 139 | ProjectHookAuthority { |
| 140 | workspace: reviewed.workspace, |
| 141 | digest: reviewed.digest, |
| 142 | }, |
| 143 | reviewed.contents, |
| 144 | )) |
| 145 | } |
| 146 | |
| 147 | pub fn verify_hook_authorities( |
| 148 | plugin: Option<&codewhale_config::PluginAuthority>, |
| 149 | project: Option<&ProjectHookAuthority>, |
| 150 | ) -> Result<(), String> { |
| 151 | if let Some(authority) = plugin { |
| 152 | codewhale_plugins::registry::verify_plugin_component_authority( |
| 153 | authority, |
| 154 | codewhale_plugins::activation::PluginActivationCapability::Hooks, |
| 155 | )?; |
| 156 | } |
| 157 | if let Some(authority) = project { |
| 158 | let (current, _) = approved_project_hooks(&authority.workspace)?; |
| 159 | if ¤t != authority { |
| 160 | return Err("Project hooks changed after loading; review and approve again".into()); |
| 161 | } |
| 162 | } |
| 163 | Ok(()) |
| 164 | } |
| 165 | |
| 166 | pub fn approve_project_hooks(workspace: &Path, reviewed_digest: &str) -> Result<(), String> { |
| 167 | approve_project_file(workspace, HOOKS_FILE, reviewed_digest, |contents| { |
| 168 | let parsed = toml::from_str::<codewhale_config::hooks::HooksConfig>(contents) |
| 169 | .map_err(|_| "Invalid hooks TOML")?; |
| 170 | if parsed.validate().iter().any(|problem| problem.rejected) { |
| 171 | return Err("Hooks contain invalid entries; correct them before approval".into()); |
| 172 | } |
| 173 | Ok(()) |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | /// The Native floor is sufficient for Native-authored shell contributions; it is not a legacy Hooks grant. |
| 178 | pub fn verify_hook(hook: &codewhale_config::hooks::Hook) -> Result<(), String> { |
| 179 | if hook.native_shell.is_some() { |
| 180 | let authority = hook |
| 181 | .plugin_authority |
| 182 | .as_ref() |
| 183 | .ok_or("Native hook has no authority")?; |
| 184 | codewhale_plugins::registry::verify_plugin_component_authority( |
| 185 | authority, |
| 186 | codewhale_plugins::activation::PluginActivationCapability::Native, |
| 187 | )?; |
| 188 | Ok(()) |
| 189 | } else { |
| 190 | verify_hook_authorities( |
| 191 | hook.plugin_authority.as_ref(), |
| 192 | hook.project_authority.as_ref(), |
| 193 | ) |
| 194 | } |
| 195 | } |
| 196 |