safehell 0.6.1

Approval-gated SSH broker for AI coding agents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use std::collections::BTreeMap;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::vault;

pub const CONFIG_NAME: &str = ".safehell.toml";
pub const MAX_TIMEOUT_SECONDS: u64 = 600;
pub const MAX_OUTPUT_BYTES: usize = 10 * 1024 * 1024;
pub const MAX_APPROVAL_TIMEOUT_SECONDS: u64 = 3600;
pub const MAX_APPROVAL_TTL_SECONDS: u64 = 3600;
pub const MAX_DEDUP_SECONDS: u64 = 3600;
pub const MAX_TRANSFER_BYTES: usize = 10 * 1024 * 1024;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProjectConfig {
    pub version: u8,
    pub project_id: Uuid,
    #[serde(default)]
    pub limits: Limits,
    #[serde(default)]
    pub servers: BTreeMap<String, Server>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GlobalConfig {
    pub version: u8,
    #[serde(default)]
    pub servers: BTreeMap<String, Server>,
}

impl Default for GlobalConfig {
    fn default() -> Self {
        Self {
            version: 1,
            servers: BTreeMap::new(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Limits {
    #[serde(default = "default_timeout")]
    pub timeout_seconds: u64,
    #[serde(default = "default_output")]
    pub max_output_bytes: usize,
    /// How long the broker waits for a human decision before denying the request.
    #[serde(default = "default_approval_timeout")]
    pub approval_timeout_seconds: u64,
    /// Upper bound on executed commands per rolling hour, across all servers.
    #[serde(default = "default_commands_per_hour")]
    pub max_commands_per_hour: u32,
    /// Window in which an identical command is rejected instead of run again.
    #[serde(default = "default_dedup")]
    pub dedup_seconds: u64,
    /// How long a manual approval keeps covering the exact same command.
    #[serde(default = "default_approval_ttl")]
    pub approval_ttl_seconds: u64,
    /// Largest file `get_file` and `put_file` will move in one request.
    #[serde(default = "default_transfer_bytes")]
    pub max_transfer_bytes: usize,
}

impl Default for Limits {
    fn default() -> Self {
        Self {
            timeout_seconds: default_timeout(),
            max_output_bytes: default_output(),
            approval_timeout_seconds: default_approval_timeout(),
            max_commands_per_hour: default_commands_per_hour(),
            dedup_seconds: default_dedup(),
            approval_ttl_seconds: default_approval_ttl(),
            max_transfer_bytes: default_transfer_bytes(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Server {
    pub host: String,
    #[serde(default = "default_port")]
    pub port: u16,
    pub username: String,
    pub auth: Auth,
    #[serde(default)]
    pub autoapprove: AutoApprove,
}

/// Command patterns that may skip the manual prompt. `deny` always wins.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AutoApprove {
    #[serde(default)]
    pub allow: Vec<String>,
    #[serde(default)]
    pub deny: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case", deny_unknown_fields)]
pub enum Auth {
    Password { credential_id: Uuid },
    SshAgent,
}

impl Auth {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Password { .. } => "password",
            Self::SshAgent => "ssh-agent",
        }
    }
}

pub struct Project {
    pub root: PathBuf,
    pub path: PathBuf,
    pub config: ProjectConfig,
}

pub fn discover(start: &Path) -> Result<Project> {
    for directory in start.ancestors() {
        let path = directory.join(CONFIG_NAME);
        if path.is_file() {
            let config = load(&path)?;
            return Ok(Project {
                root: directory.to_path_buf(),
                path,
                config,
            });
        }
    }
    bail!("no {CONFIG_NAME} found in this directory or its parents; run `safehell init`")
}

pub fn load(path: &Path) -> Result<ProjectConfig> {
    let raw =
        fs::read_to_string(path).with_context(|| format!("cannot read {}", path.display()))?;
    let config: ProjectConfig = toml::from_str(&raw).with_context(|| {
        format!(
            "invalid config {}; plaintext secret fields are forbidden",
            path.display()
        )
    })?;
    validate(&config)?;
    Ok(config)
}

pub fn save(path: &Path, config: &ProjectConfig) -> Result<()> {
    validate(config)?;
    let raw = toml::to_string_pretty(config)?;
    atomic_write(path, raw.as_bytes())
}

pub fn load_global() -> Result<GlobalConfig> {
    let path = vault::servers_path()?;
    if !path.exists() {
        return Ok(GlobalConfig::default());
    }
    let raw =
        fs::read_to_string(&path).with_context(|| format!("cannot read {}", path.display()))?;
    let config: GlobalConfig = toml::from_str(&raw)
        .with_context(|| format!("invalid global server config {}", path.display()))?;
    validate_global(&config)?;
    Ok(config)
}

pub fn save_global(config: &GlobalConfig) -> Result<()> {
    validate_global(config)?;
    let raw = toml::to_string_pretty(config)?;
    atomic_write(&vault::servers_path()?, raw.as_bytes())
}

pub fn resolve_server(project: &Project, alias: &str) -> Result<Server> {
    let (scope, name) = alias.split_once('/').unwrap_or(("", alias));
    match scope {
        "project" => project
            .config
            .servers
            .get(name)
            .cloned()
            .context("project server alias not found"),
        "global" => load_global()?
            .servers
            .get(name)
            .cloned()
            .context("global server alias not found"),
        "" => {
            if let Some(server) = project.config.servers.get(name) {
                return Ok(server.clone());
            }
            load_global()?
                .servers
                .get(name)
                .cloned()
                .context("server alias not found")
        }
        _ => bail!("server alias must be plain, project/<alias>, or global/<alias>"),
    }
}

pub fn init_project(root: &Path) -> Result<()> {
    let path = root.join(CONFIG_NAME);
    if path.exists() {
        bail!("{} already exists", path.display());
    }
    let config = ProjectConfig {
        version: 1,
        project_id: Uuid::new_v4(),
        limits: Limits::default(),
        servers: BTreeMap::new(),
    };
    save(&path, &config)?;
    add_git_exclude(root)?;
    println!("Created {}", path.display());
    Ok(())
}

pub fn validate_alias(alias: &str) -> Result<()> {
    if alias.is_empty()
        || alias.len() > 64
        || !alias
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
    {
        bail!("alias must be 1-64 ASCII letters, digits, '.', '_' or '-'");
    }
    Ok(())
}

fn validate(config: &ProjectConfig) -> Result<()> {
    if config.version != 1 {
        bail!("unsupported config version {}", config.version);
    }
    if !(1..=MAX_TIMEOUT_SECONDS).contains(&config.limits.timeout_seconds) {
        bail!("timeout_seconds must be between 1 and {MAX_TIMEOUT_SECONDS}");
    }
    if !(1024..=MAX_OUTPUT_BYTES).contains(&config.limits.max_output_bytes) {
        bail!("max_output_bytes must be between 1024 and {MAX_OUTPUT_BYTES}");
    }
    if !(1..=MAX_APPROVAL_TIMEOUT_SECONDS).contains(&config.limits.approval_timeout_seconds) {
        bail!("approval_timeout_seconds must be between 1 and {MAX_APPROVAL_TIMEOUT_SECONDS}");
    }
    if config.limits.max_commands_per_hour == 0 {
        bail!("max_commands_per_hour must be at least 1");
    }
    if config.limits.approval_ttl_seconds > MAX_APPROVAL_TTL_SECONDS {
        bail!("approval_ttl_seconds must be at most {MAX_APPROVAL_TTL_SECONDS}");
    }
    if config.limits.dedup_seconds > MAX_DEDUP_SECONDS {
        bail!("dedup_seconds must be at most {MAX_DEDUP_SECONDS}");
    }
    if !(1..=MAX_TRANSFER_BYTES).contains(&config.limits.max_transfer_bytes) {
        bail!("max_transfer_bytes must be between 1 and {MAX_TRANSFER_BYTES}");
    }
    validate_servers(&config.servers)
}

fn validate_global(config: &GlobalConfig) -> Result<()> {
    if config.version != 1 {
        bail!("unsupported global config version {}", config.version);
    }
    validate_servers(&config.servers)
}

fn validate_servers(servers: &BTreeMap<String, Server>) -> Result<()> {
    for (alias, server) in servers {
        validate_alias(alias)?;
        if server.host.trim().is_empty() || server.username.trim().is_empty() {
            bail!("server {alias} has an empty host or username");
        }
        if server.port == 0 {
            bail!("server {alias} has invalid port 0");
        }
        if server
            .autoapprove
            .allow
            .iter()
            .chain(&server.autoapprove.deny)
            .any(|pattern| pattern.trim().is_empty())
        {
            bail!("server {alias} has an empty autoapprove pattern");
        }
        if server
            .autoapprove
            .allow
            .iter()
            .any(|pattern| pattern == "*")
        {
            bail!("server {alias} cannot allow the '*' pattern; list explicit commands");
        }
    }
    Ok(())
}

fn add_git_exclude(root: &Path) -> Result<()> {
    let exclude = root.join(".git/info/exclude");
    if !exclude.exists() {
        return Ok(());
    }
    let current = fs::read_to_string(&exclude).unwrap_or_default();
    if current.lines().any(|line| line.trim() == CONFIG_NAME) {
        return Ok(());
    }
    let mut next = current;
    if !next.is_empty() && !next.ends_with('\n') {
        next.push('\n');
    }
    next.push_str(CONFIG_NAME);
    next.push('\n');
    atomic_write(&exclude, next.as_bytes())
}

pub(crate) fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> {
    let parent = path.parent().context("path has no parent")?;
    fs::create_dir_all(parent)?;
    let temp = parent.join(format!(
        ".{}.{}.tmp",
        path.file_name().unwrap_or_default().to_string_lossy(),
        std::process::id()
    ));
    let mut options = fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let mut file = options.open(&temp).inspect_err(|_| {
        let _ = fs::remove_file(&temp);
    })?;
    file.write_all(bytes)?;
    file.sync_all()?;
    drop(file);
    fs::rename(&temp, path).inspect_err(|_| {
        let _ = fs::remove_file(&temp);
    })?;
    Ok(())
}

const fn default_timeout() -> u64 {
    60
}
const fn default_output() -> usize {
    1024 * 1024
}
const fn default_port() -> u16 {
    22
}
const fn default_approval_timeout() -> u64 {
    120
}
const fn default_commands_per_hour() -> u32 {
    60
}
const fn default_dedup() -> u64 {
    10
}
const fn default_approval_ttl() -> u64 {
    300
}
const fn default_transfer_bytes() -> usize {
    1024 * 1024
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rejects_plaintext_secret_fields() {
        let raw = r#"
version = 1
project_id = "00000000-0000-0000-0000-000000000001"
[servers.prod]
host = "example.com"
username = "root"
password = "never"
[servers.prod.auth]
type = "ssh-agent"
"#;
        assert!(toml::from_str::<ProjectConfig>(raw).is_err());
    }

    #[test]
    fn validates_bounds_and_aliases() {
        assert!(validate_alias("prod.eu-1").is_ok());
        assert!(validate_alias("bad alias").is_err());
        let mut cfg = ProjectConfig {
            version: 1,
            project_id: Uuid::nil(),
            limits: Limits::default(),
            servers: BTreeMap::new(),
        };
        cfg.limits.timeout_seconds = 0;
        assert!(validate(&cfg).is_err());
    }

    #[test]
    fn rejects_blanket_autoapprove_and_out_of_range_windows() {
        let mut cfg = ProjectConfig {
            version: 1,
            project_id: Uuid::nil(),
            limits: Limits::default(),
            servers: BTreeMap::new(),
        };
        cfg.servers.insert(
            "prod".into(),
            Server {
                host: "example.com".into(),
                port: 22,
                username: "deploy".into(),
                auth: Auth::SshAgent,
                autoapprove: AutoApprove {
                    allow: vec!["*".into()],
                    deny: Vec::new(),
                },
            },
        );
        assert!(validate(&cfg).is_err());
        cfg.servers.get_mut("prod").unwrap().autoapprove.allow = vec!["docker logs *".into()];
        assert!(validate(&cfg).is_ok());
        cfg.limits.dedup_seconds = MAX_DEDUP_SECONDS + 1;
        assert!(validate(&cfg).is_err());
    }
}