| 1 | //! Integration tests for the community-skill CLI flows (#140). |
| 2 | //! |
| 3 | //! Keep this file name free of `install`: Windows can treat unmanifested test |
| 4 | //! binaries with installer-like names as elevation-required programs. |
| 5 | //! |
| 6 | //! These tests exercise the full validation pipeline against a tiny in-process |
| 7 | //! HTTP server, so the network gate, download cap, tarball validation, atomic |
| 8 | //! rename, and `.installed-from` marker all run end-to-end through the public |
| 9 | //! `codewhale-skill-install` surface (its `test-support` feature is a tui |
| 10 | //! dev-dependency only). |
| 11 | |
| 12 | use std::io::Write; |
| 13 | use std::path::Path; |
| 14 | |
| 15 | use flate2::Compression; |
| 16 | use flate2::write::GzEncoder; |
| 17 | use tempfile::TempDir; |
| 18 | use tiny_http::{Method, Response, Server}; |
| 19 | |
| 20 | use codewhale_net::network_policy::{DecisionToml, NetworkPolicy}; |
| 21 | use codewhale_skill_install::install::{InstallOutcome, InstallSource, UpdateResult}; |
| 22 | |
| 23 | /// Construct a gzipped tarball from `(path, body)` pairs. Permissions are set |
| 24 | /// to 0o644 so umask differences across platforms don't perturb the bytes. |
| 25 | fn make_tarball(entries: &[(&str, &[u8])]) -> Vec<u8> { |
| 26 | let mut gz = GzEncoder::new(Vec::new(), Compression::default()); |
| 27 | { |
| 28 | let mut builder = tar::Builder::new(&mut gz); |
| 29 | for (path, body) in entries { |
| 30 | let mut header = tar::Header::new_gnu(); |
| 31 | header.set_size(body.len() as u64); |
| 32 | header.set_mode(0o644); |
| 33 | header.set_cksum(); |
| 34 | builder |
| 35 | .append_data(&mut header, path, *body) |
| 36 | .expect("append_data"); |
| 37 | } |
| 38 | builder.finish().expect("finish tar"); |
| 39 | } |
| 40 | gz.finish().expect("finish gz") |
| 41 | } |
| 42 | |
| 43 | fn skill_md(name: &str, description: &str) -> Vec<u8> { |
| 44 | format!( |
| 45 | "---\nname: {name}\ndescription: {description}\n---\n# {name}\n\nThis is a test skill.\n" |
| 46 | ) |
| 47 | .into_bytes() |
| 48 | } |
| 49 | |
| 50 | fn allow_all_policy() -> NetworkPolicy { |
| 51 | NetworkPolicy { |
| 52 | default: DecisionToml::Allow, |
| 53 | allow: Vec::new(), |
| 54 | deny: Vec::new(), |
| 55 | proxy: Vec::new(), |
| 56 | proxy_fake_ip_cidrs: Vec::new(), |
| 57 | audit: false, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | fn deny_all_policy() -> NetworkPolicy { |
| 62 | NetworkPolicy { |
| 63 | default: DecisionToml::Deny, |
| 64 | allow: Vec::new(), |
| 65 | deny: Vec::new(), |
| 66 | proxy: Vec::new(), |
| 67 | proxy_fake_ip_cidrs: Vec::new(), |
| 68 | audit: false, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn prompt_all_policy() -> NetworkPolicy { |
| 73 | NetworkPolicy { |
| 74 | default: DecisionToml::Prompt, |
| 75 | allow: Vec::new(), |
| 76 | deny: Vec::new(), |
| 77 | proxy: Vec::new(), |
| 78 | proxy_fake_ip_cidrs: Vec::new(), |
| 79 | audit: false, |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Spawn a tiny HTTP server that serves `bytes` at any path with 200 OK and |
| 84 | /// returns the bound URL. The server replies to *every* request (we re-use it |
| 85 | /// across multiple installs in the same test). |
| 86 | fn spawn_tarball_server( |
| 87 | bytes: Vec<u8>, |
| 88 | ) -> ( |
| 89 | String, |
| 90 | std::sync::mpsc::Sender<()>, |
| 91 | std::thread::JoinHandle<()>, |
| 92 | ) { |
| 93 | let server = Server::http("127.0.0.1:0").expect("bind ephemeral port"); |
| 94 | let url = format!( |
| 95 | "http://{}/skill.tar.gz", |
| 96 | server.server_addr().to_ip().expect("ip addr") |
| 97 | ); |
| 98 | let (shutdown_tx, shutdown_rx) = std::sync::mpsc::channel::<()>(); |
| 99 | let handle = std::thread::spawn(move || { |
| 100 | loop { |
| 101 | // Poll-style with a small recv timeout so we can break out cleanly. |
| 102 | match server.recv_timeout(std::time::Duration::from_millis(100)) { |
| 103 | Ok(Some(req)) => { |
| 104 | if req.method() != &Method::Get { |
| 105 | continue; |
| 106 | } |
| 107 | let response = Response::from_data(bytes.clone()); |
| 108 | let _ = req.respond(response); |
| 109 | } |
| 110 | Ok(None) => {} |
| 111 | Err(_) => break, |
| 112 | } |
| 113 | if shutdown_rx.try_recv().is_ok() { |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | }); |
| 118 | (url, shutdown_tx, handle) |
| 119 | } |
| 120 | |
| 121 | fn shutdown(tx: std::sync::mpsc::Sender<()>, handle: std::thread::JoinHandle<()>) { |
| 122 | let _ = tx.send(()); |
| 123 | let _ = handle.join(); |
| 124 | } |
| 125 | |
| 126 | #[tokio::test] |
| 127 | async fn install_happy_path_writes_skill_and_marker() { |
| 128 | let tarball = make_tarball(&[ |
| 129 | ( |
| 130 | "test-skill-main/SKILL.md", |
| 131 | &skill_md("test-skill", "Test skill"), |
| 132 | ), |
| 133 | ("test-skill-main/notes.txt", b"hello world"), |
| 134 | ]); |
| 135 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 136 | |
| 137 | let tmp = TempDir::new().unwrap(); |
| 138 | let policy = allow_all_policy(); |
| 139 | |
| 140 | let outcome = codewhale_skill_install::install::install( |
| 141 | InstallSource::DirectUrl(url), |
| 142 | tmp.path(), |
| 143 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 144 | &policy, |
| 145 | false, |
| 146 | ) |
| 147 | .await |
| 148 | .expect("install ok"); |
| 149 | |
| 150 | let installed = match outcome { |
| 151 | InstallOutcome::Installed(s) => s, |
| 152 | other => panic!("expected Installed, got {other:?}"), |
| 153 | }; |
| 154 | assert_eq!(installed.name, "test-skill"); |
| 155 | |
| 156 | let installed_dir = tmp.path().join("test-skill"); |
| 157 | assert!(installed_dir.is_dir(), "skill dir created"); |
| 158 | assert!(installed_dir.join("SKILL.md").is_file(), "SKILL.md present"); |
| 159 | assert!( |
| 160 | installed_dir.join("notes.txt").is_file(), |
| 161 | "extra file present" |
| 162 | ); |
| 163 | assert!( |
| 164 | installed_dir |
| 165 | .join(codewhale_skill_install::install::INSTALLED_FROM_MARKER) |
| 166 | .is_file(), |
| 167 | ".installed-from marker present" |
| 168 | ); |
| 169 | let marker = std::fs::read_to_string( |
| 170 | installed_dir.join(codewhale_skill_install::install::INSTALLED_FROM_MARKER), |
| 171 | ) |
| 172 | .expect("read marker"); |
| 173 | assert!( |
| 174 | marker.contains("\"schema_version\": 2") || marker.contains("\"schema_version\":2"), |
| 175 | "install must write metadata v2: {marker}" |
| 176 | ); |
| 177 | assert!( |
| 178 | marker.contains("content_digest"), |
| 179 | "install must bind content_digest: {marker}" |
| 180 | ); |
| 181 | |
| 182 | shutdown(tx, handle); |
| 183 | } |
| 184 | |
| 185 | #[tokio::test] |
| 186 | async fn install_rejects_path_traversal() { |
| 187 | // `tar::Builder::append_data` rejects `..` itself, so we craft the bad |
| 188 | // entry by writing the raw header bytes via `append`. |
| 189 | let mut gz = GzEncoder::new(Vec::new(), Compression::default()); |
| 190 | { |
| 191 | let mut builder = tar::Builder::new(&mut gz); |
| 192 | let body = skill_md("test-skill", "T"); |
| 193 | let mut hdr = tar::Header::new_gnu(); |
| 194 | hdr.set_size(body.len() as u64); |
| 195 | hdr.set_mode(0o644); |
| 196 | hdr.set_cksum(); |
| 197 | builder |
| 198 | .append_data(&mut hdr, "test-skill-main/SKILL.md", body.as_slice()) |
| 199 | .unwrap(); |
| 200 | |
| 201 | // Path-traversal entry. The `tar` crate's `set_path` rejects `..` |
| 202 | // itself, so we patch the raw 100-byte name field in the header. |
| 203 | let evil_body: &[u8] = b"not gonna happen"; |
| 204 | let mut evil_hdr = tar::Header::new_gnu(); |
| 205 | evil_hdr.set_size(evil_body.len() as u64); |
| 206 | evil_hdr.set_mode(0o644); |
| 207 | // Write a name with a `..` directly into the legacy "name" field. |
| 208 | let bytes = evil_hdr.as_old_mut(); |
| 209 | let evil_name = b"../etc/passwd"; |
| 210 | bytes.name[..evil_name.len()].copy_from_slice(evil_name); |
| 211 | evil_hdr.set_cksum(); |
| 212 | builder.append(&evil_hdr, evil_body).unwrap(); |
| 213 | builder.finish().unwrap(); |
| 214 | } |
| 215 | let tarball = gz.finish().unwrap(); |
| 216 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 217 | |
| 218 | let tmp = TempDir::new().unwrap(); |
| 219 | let policy = allow_all_policy(); |
| 220 | let err = codewhale_skill_install::install::install( |
| 221 | InstallSource::DirectUrl(url), |
| 222 | tmp.path(), |
| 223 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 224 | &policy, |
| 225 | false, |
| 226 | ) |
| 227 | .await |
| 228 | .expect_err("path traversal must be rejected"); |
| 229 | let msg = format!("{err:#}"); |
| 230 | assert!( |
| 231 | msg.contains("escapes destination"), |
| 232 | "expected path-traversal error, got: {msg}" |
| 233 | ); |
| 234 | |
| 235 | shutdown(tx, handle); |
| 236 | } |
| 237 | |
| 238 | #[tokio::test] |
| 239 | async fn install_rejects_oversized_tarball() { |
| 240 | let big = vec![b'a'; 256 * 1024]; // 256 KiB per file |
| 241 | let mut entries: Vec<(String, Vec<u8>)> = Vec::new(); |
| 242 | entries.push(( |
| 243 | "test-skill-main/SKILL.md".to_string(), |
| 244 | skill_md("test-skill", "T"), |
| 245 | )); |
| 246 | for i in 0..50 { |
| 247 | entries.push((format!("test-skill-main/big-{i}.bin"), big.clone())); |
| 248 | } |
| 249 | let entry_refs: Vec<(&str, &[u8])> = entries |
| 250 | .iter() |
| 251 | .map(|(p, b)| (p.as_str(), b.as_slice())) |
| 252 | .collect(); |
| 253 | let tarball = make_tarball(&entry_refs); |
| 254 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 255 | |
| 256 | let tmp = TempDir::new().unwrap(); |
| 257 | let policy = allow_all_policy(); |
| 258 | let small_cap = 1024 * 1024; |
| 259 | let err = codewhale_skill_install::install::install( |
| 260 | InstallSource::DirectUrl(url), |
| 261 | tmp.path(), |
| 262 | small_cap, |
| 263 | &policy, |
| 264 | false, |
| 265 | ) |
| 266 | .await |
| 267 | .expect_err("oversized must be rejected"); |
| 268 | let msg = format!("{err:#}"); |
| 269 | assert!( |
| 270 | msg.contains("too large") || msg.contains("exceed"), |
| 271 | "expected size cap error, got: {msg}" |
| 272 | ); |
| 273 | |
| 274 | shutdown(tx, handle); |
| 275 | } |
| 276 | |
| 277 | #[tokio::test] |
| 278 | async fn install_rejects_missing_skill_md() { |
| 279 | let tarball = make_tarball(&[("repo-main/README.md", b"not a skill")]); |
| 280 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 281 | |
| 282 | let tmp = TempDir::new().unwrap(); |
| 283 | let policy = allow_all_policy(); |
| 284 | let err = codewhale_skill_install::install::install( |
| 285 | InstallSource::DirectUrl(url), |
| 286 | tmp.path(), |
| 287 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 288 | &policy, |
| 289 | false, |
| 290 | ) |
| 291 | .await |
| 292 | .expect_err("missing SKILL.md must be rejected"); |
| 293 | assert!(format!("{err:#}").contains("missing SKILL.md"), "{err:#}"); |
| 294 | |
| 295 | shutdown(tx, handle); |
| 296 | } |
| 297 | |
| 298 | #[tokio::test] |
| 299 | async fn install_accepts_claude_compatible_skill_directory_archive() { |
| 300 | let tarball = make_tarball(&[ |
| 301 | ( |
| 302 | "repo-main/.claude/skills/workflow-pack/SKILL.md", |
| 303 | &skill_md("workflow-pack", "Workflow pack"), |
| 304 | ), |
| 305 | ( |
| 306 | "repo-main/.claude/skills/workflow-pack/scripts/check.sh", |
| 307 | b"echo ok", |
| 308 | ), |
| 309 | ("repo-main/README.md", b"outside the selected skill subtree"), |
| 310 | ]); |
| 311 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 312 | |
| 313 | let tmp = TempDir::new().unwrap(); |
| 314 | let policy = allow_all_policy(); |
| 315 | let outcome = codewhale_skill_install::install::install( |
| 316 | InstallSource::DirectUrl(url), |
| 317 | tmp.path(), |
| 318 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 319 | &policy, |
| 320 | false, |
| 321 | ) |
| 322 | .await |
| 323 | .expect("claude-compatible skill dir should install"); |
| 324 | let installed = match outcome { |
| 325 | InstallOutcome::Installed(installed) => installed, |
| 326 | other => panic!("expected Installed, got {other:?}"), |
| 327 | }; |
| 328 | |
| 329 | assert_eq!(installed.name, "workflow-pack"); |
| 330 | assert!(installed.path.join("SKILL.md").is_file()); |
| 331 | assert!(installed.path.join("scripts/check.sh").is_file()); |
| 332 | assert!(!installed.path.join("README.md").exists()); |
| 333 | |
| 334 | shutdown(tx, handle); |
| 335 | } |
| 336 | |
| 337 | #[tokio::test] |
| 338 | async fn install_accepts_nested_workflow_pack_skill_directory() { |
| 339 | let tarball = make_tarball(&[ |
| 340 | ( |
| 341 | "repo-main/packages/superpowers/5.1.0/skills/using-superpowers/SKILL.md", |
| 342 | &skill_md("using-superpowers", "Use Superpowers workflow"), |
| 343 | ), |
| 344 | ( |
| 345 | "repo-main/packages/superpowers/5.1.0/skills/using-superpowers/references/guide.md", |
| 346 | b"guide", |
| 347 | ), |
| 348 | ]); |
| 349 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 350 | |
| 351 | let tmp = TempDir::new().unwrap(); |
| 352 | let policy = allow_all_policy(); |
| 353 | let outcome = codewhale_skill_install::install::install( |
| 354 | InstallSource::DirectUrl(url), |
| 355 | tmp.path(), |
| 356 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 357 | &policy, |
| 358 | false, |
| 359 | ) |
| 360 | .await |
| 361 | .expect("nested workflow-pack skill dir should install"); |
| 362 | let installed = match outcome { |
| 363 | InstallOutcome::Installed(installed) => installed, |
| 364 | other => panic!("expected Installed, got {other:?}"), |
| 365 | }; |
| 366 | |
| 367 | assert_eq!(installed.name, "using-superpowers"); |
| 368 | assert!(installed.path.join("SKILL.md").is_file()); |
| 369 | assert!(installed.path.join("references/guide.md").is_file()); |
| 370 | |
| 371 | shutdown(tx, handle); |
| 372 | } |
| 373 | |
| 374 | #[tokio::test] |
| 375 | async fn install_rejects_multi_skill_claude_plugin_archive() { |
| 376 | let tarball = make_tarball(&[ |
| 377 | ( |
| 378 | "repo-main/.claude-plugin/plugin.json", |
| 379 | br#"{"name":"workflow-pack","version":"1.0.0"}"#, |
| 380 | ), |
| 381 | ( |
| 382 | "repo-main/skills/plan/SKILL.md", |
| 383 | &skill_md("plan", "Planning skill"), |
| 384 | ), |
| 385 | ( |
| 386 | "repo-main/skills/review/SKILL.md", |
| 387 | &skill_md("review", "Review skill"), |
| 388 | ), |
| 389 | ]); |
| 390 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 391 | |
| 392 | let tmp = TempDir::new().unwrap(); |
| 393 | let policy = allow_all_policy(); |
| 394 | let err = codewhale_skill_install::install::install( |
| 395 | InstallSource::DirectUrl(url), |
| 396 | tmp.path(), |
| 397 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 398 | &policy, |
| 399 | false, |
| 400 | ) |
| 401 | .await |
| 402 | .expect_err("multi-skill Claude plugin archive should not be flattened"); |
| 403 | let msg = format!("{err:#}"); |
| 404 | assert!( |
| 405 | msg.contains("Claude Code plugin archive contains multiple SKILL.md entries"), |
| 406 | "expected Claude plugin compatibility error, got: {msg}" |
| 407 | ); |
| 408 | assert!( |
| 409 | std::fs::read_dir(tmp.path()).unwrap().next().is_none(), |
| 410 | "rejected plugin archive must not write an installed skill" |
| 411 | ); |
| 412 | |
| 413 | shutdown(tx, handle); |
| 414 | } |
| 415 | |
| 416 | #[tokio::test] |
| 417 | async fn install_accepts_single_skill_subdirectory_archive() { |
| 418 | let tarball = make_tarball(&[ |
| 419 | ( |
| 420 | "repo-main/my-workflow/SKILL.md", |
| 421 | &skill_md("my-workflow", "Nested workflow"), |
| 422 | ), |
| 423 | ("repo-main/my-workflow/examples/example.md", b"example"), |
| 424 | ("repo-main/README.md", b"outside the selected skill subtree"), |
| 425 | ]); |
| 426 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 427 | |
| 428 | let tmp = TempDir::new().unwrap(); |
| 429 | let policy = allow_all_policy(); |
| 430 | let outcome = codewhale_skill_install::install::install( |
| 431 | InstallSource::DirectUrl(url), |
| 432 | tmp.path(), |
| 433 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 434 | &policy, |
| 435 | false, |
| 436 | ) |
| 437 | .await |
| 438 | .expect("single nested skill dir should install"); |
| 439 | let installed = match outcome { |
| 440 | InstallOutcome::Installed(installed) => installed, |
| 441 | other => panic!("expected Installed, got {other:?}"), |
| 442 | }; |
| 443 | |
| 444 | assert_eq!(installed.name, "my-workflow"); |
| 445 | assert!(installed.path.join("SKILL.md").is_file()); |
| 446 | assert!(installed.path.join("examples/example.md").is_file()); |
| 447 | assert!(!installed.path.join("README.md").exists()); |
| 448 | |
| 449 | shutdown(tx, handle); |
| 450 | } |
| 451 | |
| 452 | #[tokio::test] |
| 453 | async fn install_rejects_missing_required_frontmatter() { |
| 454 | let tarball = make_tarball(&[("repo-main/SKILL.md", b"---\nname: test\n---\nbody\n")]); |
| 455 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 456 | |
| 457 | let tmp = TempDir::new().unwrap(); |
| 458 | let policy = allow_all_policy(); |
| 459 | let err = codewhale_skill_install::install::install( |
| 460 | InstallSource::DirectUrl(url), |
| 461 | tmp.path(), |
| 462 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 463 | &policy, |
| 464 | false, |
| 465 | ) |
| 466 | .await |
| 467 | .expect_err("missing description must be rejected"); |
| 468 | assert!(format!("{err:#}").contains("description"), "{err:#}"); |
| 469 | |
| 470 | shutdown(tx, handle); |
| 471 | } |
| 472 | |
| 473 | #[tokio::test] |
| 474 | async fn install_idempotent_then_uninstall_then_reinstall() { |
| 475 | let tarball_bytes = |
| 476 | make_tarball(&[("repo-main/SKILL.md", &skill_md("idem-skill", "Idempotent"))]); |
| 477 | let (url, tx, handle) = spawn_tarball_server(tarball_bytes); |
| 478 | |
| 479 | let tmp = TempDir::new().unwrap(); |
| 480 | let policy = allow_all_policy(); |
| 481 | |
| 482 | codewhale_skill_install::install::install( |
| 483 | InstallSource::DirectUrl(url.clone()), |
| 484 | tmp.path(), |
| 485 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 486 | &policy, |
| 487 | false, |
| 488 | ) |
| 489 | .await |
| 490 | .expect("first install ok"); |
| 491 | |
| 492 | // Second install with `update = false` must reject. |
| 493 | let err = codewhale_skill_install::install::install( |
| 494 | InstallSource::DirectUrl(url.clone()), |
| 495 | tmp.path(), |
| 496 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 497 | &policy, |
| 498 | false, |
| 499 | ) |
| 500 | .await |
| 501 | .expect_err("second install must reject"); |
| 502 | let msg = format!("{err:#}"); |
| 503 | assert!( |
| 504 | msg.contains("already installed"), |
| 505 | "expected already-installed error, got: {msg}" |
| 506 | ); |
| 507 | |
| 508 | // Uninstall then reinstall. |
| 509 | codewhale_skill_install::install::uninstall("idem-skill", tmp.path()).expect("uninstall ok"); |
| 510 | assert!(!tmp.path().join("idem-skill").exists()); |
| 511 | |
| 512 | codewhale_skill_install::install::install( |
| 513 | InstallSource::DirectUrl(url), |
| 514 | tmp.path(), |
| 515 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 516 | &policy, |
| 517 | false, |
| 518 | ) |
| 519 | .await |
| 520 | .expect("reinstall ok"); |
| 521 | |
| 522 | assert!(tmp.path().join("idem-skill").join("SKILL.md").is_file()); |
| 523 | shutdown(tx, handle); |
| 524 | } |
| 525 | |
| 526 | #[tokio::test] |
| 527 | async fn update_no_change_returns_nochange_without_overwriting() { |
| 528 | let tarball_bytes = |
| 529 | make_tarball(&[("repo-main/SKILL.md", &skill_md("upd-skill", "Update test"))]); |
| 530 | let (url, tx, handle) = spawn_tarball_server(tarball_bytes); |
| 531 | let tmp = TempDir::new().unwrap(); |
| 532 | let policy = allow_all_policy(); |
| 533 | |
| 534 | codewhale_skill_install::install::install( |
| 535 | InstallSource::DirectUrl(url.clone()), |
| 536 | tmp.path(), |
| 537 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 538 | &policy, |
| 539 | false, |
| 540 | ) |
| 541 | .await |
| 542 | .unwrap(); |
| 543 | |
| 544 | // Patch the marker so update() re-fetches the same URL. |
| 545 | let marker_path = tmp |
| 546 | .path() |
| 547 | .join("upd-skill") |
| 548 | .join(codewhale_skill_install::install::INSTALLED_FROM_MARKER); |
| 549 | let marker_body = std::fs::read_to_string(&marker_path).unwrap(); |
| 550 | let mut marker_json: serde_json::Value = serde_json::from_str(&marker_body).unwrap(); |
| 551 | marker_json["spec"] = serde_json::Value::String(url); |
| 552 | std::fs::write(&marker_path, marker_json.to_string()).unwrap(); |
| 553 | |
| 554 | // Capture mtime so we can confirm SKILL.md wasn't rewritten. |
| 555 | let skill_md_path = tmp.path().join("upd-skill").join("SKILL.md"); |
| 556 | let mtime_before = std::fs::metadata(&skill_md_path) |
| 557 | .unwrap() |
| 558 | .modified() |
| 559 | .unwrap(); |
| 560 | |
| 561 | let result = codewhale_skill_install::install::update( |
| 562 | "upd-skill", |
| 563 | tmp.path(), |
| 564 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 565 | &policy, |
| 566 | ) |
| 567 | .await |
| 568 | .expect("update ok"); |
| 569 | assert!(matches!(result, UpdateResult::NoChange)); |
| 570 | |
| 571 | let mtime_after = std::fs::metadata(&skill_md_path) |
| 572 | .unwrap() |
| 573 | .modified() |
| 574 | .unwrap(); |
| 575 | assert_eq!(mtime_before, mtime_after, "SKILL.md must not be rewritten"); |
| 576 | shutdown(tx, handle); |
| 577 | } |
| 578 | |
| 579 | #[tokio::test] |
| 580 | async fn install_with_deny_policy_returns_network_denied() { |
| 581 | let tmp = TempDir::new().unwrap(); |
| 582 | let policy = deny_all_policy(); |
| 583 | let outcome = codewhale_skill_install::install::install( |
| 584 | InstallSource::DirectUrl("https://example.invalid/skill.tar.gz".to_string()), |
| 585 | tmp.path(), |
| 586 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 587 | &policy, |
| 588 | false, |
| 589 | ) |
| 590 | .await |
| 591 | .expect("policy outcome should be Ok"); |
| 592 | match outcome { |
| 593 | InstallOutcome::NetworkDenied(host) => { |
| 594 | assert!(host.contains("example.invalid"), "got host {host}"); |
| 595 | } |
| 596 | other => panic!("expected NetworkDenied, got {other:?}"), |
| 597 | } |
| 598 | |
| 599 | // Verify the temp dir is untouched. |
| 600 | assert!( |
| 601 | std::fs::read_dir(tmp.path()).unwrap().next().is_none(), |
| 602 | "temp dir must be untouched" |
| 603 | ); |
| 604 | } |
| 605 | |
| 606 | #[tokio::test] |
| 607 | async fn install_with_prompt_policy_returns_needs_approval() { |
| 608 | let tmp = TempDir::new().unwrap(); |
| 609 | let policy = prompt_all_policy(); |
| 610 | let outcome = codewhale_skill_install::install::install( |
| 611 | InstallSource::DirectUrl("https://example.invalid/skill.tar.gz".to_string()), |
| 612 | tmp.path(), |
| 613 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 614 | &policy, |
| 615 | false, |
| 616 | ) |
| 617 | .await |
| 618 | .expect("policy outcome should be Ok"); |
| 619 | match outcome { |
| 620 | InstallOutcome::NeedsApproval(host) => { |
| 621 | assert!(host.contains("example.invalid"), "got host {host}"); |
| 622 | } |
| 623 | other => panic!("expected NeedsApproval, got {other:?}"), |
| 624 | } |
| 625 | assert!( |
| 626 | std::fs::read_dir(tmp.path()).unwrap().next().is_none(), |
| 627 | "temp dir must be untouched on prompt" |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | #[tokio::test] |
| 632 | async fn install_rejects_symlink_entry() { |
| 633 | let mut gz = GzEncoder::new(Vec::new(), Compression::default()); |
| 634 | { |
| 635 | let mut builder = tar::Builder::new(&mut gz); |
| 636 | |
| 637 | let body = skill_md("link-skill", "x"); |
| 638 | let mut hdr = tar::Header::new_gnu(); |
| 639 | hdr.set_size(body.len() as u64); |
| 640 | hdr.set_mode(0o644); |
| 641 | hdr.set_cksum(); |
| 642 | builder |
| 643 | .append_data(&mut hdr, "repo-main/SKILL.md", body.as_slice()) |
| 644 | .unwrap(); |
| 645 | |
| 646 | let mut link_hdr = tar::Header::new_gnu(); |
| 647 | link_hdr.set_entry_type(tar::EntryType::Symlink); |
| 648 | link_hdr.set_size(0); |
| 649 | link_hdr.set_mode(0o777); |
| 650 | builder |
| 651 | .append_link(&mut link_hdr, "repo-main/escape", Path::new("/etc/passwd")) |
| 652 | .unwrap(); |
| 653 | builder.finish().unwrap(); |
| 654 | } |
| 655 | let tarball = gz.finish().unwrap(); |
| 656 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 657 | |
| 658 | let tmp = TempDir::new().unwrap(); |
| 659 | let policy = allow_all_policy(); |
| 660 | let err = codewhale_skill_install::install::install( |
| 661 | InstallSource::DirectUrl(url), |
| 662 | tmp.path(), |
| 663 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 664 | &policy, |
| 665 | false, |
| 666 | ) |
| 667 | .await |
| 668 | .expect_err("symlinks must be rejected"); |
| 669 | assert!(format!("{err:#}").contains("symlink"), "{err:#}"); |
| 670 | |
| 671 | shutdown(tx, handle); |
| 672 | } |
| 673 | |
| 674 | #[tokio::test] |
| 675 | async fn install_ignores_symlink_outside_selected_skill_root() { |
| 676 | let mut gz = GzEncoder::new(Vec::new(), Compression::default()); |
| 677 | { |
| 678 | let mut builder = tar::Builder::new(&mut gz); |
| 679 | |
| 680 | let mut link_hdr = tar::Header::new_gnu(); |
| 681 | link_hdr.set_entry_type(tar::EntryType::Symlink); |
| 682 | link_hdr.set_size(0); |
| 683 | link_hdr.set_mode(0o777); |
| 684 | builder |
| 685 | .append_link(&mut link_hdr, "repo-main/AGENTS.md", Path::new("CLAUDE.md")) |
| 686 | .unwrap(); |
| 687 | |
| 688 | let body = skill_md("nested-skill", "Nested skill"); |
| 689 | let mut hdr = tar::Header::new_gnu(); |
| 690 | hdr.set_size(body.len() as u64); |
| 691 | hdr.set_mode(0o644); |
| 692 | hdr.set_cksum(); |
| 693 | builder |
| 694 | .append_data( |
| 695 | &mut hdr, |
| 696 | "repo-main/skills/nested-skill/SKILL.md", |
| 697 | body.as_slice(), |
| 698 | ) |
| 699 | .unwrap(); |
| 700 | |
| 701 | let notes = b"selected subtree only"; |
| 702 | let mut notes_hdr = tar::Header::new_gnu(); |
| 703 | notes_hdr.set_size(notes.len() as u64); |
| 704 | notes_hdr.set_mode(0o644); |
| 705 | notes_hdr.set_cksum(); |
| 706 | builder |
| 707 | .append_data( |
| 708 | &mut notes_hdr, |
| 709 | "repo-main/skills/nested-skill/notes.txt", |
| 710 | notes.as_slice(), |
| 711 | ) |
| 712 | .unwrap(); |
| 713 | |
| 714 | builder.finish().unwrap(); |
| 715 | } |
| 716 | let tarball = gz.finish().unwrap(); |
| 717 | let (url, tx, handle) = spawn_tarball_server(tarball); |
| 718 | |
| 719 | let tmp = TempDir::new().unwrap(); |
| 720 | let policy = allow_all_policy(); |
| 721 | let outcome = codewhale_skill_install::install::install( |
| 722 | InstallSource::DirectUrl(url), |
| 723 | tmp.path(), |
| 724 | codewhale_skill_install::install::DEFAULT_MAX_SIZE_BYTES, |
| 725 | &policy, |
| 726 | false, |
| 727 | ) |
| 728 | .await |
| 729 | .expect("repo-level symlink outside selected skill root should be ignored"); |
| 730 | let installed = match outcome { |
| 731 | InstallOutcome::Installed(installed) => installed, |
| 732 | other => panic!("expected Installed, got {other:?}"), |
| 733 | }; |
| 734 | |
| 735 | assert_eq!(installed.name, "nested-skill"); |
| 736 | assert!(installed.path.join("SKILL.md").exists()); |
| 737 | assert!(installed.path.join("notes.txt").exists()); |
| 738 | assert!(!installed.path.join("AGENTS.md").exists()); |
| 739 | |
| 740 | shutdown(tx, handle); |
| 741 | } |
| 742 | |
| 743 | #[test] |
| 744 | fn uninstall_refuses_system_skill() { |
| 745 | let tmp = TempDir::new().unwrap(); |
| 746 | let dir = tmp.path().join("system-skill"); |
| 747 | std::fs::create_dir_all(&dir).unwrap(); |
| 748 | let mut f = std::fs::File::create(dir.join("SKILL.md")).unwrap(); |
| 749 | f.write_all(b"---\nname: system-skill\ndescription: x\n---\n") |
| 750 | .unwrap(); |
| 751 | // No `.installed-from` marker — looks like a system skill. |
| 752 | |
| 753 | let err = codewhale_skill_install::install::uninstall("system-skill", tmp.path()) |
| 754 | .expect_err("must refuse"); |
| 755 | assert!(format!("{err:#}").contains("not installed via")); |
| 756 | assert!(dir.exists(), "directory must be left alone"); |
| 757 | } |
| 758 |