spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_adversarial_containment.rs
| 1 | use p256::ecdsa::{SigningKey, VerifyingKey}; |
| 2 | use spark_rsi::evaluator::layers::{ |
| 3 | CorrectnessLayer, LongitudinalReplayLayer, PerformanceLayer, ResourceEfficiencyLayer, |
| 4 | SecurityLayer, StyleLayer, |
| 5 | }; |
| 6 | use spark_rsi::evaluator::ObjectiveEvaluator; |
| 7 | use spark_rsi::isolation::{ArtifactManifest, BuildJail, GpuEvaluationJail, RootOfTrust}; |
| 8 | use std::fs; |
| 9 | |
| 10 | #[test] |
| 11 | fn test_adversarial_holdout_secrecy_enforced() { |
| 12 | let tmp = tempfile::tempdir().unwrap(); |
| 13 | let art = tmp.path().join("artifacts"); |
| 14 | fs::create_dir_all(&art).unwrap(); |
| 15 | |
| 16 | let jail = GpuEvaluationJail::new("spark-rsi-eval@sha256:123456", &art); |
| 17 | let args = jail.build_docker_args(&["./bench_workload"]); |
| 18 | |
| 19 | // Candidate container arguments must NEVER mount holdout paths |
| 20 | for arg in &args { |
| 21 | assert!( |
| 22 | !arg.contains("holdout"), |
| 23 | "CRITICAL SECURITY VIOLATION: Candidate jail exposed holdouts: {}", |
| 24 | arg |
| 25 | ); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn test_adversarial_network_isolation_enforced() { |
| 31 | let tmp = tempfile::tempdir().unwrap(); |
| 32 | let src = tmp.path().join("src"); |
| 33 | let out = tmp.path().join("out"); |
| 34 | fs::create_dir_all(&src).unwrap(); |
| 35 | fs::create_dir_all(&out).unwrap(); |
| 36 | |
| 37 | let build_jail = BuildJail::new("builder-image", &src, &out); |
| 38 | let build_args = build_jail.build_docker_args(&["cargo", "build"]); |
| 39 | assert!(build_args.contains(&"--network".to_string())); |
| 40 | assert!(build_args.contains(&"none".to_string())); |
| 41 | |
| 42 | let gpu_jail = GpuEvaluationJail::new("eval-image", &out); |
| 43 | let gpu_args = gpu_jail.build_docker_args(&["./bench"]); |
| 44 | assert!(gpu_args.contains(&"--network".to_string())); |
| 45 | assert!(gpu_args.contains(&"none".to_string())); |
| 46 | |
| 47 | // Verify SecurityLayer catches network primitives |
| 48 | let files = vec!["src/observe.rs".to_string()]; |
| 49 | let rogue_diff = "+ let stream = std::net::TcpStream::connect(\"198.51.100.1:443\");"; |
| 50 | let eval = SecurityLayer::evaluate_candidate(&files, rogue_diff, 0); |
| 51 | |
| 52 | assert!(!eval.passed); |
| 53 | assert!(!eval.network_isolation_compliant); |
| 54 | assert!(eval |
| 55 | .violations |
| 56 | .iter() |
| 57 | .any(|v| v.contains("Unauthorized network primitive"))); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn test_adversarial_root_of_trust_tampering_rejected() { |
| 62 | // Attempting to modify protected files at tier 0 or tier 1 must fail |
| 63 | assert!(RootOfTrust::assert_patch_permitted("Cargo.toml", 0).is_err()); |
| 64 | assert!(RootOfTrust::assert_patch_permitted("src/isolation/container.rs", 1).is_err()); |
| 65 | assert!(RootOfTrust::assert_patch_permitted("src/evaluator/mod.rs", 0).is_err()); |
| 66 | assert!(RootOfTrust::assert_patch_permitted(".rsi/ledger.db", 1).is_err()); |
| 67 | assert!(RootOfTrust::assert_patch_permitted("CONSTITUTION.md", 0).is_err()); |
| 68 | |
| 69 | let files = vec!["Cargo.toml".to_string(), "src/observe.rs".to_string()]; |
| 70 | let diff = "+ p256 = \"0.13\""; |
| 71 | let eval = SecurityLayer::evaluate_candidate(&files, diff, 0); |
| 72 | |
| 73 | assert!(!eval.passed); |
| 74 | assert!(!eval.root_of_trust_compliant); |
| 75 | assert!(eval |
| 76 | .violations |
| 77 | .iter() |
| 78 | .any(|v| v.contains("protected root-of-trust file"))); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn test_adversarial_manifest_rejects_unmanifested_extra_files() { |
| 83 | let tmp = tempfile::tempdir().unwrap(); |
| 84 | let out_dir = tmp.path().join("output"); |
| 85 | fs::create_dir_all(&out_dir).unwrap(); |
| 86 | |
| 87 | let bin_path = out_dir.join("legitimate_service"); |
| 88 | fs::write(&bin_path, "legitimate compiled binary payload").unwrap(); |
| 89 | |
| 90 | let manifest = ArtifactManifest::generate_from_output_dir( |
| 91 | "cand-sec-01", |
| 92 | "builder@sha256:abc", |
| 93 | "src@sha256:def", |
| 94 | &out_dir, |
| 95 | ) |
| 96 | .expect("Manifest generation failed"); |
| 97 | |
| 98 | // Clean verification must pass |
| 99 | assert!(manifest.verify_integrity(&out_dir).unwrap()); |
| 100 | |
| 101 | // Adversarial injection of rogue unmanifested artifact |
| 102 | let rogue_script = out_dir.join("exfiltrate.sh"); |
| 103 | fs::write(&rogue_script, "#!/bin/sh\necho compromised").unwrap(); |
| 104 | |
| 105 | // Verification must detect the extra file and reject |
| 106 | let is_valid = manifest.verify_integrity(&out_dir).unwrap(); |
| 107 | assert!( |
| 108 | !is_valid, |
| 109 | "ArtifactManifest failed to reject unmanifested extra file on disk" |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn test_adversarial_forged_receipt_signature_rejected() { |
| 115 | let correctness = CorrectnessLayer::evaluate_synthetic(true, 10, 0, 2, 0, true); |
| 116 | let security = SecurityLayer::evaluate_candidate(&["src/observe.rs".to_string()], "+ ok", 0); |
| 117 | let style = StyleLayer::evaluate_text("Clean text without violations."); |
| 118 | let perf = PerformanceLayer::evaluate_latencies( |
| 119 | &(0..30).map(|i| 100.0 + i as f64 * 0.5).collect::<Vec<_>>(), |
| 120 | &(0..30).map(|i| 80.0 + i as f64 * 0.4).collect::<Vec<_>>(), |
| 121 | true, |
| 122 | 2000, |
| 123 | Some(42), |
| 124 | ) |
| 125 | .unwrap(); |
| 126 | let res_eff = ResourceEfficiencyLayer::evaluate( |
| 127 | &spark_rsi::evaluator::metrics::RusageMetrics { |
| 128 | user_time_us: 10_000, |
| 129 | system_time_us: 5000, |
| 130 | max_rss_kb: 50_000, |
| 131 | voluntary_context_switches: 100, |
| 132 | involuntary_context_switches: 20, |
| 133 | }, |
| 134 | &spark_rsi::evaluator::metrics::RusageMetrics { |
| 135 | user_time_us: 8000, |
| 136 | system_time_us: 4000, |
| 137 | max_rss_kb: 50_200, |
| 138 | voluntary_context_switches: 80, |
| 139 | involuntary_context_switches: 15, |
| 140 | }, |
| 141 | None, |
| 142 | Some(49_152), |
| 143 | ); |
| 144 | let long_rep = LongitudinalReplayLayer::evaluate_results( |
| 145 | &LongitudinalReplayLayer::builtin_regression_corpus(), |
| 146 | ); |
| 147 | |
| 148 | let mut receipt = ObjectiveEvaluator::evaluate_candidate( |
| 149 | "cycle-sec-99", |
| 150 | "cand-sec-99", |
| 151 | "parent-sec-00", |
| 152 | correctness, |
| 153 | security, |
| 154 | style, |
| 155 | perf, |
| 156 | res_eff, |
| 157 | long_rep, |
| 158 | ); |
| 159 | |
| 160 | let signing_key_authorized = SigningKey::from_bytes(&[101u8; 32].into()).unwrap(); |
| 161 | let verifying_key_authorized = VerifyingKey::from(&signing_key_authorized); |
| 162 | |
| 163 | let signing_key_attacker = SigningKey::from_bytes(&[202u8; 32].into()).unwrap(); |
| 164 | let verifying_key_attacker = VerifyingKey::from(&signing_key_attacker); |
| 165 | |
| 166 | // Legitimate signature verification passes |
| 167 | receipt.sign(&signing_key_authorized); |
| 168 | assert!(receipt.verify_signature(&verifying_key_authorized)); |
| 169 | |
| 170 | // Attack 1: Verify against attacker public key fails |
| 171 | assert!(!receipt.verify_signature(&verifying_key_attacker)); |
| 172 | |
| 173 | // Attack 2: Tamper with candidate ID or fields |
| 174 | let mut tampered_receipt = receipt.clone(); |
| 175 | tampered_receipt.candidate_id = "cand-attacker-substituted".to_string(); |
| 176 | assert!(!tampered_receipt.verify_signature(&verifying_key_authorized)); |
| 177 | |
| 178 | // Attack 3: Forged arbitrary signature string |
| 179 | let mut forged_receipt = receipt.clone(); |
| 180 | forged_receipt.signature = Some("tpm2-p256:00112233445566778899aabbccddeeff".to_string()); |
| 181 | assert!(!forged_receipt.verify_signature(&verifying_key_authorized)); |
| 182 | |
| 183 | // Attack 4: Attacker signs tampered receipt with attacker key, evaluated against authorized key |
| 184 | let mut re_signed_receipt = receipt.clone(); |
| 185 | re_signed_receipt.admitted = true; |
| 186 | re_signed_receipt.sign(&signing_key_attacker); |
| 187 | assert!(!re_signed_receipt.verify_signature(&verifying_key_authorized)); |
| 188 | } |