Skip to main content

vtcode_config/core/
security.rs

1use crate::env_helpers::default_true;
2use serde::{Deserialize, Serialize};
3use vtcode_commons::{VtCodePaths, http::PostQuantumTlsPolicy};
4
5/// Gatekeeper mitigation configuration (macOS only)
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct GatekeeperConfig {
9    /// Warn when a quarantined executable is detected
10    #[serde(default = "default_true")]
11    pub warn_on_quarantine: bool,
12
13    /// Attempt to clear quarantine automatically (opt-in)
14    #[serde(default)]
15    pub auto_clear_quarantine: bool,
16
17    /// Paths eligible for quarantine auto-clear
18    #[serde(default = "default_gatekeeper_auto_clear_paths")]
19    pub auto_clear_paths: Vec<String>,
20}
21
22/// Security configuration
23#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
24#[derive(Debug, Clone, Deserialize, Serialize)]
25pub struct SecurityConfig {
26    /// Require human confirmation for critical actions
27    #[serde(default = "default_true")]
28    pub human_in_the_loop: bool,
29
30    /// Require a successful write tool before accepting claims like
31    /// "I've updated the file" as applied. When true, such claims are
32    /// treated as proposals unless a write tool executed successfully.
33    #[serde(default = "default_true")]
34    require_write_tool_for_claims: bool,
35
36    /// Automatically apply detected patch blocks in assistant replies
37    /// when no write tool was executed. Defaults to false for safety.
38    #[serde(default)]
39    auto_apply_detected_patches: bool,
40
41    /// Enable zero-trust checks between components.
42    #[serde(default = "default_true")]
43    pub zero_trust_mode: bool,
44
45    /// Encrypt payloads passed across executors.
46    #[serde(default = "default_true")]
47    encrypt_payloads: bool,
48
49    /// Enable runtime integrity tagging for critical paths.
50    #[serde(default = "default_true")]
51    pub integrity_checks: bool,
52
53    /// TLS key-exchange policy for Rustls clients. Options: "prefer" (hybrid
54    /// key exchange with classical fallback) or "require" (hybrid key exchange
55    /// and TLS 1.3 AES-256-GCM only). Changing it requires restart.
56    #[serde(default)]
57    pub post_quantum_tls: PostQuantumTlsPolicy,
58
59    /// Play terminal bell notification when HITL approval is required.
60    #[serde(default = "default_true")]
61    pub hitl_notification_bell: bool,
62
63    /// Gatekeeper mitigation options (macOS only)
64    #[serde(default)]
65    pub gatekeeper: GatekeeperConfig,
66}
67
68impl Default for SecurityConfig {
69    fn default() -> Self {
70        Self {
71            human_in_the_loop: default_true(),
72            require_write_tool_for_claims: default_true(),
73            auto_apply_detected_patches: false,
74            zero_trust_mode: default_true(),
75            encrypt_payloads: default_true(),
76            integrity_checks: default_true(),
77            post_quantum_tls: PostQuantumTlsPolicy::default(),
78            hitl_notification_bell: default_true(),
79            gatekeeper: GatekeeperConfig::default(),
80        }
81    }
82}
83
84fn default_gatekeeper_auto_clear_paths() -> Vec<String> {
85    let mut paths = vec![".vtcode/bin".to_string()];
86    if let Ok(resolver) = VtCodePaths::resolve() {
87        paths.push(resolver.executable_dir().display().to_string());
88        paths.push(resolver.legacy_dir().join("bin").display().to_string());
89    } else {
90        paths.push(".local/bin".to_string());
91    }
92    paths.sort();
93    paths.dedup();
94    paths
95}
96
97impl Default for GatekeeperConfig {
98    fn default() -> Self {
99        Self {
100            warn_on_quarantine: default_true(),
101            auto_clear_quarantine: false,
102            auto_clear_paths: default_gatekeeper_auto_clear_paths(),
103        }
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::SecurityConfig;
110    use vtcode_commons::http::PostQuantumTlsPolicy;
111
112    #[test]
113    fn post_quantum_tls_policy_defaults_to_interoperable_preference() {
114        let config: SecurityConfig = serde_json::from_str("{}").expect("default security config");
115
116        assert_eq!(config.post_quantum_tls, PostQuantumTlsPolicy::Prefer);
117    }
118
119    #[test]
120    fn post_quantum_tls_policy_deserializes_and_serializes_as_a_typed_value() {
121        let config: SecurityConfig =
122            serde_json::from_str(r#"{"post_quantum_tls":"require"}"#).expect("strict TLS policy");
123
124        assert_eq!(config.post_quantum_tls, PostQuantumTlsPolicy::Require);
125        assert_eq!(serde_json::to_value(config).expect("serialize security config")["post_quantum_tls"], "require");
126        assert!(serde_json::from_str::<SecurityConfig>(r#"{"post_quantum_tls":"strict"}"#).is_err());
127    }
128}