Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-switchboard/tests/launch.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-switchboard / tests / launch.rs` 338 lines ยท 11317 bytes Raw Blame History 1 //! `switchboard launch`: isolated profile folder, explicit environment 2 //! allowlist, typed errors. Uses a temp HOME and the fake spawner only. 3 4 mod common; 5 6 use aien_switchboard::launch::{ 7 check_profile_dir, launch_env, DirProblem, DirState, PROXY_ENV, STRIPPED_ENV, 8 }; 9 use aien_switchboard::*; 10 use common::*; 11 use std::ffi::OsString; 12 use std::fs; 13 use std::os::unix::fs::PermissionsExt; 14 15 /// Every variable issue #592 names, plus the cloud credential families and 16 /// code-injection variables. 17 const MUST_STRIP: &[&str] = &[ 18 "ANTHROPIC_API_KEY", 19 "ANTHROPIC_AUTH_TOKEN", 20 "CLAUDE_CODE_OAUTH_TOKEN", 21 "ANTHROPIC_PROFILE", 22 "ANTHROPIC_FEDERATION_RULE_ID", 23 "ANTHROPIC_ORGANIZATION_ID", 24 "CLAUDE_CODE_USE_BEDROCK", 25 "CLAUDE_CODE_USE_VERTEX", 26 "CLAUDE_CODE_USE_FOUNDRY", 27 "ANTHROPIC_BASE_URL", 28 "AWS_ACCESS_KEY_ID", 29 "AWS_SECRET_ACCESS_KEY", 30 "AWS_SESSION_TOKEN", 31 "AWS_PROFILE", 32 "AWS_BEARER_TOKEN_BEDROCK", 33 "GOOGLE_APPLICATION_CREDENTIALS", 34 "GOOGLE_CLOUD_PROJECT", 35 "ANTHROPIC_VERTEX_PROJECT_ID", 36 "ANTHROPIC_VERTEX_BASE_URL", 37 "ANTHROPIC_BEDROCK_BASE_URL", 38 "CLOUD_ML_REGION", 39 "GH_TOKEN", 40 "OPENAI_API_KEY", 41 // Code-injection and config-relocation variables (review of #619). 42 "LD_PRELOAD", 43 "LD_LIBRARY_PATH", 44 "NODE_OPTIONS", 45 "BASH_ENV", 46 "XDG_CONFIG_HOME", 47 "GIT_SSH_COMMAND", 48 "anthropic_api_key", 49 "claude_config_dir", 50 ]; 51 52 #[test] 53 fn stripped_list_names_every_variable_from_the_issue() { 54 for name in &MUST_STRIP[..10] { 55 assert!( 56 STRIPPED_ENV.contains(name), 57 "{name} missing from STRIPPED_ENV" 58 ); 59 } 60 } 61 62 #[test] 63 fn allowlist_strips_every_listed_variable() { 64 let f = Fixture::new(); 65 let mut inherited = f.base_env(); 66 for name in MUST_STRIP { 67 inherited.insert(OsString::from(name), OsString::from("SENTINEL-VALUE")); 68 } 69 inherited.insert("LANG".into(), "C.UTF-8".into()); 70 inherited.insert("LC_ALL".into(), "C.UTF-8".into()); 71 inherited.insert("TERM".into(), "xterm-256color".into()); 72 73 let dir = f.profile_dir("anthropic-1"); 74 let out = launch_env(&inherited, &dir, &[]); 75 for name in MUST_STRIP { 76 assert!( 77 !has(&out, name), 78 "{name} leaked into the launch environment" 79 ); 80 } 81 for keep in ["PATH", "HOME", "LANG", "LC_ALL", "TERM"] { 82 assert!(has(&out, keep), "{keep} should pass"); 83 } 84 assert_eq!(out[&OsString::from("CLAUDE_CONFIG_DIR")], dir.as_os_str()); 85 assert!(!out.values().any(|v| v == "SENTINEL-VALUE")); 86 } 87 88 #[test] 89 fn inherited_claude_config_dir_is_overridden() { 90 let f = Fixture::new(); 91 f.install_fake_claude(); 92 let mut inherited = f.base_env(); 93 inherited.insert("CLAUDE_CONFIG_DIR".into(), "/somewhere/else".into()); 94 let plan = plan_launch(&f.rules(""), "anthropic-2", &inherited).unwrap(); 95 assert_eq!( 96 plan.env[&OsString::from("CLAUDE_CONFIG_DIR")], 97 f.profile_dir("anthropic-2").as_os_str() 98 ); 99 assert_eq!(plan.config_dir, f.profile_dir("anthropic-2")); 100 } 101 102 #[test] 103 fn proxy_variables_pass_only_when_listed_in_local_config() { 104 let f = Fixture::new(); 105 f.install_fake_claude(); 106 let mut inherited = f.base_env(); 107 inherited.insert("HTTPS_PROXY".into(), "http://proxy:3128".into()); 108 inherited.insert("NO_PROXY".into(), "localhost".into()); 109 110 let plain = plan_launch(&f.rules(""), "anthropic-1", &inherited).unwrap(); 111 assert!(!has(&plain.env, "HTTPS_PROXY")); 112 assert!(!has(&plain.env, "NO_PROXY")); 113 114 let listed = f.rules("[launch]\npass_env = [\"HTTPS_PROXY\"]\n"); 115 assert_eq!(listed.pass_env(), ["HTTPS_PROXY".to_string()]); 116 let plan = plan_launch(&listed, "anthropic-1", &inherited).unwrap(); 117 assert!(has(&plan.env, "HTTPS_PROXY")); 118 assert!(!has(&plan.env, "NO_PROXY")); 119 } 120 121 #[test] 122 fn local_config_may_only_pass_proxy_variables() { 123 let f = Fixture::new(); 124 assert!(PROXY_ENV.contains(&"HTTPS_PROXY")); 125 for bad in [ 126 "ANTHROPIC_API_KEY", 127 "CLAUDE_CONFIG_DIR", 128 "AWS_PROFILE", 129 "EDITOR", 130 ] { 131 let toml = f.rules_toml(&format!("[launch]\npass_env = [\"{bad}\"]\n")); 132 match RuleTable::from_toml_str(&toml) { 133 Err(RulesError::Invalid(m)) => assert!(m.contains(bad), "{m}"), 134 other => panic!("{bad} accepted: {other:?}"), 135 } 136 } 137 } 138 139 #[test] 140 fn unknown_alias_is_a_typed_error() { 141 let f = Fixture::new(); 142 f.install_fake_claude(); 143 let err = plan_launch(&f.rules(""), "anthropic-9", &f.base_env()).unwrap_err(); 144 match err { 145 LaunchError::UnknownAlias { alias, known } => { 146 assert_eq!(alias, "anthropic-9"); 147 assert_eq!(known, vec!["anthropic-1", "anthropic-2"]); 148 } 149 other => panic!("wrong error {other:?}"), 150 } 151 } 152 153 #[test] 154 fn non_claude_account_is_a_typed_error() { 155 let f = Fixture::new(); 156 f.install_fake_claude(); 157 let err = plan_launch(&f.rules(""), "openrouter-1", &f.base_env()).unwrap_err(); 158 assert_eq!( 159 err, 160 LaunchError::NotAClaudeProfile { 161 alias: "openrouter-1".into() 162 } 163 ); 164 } 165 166 #[test] 167 fn missing_executable_is_a_typed_error() { 168 let f = Fixture::new(); 169 fs::create_dir_all(f.bin()).unwrap(); // empty: no claude on PATH 170 let err = plan_launch(&f.rules(""), "anthropic-1", &f.base_env()).unwrap_err(); 171 assert_eq!( 172 err, 173 LaunchError::ExecutableNotFound { 174 program: "claude".into() 175 } 176 ); 177 } 178 179 #[test] 180 fn non_executable_file_on_path_is_not_found() { 181 let f = Fixture::new(); 182 let exe = f.install_fake_claude(); 183 fs::set_permissions(&exe, fs::Permissions::from_mode(0o644)).unwrap(); 184 let err = plan_launch(&f.rules(""), "anthropic-1", &f.base_env()).unwrap_err(); 185 assert!(matches!(err, LaunchError::ExecutableNotFound { .. })); 186 } 187 188 #[test] 189 fn launch_creates_private_folder_and_starts_resolved_program_once() { 190 let f = Fixture::new(); 191 let exe = f.install_fake_claude(); 192 let mut sp = FakeSpawner { 193 exit_code: 7, 194 ..FakeSpawner::default() 195 }; 196 let dir = f.profile_dir("anthropic-1"); 197 assert_eq!(check_profile_dir(&dir), Ok(DirState::Missing)); 198 199 let code = launch(&f.rules(""), "anthropic-1", &f.base_env(), &mut sp).unwrap(); 200 assert_eq!(code, 7); 201 assert_eq!(sp.plans.len(), 1); 202 assert_eq!(sp.plans[0].program, exe); 203 assert_eq!(sp.plans[0].alias, "anthropic-1"); 204 let mode = fs::metadata(&dir).unwrap().permissions().mode() & 0o777; 205 assert_eq!(mode, 0o700); 206 assert_eq!(check_profile_dir(&dir), Ok(DirState::Private)); 207 } 208 209 #[test] 210 fn launch_refuses_a_folder_others_can_read() { 211 let f = Fixture::new(); 212 f.install_fake_claude(); 213 let dir = f.profile_dir("anthropic-1"); 214 fs::create_dir_all(&dir).unwrap(); 215 fs::set_permissions(&dir, fs::Permissions::from_mode(0o755)).unwrap(); 216 let mut sp = FakeSpawner::default(); 217 let err = launch(&f.rules(""), "anthropic-1", &f.base_env(), &mut sp).unwrap_err(); 218 assert_eq!( 219 err, 220 LaunchError::ProfileDir { 221 path: dir.clone(), 222 problem: DirProblem::TooOpen { mode: 0o755 } 223 } 224 ); 225 assert!(sp.plans.is_empty(), "nothing may start"); 226 } 227 228 #[test] 229 fn launch_refuses_a_symlinked_folder() { 230 let f = Fixture::new(); 231 f.install_fake_claude(); 232 let real = f.tmp.path().join("shared"); 233 f.make_private_dir(&real); 234 let dir = f.profile_dir("anthropic-1"); 235 fs::create_dir_all(dir.parent().unwrap()).unwrap(); 236 std::os::unix::fs::symlink(&real, &dir).unwrap(); 237 let mut sp = FakeSpawner::default(); 238 let err = launch(&f.rules(""), "anthropic-1", &f.base_env(), &mut sp).unwrap_err(); 239 assert!(matches!( 240 err, 241 LaunchError::ProfileDir { 242 problem: DirProblem::Symlink, 243 .. 244 } 245 )); 246 assert!(sp.plans.is_empty()); 247 } 248 249 #[test] 250 fn plan_debug_and_error_text_never_show_environment_values() { 251 let f = Fixture::new(); 252 f.install_fake_claude(); 253 let mut inherited = f.base_env(); 254 inherited.insert("ANTHROPIC_API_KEY".into(), "sk-ant-SENTINEL".into()); 255 inherited.insert("LANG".into(), "LANG-SENTINEL".into()); 256 let plan = plan_launch(&f.rules(""), "anthropic-1", &inherited).unwrap(); 257 let dbg = format!("{plan:?}"); 258 // Assertion messages never echo the text under test: it was built from 259 // an environment holding a (fake) key. 260 assert!(dbg.contains("LANG"), "variable names should be shown"); 261 assert!( 262 !dbg.contains("SENTINEL"), 263 "variable values must not be shown" 264 ); 265 let err = plan_launch(&f.rules(""), "nope", &inherited).unwrap_err(); 266 assert!(!err.to_string().contains("SENTINEL")); 267 } 268 269 #[test] 270 fn relative_and_empty_path_entries_never_reach_claude() { 271 let f = Fixture::new(); 272 let mut inherited = f.base_env(); 273 inherited.insert("PATH".into(), ".:bin::/usr/bin:rel/x:/opt/b".into()); 274 let out = launch_env(&inherited, &f.profile_dir("anthropic-1"), &[]); 275 assert_eq!(out[&OsString::from("PATH")], "/usr/bin:/opt/b"); 276 } 277 278 #[test] 279 fn absolute_program_path_is_used_as_given() { 280 let f = Fixture::new(); 281 let exe = f.install_fake_claude(); 282 let toml = f.rules_toml("").replacen( 283 "program = \"claude\"", 284 &format!("program = \"{}\"", exe.display()), 285 1, 286 ); 287 let rules = RuleTable::from_toml_str(&toml).unwrap(); 288 let mut env = f.base_env(); 289 env.insert("PATH".into(), "/nonexistent".into()); 290 assert_eq!( 291 plan_launch(&rules, "anthropic-1", &env).unwrap().program, 292 exe 293 ); 294 } 295 296 #[test] 297 fn two_profiles_reaching_one_folder_through_a_link_are_refused() { 298 let f = Fixture::new(); 299 f.install_fake_claude(); 300 // anthropic-2 = /.switchboard/anthropic-2; make its parent a link 301 // so it resolves to anthropic-1's folder. 302 let real = f.tmp.path().join("real"); 303 fs::create_dir_all(&real).unwrap(); 304 let a1 = real.join("p"); 305 let link = f.tmp.path().join("link"); 306 std::os::unix::fs::symlink(&real, &link).unwrap(); 307 let a2 = link.join("p"); 308 let toml = format!( 309 "[[accounts]]\nid=\"a1\"\nprovider=\"anthropic\"\nprogram=\"claude\"\nsettings_dir=\"{}\"\n\ 310 [[accounts]]\nid=\"a2\"\nprovider=\"anthropic\"\nprogram=\"claude\"\nsettings_dir=\"{}\"\n\ 311 [[accounts]]\nid=\"local\"\nprovider=\"local\"\nprogram=\"aien\"\n[routes]\ndefault=[\"local\"]\n", 312 a1.display(), 313 a2.display() 314 ); 315 let rules = RuleTable::from_toml_str(&toml).unwrap(); 316 let mut sp = FakeSpawner::default(); 317 let err = launch(&rules, "a2", &f.base_env(), &mut sp).unwrap_err(); 318 assert!( 319 matches!(err, LaunchError::ProfileDir { problem: DirProblem::SharedWith(ref o), .. } if o == "a1"), 320 "{err:?}" 321 ); 322 assert!(sp.plans.is_empty()); 323 } 324 325 #[test] 326 fn folder_owned_by_someone_else_is_refused() { 327 use aien_switchboard::launch::check_profile_dir_owned_by; 328 use std::os::unix::fs::MetadataExt; 329 let f = Fixture::new(); 330 let dir = f.profile_dir("anthropic-1"); 331 f.make_private_dir(&dir); 332 let me = fs::metadata(&dir).unwrap().uid(); 333 assert_eq!(check_profile_dir_owned_by(&dir, me), Ok(DirState::Private)); 334 assert_eq!( 335 check_profile_dir_owned_by(&dir, me + 1), 336 Err(DirProblem::NotOwner { uid: me }) 337 ); 338 }