httpjail 0.6.2

Monitor and restrict HTTP/HTTPS requests from processes
Documentation
use anyhow::{Context, Result};
use rand::Rng;
use std::os::unix::process::{CommandExt, ExitStatusExt};
use std::process::{Command, ExitStatus};
use std::time::{Duration, Instant};

pub mod weak;

#[cfg(target_os = "linux")]
pub mod linux;

#[cfg(any(target_os = "macos", target_os = "linux"))]
pub mod managed;

/// Wait for an executed command, killing its entire process group before returning on timeout.
/// The caller must not clean up network isolation while the command is still running.
pub(crate) fn run_command(mut command: Command, limit: Option<Duration>) -> Result<ExitStatus> {
    let Some(limit) = limit else {
        return command.status().context("Failed to execute command");
    };

    // Put descendants in a distinct process group, rather than killing only the
    // `ip netns exec` / docker / shell wrapper on timeout.
    command.process_group(0);
    let mut child = command.spawn().context("Failed to execute command")?;
    let started = Instant::now();
    loop {
        if let Some(status) = child.try_wait()? {
            return Ok(status);
        }
        if started.elapsed() >= limit {
            let result = unsafe { libc::kill(-(child.id() as i32), libc::SIGKILL) };
            let kill_error = (result != 0).then(std::io::Error::last_os_error);
            // Reap the direct child even if it exited concurrently with the signal.
            let _ = child.kill();
            child.wait()?;
            if let Some(error) = kill_error
                && error.raw_os_error() != Some(libc::ESRCH)
            {
                return Err(error).context("Failed to kill timed-out process group");
            }
            return Ok(ExitStatus::from_raw(124 << 8));
        }
        std::thread::sleep(Duration::from_millis(10).min(limit.saturating_sub(started.elapsed())));
    }
}

/// Trait for platform-specific jail implementations
#[allow(dead_code)]
pub trait Jail: Send + Sync {
    /// Setup jail for a specific session
    fn setup(&mut self, proxy_port: u16) -> Result<()>;

    /// Execute a command within the jail with additional environment variables.
    ///
    /// # Important
    ///
    /// System-native jail implementations (macOS/Linux) do NOT set HTTP_PROXY/HTTPS_PROXY
    /// environment variables. Instead, they use platform-specific mechanisms to transparently
    /// redirect traffic:
    /// - macOS: Uses PF (Packet Filter) rules to redirect traffic from the httpjail group
    /// - Linux: Uses iptables rules in a network namespace to redirect traffic
    ///
    /// The WeakJail implementation does set HTTP_PROXY/HTTPS_PROXY environment variables
    /// since it relies on applications respecting these variables.
    ///
    /// This approach ensures that system-native jails capture all network traffic,
    /// even from applications that don't respect proxy environment variables.
    fn execute(
        &self,
        command: &[String],
        extra_env: &[(String, String)],
    ) -> Result<std::process::ExitStatus>;

    /// Execute with a deadline; terminate and reap the command before returning on timeout.
    fn execute_with_timeout(
        &self,
        command: &[String],
        extra_env: &[(String, String)],
        timeout: Duration,
    ) -> Result<std::process::ExitStatus>;

    /// Cleanup jail resources
    fn cleanup(&self) -> Result<()>;

    /// Get the unique jail ID for this instance
    fn jail_id(&self) -> &str;

    /// Cleanup orphaned resources for a given jail_id (static dispatch)
    /// This is called when detecting stale canaries from other processes
    fn cleanup_orphaned(jail_id: &str) -> Result<()>
    where
        Self: Sized;
}

/// Only automatically clean up resource names generated by this tool.
pub fn valid_jail_id(id: &str) -> bool {
    id.len() == 8
        && id
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit())
}

/// A stale heartbeat is not evidence of process death (setup may be slow or paused).
/// Unknown/legacy canaries are treated as live rather than risking deletion of a live jail.
pub fn canary_owner_alive(path: &std::path::Path) -> bool {
    let Ok(contents) = std::fs::read_to_string(path) else {
        return true;
    };
    let Ok(pid) = contents.trim().parse::<i32>() else {
        return true;
    };
    if pid <= 0 {
        return true;
    }
    unsafe {
        libc::kill(pid, 0) == 0
            || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
    }
}

#[cfg(target_os = "linux")]
pub(crate) fn ensure_trusted_root_dir(path: &std::path::Path) -> anyhow::Result<()> {
    use std::os::unix::fs::{DirBuilderExt, MetadataExt};
    let mut builder = std::fs::DirBuilder::new();
    builder.recursive(true).mode(0o700).create(path)?;
    for ancestor in path.ancestors() {
        let metadata = std::fs::symlink_metadata(ancestor)?;
        anyhow::ensure!(
            metadata.file_type().is_dir() && metadata.uid() == 0 && metadata.mode() & 0o022 == 0,
            "Directory and ancestors must be root-owned and not writable by other users: {}",
            ancestor.display()
        );
    }
    Ok(())
}

/// Get the canary directory for tracking jail lifetimes
pub fn get_canary_dir() -> std::path::PathBuf {
    #[cfg(target_os = "linux")]
    if unsafe { libc::geteuid() == 0 } {
        // sudo -E may preserve an invoking-user-owned HOME. Root canaries must
        // not live beneath a directory that user can rename or replace.
        return std::path::PathBuf::from("/var/lib/httpjail/canaries");
    }

    // Use user data directory instead of /tmp to avoid issues with tmp cleaners
    // This ensures canaries persist across reboots and are only removed when we want them to be
    if let Some(data_dir) = dirs::data_dir() {
        data_dir.join("httpjail").join("canaries")
    } else {
        // Fallback to /tmp if we can't get user data dir (should rarely happen)
        std::path::PathBuf::from("/tmp/httpjail")
    }
}

/// Get the directory for httpjail temporary files (like resolv.conf)
pub fn get_temp_dir() -> std::path::PathBuf {
    std::path::PathBuf::from("/tmp/httpjail")
}

/// Jail configuration
#[derive(Clone, Debug)]
pub struct JailConfig {
    /// Port where the HTTP proxy is listening
    pub http_proxy_port: u16,

    /// Port for HTTPS proxy
    pub https_proxy_port: u16,

    /// Unique identifier for this jail instance
    pub jail_id: String,

    /// Whether to enable heartbeat monitoring
    pub enable_heartbeat: bool,

    /// Interval in seconds between heartbeat touches
    pub heartbeat_interval_secs: u64,

    /// Timeout in seconds before considering a jail orphaned
    pub orphan_timeout_secs: u64,
}

impl JailConfig {
    /// Create a new configuration with a unique jail_id
    pub fn new() -> Self {
        // Generate a random 8-character base36 ID (a-z0-9)
        // This gives us 36^8 = ~2.8 trillion possible IDs (~41 bits of entropy)
        let jail_id = Self::generate_base36_id(8);

        Self {
            http_proxy_port: 8040,
            https_proxy_port: 8043,
            jail_id,
            enable_heartbeat: true,
            heartbeat_interval_secs: 1,
            orphan_timeout_secs: 10,
        }
    }

    /// Generate a random base36 ID of the specified length
    fn generate_base36_id(length: usize) -> String {
        const CHARSET: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";
        let mut rng = rand::thread_rng();

        (0..length)
            .map(|_| {
                let idx = rng.gen_range(0..CHARSET.len());
                CHARSET[idx] as char
            })
            .collect()
    }
}

impl Default for JailConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// Create a platform-specific jail implementation wrapped with lifecycle management
pub fn create_jail(
    config: JailConfig,
    weak_mode: bool,
    docker_mode: bool,
) -> Result<Box<dyn Jail>> {
    use self::weak::WeakJail;

    // Always use weak jail on macOS due to PF limitations
    // (PF translation rules cannot match on user/group)
    #[cfg(target_os = "macos")]
    {
        let _ = weak_mode; // Suppress unused warning on macOS
        let _ = docker_mode; // Docker mode not supported on macOS
        // WeakJail doesn't need lifecycle management since it creates no system resources
        Ok(Box::new(WeakJail::new(config)?))
    }

    #[cfg(target_os = "linux")]
    {
        use self::linux::LinuxJail;
        use self::linux::docker::DockerLinux;

        if weak_mode {
            // WeakJail doesn't need lifecycle management since it creates no system resources
            Ok(Box::new(WeakJail::new(config)?))
        } else if docker_mode {
            // DockerLinux wraps LinuxJail with Docker-specific execution
            Ok(Box::new(self::managed::ManagedJail::new(
                DockerLinux::new(config.clone())?,
                &config,
            )?))
        } else {
            // LinuxJail creates system resources (namespaces, iptables) that need cleanup
            Ok(Box::new(self::managed::ManagedJail::new(
                LinuxJail::new(config.clone())?,
                &config,
            )?))
        }
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        anyhow::bail!("Unsupported platform")
    }
}

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

    #[test]
    fn test_base36_jail_id_generation() {
        // Generate multiple IDs and verify they are valid base36
        for _ in 0..100 {
            let config = JailConfig::new();
            let id = &config.jail_id;

            // Check length
            assert_eq!(id.len(), 8, "ID should be 8 characters long");

            // Check all characters are base36 (0-9, a-z)
            assert!(
                id.chars()
                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()),
                "ID should only contain lowercase letters and digits: {}",
                id
            );
        }
    }

    #[test]
    fn stale_heartbeat_does_not_make_a_live_jail_an_orphan() {
        let temp = tempfile::tempdir().unwrap();
        let canary = temp.path().join("abc12345");
        std::fs::write(&canary, std::process::id().to_string()).unwrap();
        assert!(valid_jail_id("abc12345"));
        assert!(!valid_jail_id("../etc/passwd"));
        assert!(canary_owner_alive(&canary));
        std::fs::write(&canary, i32::MAX.to_string()).unwrap();
        assert!(!canary_owner_alive(&canary));
    }

    #[test]
    fn test_jail_id_uniqueness() {
        // Generate many IDs and check for collisions
        use std::collections::HashSet;
        let mut ids = HashSet::new();

        for _ in 0..1000 {
            let config = JailConfig::new();
            let id = config.jail_id.clone();

            // Check that this ID hasn't been seen before
            assert!(ids.insert(id.clone()), "Duplicate ID generated: {}", id);
        }

        // We generated 1000 unique IDs
        assert_eq!(ids.len(), 1000);
    }
}