| 1 | use std::path::{Component, Path, PathBuf}; |
| 2 | |
| 3 | use anyhow::{Result, bail}; |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | |
| 6 | use crate::ProviderKind; |
| 7 | |
| 8 | /// Schema version for informed consent to another CLI's credential file. |
| 9 | pub const EXTERNAL_CREDENTIAL_CONSENT_VERSION: u32 = 1; |
| 10 | |
| 11 | /// The complete side-effect contract for read-only external credentials. |
| 12 | pub const EXTERNAL_CREDENTIAL_READ_ONLY_SEMANTICS: &str = "read this exact file; no refresh, identity-provider or discovery requests, external-file writes, or rewrites; normal requests to the explicitly selected provider may use the token"; |
| 13 | |
| 14 | pub use codewhale_protocol::display::quote_os_path; |
| 15 | |
| 16 | /// Resolve a user-selected path without touching the filesystem. |
| 17 | /// |
| 18 | /// Consent is bound to the exact logical path, so this intentionally avoids |
| 19 | /// canonicalization (which would stat the candidate before consent exists). |
| 20 | pub fn resolve_external_credential_path(path: impl AsRef<Path>) -> Result<PathBuf> { |
| 21 | let path = path.as_ref(); |
| 22 | let absolute = if path.is_absolute() { |
| 23 | path.to_path_buf() |
| 24 | } else { |
| 25 | std::env::current_dir() |
| 26 | .map_err(|err| anyhow::anyhow!("resolving external credential path: {err}"))? |
| 27 | .join(path) |
| 28 | }; |
| 29 | |
| 30 | // Normalize only lexical `.` / `..` components. Canonicalization would |
| 31 | // inspect a credential path before consent exists and would also silently |
| 32 | // bless a symlink target. The secure reader rejects symlink/reparse-point |
| 33 | // components when the granted capability is actually consumed. |
| 34 | let mut normalized = PathBuf::new(); |
| 35 | for component in absolute.components() { |
| 36 | match component { |
| 37 | Component::Prefix(prefix) => normalized.push(prefix.as_os_str()), |
| 38 | Component::RootDir => normalized.push(component.as_os_str()), |
| 39 | Component::CurDir => {} |
| 40 | Component::ParentDir => { |
| 41 | if !normalized.pop() { |
| 42 | bail!( |
| 43 | "external credential path escapes its absolute root: {}", |
| 44 | quote_os_path(&absolute) |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | Component::Normal(part) => normalized.push(part), |
| 49 | } |
| 50 | } |
| 51 | if !normalized.is_absolute() { |
| 52 | bail!( |
| 53 | "external credential path must resolve to an absolute path: {}", |
| 54 | quote_os_path(&normalized) |
| 55 | ); |
| 56 | } |
| 57 | Ok(normalized) |
| 58 | } |
| 59 | |
| 60 | /// The side-effect envelope Codewhale may use for an external credential. |
| 61 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 62 | #[serde(rename_all = "snake_case")] |
| 63 | pub enum ExternalCredentialAccess { |
| 64 | /// Do not inspect or access the external credential store. |
| 65 | #[default] |
| 66 | Disabled, |
| 67 | /// Read the exact selected file without refreshing or rewriting it. |
| 68 | ReadOnly, |
| 69 | /// Permit a documented preservation adapter to refresh and rewrite it. |
| 70 | Managed, |
| 71 | } |
| 72 | |
| 73 | impl ExternalCredentialAccess { |
| 74 | #[must_use] |
| 75 | pub const fn as_str(self) -> &'static str { |
| 76 | match self { |
| 77 | Self::Disabled => "disabled", |
| 78 | Self::ReadOnly => "read_only", |
| 79 | Self::Managed => "managed", |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// External credential owners supported by the consent schema. |
| 85 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 86 | #[serde(rename_all = "snake_case")] |
| 87 | pub enum ExternalCredentialSource { |
| 88 | CodexCli, |
| 89 | KimiCodeCli, |
| 90 | GrokCli, |
| 91 | /// Official DeepSeek Harness (`dsh`) `$DSH_HOME/.credentials.yaml`. |
| 92 | DshCli, |
| 93 | /// Legacy tombstone retained only so old Codewhale consent records can |
| 94 | /// deserialize and be cleared. No runtime may resolve or read this source. |
| 95 | AgyCli, |
| 96 | } |
| 97 | |
| 98 | /// Default DeepSeek Harness credentials document, resolved without probing. |
| 99 | /// |
| 100 | /// Matches dsh-credentials-local: `$DSH_HOME/.credentials.yaml`, or |
| 101 | /// `~/.dsh/.credentials.yaml` when `DSH_HOME` is unset. Consent is pinned to |
| 102 | /// this exact path; a later `DSH_HOME` change is reported, never followed. |
| 103 | #[must_use] |
| 104 | pub fn default_dsh_credentials_path() -> PathBuf { |
| 105 | let home = match std::env::var_os("DSH_HOME") { |
| 106 | Some(value) if !value.is_empty() => PathBuf::from(value), |
| 107 | _ => codewhale_paths::user_home() |
| 108 | .unwrap_or_else(|| PathBuf::from(".")) |
| 109 | .join(".dsh"), |
| 110 | }; |
| 111 | home.join(".credentials.yaml") |
| 112 | } |
| 113 | |
| 114 | impl ExternalCredentialSource { |
| 115 | #[must_use] |
| 116 | pub const fn as_str(self) -> &'static str { |
| 117 | match self { |
| 118 | Self::CodexCli => "codex_cli", |
| 119 | Self::KimiCodeCli => "kimi_code_cli", |
| 120 | Self::GrokCli => "grok_cli", |
| 121 | Self::DshCli => "dsh_cli", |
| 122 | Self::AgyCli => "agy_cli", |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /// Human-facing owner name used in informed-consent disclosures. |
| 127 | #[must_use] |
| 128 | pub const fn owner_label(self) -> &'static str { |
| 129 | match self { |
| 130 | Self::CodexCli => "Codex CLI", |
| 131 | Self::KimiCodeCli => "Kimi Code CLI", |
| 132 | Self::GrokCli => "Grok CLI", |
| 133 | Self::DshCli => "DeepSeek Harness", |
| 134 | Self::AgyCli => "retired Antigravity consent", |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// Side-effect-free projection used by picker, config, and doctor surfaces. |
| 140 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 141 | pub struct ExternalCredentialConsentStatus { |
| 142 | pub access: ExternalCredentialAccess, |
| 143 | pub provider: String, |
| 144 | pub source: ExternalCredentialSource, |
| 145 | pub owner: &'static str, |
| 146 | pub path: PathBuf, |
| 147 | pub consent_version: u32, |
| 148 | pub configured: bool, |
| 149 | pub scope_valid: bool, |
| 150 | /// True when the ambient CLI path now differs from the persisted pinned |
| 151 | /// path. This is informational; it never redirects or deactivates consent. |
| 152 | pub ambient_path_changed: bool, |
| 153 | pub route_state: &'static str, |
| 154 | pub semantics: &'static str, |
| 155 | pub revoke_command: String, |
| 156 | } |
| 157 | |
| 158 | impl ExternalCredentialConsentStatus { |
| 159 | /// Warn without displaying the untrusted ambient replacement. The |
| 160 | /// persisted path remains authoritative and is escaped for one line. |
| 161 | #[must_use] |
| 162 | pub fn ambient_path_warning(&self) -> Option<String> { |
| 163 | self.ambient_path_changed.then(|| { |
| 164 | format!( |
| 165 | "warning: ambient {} credential path changed; consent remains pinned to {} and was not redirected", |
| 166 | self.owner, |
| 167 | quote_os_path(&self.path) |
| 168 | ) |
| 169 | }) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// Describe persisted external-credential policy without filesystem or network |
| 174 | /// access. `expected_path` is resolved lexically by the caller. |
| 175 | #[must_use] |
| 176 | pub fn external_credential_consent_status( |
| 177 | consent: Option<&ExternalCredentialConsentToml>, |
| 178 | provider: ProviderKind, |
| 179 | source: ExternalCredentialSource, |
| 180 | expected_path: &Path, |
| 181 | active_provider: ProviderKind, |
| 182 | ) -> ExternalCredentialConsentStatus { |
| 183 | let configured = consent.is_some(); |
| 184 | let access = consent.map_or(ExternalCredentialAccess::Disabled, |value| value.access); |
| 185 | // User-facing status identifies the route being inspected. Persisted |
| 186 | // provider/source fields are untrusted config input and are represented by |
| 187 | // `scope_valid` rather than echoed into a terminal surface. |
| 188 | let reported_provider = provider.as_str().to_string(); |
| 189 | let reported_source = source; |
| 190 | let reported_path = consent |
| 191 | .map(|value| value.path.clone()) |
| 192 | .unwrap_or_else(|| expected_path.to_path_buf()); |
| 193 | let consent_version = consent.map_or(EXTERNAL_CREDENTIAL_CONSENT_VERSION, |value| { |
| 194 | value.consent_version |
| 195 | }); |
| 196 | let scope_valid = consent.is_some_and(|value| { |
| 197 | value |
| 198 | .validate_read_scope(provider, source, &value.path) |
| 199 | .is_ok() |
| 200 | }); |
| 201 | let ambient_path_changed = consent.is_some_and(|value| value.path != expected_path); |
| 202 | let active = |
| 203 | provider == active_provider && access == ExternalCredentialAccess::ReadOnly && scope_valid; |
| 204 | let route_state = if active { "active" } else { "dormant" }; |
| 205 | let semantics = match access { |
| 206 | ExternalCredentialAccess::Disabled => { |
| 207 | "disabled; no external-credential probing, reading, refresh, discovery, identity-provider or network acquisition, writes, or rewrites; normal requests to the explicitly selected provider may use Codewhale-owned credentials" |
| 208 | } |
| 209 | ExternalCredentialAccess::ReadOnly => EXTERNAL_CREDENTIAL_READ_ONLY_SEMANTICS, |
| 210 | ExternalCredentialAccess::Managed => { |
| 211 | "managed access unavailable; no schema-safe preservation adapter" |
| 212 | } |
| 213 | }; |
| 214 | |
| 215 | ExternalCredentialConsentStatus { |
| 216 | access, |
| 217 | provider: reported_provider, |
| 218 | source: reported_source, |
| 219 | owner: reported_source.owner_label(), |
| 220 | path: reported_path, |
| 221 | consent_version, |
| 222 | configured, |
| 223 | scope_valid, |
| 224 | ambient_path_changed, |
| 225 | route_state, |
| 226 | semantics, |
| 227 | revoke_command: format!( |
| 228 | "codewhale auth external-revoke --provider {}", |
| 229 | provider.as_str() |
| 230 | ), |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// Persisted, provider-scoped consent for one exact external credential file. |
| 235 | /// |
| 236 | /// Provider and source are repeated intentionally. A copied provider table or |
| 237 | /// a future source-path remap must fail closed instead of inheriting authority. |
| 238 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 239 | #[serde(deny_unknown_fields)] |
| 240 | pub struct ExternalCredentialConsentToml { |
| 241 | pub access: ExternalCredentialAccess, |
| 242 | pub provider: String, |
| 243 | pub source: ExternalCredentialSource, |
| 244 | pub path: PathBuf, |
| 245 | pub consent_version: u32, |
| 246 | } |
| 247 | |
| 248 | impl ExternalCredentialConsentToml { |
| 249 | #[must_use] |
| 250 | pub fn read_only( |
| 251 | provider: ProviderKind, |
| 252 | source: ExternalCredentialSource, |
| 253 | path: PathBuf, |
| 254 | ) -> Self { |
| 255 | Self { |
| 256 | access: ExternalCredentialAccess::ReadOnly, |
| 257 | provider: provider.as_str().to_string(), |
| 258 | source, |
| 259 | path, |
| 260 | consent_version: EXTERNAL_CREDENTIAL_CONSENT_VERSION, |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /// Validate that this record is a current read-only consent for one exact |
| 265 | /// provider/source/path tuple without minting an I/O capability. |
| 266 | /// |
| 267 | /// This is intentionally side-effect free so inventory and picker surfaces |
| 268 | /// can acknowledge dormant consent without inspecting the external file. |
| 269 | pub fn validate_read_scope( |
| 270 | &self, |
| 271 | provider: ProviderKind, |
| 272 | source: ExternalCredentialSource, |
| 273 | resolved_path: &Path, |
| 274 | ) -> Result<()> { |
| 275 | if self.access == ExternalCredentialAccess::Disabled { |
| 276 | bail!( |
| 277 | "external credential access is disabled for {}", |
| 278 | provider.as_str() |
| 279 | ); |
| 280 | } |
| 281 | if self.access == ExternalCredentialAccess::Managed { |
| 282 | bail!( |
| 283 | "managed external credential access is unsupported for {}; no schema-safe preservation adapter is available", |
| 284 | provider.as_str() |
| 285 | ); |
| 286 | } |
| 287 | if self.consent_version != EXTERNAL_CREDENTIAL_CONSENT_VERSION { |
| 288 | bail!( |
| 289 | "external credential consent for {} uses unsupported version {}; revoke and consent again", |
| 290 | provider.as_str(), |
| 291 | self.consent_version |
| 292 | ); |
| 293 | } |
| 294 | if self.provider != provider.as_str() { |
| 295 | bail!( |
| 296 | "external credential consent is scoped to provider {:?}, not {}", |
| 297 | self.provider, |
| 298 | provider.as_str() |
| 299 | ); |
| 300 | } |
| 301 | if self.source != source { |
| 302 | bail!( |
| 303 | "external credential consent source mismatch for {} (expected {})", |
| 304 | provider.as_str(), |
| 305 | source.as_str() |
| 306 | ); |
| 307 | } |
| 308 | if !self.path.is_absolute() { |
| 309 | bail!( |
| 310 | "external credential consent path for {} must be absolute", |
| 311 | provider.as_str() |
| 312 | ); |
| 313 | } |
| 314 | let normalized = resolve_external_credential_path(&self.path)?; |
| 315 | if normalized != self.path { |
| 316 | bail!( |
| 317 | "external credential consent path for {} must be lexically normalized: {}", |
| 318 | provider.as_str(), |
| 319 | quote_os_path(&self.path) |
| 320 | ); |
| 321 | } |
| 322 | if self.path != resolved_path { |
| 323 | bail!( |
| 324 | "external credential path changed for {}; consent covers {}, current path is {}", |
| 325 | provider.as_str(), |
| 326 | quote_os_path(&self.path), |
| 327 | quote_os_path(resolved_path) |
| 328 | ); |
| 329 | } |
| 330 | Ok(()) |
| 331 | } |
| 332 | |
| 333 | /// Validate and mint the read capability consumed by credential adapters. |
| 334 | /// No filesystem operation occurs while validating the policy. |
| 335 | pub fn read_grant( |
| 336 | &self, |
| 337 | provider: ProviderKind, |
| 338 | source: ExternalCredentialSource, |
| 339 | resolved_path: &Path, |
| 340 | ) -> Result<ExternalCredentialReadGrant> { |
| 341 | self.validate_read_scope(provider, source, resolved_path)?; |
| 342 | Ok(ExternalCredentialReadGrant { |
| 343 | provider, |
| 344 | source, |
| 345 | path: resolved_path.to_path_buf(), |
| 346 | consent_version: self.consent_version, |
| 347 | }) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /// Opaque proof that one exact provider/source/path tuple may be read. |
| 352 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 353 | pub struct ExternalCredentialReadGrant { |
| 354 | provider: ProviderKind, |
| 355 | source: ExternalCredentialSource, |
| 356 | path: PathBuf, |
| 357 | consent_version: u32, |
| 358 | } |
| 359 | |
| 360 | impl ExternalCredentialReadGrant { |
| 361 | #[must_use] |
| 362 | pub fn provider(&self) -> ProviderKind { |
| 363 | self.provider |
| 364 | } |
| 365 | |
| 366 | #[must_use] |
| 367 | pub fn source(&self) -> ExternalCredentialSource { |
| 368 | self.source |
| 369 | } |
| 370 | |
| 371 | #[must_use] |
| 372 | pub fn path(&self) -> &Path { |
| 373 | &self.path |
| 374 | } |
| 375 | |
| 376 | #[must_use] |
| 377 | pub fn consent_version(&self) -> u32 { |
| 378 | self.consent_version |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | #[cfg(test)] |
| 383 | mod tests { |
| 384 | use super::*; |
| 385 | |
| 386 | fn absolute_test_path(file: &str) -> PathBuf { |
| 387 | if cfg!(windows) { |
| 388 | PathBuf::from(format!(r"C:\Users\test\{file}")) |
| 389 | } else { |
| 390 | PathBuf::from(format!("/tmp/{file}")) |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | #[test] |
| 395 | fn default_dsh_credentials_path_uses_dsh_home_or_dot_dsh() { |
| 396 | let previous = std::env::var_os("DSH_HOME"); |
| 397 | unsafe { |
| 398 | std::env::set_var("DSH_HOME", "/opt/dsh-home"); |
| 399 | } |
| 400 | let with_home = default_dsh_credentials_path(); |
| 401 | match previous { |
| 402 | Some(value) => unsafe { std::env::set_var("DSH_HOME", value) }, |
| 403 | None => unsafe { std::env::remove_var("DSH_HOME") }, |
| 404 | } |
| 405 | assert_eq!(with_home, PathBuf::from("/opt/dsh-home/.credentials.yaml")); |
| 406 | assert_eq!(ExternalCredentialSource::DshCli.as_str(), "dsh_cli"); |
| 407 | assert_eq!( |
| 408 | ExternalCredentialSource::DshCli.owner_label(), |
| 409 | "DeepSeek Harness" |
| 410 | ); |
| 411 | } |
| 412 | |
| 413 | #[test] |
| 414 | fn disclosed_paths_are_absolute_and_lexically_normalized_without_io() { |
| 415 | let resolved = |
| 416 | resolve_external_credential_path("one/./two/../auth.json").expect("lexical resolution"); |
| 417 | assert!(resolved.is_absolute()); |
| 418 | assert!( |
| 419 | resolved.ends_with(Path::new("one/auth.json")), |
| 420 | "{}", |
| 421 | resolved.display() |
| 422 | ); |
| 423 | assert!(!resolved.to_string_lossy().contains("/./")); |
| 424 | assert!(!resolved.to_string_lossy().contains("/../")); |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn structural_status_reports_full_scope_without_io() { |
| 429 | let path = absolute_test_path("codex-auth.json"); |
| 430 | let consent = ExternalCredentialConsentToml::read_only( |
| 431 | ProviderKind::OpenaiCodex, |
| 432 | ExternalCredentialSource::CodexCli, |
| 433 | path.clone(), |
| 434 | ); |
| 435 | let active = external_credential_consent_status( |
| 436 | Some(&consent), |
| 437 | ProviderKind::OpenaiCodex, |
| 438 | ExternalCredentialSource::CodexCli, |
| 439 | &path, |
| 440 | ProviderKind::OpenaiCodex, |
| 441 | ); |
| 442 | assert_eq!(active.access, ExternalCredentialAccess::ReadOnly); |
| 443 | assert_eq!(active.owner, "Codex CLI"); |
| 444 | assert_eq!(active.path, path); |
| 445 | assert_eq!(active.route_state, "active"); |
| 446 | assert!(active.scope_valid); |
| 447 | assert!(active.semantics.contains("no refresh")); |
| 448 | assert_eq!( |
| 449 | active.revoke_command, |
| 450 | "codewhale auth external-revoke --provider openai-codex" |
| 451 | ); |
| 452 | |
| 453 | let changed_path = absolute_test_path("moved-auth.json"); |
| 454 | let pinned = external_credential_consent_status( |
| 455 | Some(&consent), |
| 456 | ProviderKind::OpenaiCodex, |
| 457 | ExternalCredentialSource::CodexCli, |
| 458 | &changed_path, |
| 459 | ProviderKind::OpenaiCodex, |
| 460 | ); |
| 461 | assert!(pinned.scope_valid); |
| 462 | assert_eq!(pinned.route_state, "active"); |
| 463 | assert!(pinned.ambient_path_changed); |
| 464 | assert_eq!(pinned.path, path, "report the pinned persisted grant path"); |
| 465 | let warning = pinned |
| 466 | .ambient_path_warning() |
| 467 | .expect("ambient mismatch warning"); |
| 468 | assert!(warning.contains("remains pinned"), "{warning}"); |
| 469 | assert!(warning.contains("e_os_path(&path)), "{warning}"); |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn displayed_paths_escape_terminal_and_bidi_controls_on_one_line() { |
| 474 | let path = PathBuf::from( |
| 475 | "/safe/line\nmanaged\u{1b}[2J\u{2028}first\u{2029}second\u{202e}name.json", |
| 476 | ); |
| 477 | let quoted = quote_os_path(&path); |
| 478 | assert!(quoted.starts_with('"') && quoted.ends_with('"')); |
| 479 | assert!(quoted.contains("\\n"), "{quoted}"); |
| 480 | assert!(quoted.contains("\\x1b"), "{quoted}"); |
| 481 | assert!(quoted.contains("\\u{2028}"), "{quoted}"); |
| 482 | assert!(quoted.contains("\\u{2029}"), "{quoted}"); |
| 483 | assert!(quoted.contains("\\u{202e}"), "{quoted}"); |
| 484 | assert!(!quoted.contains('\n')); |
| 485 | assert!(!quoted.contains('\u{1b}')); |
| 486 | assert!(!quoted.contains('\u{2028}')); |
| 487 | assert!(!quoted.contains('\u{2029}')); |
| 488 | assert!(!quoted.contains('\u{202e}')); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn disabled_disclosure_does_not_imply_normal_provider_network_is_disabled() { |
| 493 | let path = absolute_test_path("codex-auth.json"); |
| 494 | let status = external_credential_consent_status( |
| 495 | None, |
| 496 | ProviderKind::OpenaiCodex, |
| 497 | ExternalCredentialSource::CodexCli, |
| 498 | &path, |
| 499 | ProviderKind::OpenaiCodex, |
| 500 | ); |
| 501 | assert!(status.semantics.contains("no external-credential")); |
| 502 | assert!(status.semantics.contains("normal requests")); |
| 503 | assert!(!status.semantics.contains("no network requests")); |
| 504 | } |
| 505 | |
| 506 | #[test] |
| 507 | fn read_grant_requires_exact_provider_source_path_and_version() { |
| 508 | let path = absolute_test_path("codex-auth.json"); |
| 509 | let consent = ExternalCredentialConsentToml::read_only( |
| 510 | ProviderKind::OpenaiCodex, |
| 511 | ExternalCredentialSource::CodexCli, |
| 512 | path.clone(), |
| 513 | ); |
| 514 | |
| 515 | let grant = consent |
| 516 | .read_grant( |
| 517 | ProviderKind::OpenaiCodex, |
| 518 | ExternalCredentialSource::CodexCli, |
| 519 | &path, |
| 520 | ) |
| 521 | .expect("exact consent tuple"); |
| 522 | assert_eq!(grant.path(), path); |
| 523 | |
| 524 | assert!( |
| 525 | consent |
| 526 | .read_grant(ProviderKind::Xai, ExternalCredentialSource::CodexCli, &path) |
| 527 | .is_err() |
| 528 | ); |
| 529 | assert!( |
| 530 | consent |
| 531 | .read_grant( |
| 532 | ProviderKind::OpenaiCodex, |
| 533 | ExternalCredentialSource::GrokCli, |
| 534 | &path |
| 535 | ) |
| 536 | .is_err() |
| 537 | ); |
| 538 | assert!( |
| 539 | consent |
| 540 | .read_grant( |
| 541 | ProviderKind::OpenaiCodex, |
| 542 | ExternalCredentialSource::CodexCli, |
| 543 | &path.with_file_name("other.json") |
| 544 | ) |
| 545 | .is_err() |
| 546 | ); |
| 547 | } |
| 548 | |
| 549 | #[test] |
| 550 | fn persisted_consent_path_must_be_lexically_normalized() { |
| 551 | let raw_path = if cfg!(windows) { |
| 552 | PathBuf::from(r"C:\Users\test\credentials\..\auth.json") |
| 553 | } else { |
| 554 | PathBuf::from("/tmp/credentials/../auth.json") |
| 555 | }; |
| 556 | let consent = ExternalCredentialConsentToml::read_only( |
| 557 | ProviderKind::Xai, |
| 558 | ExternalCredentialSource::GrokCli, |
| 559 | raw_path.clone(), |
| 560 | ); |
| 561 | assert!( |
| 562 | consent |
| 563 | .read_grant( |
| 564 | ProviderKind::Xai, |
| 565 | ExternalCredentialSource::GrokCli, |
| 566 | &raw_path |
| 567 | ) |
| 568 | .is_err() |
| 569 | ); |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn managed_consent_is_explicitly_unsupported_without_an_adapter() { |
| 574 | let path = absolute_test_path("grok-auth.json"); |
| 575 | let mut consent = ExternalCredentialConsentToml::read_only( |
| 576 | ProviderKind::Xai, |
| 577 | ExternalCredentialSource::GrokCli, |
| 578 | path.clone(), |
| 579 | ); |
| 580 | consent.access = ExternalCredentialAccess::Managed; |
| 581 | |
| 582 | let error = consent |
| 583 | .read_grant(ProviderKind::Xai, ExternalCredentialSource::GrokCli, &path) |
| 584 | .expect_err("managed access must fail closed"); |
| 585 | assert!( |
| 586 | error |
| 587 | .to_string() |
| 588 | .contains("schema-safe preservation adapter") |
| 589 | ); |
| 590 | } |
| 591 | |
| 592 | #[test] |
| 593 | fn consent_round_trips_every_scope_field() { |
| 594 | let path = absolute_test_path("codex-auth.json"); |
| 595 | let consent = ExternalCredentialConsentToml::read_only( |
| 596 | ProviderKind::OpenaiCodex, |
| 597 | ExternalCredentialSource::CodexCli, |
| 598 | path, |
| 599 | ); |
| 600 | |
| 601 | let encoded = toml::to_string(&consent).expect("serialize consent"); |
| 602 | let decoded: ExternalCredentialConsentToml = |
| 603 | toml::from_str(&encoded).expect("deserialize consent"); |
| 604 | assert_eq!(decoded, consent); |
| 605 | assert!(encoded.contains("access = \"read_only\"")); |
| 606 | assert!(encoded.contains("provider = \"openai-codex\"")); |
| 607 | assert!(encoded.contains("source = \"codex_cli\"")); |
| 608 | assert!(encoded.contains("consent_version = 1")); |
| 609 | } |
| 610 | |
| 611 | #[test] |
| 612 | fn disabled_stale_and_relative_consent_fail_before_a_grant() { |
| 613 | let path = absolute_test_path("grok-auth.json"); |
| 614 | let mut consent = ExternalCredentialConsentToml::read_only( |
| 615 | ProviderKind::Xai, |
| 616 | ExternalCredentialSource::GrokCli, |
| 617 | path.clone(), |
| 618 | ); |
| 619 | |
| 620 | consent.access = ExternalCredentialAccess::Disabled; |
| 621 | assert!( |
| 622 | consent |
| 623 | .read_grant(ProviderKind::Xai, ExternalCredentialSource::GrokCli, &path) |
| 624 | .expect_err("disabled consent") |
| 625 | .to_string() |
| 626 | .contains("disabled") |
| 627 | ); |
| 628 | |
| 629 | consent.access = ExternalCredentialAccess::ReadOnly; |
| 630 | consent.consent_version = EXTERNAL_CREDENTIAL_CONSENT_VERSION + 1; |
| 631 | assert!( |
| 632 | consent |
| 633 | .read_grant(ProviderKind::Xai, ExternalCredentialSource::GrokCli, &path) |
| 634 | .expect_err("stale consent") |
| 635 | .to_string() |
| 636 | .contains("unsupported version") |
| 637 | ); |
| 638 | |
| 639 | consent.consent_version = EXTERNAL_CREDENTIAL_CONSENT_VERSION; |
| 640 | consent.path = PathBuf::from("relative/auth.json"); |
| 641 | assert!( |
| 642 | consent |
| 643 | .read_grant( |
| 644 | ProviderKind::Xai, |
| 645 | ExternalCredentialSource::GrokCli, |
| 646 | Path::new("relative/auth.json"), |
| 647 | ) |
| 648 | .expect_err("relative path") |
| 649 | .to_string() |
| 650 | .contains("must be absolute") |
| 651 | ); |
| 652 | } |
| 653 | } |
| 654 |