| 1 | use std::fs; |
| 2 | use std::path::Path; |
| 3 | |
| 4 | use serde_json::{Value, json}; |
| 5 | |
| 6 | use super::activation::TestPolicyGuard; |
| 7 | use super::discovery::{DiscoveryConfig, discover_with_config}; |
| 8 | use super::host_services::{ |
| 9 | AgentApproval, AgentPosture, HostService, PersonVia, PluginApprover, ReviewApproval, TestOffer, |
| 10 | }; |
| 11 | use super::registry::{PluginRegistry, granted_host_services, verify_plugin_state_authority}; |
| 12 | |
| 13 | type Edit = fn(&mut Value); |
| 14 | |
| 15 | fn config(root: &Path) -> DiscoveryConfig { |
| 16 | DiscoveryConfig { |
| 17 | workspace: root.join("project"), |
| 18 | user_plugins_dir: root.join("user"), |
| 19 | workspace_plugins_dir: root.join("workspace"), |
| 20 | builtin_plugin_dirs: Vec::new(), |
| 21 | state_path: root.join("state/plugin-state.json"), |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn write_plugin(config: &DiscoveryConfig, name: &str, asks: &[&str], body: &str) { |
| 26 | write_plugin_in(&config.user_plugins_dir, name, asks, body); |
| 27 | } |
| 28 | |
| 29 | fn write_plugin_in(dir: &Path, name: &str, asks: &[&str], body: &str) { |
| 30 | let plugin = dir.join(name); |
| 31 | fs::create_dir_all(plugin.join("native")).unwrap(); |
| 32 | fs::write(plugin.join("native/index.mjs"), body).unwrap(); |
| 33 | let asked = if asks.is_empty() { |
| 34 | String::new() |
| 35 | } else { |
| 36 | format!( |
| 37 | "[capabilities]\nhost_services = [{}]\n", |
| 38 | asks.iter() |
| 39 | .map(|service| format!("{service:?}")) |
| 40 | .collect::<Vec<_>>() |
| 41 | .join(", ") |
| 42 | ) |
| 43 | }; |
| 44 | fs::write( |
| 45 | plugin.join("plugin.toml"), |
| 46 | format!( |
| 47 | "schema_version = 1\n[plugin]\nname = {name:?}\nversion = \"1.0.0\"\n[native]\npath = \"native/index.mjs\"\n{asked}" |
| 48 | ), |
| 49 | ) |
| 50 | .unwrap(); |
| 51 | } |
| 52 | |
| 53 | fn person() -> PluginApprover { |
| 54 | PluginApprover::person(PersonVia::Terminal) |
| 55 | } |
| 56 | |
| 57 | fn agent(config: &DiscoveryConfig, posture: AgentPosture) -> PluginApprover { |
| 58 | PluginApprover::Agent(AgentApproval { |
| 59 | posture, |
| 60 | workspace: config.workspace.to_string_lossy().into_owned(), |
| 61 | thread_id: Some("thread-1".into()), |
| 62 | turn_id: Some("turn-1".into()), |
| 63 | tool_call_id: Some("call-1".into()), |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | fn approve(registry: &mut PluginRegistry, name: &str, approved_by: PluginApprover) { |
| 68 | let shown = registry |
| 69 | .host_service_review_for(name, &approved_by) |
| 70 | .filter(|_| approved_by != PluginApprover::CommandLine) |
| 71 | .map(|review| review.token); |
| 72 | registry |
| 73 | .trust_reviewed( |
| 74 | name, |
| 75 | ReviewApproval { |
| 76 | approved_by, |
| 77 | host_services: shown, |
| 78 | }, |
| 79 | ) |
| 80 | .unwrap(); |
| 81 | registry.enable(name).unwrap(); |
| 82 | } |
| 83 | |
| 84 | fn granted(config: &DiscoveryConfig, name: &str) -> Result<Vec<HostService>, String> { |
| 85 | let registry = discover_with_config(config); |
| 86 | let authority = registry |
| 87 | .authority_for(name) |
| 88 | .ok_or_else(|| "no authority".to_string())?; |
| 89 | granted_host_services(&authority) |
| 90 | } |
| 91 | |
| 92 | fn edit_receipt(config: &DiscoveryConfig, name: &str, edit: impl FnOnce(&mut Value)) { |
| 93 | let mut state: Value = |
| 94 | serde_json::from_str(&fs::read_to_string(&config.state_path).unwrap()).unwrap(); |
| 95 | let entry = state["plugins"] |
| 96 | .as_object_mut() |
| 97 | .unwrap() |
| 98 | .iter_mut() |
| 99 | .find(|(id, _)| id.ends_with(&format!("/{name}"))) |
| 100 | .map(|(_, entry)| entry) |
| 101 | .unwrap(); |
| 102 | edit(&mut entry["trust"]); |
| 103 | fs::write( |
| 104 | &config.state_path, |
| 105 | serde_json::to_string_pretty(&state).unwrap(), |
| 106 | ) |
| 107 | .unwrap(); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn a_reviewed_grant_is_recorded_with_its_approver_and_read_back_for_the_reviewed_content() { |
| 112 | let _policy = TestPolicyGuard::extension_host(true); |
| 113 | let _offer = TestOffer::only(&[HostService::Network]); |
| 114 | let tmp = tempfile::tempdir().unwrap(); |
| 115 | let config = config(tmp.path()); |
| 116 | write_plugin( |
| 117 | &config, |
| 118 | "alpha", |
| 119 | &["network", "llm"], |
| 120 | "export function apply() {}\n", |
| 121 | ); |
| 122 | |
| 123 | let mut registry = discover_with_config(&config); |
| 124 | let review = registry.host_service_review("alpha").unwrap(); |
| 125 | assert_eq!( |
| 126 | review |
| 127 | .lines |
| 128 | .iter() |
| 129 | .map(|line| (line.service, line.offered)) |
| 130 | .collect::<Vec<_>>(), |
| 131 | vec![(HostService::Llm, false), (HostService::Network, true)] |
| 132 | ); |
| 133 | assert!(review.text()[1].contains("does not offer it")); |
| 134 | assert!(review.text()[2].ends_with("No other reviewed plugin holds it.")); |
| 135 | let by_agent = agent(&config, AgentPosture::FullAccess); |
| 136 | approve(&mut registry, "alpha", by_agent.clone()); |
| 137 | |
| 138 | let plugin = registry.get("alpha").unwrap(); |
| 139 | assert_eq!(plugin.host_services, vec![HostService::Network]); |
| 140 | assert_eq!(plugin.approved_by, Some(by_agent.clone())); |
| 141 | assert_eq!(plugin.enabled_by, Some(person())); |
| 142 | assert!(plugin.active()); |
| 143 | assert_eq!( |
| 144 | granted(&config, "alpha").unwrap(), |
| 145 | vec![HostService::Network] |
| 146 | ); |
| 147 | let state: Value = |
| 148 | serde_json::from_str(&fs::read_to_string(&config.state_path).unwrap()).unwrap(); |
| 149 | let receipt = &state["plugins"] |
| 150 | .as_object() |
| 151 | .unwrap() |
| 152 | .values() |
| 153 | .next() |
| 154 | .unwrap()["trust"]; |
| 155 | assert_eq!(receipt["version"], 2); |
| 156 | assert_eq!( |
| 157 | receipt["approved_by"], |
| 158 | json!({ |
| 159 | "kind": "agent", |
| 160 | "posture": "full_access", |
| 161 | "workspace": config.workspace.to_string_lossy(), |
| 162 | "thread_id": "thread-1", |
| 163 | "turn_id": "turn-1", |
| 164 | "tool_call_id": "call-1", |
| 165 | }) |
| 166 | ); |
| 167 | assert_eq!(receipt["host_services"], json!(["network"])); |
| 168 | assert_eq!( |
| 169 | receipt["reviewed_capabilities"]["host_services"], |
| 170 | json!(["llm", "network"]) |
| 171 | ); |
| 172 | |
| 173 | write_plugin( |
| 174 | &config, |
| 175 | "alpha", |
| 176 | &["network", "llm"], |
| 177 | "export function apply() { globalThis.x = 1 }\n", |
| 178 | ); |
| 179 | let changed = discover_with_config(&config); |
| 180 | assert!(!changed.get("alpha").unwrap().trusted()); |
| 181 | assert!(changed.get("alpha").unwrap().host_services.is_empty()); |
| 182 | assert_eq!(changed.get("alpha").unwrap().approved_by, None); |
| 183 | assert!(granted(&config, "alpha").is_err()); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn trust_without_the_shown_review_grants_nothing_and_a_stale_token_refuses() { |
| 188 | let _policy = TestPolicyGuard::extension_host(true); |
| 189 | let _offer = TestOffer::only(&[HostService::Network]); |
| 190 | let tmp = tempfile::tempdir().unwrap(); |
| 191 | let config = config(tmp.path()); |
| 192 | write_plugin( |
| 193 | &config, |
| 194 | "alpha", |
| 195 | &["network"], |
| 196 | "export function apply() {}\n", |
| 197 | ); |
| 198 | write_plugin( |
| 199 | &config, |
| 200 | "beta", |
| 201 | &["network"], |
| 202 | "export function apply() {}\n", |
| 203 | ); |
| 204 | write_plugin(&config, "plain", &[], "export function apply() {}\n"); |
| 205 | |
| 206 | let mut registry = discover_with_config(&config); |
| 207 | registry.trust("alpha").unwrap(); |
| 208 | registry.enable("alpha").unwrap(); |
| 209 | assert!(registry.get("alpha").unwrap().trusted()); |
| 210 | assert_eq!(registry.get("alpha").unwrap().approved_by, Some(person())); |
| 211 | assert!(granted(&config, "alpha").unwrap().is_empty()); |
| 212 | let plain = registry.host_service_review("plain").unwrap(); |
| 213 | assert!(plain.lines.is_empty() && plain.text().is_empty()); |
| 214 | assert!( |
| 215 | registry |
| 216 | .trust_reviewed( |
| 217 | "plain", |
| 218 | ReviewApproval { |
| 219 | approved_by: person(), |
| 220 | host_services: Some("0".repeat(64)), |
| 221 | }, |
| 222 | ) |
| 223 | .unwrap_err() |
| 224 | .contains("changed since it was shown") |
| 225 | ); |
| 226 | |
| 227 | let sole = registry.host_service_review("beta").unwrap(); |
| 228 | assert!(sole.lines[0].shared_with.is_empty()); |
| 229 | assert_eq!(sole.lines[0].withheld_by[0].name, "alpha"); |
| 230 | assert!( |
| 231 | sole.text()[1] |
| 232 | .ends_with("Off for now: `alpha` is turned on and its review does not include it.") |
| 233 | ); |
| 234 | approve(&mut registry, "alpha", person()); |
| 235 | let before = fs::read(&config.state_path).unwrap(); |
| 236 | let refused = registry |
| 237 | .trust_reviewed( |
| 238 | "beta", |
| 239 | ReviewApproval { |
| 240 | approved_by: person(), |
| 241 | host_services: Some(sole.token.clone()), |
| 242 | }, |
| 243 | ) |
| 244 | .unwrap_err(); |
| 245 | assert!(refused.contains("changed since it was shown"), "{refused}"); |
| 246 | assert_eq!(fs::read(&config.state_path).unwrap(), before); |
| 247 | assert!(!registry.get("beta").unwrap().trusted()); |
| 248 | |
| 249 | let shared = registry.host_service_review("beta").unwrap(); |
| 250 | assert_ne!(shared.token, sole.token); |
| 251 | assert_eq!(shared.lines[0].shared_with[0].name, "alpha"); |
| 252 | assert!(shared.lines[0].withheld_by.is_empty()); |
| 253 | assert!(shared.text()[1].contains("Shared with `alpha`: each can use it as this plugin")); |
| 254 | approve(&mut registry, "beta", person()); |
| 255 | assert_eq!( |
| 256 | granted(&config, "beta").unwrap(), |
| 257 | vec![HostService::Network] |
| 258 | ); |
| 259 | assert_eq!( |
| 260 | granted(&config, "alpha").unwrap(), |
| 261 | vec![HostService::Network] |
| 262 | ); |
| 263 | let again = registry.host_service_review("alpha").unwrap(); |
| 264 | assert_eq!(again.lines[0].shared_with[0].name, "beta"); |
| 265 | } |
| 266 | |
| 267 | #[test] |
| 268 | fn an_older_altered_or_unreadable_record_grants_nothing() { |
| 269 | let _policy = TestPolicyGuard::extension_host(true); |
| 270 | let _offer = TestOffer::only(&[HostService::Network, HostService::Llm]); |
| 271 | let tmp = tempfile::tempdir().unwrap(); |
| 272 | let config = config(tmp.path()); |
| 273 | write_plugin( |
| 274 | &config, |
| 275 | "alpha", |
| 276 | &["network"], |
| 277 | "export function apply() {}\n", |
| 278 | ); |
| 279 | let mut registry = discover_with_config(&config); |
| 280 | approve(&mut registry, "alpha", person()); |
| 281 | let authority = registry.authority_for("alpha").unwrap(); |
| 282 | assert_eq!( |
| 283 | granted_host_services(&authority).unwrap(), |
| 284 | vec![HostService::Network] |
| 285 | ); |
| 286 | let sound = fs::read(&config.state_path).unwrap(); |
| 287 | |
| 288 | edit_receipt(&config, "alpha", |receipt| { |
| 289 | receipt.as_object_mut().unwrap().remove("version"); |
| 290 | receipt.as_object_mut().unwrap().remove("approved_by"); |
| 291 | }); |
| 292 | assert!(verify_plugin_state_authority(&authority).is_ok()); |
| 293 | assert!(granted_host_services(&authority).unwrap().is_empty()); |
| 294 | let older = discover_with_config(&config); |
| 295 | assert!(older.get("alpha").unwrap().trusted()); |
| 296 | assert_eq!(older.get("alpha").unwrap().approved_by, None); |
| 297 | assert!(older.get("alpha").unwrap().host_services.is_empty()); |
| 298 | |
| 299 | let edits: [(&str, Edit); 3] = [ |
| 300 | ("a later format", |receipt| receipt["version"] = json!(3)), |
| 301 | ("a grant beyond what was asked", |receipt| { |
| 302 | receipt["host_services"] = json!(["network", "llm"]); |
| 303 | }), |
| 304 | ("an edited reviewed inventory", |receipt| { |
| 305 | receipt["host_services"] = json!(["network", "llm"]); |
| 306 | receipt["reviewed_capabilities"]["host_services"] = json!(["llm", "network"]); |
| 307 | }), |
| 308 | ]; |
| 309 | for (what, edit) in edits { |
| 310 | fs::write(&config.state_path, &sound).unwrap(); |
| 311 | edit_receipt(&config, "alpha", edit); |
| 312 | assert!( |
| 313 | granted_host_services(&authority).unwrap().is_empty(), |
| 314 | "{what}" |
| 315 | ); |
| 316 | assert!( |
| 317 | discover_with_config(&config) |
| 318 | .get("alpha") |
| 319 | .unwrap() |
| 320 | .host_services |
| 321 | .is_empty(), |
| 322 | "{what}" |
| 323 | ); |
| 324 | } |
| 325 | |
| 326 | fs::write(&config.state_path, &sound).unwrap(); |
| 327 | edit_receipt(&config, "alpha", |receipt| { |
| 328 | receipt["capability_hash"] = json!("0".repeat(64)); |
| 329 | }); |
| 330 | assert!(granted_host_services(&authority).is_err()); |
| 331 | |
| 332 | fs::write(&config.state_path, &sound).unwrap(); |
| 333 | edit_receipt(&config, "alpha", |receipt| { |
| 334 | receipt["host_services"] = json!(["everything"]); |
| 335 | }); |
| 336 | assert!(granted_host_services(&authority).is_err()); |
| 337 | assert!( |
| 338 | !discover_with_config(&config) |
| 339 | .get("alpha") |
| 340 | .unwrap() |
| 341 | .trusted() |
| 342 | ); |
| 343 | |
| 344 | fs::write(&config.state_path, b"{ not json").unwrap(); |
| 345 | assert!(granted_host_services(&authority).is_err()); |
| 346 | fs::remove_file(&config.state_path).unwrap(); |
| 347 | assert!(granted_host_services(&authority).is_err()); |
| 348 | |
| 349 | fs::write(&config.state_path, &sound).unwrap(); |
| 350 | assert_eq!( |
| 351 | granted_host_services(&authority).unwrap(), |
| 352 | vec![HostService::Network] |
| 353 | ); |
| 354 | } |
| 355 | |
| 356 | #[test] |
| 357 | fn turning_off_revoking_or_reviewing_again_withdraws_the_grant_from_the_old_authority() { |
| 358 | let _policy = TestPolicyGuard::extension_host(true); |
| 359 | let _offer = TestOffer::only(&[HostService::Network]); |
| 360 | let tmp = tempfile::tempdir().unwrap(); |
| 361 | let config = config(tmp.path()); |
| 362 | write_plugin( |
| 363 | &config, |
| 364 | "alpha", |
| 365 | &["network"], |
| 366 | "export function apply() {}\n", |
| 367 | ); |
| 368 | let mut registry = discover_with_config(&config); |
| 369 | approve(&mut registry, "alpha", person()); |
| 370 | let held = registry.authority_for("alpha").unwrap(); |
| 371 | assert_eq!( |
| 372 | granted_host_services(&held).unwrap(), |
| 373 | vec![HostService::Network] |
| 374 | ); |
| 375 | |
| 376 | discover_with_config(&config).disable("alpha").unwrap(); |
| 377 | assert!(granted_host_services(&held).is_err()); |
| 378 | let mut registry = discover_with_config(&config); |
| 379 | registry.enable("alpha").unwrap(); |
| 380 | assert!( |
| 381 | granted_host_services(&held).is_err(), |
| 382 | "an authority from before the plugin was turned off is spent" |
| 383 | ); |
| 384 | let current = registry.authority_for("alpha").unwrap(); |
| 385 | assert_eq!( |
| 386 | granted_host_services(¤t).unwrap(), |
| 387 | vec![HostService::Network] |
| 388 | ); |
| 389 | |
| 390 | discover_with_config(&config).revoke_trust("alpha").unwrap(); |
| 391 | assert!(granted_host_services(¤t).is_err()); |
| 392 | |
| 393 | let mut registry = discover_with_config(&config); |
| 394 | registry.trust("alpha").unwrap(); |
| 395 | registry.enable("alpha").unwrap(); |
| 396 | assert!(granted_host_services(¤t).is_err()); |
| 397 | assert!( |
| 398 | granted_host_services(®istry.authority_for("alpha").unwrap()) |
| 399 | .unwrap() |
| 400 | .is_empty(), |
| 401 | "a review that did not show the services grants none" |
| 402 | ); |
| 403 | |
| 404 | write_plugin(&config, "alpha", &[], "export function apply() {}\n"); |
| 405 | let narrowed = discover_with_config(&config); |
| 406 | assert_eq!( |
| 407 | narrowed.get("alpha").unwrap().trust_status.as_str(), |
| 408 | "capabilities-changed" |
| 409 | ); |
| 410 | assert!(narrowed.authority_for("alpha").is_none() || granted(&config, "alpha").is_err()); |
| 411 | } |
| 412 | |
| 413 | #[test] |
| 414 | fn the_importer_asks_only_for_services_its_own_server_modules_inject() { |
| 415 | use std::collections::BTreeMap; |
| 416 | |
| 417 | use super::install::dsh::{DshClientClosure, asked_host_services}; |
| 418 | |
| 419 | let table = [ |
| 420 | ("llm", HostService::Llm), |
| 421 | ("credentials", HostService::Network), |
| 422 | ("remote", HostService::Remote), |
| 423 | ("terminal", HostService::Terminal), |
| 424 | ]; |
| 425 | let file = |text: &str| text.as_bytes().to_vec(); |
| 426 | let files: BTreeMap<String, Vec<u8>> = [ |
| 427 | ( |
| 428 | "source/lib/index.js", |
| 429 | file("export const inject = ['tools', 'llm']"), |
| 430 | ), |
| 431 | ( |
| 432 | "source/lib/client.js", |
| 433 | file("export const inject = [\"slots\", \"remote\"]"), |
| 434 | ), |
| 435 | ("source/README.md", file("inject = ['terminal']")), |
| 436 | ( |
| 437 | "source/node_modules/@scope/row/index.mjs", |
| 438 | file("export const inject = ['credentials']"), |
| 439 | ), |
| 440 | ( |
| 441 | "source/node_modules/@scope/row/node_modules/deep/index.js", |
| 442 | file("export const inject = ['terminal']"), |
| 443 | ), |
| 444 | ( |
| 445 | "source/node_modules/other/index.js", |
| 446 | file("export const inject = ['terminal']"), |
| 447 | ), |
| 448 | ( |
| 449 | "native/index.mjs", |
| 450 | file("export const inject = ['terminal']"), |
| 451 | ), |
| 452 | ] |
| 453 | .into_iter() |
| 454 | .map(|(path, bytes)| (path.to_string(), bytes)) |
| 455 | .collect(); |
| 456 | let client = DshClientClosure { |
| 457 | package: "demo".into(), |
| 458 | root: String::new(), |
| 459 | entry: "source/lib/client.js".into(), |
| 460 | platform: None, |
| 461 | inject: vec!["remote".into()], |
| 462 | host_provided: Vec::new(), |
| 463 | files: [("source/lib/client.js".to_string(), String::new())].into(), |
| 464 | imports: Default::default(), |
| 465 | bytes: 0, |
| 466 | }; |
| 467 | let rows = vec!["demo".to_string(), "@scope/row".to_string()]; |
| 468 | assert_eq!( |
| 469 | asked_host_services(&files, &rows, std::slice::from_ref(&client), &table), |
| 470 | vec![HostService::Llm, HostService::Network] |
| 471 | ); |
| 472 | assert_eq!( |
| 473 | asked_host_services(&files, &rows, &[], &table), |
| 474 | vec![HostService::Llm, HostService::Network, HostService::Remote] |
| 475 | ); |
| 476 | assert!(asked_host_services(&files, &rows, &[], &[]).is_empty()); |
| 477 | assert_eq!( |
| 478 | asked_host_services( |
| 479 | &files, |
| 480 | &rows, |
| 481 | &[], |
| 482 | super::host_services::HOST_SERVICE_INJECTS |
| 483 | ), |
| 484 | vec![HostService::Llm, HostService::Network, HostService::Remote] |
| 485 | ); |
| 486 | } |
| 487 | |
| 488 | #[test] |
| 489 | fn the_agent_is_an_approver_only_with_the_token_of_its_own_review_and_in_its_own_project() { |
| 490 | let _policy = TestPolicyGuard::extension_host(true); |
| 491 | let _offer = TestOffer::only(&[HostService::Network]); |
| 492 | let tmp = tempfile::tempdir().unwrap(); |
| 493 | let config = config(tmp.path()); |
| 494 | write_plugin( |
| 495 | &config, |
| 496 | "alpha", |
| 497 | &["network"], |
| 498 | "export function apply() {}\n", |
| 499 | ); |
| 500 | write_plugin( |
| 501 | &config, |
| 502 | "beta", |
| 503 | &["network"], |
| 504 | "export function apply() {}\n", |
| 505 | ); |
| 506 | let mut registry = discover_with_config(&config); |
| 507 | approve(&mut registry, "alpha", person()); |
| 508 | let by_agent = agent(&config, AgentPosture::FullAccess); |
| 509 | let agents_review = registry.host_service_review_for("beta", &by_agent).unwrap(); |
| 510 | let persons_review = registry.host_service_review("beta").unwrap(); |
| 511 | assert!(agents_review.lines[0].shared_with.is_empty()); |
| 512 | assert_eq!(persons_review.lines[0].shared_with[0].name, "alpha"); |
| 513 | assert_ne!(agents_review.token, persons_review.token); |
| 514 | |
| 515 | let before = fs::read(&config.state_path).unwrap(); |
| 516 | let PluginApprover::Agent(fields) = by_agent.clone() else { |
| 517 | unreachable!() |
| 518 | }; |
| 519 | let refusals = [ |
| 520 | (by_agent.clone(), None, "must carry the token"), |
| 521 | ( |
| 522 | PluginApprover::Agent(AgentApproval { |
| 523 | tool_call_id: None, |
| 524 | ..fields.clone() |
| 525 | }), |
| 526 | Some(agents_review.token.clone()), |
| 527 | "must name the thread, turn and tool call", |
| 528 | ), |
| 529 | ( |
| 530 | PluginApprover::Agent(AgentApproval { |
| 531 | workspace: tmp.path().join("elsewhere").to_string_lossy().into_owned(), |
| 532 | ..fields.clone() |
| 533 | }), |
| 534 | Some(agents_review.token.clone()), |
| 535 | "limited to its own project", |
| 536 | ), |
| 537 | ( |
| 538 | by_agent.clone(), |
| 539 | Some(persons_review.token.clone()), |
| 540 | "changed since it was shown", |
| 541 | ), |
| 542 | ]; |
| 543 | for (approved_by, host_services, says) in refusals { |
| 544 | let refused = registry |
| 545 | .trust_reviewed( |
| 546 | "beta", |
| 547 | ReviewApproval { |
| 548 | approved_by, |
| 549 | host_services, |
| 550 | }, |
| 551 | ) |
| 552 | .unwrap_err(); |
| 553 | assert!(refused.contains(says), "{refused}"); |
| 554 | assert_eq!(fs::read(&config.state_path).unwrap(), before); |
| 555 | } |
| 556 | |
| 557 | approve(&mut registry, "beta", by_agent.clone()); |
| 558 | assert_eq!(registry.get("beta").unwrap().approved_by, Some(by_agent)); |
| 559 | assert_eq!( |
| 560 | granted(&config, "beta").unwrap(), |
| 561 | vec![HostService::Network] |
| 562 | ); |
| 563 | let alphas = registry.host_service_review("alpha").unwrap(); |
| 564 | assert!( |
| 565 | alphas.lines[0].shared_with.is_empty(), |
| 566 | "a reviewed plugin shares nothing with an agent-approved one" |
| 567 | ); |
| 568 | |
| 569 | let auto_review_agent = PluginApprover::auto_review( |
| 570 | &config.workspace, |
| 571 | Some("th".into()), |
| 572 | Some("tr".into()), |
| 573 | Some("tc".into()), |
| 574 | ); |
| 575 | assert_eq!( |
| 576 | crate::host_services::approver_label(Some(&auto_review_agent)), |
| 577 | "approved by the agent in Auto-Review" |
| 578 | ); |
| 579 | let missing_call_id = PluginApprover::auto_review( |
| 580 | &config.workspace, |
| 581 | Some("th".into()), |
| 582 | Some("tr".into()), |
| 583 | None, |
| 584 | ); |
| 585 | let missing_err = registry |
| 586 | .trust_reviewed( |
| 587 | "beta", |
| 588 | ReviewApproval { |
| 589 | approved_by: missing_call_id, |
| 590 | host_services: Some(agents_review.token.clone()), |
| 591 | }, |
| 592 | ) |
| 593 | .unwrap_err(); |
| 594 | assert!( |
| 595 | missing_err.contains("must name the thread, turn and tool call"), |
| 596 | "{missing_err}" |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | #[test] |
| 601 | fn an_agent_approved_record_is_active_only_in_the_project_it_names() { |
| 602 | let _policy = TestPolicyGuard::extension_host(true); |
| 603 | let tmp = tempfile::tempdir().unwrap(); |
| 604 | let config = config(tmp.path()); |
| 605 | write_plugin(&config, "alpha", &[], "export function apply() {}\n"); |
| 606 | write_plugin(&config, "beta", &[], "export function apply() {}\n"); |
| 607 | let mut registry = discover_with_config(&config); |
| 608 | approve( |
| 609 | &mut registry, |
| 610 | "alpha", |
| 611 | agent(&config, AgentPosture::AutoReview), |
| 612 | ); |
| 613 | approve(&mut registry, "beta", person()); |
| 614 | assert!(registry.get("alpha").unwrap().active()); |
| 615 | |
| 616 | let elsewhere = DiscoveryConfig { |
| 617 | workspace: tmp.path().join("other-project"), |
| 618 | ..config.clone() |
| 619 | }; |
| 620 | let other = discover_with_config(&elsewhere); |
| 621 | let alpha = other.get("alpha").unwrap(); |
| 622 | assert!(alpha.trusted() && alpha.enabled && alpha.outside_reach); |
| 623 | assert!(!alpha.active()); |
| 624 | assert!(other.get("beta").unwrap().active()); |
| 625 | |
| 626 | let again = discover_with_config(&config); |
| 627 | assert!(!again.get("alpha").unwrap().outside_reach); |
| 628 | assert!(again.get("alpha").unwrap().active()); |
| 629 | } |
| 630 | |
| 631 | #[test] |
| 632 | fn a_command_line_trust_is_recorded_as_such_and_the_agents_mark_makes_it_the_agents() { |
| 633 | let _policy = TestPolicyGuard::extension_host(true); |
| 634 | let tmp = tempfile::tempdir().unwrap(); |
| 635 | let config = config(tmp.path()); |
| 636 | write_plugin(&config, "alpha", &[], "export function apply() {}\n"); |
| 637 | write_plugin(&config, "beta", &[], "export function apply() {}\n"); |
| 638 | let mut registry = discover_with_config(&config); |
| 639 | approve(&mut registry, "alpha", PluginApprover::command_line(None)); |
| 640 | let marked = PluginApprover::command_line(Some(&config.workspace)); |
| 641 | assert!( |
| 642 | registry |
| 643 | .trust_reviewed( |
| 644 | "beta", |
| 645 | ReviewApproval { |
| 646 | approved_by: marked.clone(), |
| 647 | host_services: None, |
| 648 | }, |
| 649 | ) |
| 650 | .unwrap_err() |
| 651 | .contains("must carry the token") |
| 652 | ); |
| 653 | approve(&mut registry, "beta", marked.clone()); |
| 654 | |
| 655 | let state: Value = |
| 656 | serde_json::from_str(&fs::read_to_string(&config.state_path).unwrap()).unwrap(); |
| 657 | let record = |name: &str| { |
| 658 | state["plugins"] |
| 659 | .as_object() |
| 660 | .unwrap() |
| 661 | .iter() |
| 662 | .find(|(id, _)| id.ends_with(&format!("/{name}"))) |
| 663 | .map(|(_, entry)| entry.clone()) |
| 664 | .unwrap() |
| 665 | }; |
| 666 | assert_eq!( |
| 667 | record("alpha")["trust"]["approved_by"], |
| 668 | json!({"kind": "command_line"}) |
| 669 | ); |
| 670 | assert_eq!( |
| 671 | record("beta")["trust"]["approved_by"], |
| 672 | json!({"kind": "agent", "posture": "command_line", "workspace": config.workspace.to_string_lossy()}) |
| 673 | ); |
| 674 | assert_eq!( |
| 675 | record("beta")["enabled_by"], |
| 676 | json!({"kind": "person", "via": "terminal"}) |
| 677 | ); |
| 678 | |
| 679 | let mut registry = discover_with_config(&config); |
| 680 | assert!( |
| 681 | registry |
| 682 | .enable_by("alpha", marked.clone()) |
| 683 | .unwrap_err() |
| 684 | .contains("approved for every project") |
| 685 | ); |
| 686 | registry.disable("beta").unwrap(); |
| 687 | registry.enable_by("beta", marked.clone()).unwrap(); |
| 688 | assert_eq!(registry.get("beta").unwrap().enabled_by, Some(marked)); |
| 689 | registry.disable("alpha").unwrap(); |
| 690 | assert_eq!(registry.get("alpha").unwrap().enabled_by, None); |
| 691 | registry.enable("alpha").unwrap(); |
| 692 | registry.trust("alpha").unwrap(); |
| 693 | let alpha = registry.get("alpha").unwrap(); |
| 694 | assert!(!alpha.enabled && alpha.enabled_by.is_none()); |
| 695 | |
| 696 | let elsewhere = DiscoveryConfig { |
| 697 | workspace: tmp.path().join("other-project"), |
| 698 | ..config.clone() |
| 699 | }; |
| 700 | assert!( |
| 701 | !discover_with_config(&elsewhere) |
| 702 | .get("beta") |
| 703 | .unwrap() |
| 704 | .active() |
| 705 | ); |
| 706 | } |
| 707 | |
| 708 | #[test] |
| 709 | fn the_agent_turns_on_only_a_plugin_it_approved_in_its_own_project() { |
| 710 | let _policy = TestPolicyGuard::extension_host(true); |
| 711 | let tmp = tempfile::tempdir().unwrap(); |
| 712 | let config = config(tmp.path()); |
| 713 | write_plugin(&config, "alpha", &[], "export function apply() {}\n"); |
| 714 | write_plugin(&config, "beta", &[], "export function apply() {}\n"); |
| 715 | let mut registry = discover_with_config(&config); |
| 716 | approve(&mut registry, "alpha", person()); |
| 717 | approve( |
| 718 | &mut registry, |
| 719 | "beta", |
| 720 | agent(&config, AgentPosture::FullAccess), |
| 721 | ); |
| 722 | registry.disable("alpha").unwrap(); |
| 723 | registry.disable("beta").unwrap(); |
| 724 | let by_agent = agent(&config, AgentPosture::FullAccess); |
| 725 | let PluginApprover::Agent(fields) = by_agent.clone() else { |
| 726 | unreachable!() |
| 727 | }; |
| 728 | let elsewhere = DiscoveryConfig { |
| 729 | workspace: tmp.path().join("other-project"), |
| 730 | ..config.clone() |
| 731 | }; |
| 732 | let mut other = discover_with_config(&elsewhere); |
| 733 | let before = fs::read(&config.state_path).unwrap(); |
| 734 | let refusals = [ |
| 735 | ( |
| 736 | &mut registry, |
| 737 | "alpha", |
| 738 | by_agent.clone(), |
| 739 | "approved for every project", |
| 740 | ), |
| 741 | ( |
| 742 | &mut discover_with_config(&config), |
| 743 | "beta", |
| 744 | PluginApprover::Agent(AgentApproval { |
| 745 | workspace: tmp.path().join("elsewhere").to_string_lossy().into_owned(), |
| 746 | ..fields.clone() |
| 747 | }), |
| 748 | "limited to its own project", |
| 749 | ), |
| 750 | ( |
| 751 | &mut discover_with_config(&config), |
| 752 | "beta", |
| 753 | PluginApprover::Agent(AgentApproval { |
| 754 | tool_call_id: None, |
| 755 | ..fields.clone() |
| 756 | }), |
| 757 | "must name the thread, turn and tool call", |
| 758 | ), |
| 759 | ( |
| 760 | &mut other, |
| 761 | "beta", |
| 762 | agent(&elsewhere, AgentPosture::FullAccess), |
| 763 | "agent of another project", |
| 764 | ), |
| 765 | ]; |
| 766 | for (registry, name, enabled_by, says) in refusals { |
| 767 | let refused = registry.enable_by(name, enabled_by).unwrap_err(); |
| 768 | assert!(refused.contains(says), "{name}: {refused}"); |
| 769 | assert_eq!(fs::read(&config.state_path).unwrap(), before); |
| 770 | } |
| 771 | |
| 772 | registry.enable_by("beta", by_agent.clone()).unwrap(); |
| 773 | let beta = registry.get("beta").unwrap(); |
| 774 | assert!(beta.active()); |
| 775 | assert_eq!(beta.enabled_by, Some(by_agent)); |
| 776 | assert!( |
| 777 | !discover_with_config(&elsewhere) |
| 778 | .get("beta") |
| 779 | .unwrap() |
| 780 | .active() |
| 781 | ); |
| 782 | } |
| 783 | |
| 784 | #[test] |
| 785 | fn the_agent_never_replaces_a_review_held_for_every_project() { |
| 786 | let _policy = TestPolicyGuard::extension_host(true); |
| 787 | let _offer = TestOffer::only(&[HostService::Network]); |
| 788 | let tmp = tempfile::tempdir().unwrap(); |
| 789 | let config = DiscoveryConfig { |
| 790 | builtin_plugin_dirs: vec![tmp.path().join("builtin")], |
| 791 | ..config(tmp.path()) |
| 792 | }; |
| 793 | for name in ["alpha", "beta", "gamma"] { |
| 794 | write_plugin(&config, name, &["network"], "export function apply() {}\n"); |
| 795 | } |
| 796 | write_plugin_in( |
| 797 | &config.builtin_plugin_dirs[0], |
| 798 | "delta", |
| 799 | &[], |
| 800 | "export function apply() {}\n", |
| 801 | ); |
| 802 | let mut registry = discover_with_config(&config); |
| 803 | approve(&mut registry, "alpha", person()); |
| 804 | approve(&mut registry, "beta", PluginApprover::CommandLine); |
| 805 | approve(&mut registry, "gamma", person()); |
| 806 | edit_receipt(&config, "gamma", |receipt| { |
| 807 | receipt.as_object_mut().unwrap().remove("version"); |
| 808 | receipt.as_object_mut().unwrap().remove("approved_by"); |
| 809 | }); |
| 810 | let mut registry = discover_with_config(&config); |
| 811 | assert_eq!(registry.get("gamma").unwrap().approved_by, None); |
| 812 | assert!(registry.get("gamma").unwrap().trusted()); |
| 813 | let by_agent = agent(&config, AgentPosture::FullAccess); |
| 814 | |
| 815 | let before = fs::read(&config.state_path).unwrap(); |
| 816 | for (name, says) in [ |
| 817 | ("alpha", "holds a review for every project"), |
| 818 | ("beta", "holds a review for every project"), |
| 819 | ("gamma", "holds a review for every project"), |
| 820 | ("delta", "built-in plugin"), |
| 821 | ] { |
| 822 | let shown = registry |
| 823 | .host_service_review_for(name, &by_agent) |
| 824 | .map(|review| review.token); |
| 825 | let refused = registry |
| 826 | .trust_reviewed( |
| 827 | name, |
| 828 | ReviewApproval { |
| 829 | approved_by: by_agent.clone(), |
| 830 | host_services: shown, |
| 831 | }, |
| 832 | ) |
| 833 | .unwrap_err(); |
| 834 | assert!(refused.contains(says), "{name}: {refused}"); |
| 835 | assert_eq!(fs::read(&config.state_path).unwrap(), before, "{name}"); |
| 836 | } |
| 837 | let refused = registry.enable_by("alpha", by_agent.clone()).unwrap_err(); |
| 838 | assert!(refused.contains("approved for every project"), "{refused}"); |
| 839 | let alpha = discover_with_config(&config); |
| 840 | let alpha = alpha.get("alpha").unwrap(); |
| 841 | assert_eq!(alpha.approved_by, Some(person())); |
| 842 | assert!(alpha.active()); |
| 843 | assert_eq!( |
| 844 | granted(&config, "alpha").unwrap(), |
| 845 | vec![HostService::Network] |
| 846 | ); |
| 847 | } |
| 848 | |
| 849 | #[test] |
| 850 | fn the_agent_cannot_replace_a_review_made_after_its_registry_was_read() { |
| 851 | let _policy = TestPolicyGuard::extension_host(true); |
| 852 | let _offer = TestOffer::only(&[HostService::Network]); |
| 853 | let tmp = tempfile::tempdir().unwrap(); |
| 854 | let config = config(tmp.path()); |
| 855 | write_plugin( |
| 856 | &config, |
| 857 | "alpha", |
| 858 | &["network"], |
| 859 | "export function apply() {}\n", |
| 860 | ); |
| 861 | let mut stale = discover_with_config(&config); |
| 862 | let mut current = discover_with_config(&config); |
| 863 | approve(&mut current, "alpha", person()); |
| 864 | let before = fs::read(&config.state_path).unwrap(); |
| 865 | |
| 866 | let by_agent = agent(&config, AgentPosture::FullAccess); |
| 867 | let shown = stale |
| 868 | .host_service_review_for("alpha", &by_agent) |
| 869 | .map(|review| review.token); |
| 870 | let refused = stale |
| 871 | .trust_reviewed( |
| 872 | "alpha", |
| 873 | ReviewApproval { |
| 874 | approved_by: by_agent, |
| 875 | host_services: shown, |
| 876 | }, |
| 877 | ) |
| 878 | .unwrap_err(); |
| 879 | assert!(refused.contains("state or review changed"), "{refused}"); |
| 880 | assert_eq!(fs::read(&config.state_path).unwrap(), before); |
| 881 | let now = discover_with_config(&config); |
| 882 | let alpha = now.get("alpha").unwrap(); |
| 883 | assert_eq!(alpha.approved_by, Some(person())); |
| 884 | assert!(alpha.active()); |
| 885 | assert_eq!( |
| 886 | granted(&config, "alpha").unwrap(), |
| 887 | vec![HostService::Network] |
| 888 | ); |
| 889 | } |
| 890 | |
| 891 | #[test] |
| 892 | fn the_agent_of_another_project_cannot_take_over_an_agents_record() { |
| 893 | let _policy = TestPolicyGuard::extension_host(true); |
| 894 | let tmp = tempfile::tempdir().unwrap(); |
| 895 | let config = config(tmp.path()); |
| 896 | write_plugin(&config, "alpha", &[], "export function apply() {}\n"); |
| 897 | let mut registry = discover_with_config(&config); |
| 898 | let home_agent = agent(&config, AgentPosture::FullAccess); |
| 899 | approve(&mut registry, "alpha", home_agent.clone()); |
| 900 | let before = fs::read(&config.state_path).unwrap(); |
| 901 | |
| 902 | let elsewhere = DiscoveryConfig { |
| 903 | workspace: tmp.path().join("other-project"), |
| 904 | ..config.clone() |
| 905 | }; |
| 906 | let mut other = discover_with_config(&elsewhere); |
| 907 | assert!(other.get("alpha").unwrap().outside_reach); |
| 908 | let other_agent = agent(&elsewhere, AgentPosture::FullAccess); |
| 909 | let shown = other |
| 910 | .host_service_review_for("alpha", &other_agent) |
| 911 | .map(|review| review.token); |
| 912 | let refused = other |
| 913 | .trust_reviewed( |
| 914 | "alpha", |
| 915 | ReviewApproval { |
| 916 | approved_by: other_agent, |
| 917 | host_services: shown, |
| 918 | }, |
| 919 | ) |
| 920 | .unwrap_err(); |
| 921 | assert!(refused.contains("agent of another project"), "{refused}"); |
| 922 | assert_eq!(fs::read(&config.state_path).unwrap(), before); |
| 923 | let home = discover_with_config(&config); |
| 924 | let alpha = home.get("alpha").unwrap(); |
| 925 | assert_eq!(alpha.approved_by, Some(home_agent)); |
| 926 | assert!(alpha.active()); |
| 927 | } |
| 928 | |
| 929 | #[cfg(unix)] |
| 930 | #[test] |
| 931 | fn a_project_plugin_folder_that_resolves_outside_the_project_is_not_the_agents() { |
| 932 | let _policy = TestPolicyGuard::extension_host(true); |
| 933 | let tmp = tempfile::tempdir().unwrap(); |
| 934 | let project = |name: &str| { |
| 935 | let workspace = tmp.path().join(name); |
| 936 | fs::create_dir_all(&workspace).unwrap(); |
| 937 | DiscoveryConfig { |
| 938 | workspace_plugins_dir: workspace.join(".codewhale/plugins"), |
| 939 | workspace, |
| 940 | ..config(tmp.path()) |
| 941 | } |
| 942 | }; |
| 943 | let linked = project("linked"); |
| 944 | let outside = tmp.path().join("outside"); |
| 945 | fs::create_dir_all(outside.join("plugins")).unwrap(); |
| 946 | std::os::unix::fs::symlink(&outside, linked.workspace.join(".codewhale")).unwrap(); |
| 947 | write_plugin_in( |
| 948 | &linked.workspace_plugins_dir, |
| 949 | "alpha", |
| 950 | &[], |
| 951 | "export function apply() {}\n", |
| 952 | ); |
| 953 | let own = project("own"); |
| 954 | write_plugin_in( |
| 955 | &own.workspace_plugins_dir, |
| 956 | "beta", |
| 957 | &[], |
| 958 | "export function apply() {}\n", |
| 959 | ); |
| 960 | |
| 961 | let mut registry = discover_with_config(&linked); |
| 962 | let alpha = registry.get("alpha").unwrap(); |
| 963 | assert_eq!(alpha.scope, super::types::PluginScope::Workspace); |
| 964 | assert!( |
| 965 | !alpha |
| 966 | .canonical_root |
| 967 | .starts_with(fs::canonicalize(&linked.workspace).unwrap()) |
| 968 | ); |
| 969 | let by_agent = agent(&linked, AgentPosture::FullAccess); |
| 970 | let shown = registry |
| 971 | .host_service_review_for("alpha", &by_agent) |
| 972 | .map(|review| review.token); |
| 973 | let refused = registry |
| 974 | .trust_reviewed( |
| 975 | "alpha", |
| 976 | ReviewApproval { |
| 977 | approved_by: by_agent, |
| 978 | host_services: shown, |
| 979 | }, |
| 980 | ) |
| 981 | .unwrap_err(); |
| 982 | assert!( |
| 983 | refused.contains("resolves outside this project"), |
| 984 | "{refused}" |
| 985 | ); |
| 986 | assert!(registry.get("alpha").unwrap().approved_by.is_none()); |
| 987 | |
| 988 | let mut registry = discover_with_config(&own); |
| 989 | let by_agent = agent(&own, AgentPosture::FullAccess); |
| 990 | let shown = registry |
| 991 | .host_service_review_for("beta", &by_agent) |
| 992 | .map(|review| review.token); |
| 993 | registry |
| 994 | .trust_reviewed( |
| 995 | "beta", |
| 996 | ReviewApproval { |
| 997 | approved_by: by_agent.clone(), |
| 998 | host_services: shown, |
| 999 | }, |
| 1000 | ) |
| 1001 | .unwrap(); |
| 1002 | registry.enable_by("beta", by_agent).unwrap(); |
| 1003 | assert!(registry.get("beta").unwrap().active()); |
| 1004 | } |
| 1005 | |
| 1006 | #[test] |
| 1007 | fn a_command_line_approval_carries_no_host_service_grant() { |
| 1008 | let _policy = TestPolicyGuard::extension_host(true); |
| 1009 | let _offer = TestOffer::only(&[HostService::Network]); |
| 1010 | let tmp = tempfile::tempdir().unwrap(); |
| 1011 | let config = config(tmp.path()); |
| 1012 | write_plugin( |
| 1013 | &config, |
| 1014 | "alpha", |
| 1015 | &["network"], |
| 1016 | "export function apply() {}\n", |
| 1017 | ); |
| 1018 | let mut registry = discover_with_config(&config); |
| 1019 | let shown = registry |
| 1020 | .host_service_review_for("alpha", &PluginApprover::CommandLine) |
| 1021 | .map(|review| review.token); |
| 1022 | assert!(shown.is_some()); |
| 1023 | let refused = registry |
| 1024 | .trust_reviewed( |
| 1025 | "alpha", |
| 1026 | ReviewApproval { |
| 1027 | approved_by: PluginApprover::CommandLine, |
| 1028 | host_services: shown, |
| 1029 | }, |
| 1030 | ) |
| 1031 | .unwrap_err(); |
| 1032 | assert!(refused.contains("cannot grant host services"), "{refused}"); |
| 1033 | assert!(registry.get("alpha").unwrap().approved_by.is_none()); |
| 1034 | |
| 1035 | approve(&mut registry, "alpha", PluginApprover::CommandLine); |
| 1036 | assert_eq!( |
| 1037 | registry.get("alpha").unwrap().approved_by, |
| 1038 | Some(PluginApprover::CommandLine) |
| 1039 | ); |
| 1040 | assert_eq!(granted(&config, "alpha").unwrap(), Vec::new()); |
| 1041 | } |
| 1042 | |
| 1043 | mod v0_10_2 { |
| 1044 | use std::collections::BTreeMap; |
| 1045 | |
| 1046 | use serde::Deserialize; |
| 1047 | |
| 1048 | #[derive(Debug, Deserialize)] |
| 1049 | #[serde(deny_unknown_fields)] |
| 1050 | pub struct PluginStateFile { |
| 1051 | pub schema_version: u32, |
| 1052 | #[serde(default)] |
| 1053 | pub plugins: BTreeMap<String, PersistedPluginState>, |
| 1054 | } |
| 1055 | |
| 1056 | #[derive(Debug, Deserialize)] |
| 1057 | #[serde(deny_unknown_fields)] |
| 1058 | pub struct PersistedPluginState { |
| 1059 | #[serde(default)] |
| 1060 | pub generation: u64, |
| 1061 | #[serde(default)] |
| 1062 | pub enabled: bool, |
| 1063 | #[serde(default)] |
| 1064 | pub trust: Option<TrustReceipt>, |
| 1065 | #[serde(default)] |
| 1066 | pub review_history: Vec<TrustReceipt>, |
| 1067 | } |
| 1068 | |
| 1069 | #[derive(Debug, Deserialize)] |
| 1070 | #[serde(deny_unknown_fields)] |
| 1071 | pub struct TrustReceipt { |
| 1072 | pub content_hash: String, |
| 1073 | pub capability_hash: String, |
| 1074 | pub reviewed_capabilities: PluginInventory, |
| 1075 | pub reviewed_at: String, |
| 1076 | } |
| 1077 | |
| 1078 | #[derive(Debug, Deserialize)] |
| 1079 | #[serde(deny_unknown_fields)] |
| 1080 | pub struct PluginInventory { |
| 1081 | pub skills: usize, |
| 1082 | pub mcp_servers: usize, |
| 1083 | #[serde(default)] |
| 1084 | pub stdio_mcp_servers: usize, |
| 1085 | #[serde(default)] |
| 1086 | pub remote_mcp_servers: usize, |
| 1087 | pub commands: usize, |
| 1088 | pub agents: usize, |
| 1089 | pub hooks: usize, |
| 1090 | pub lsp: usize, |
| 1091 | pub native: usize, |
| 1092 | #[serde(default)] |
| 1093 | pub providers: usize, |
| 1094 | pub filesystem_roots: Vec<String>, |
| 1095 | pub network_hosts: Vec<String>, |
| 1096 | pub lifecycle_mutation: bool, |
| 1097 | } |
| 1098 | |
| 1099 | impl PluginStateFile { |
| 1100 | pub fn reviews(&self) -> Vec<String> { |
| 1101 | self.plugins |
| 1102 | .values() |
| 1103 | .flat_map(|entry| { |
| 1104 | entry |
| 1105 | .trust |
| 1106 | .iter() |
| 1107 | .chain(&entry.review_history) |
| 1108 | .map(move |receipt| { |
| 1109 | format!( |
| 1110 | "{}:{}:{}:{}:{}", |
| 1111 | entry.generation, |
| 1112 | entry.enabled, |
| 1113 | receipt.content_hash, |
| 1114 | receipt.capability_hash, |
| 1115 | receipt.reviewed_at |
| 1116 | ) + &receipt.reviewed_capabilities.counted() |
| 1117 | }) |
| 1118 | }) |
| 1119 | .collect() |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | impl PluginInventory { |
| 1124 | fn counted(&self) -> String { |
| 1125 | format!( |
| 1126 | ":{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}", |
| 1127 | self.skills, |
| 1128 | self.mcp_servers, |
| 1129 | self.stdio_mcp_servers, |
| 1130 | self.remote_mcp_servers, |
| 1131 | self.commands, |
| 1132 | self.agents, |
| 1133 | self.hooks, |
| 1134 | self.lsp, |
| 1135 | self.native, |
| 1136 | self.providers, |
| 1137 | self.filesystem_roots.len(), |
| 1138 | self.network_hosts.len(), |
| 1139 | self.lifecycle_mutation |
| 1140 | ) |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | #[test] |
| 1146 | fn v0_10_2_reads_this_state_file_until_a_plugin_is_reviewed_or_turned_on_here() { |
| 1147 | let _policy = TestPolicyGuard::extension_host(true); |
| 1148 | let tmp = tempfile::tempdir().unwrap(); |
| 1149 | let config = config(tmp.path()); |
| 1150 | write_plugin(&config, "alpha", &[], "export function apply() {}\n"); |
| 1151 | write_plugin(&config, "beta", &[], "export function apply() {}\n"); |
| 1152 | let older = |config: &DiscoveryConfig| { |
| 1153 | serde_json::from_str::<v0_10_2::PluginStateFile>( |
| 1154 | &fs::read_to_string(&config.state_path).unwrap(), |
| 1155 | ) |
| 1156 | }; |
| 1157 | |
| 1158 | let mut registry = discover_with_config(&config); |
| 1159 | approve(&mut registry, "alpha", person()); |
| 1160 | edit_receipt(&config, "alpha", |receipt| { |
| 1161 | receipt.as_object_mut().unwrap().remove("version"); |
| 1162 | receipt.as_object_mut().unwrap().remove("approved_by"); |
| 1163 | }); |
| 1164 | let mut state: Value = |
| 1165 | serde_json::from_str(&fs::read_to_string(&config.state_path).unwrap()).unwrap(); |
| 1166 | for entry in state["plugins"].as_object_mut().unwrap().values_mut() { |
| 1167 | entry.as_object_mut().unwrap().remove("enabled_by"); |
| 1168 | for receipt in entry["review_history"].as_array_mut().unwrap() { |
| 1169 | receipt.as_object_mut().unwrap().remove("version"); |
| 1170 | receipt.as_object_mut().unwrap().remove("approved_by"); |
| 1171 | } |
| 1172 | } |
| 1173 | fs::write( |
| 1174 | &config.state_path, |
| 1175 | serde_json::to_string_pretty(&state).unwrap(), |
| 1176 | ) |
| 1177 | .unwrap(); |
| 1178 | let parsed = older(&config).expect("a state file of v0.10.2's shape"); |
| 1179 | assert_eq!(parsed.schema_version, 1); |
| 1180 | let content = ®istry.get("alpha").unwrap().content_hash; |
| 1181 | assert!( |
| 1182 | parsed |
| 1183 | .reviews() |
| 1184 | .iter() |
| 1185 | .any(|review| review.contains(content.as_str())), |
| 1186 | "{:?}", |
| 1187 | parsed.reviews() |
| 1188 | ); |
| 1189 | |
| 1190 | let mut registry = discover_with_config(&config); |
| 1191 | assert!(registry.get("alpha").unwrap().active()); |
| 1192 | registry.disable("beta").unwrap(); |
| 1193 | registry.disable("alpha").unwrap(); |
| 1194 | let parsed = older(&config).expect("writes that record no review or enablement stay readable"); |
| 1195 | assert!(parsed.plugins.values().all(|entry| !entry.enabled)); |
| 1196 | |
| 1197 | registry.enable("alpha").unwrap(); |
| 1198 | let refused = older(&config).unwrap_err().to_string(); |
| 1199 | assert!(refused.contains("unknown field `enabled_by`"), "{refused}"); |
| 1200 | |
| 1201 | registry.disable("alpha").unwrap(); |
| 1202 | assert!(older(&config).is_ok()); |
| 1203 | registry.trust("beta").unwrap(); |
| 1204 | let refused = older(&config).unwrap_err().to_string(); |
| 1205 | assert!(refused.contains("unknown field `version`"), "{refused}"); |
| 1206 | } |
| 1207 | |
| 1208 | #[test] |
| 1209 | fn an_identical_reinstall_cannot_match_a_registry_read_before_the_removal() { |
| 1210 | let _policy = TestPolicyGuard::extension_host(true); |
| 1211 | let tmp = tempfile::tempdir().unwrap(); |
| 1212 | let config = config(tmp.path()); |
| 1213 | |
| 1214 | write_plugin(&config, "alpha", &[], "export function apply() {}\n"); |
| 1215 | let by_agent = agent(&config, AgentPosture::FullAccess); |
| 1216 | let mut first = discover_with_config(&config); |
| 1217 | approve(&mut first, "alpha", by_agent.clone()); |
| 1218 | |
| 1219 | let stale = discover_with_config(&config); |
| 1220 | let stale_staged = stale.get("alpha").unwrap().staged_root.clone().unwrap(); |
| 1221 | assert!(stale_staged.is_dir()); |
| 1222 | |
| 1223 | discover_with_config(&config) |
| 1224 | .prune_state_entry("alpha") |
| 1225 | .unwrap(); |
| 1226 | let mut fresh = discover_with_config(&config); |
| 1227 | approve(&mut fresh, "alpha", person()); |
| 1228 | assert!(stale_staged.is_dir()); |
| 1229 | |
| 1230 | let before_state = fs::read(&config.state_path).unwrap(); |
| 1231 | let mut stale_review = stale.clone(); |
| 1232 | let shown = stale_review |
| 1233 | .host_service_review_for("alpha", &by_agent) |
| 1234 | .map(|review| review.token); |
| 1235 | let review_refused = stale_review |
| 1236 | .trust_reviewed( |
| 1237 | "alpha", |
| 1238 | ReviewApproval { |
| 1239 | approved_by: by_agent.clone(), |
| 1240 | host_services: shown, |
| 1241 | }, |
| 1242 | ) |
| 1243 | .unwrap_err(); |
| 1244 | assert!( |
| 1245 | review_refused.contains("state or review changed"), |
| 1246 | "{review_refused}" |
| 1247 | ); |
| 1248 | assert_eq!(fs::read(&config.state_path).unwrap(), before_state); |
| 1249 | |
| 1250 | let mut stale_enable = stale; |
| 1251 | let enable_refused = stale_enable |
| 1252 | .enable_by("alpha", by_agent.clone()) |
| 1253 | .unwrap_err(); |
| 1254 | assert!( |
| 1255 | enable_refused.contains("state or review changed"), |
| 1256 | "{enable_refused}" |
| 1257 | ); |
| 1258 | assert_eq!(fs::read(&config.state_path).unwrap(), before_state); |
| 1259 | |
| 1260 | let install_tmp = tempfile::tempdir().unwrap(); |
| 1261 | let source = install_tmp.path().join("beta-src"); |
| 1262 | fs::create_dir_all(source.join("native")).unwrap(); |
| 1263 | fs::write( |
| 1264 | source.join("native/index.mjs"), |
| 1265 | "export function apply() {}\n", |
| 1266 | ) |
| 1267 | .unwrap(); |
| 1268 | fs::write( |
| 1269 | source.join("plugin.toml"), |
| 1270 | "schema_version = 1\n[plugin]\nname = \"beta\"\nversion = \"1.0.0\"\n[native]\npath = \"native/index.mjs\"\n", |
| 1271 | ) |
| 1272 | .unwrap(); |
| 1273 | let install_config = super::discovery::DiscoveryConfig { |
| 1274 | workspace: install_tmp.path().join("project"), |
| 1275 | user_plugins_dir: install_tmp.path().join("user"), |
| 1276 | workspace_plugins_dir: install_tmp.path().join("workspace"), |
| 1277 | builtin_plugin_dirs: Vec::new(), |
| 1278 | state_path: install_tmp.path().join("state/plugin-state.json"), |
| 1279 | }; |
| 1280 | fs::create_dir_all(&install_config.workspace).unwrap(); |
| 1281 | fs::create_dir_all(&install_config.user_plugins_dir).unwrap(); |
| 1282 | fs::create_dir_all(&install_config.workspace_plugins_dir).unwrap(); |
| 1283 | |
| 1284 | let runtime = tokio::runtime::Builder::new_current_thread() |
| 1285 | .enable_all() |
| 1286 | .build() |
| 1287 | .unwrap(); |
| 1288 | let network = codewhale_net::network_policy::NetworkPolicy::default(); |
| 1289 | let mut reg = discover_with_config(&install_config); |
| 1290 | let installed_first = runtime |
| 1291 | .block_on(super::mutation::execute( |
| 1292 | super::mutation::PluginMutationRequest::Install { |
| 1293 | source: super::install::PluginInstallSource::parse(source.to_str().unwrap()) |
| 1294 | .unwrap(), |
| 1295 | }, |
| 1296 | &super::mutation::PluginMutationContext { |
| 1297 | network: &network, |
| 1298 | max_size: super::install::DEFAULT_MAX_SIZE_BYTES, |
| 1299 | }, |
| 1300 | &mut reg, |
| 1301 | )) |
| 1302 | .unwrap(); |
| 1303 | assert_eq!( |
| 1304 | installed_first.outcome, |
| 1305 | super::mutation::PluginMutationOutcome::Installed |
| 1306 | ); |
| 1307 | let same_agent = agent(&install_config, AgentPosture::FullAccess); |
| 1308 | let mut reg = discover_with_config(&install_config); |
| 1309 | approve(&mut reg, "beta", same_agent.clone()); |
| 1310 | |
| 1311 | let pre_removal = discover_with_config(&install_config); |
| 1312 | let first_nonce = pre_removal.get("beta").unwrap().install_nonce.clone(); |
| 1313 | assert!(first_nonce.is_some()); |
| 1314 | let first_staged = pre_removal |
| 1315 | .get("beta") |
| 1316 | .unwrap() |
| 1317 | .staged_root |
| 1318 | .clone() |
| 1319 | .unwrap(); |
| 1320 | assert!(first_staged.is_dir()); |
| 1321 | |
| 1322 | let mut deleter = discover_with_config(&install_config); |
| 1323 | deleter.disable("beta").unwrap(); |
| 1324 | let mut deleter = discover_with_config(&install_config); |
| 1325 | runtime |
| 1326 | .block_on(super::mutation::execute( |
| 1327 | super::mutation::PluginMutationRequest::Uninstall { |
| 1328 | selector: "beta".to_string(), |
| 1329 | }, |
| 1330 | &super::mutation::PluginMutationContext { |
| 1331 | network: &network, |
| 1332 | max_size: super::install::DEFAULT_MAX_SIZE_BYTES, |
| 1333 | }, |
| 1334 | &mut deleter, |
| 1335 | )) |
| 1336 | .unwrap(); |
| 1337 | assert!(first_staged.is_dir()); |
| 1338 | |
| 1339 | let mut reinstaller = discover_with_config(&install_config); |
| 1340 | let installed_second = runtime |
| 1341 | .block_on(super::mutation::execute( |
| 1342 | super::mutation::PluginMutationRequest::Install { |
| 1343 | source: super::install::PluginInstallSource::parse(source.to_str().unwrap()) |
| 1344 | .unwrap(), |
| 1345 | }, |
| 1346 | &super::mutation::PluginMutationContext { |
| 1347 | network: &network, |
| 1348 | max_size: super::install::DEFAULT_MAX_SIZE_BYTES, |
| 1349 | }, |
| 1350 | &mut reinstaller, |
| 1351 | )) |
| 1352 | .unwrap(); |
| 1353 | assert_eq!( |
| 1354 | installed_second.outcome, |
| 1355 | super::mutation::PluginMutationOutcome::Installed |
| 1356 | ); |
| 1357 | let mut after_reinstall = discover_with_config(&install_config); |
| 1358 | let second_nonce = after_reinstall.get("beta").unwrap().install_nonce.clone(); |
| 1359 | assert_ne!(second_nonce, first_nonce); |
| 1360 | assert_ne!( |
| 1361 | after_reinstall.get("beta").unwrap().content_hash, |
| 1362 | pre_removal.get("beta").unwrap().content_hash |
| 1363 | ); |
| 1364 | approve(&mut after_reinstall, "beta", same_agent.clone()); |
| 1365 | assert!(first_staged.is_dir()); |
| 1366 | |
| 1367 | let before_reinstall_state = fs::read(&install_config.state_path).unwrap(); |
| 1368 | let mut stale_review = pre_removal.clone(); |
| 1369 | let shown = stale_review |
| 1370 | .host_service_review_for("beta", &same_agent) |
| 1371 | .map(|review| review.token); |
| 1372 | let review_err = stale_review |
| 1373 | .trust_reviewed( |
| 1374 | "beta", |
| 1375 | ReviewApproval { |
| 1376 | approved_by: same_agent.clone(), |
| 1377 | host_services: shown, |
| 1378 | }, |
| 1379 | ) |
| 1380 | .unwrap_err(); |
| 1381 | assert!( |
| 1382 | review_err.contains("state or review changed"), |
| 1383 | "{review_err}" |
| 1384 | ); |
| 1385 | assert_eq!( |
| 1386 | fs::read(&install_config.state_path).unwrap(), |
| 1387 | before_reinstall_state |
| 1388 | ); |
| 1389 | |
| 1390 | let mut stale_enable = pre_removal; |
| 1391 | let enable_err = stale_enable.enable_by("beta", same_agent).unwrap_err(); |
| 1392 | assert!( |
| 1393 | enable_err.contains("state or review changed"), |
| 1394 | "{enable_err}" |
| 1395 | ); |
| 1396 | assert_eq!( |
| 1397 | fs::read(&install_config.state_path).unwrap(), |
| 1398 | before_reinstall_state |
| 1399 | ); |
| 1400 | } |
| 1401 |