use std::future::Future;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TerminalMode {
#[default]
Sandboxed,
Unsafe,
}
impl TerminalMode {
#[must_use]
pub const fn requires_approval(self) -> bool {
matches!(self, Self::Unsafe)
}
#[must_use]
pub const fn requires_per_script_approval(self) -> bool {
matches!(self, Self::Unsafe)
}
#[must_use]
pub const fn description(self) -> &'static str {
match self {
Self::Sandboxed => "sandboxed",
Self::Unsafe => "unsafe (no sandbox, full access)",
}
}
}
pub trait PermissionHandler: Send + Sync {
fn check(
&self,
mode: TerminalMode,
script: &str,
) -> impl Future<Output = Result<bool, PermissionError>> + Send;
fn will_wait_for_approval(
&self,
mode: TerminalMode,
script: &str,
) -> impl Future<Output = bool> + Send {
let _ = (mode, script);
async { false }
}
fn check_domain(&self, domain: &str, port: u16) -> impl Future<Output = bool> + Send {
let _ = (domain, port);
async { false }
}
}
#[derive(Debug, thiserror::Error)]
pub enum PermissionError {
#[error("permission denied: {0}")]
Denied(String),
#[error("permission check interrupted")]
Interrupted,
#[error("permission error: {0}")]
Internal(#[from] anyhow::Error),
}
#[derive(Debug, Clone, Copy, Default)]
pub struct DenyUnsafe;
impl PermissionHandler for DenyUnsafe {
fn check(
&self,
mode: TerminalMode,
_script: &str,
) -> impl Future<Output = Result<bool, PermissionError>> + Send {
std::future::ready(match mode {
TerminalMode::Sandboxed => Ok(true),
TerminalMode::Unsafe => Err(PermissionError::Denied(format!(
"sandbox blocked {} execution; approval is required to escalate this command, but no interactive approval handler is configured",
mode.description()
))),
})
}
fn will_wait_for_approval(
&self,
_mode: TerminalMode,
_script: &str,
) -> impl Future<Output = bool> + Send {
std::future::ready(false)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoopPermissionHandler;
impl PermissionHandler for NoopPermissionHandler {
fn check(
&self,
_mode: TerminalMode,
_script: &str,
) -> impl Future<Output = Result<bool, PermissionError>> + Send {
std::future::ready(Ok(true))
}
fn will_wait_for_approval(
&self,
_mode: TerminalMode,
_script: &str,
) -> impl Future<Output = bool> + Send {
std::future::ready(false)
}
fn check_domain(&self, _domain: &str, _port: u16) -> impl Future<Output = bool> + Send {
std::future::ready(true)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_deny_unsafe() {
let handler = DenyUnsafe;
assert!(handler.check(TerminalMode::Sandboxed, "ls").await.unwrap());
assert!(
handler
.check(TerminalMode::Unsafe, "rm -rf /")
.await
.is_err()
);
}
#[tokio::test]
async fn test_noop_permission_handler() {
let handler = NoopPermissionHandler;
assert!(handler.check(TerminalMode::Sandboxed, "ls").await.unwrap());
assert!(
handler
.check(TerminalMode::Unsafe, "rm -rf /")
.await
.unwrap()
);
}
#[tokio::test]
async fn test_deny_unsafe_check_domain() {
let handler = DenyUnsafe;
assert!(!handler.check_domain("example.com", 443).await);
assert!(!handler.check_domain("api.github.com", 80).await);
}
#[tokio::test]
async fn test_noop_permission_handler_check_domain() {
let handler = NoopPermissionHandler;
assert!(handler.check_domain("example.com", 443).await);
assert!(handler.check_domain("malicious.com", 80).await);
}
}