nucleus-container 0.3.9

Extremely lightweight Docker alternative for agents and production services — isolated execution using cgroups, namespaces, seccomp, Landlock, and gVisor
Documentation
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4};
use std::str::FromStr;

/// Network mode for container
#[derive(Debug, Clone)]
pub enum NetworkMode {
    /// No networking (default, fully isolated)
    None,
    /// Native runtime host network namespace sharing.
    Host,
    /// gVisor hostinet mode; omits the OCI network namespace and passes
    /// `--network host` to runsc.
    GVisorHost,
    /// Bridge network with NAT
    Bridge(BridgeConfig),
}

/// NAT backend for native bridge-style networking.
///
/// `Auto` preserves the historical behavior for privileged callers while
/// enabling a userspace NAT path for rootless/native containers.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum, serde::Serialize, serde::Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum NatBackend {
    /// Select kernel bridge + iptables when privileged, otherwise userspace NAT.
    #[value(name = "auto")]
    Auto,
    /// Require the kernel bridge/veth/iptables backend.
    #[value(name = "kernel")]
    Kernel,
    /// Require the userspace NAT backend.
    #[value(name = "userspace")]
    Userspace,
}

impl NatBackend {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Auto => "auto",
            Self::Kernel => "kernel",
            Self::Userspace => "userspace",
        }
    }
}

/// Configuration for bridge networking
#[derive(Debug, Clone)]
pub struct BridgeConfig {
    /// Bridge interface name
    pub bridge_name: String,
    /// Subnet (e.g., "10.0.42.0/24")
    pub subnet: String,
    /// Container IP address (auto-assigned from subnet)
    pub container_ip: Option<String>,
    /// DNS servers
    pub dns: Vec<String>,
    /// Port forwarding rules
    pub port_forwards: Vec<PortForward>,
    /// NAT backend selection for the native runtime.
    pub nat_backend: NatBackend,
}

impl Default for BridgeConfig {
    fn default() -> Self {
        Self {
            bridge_name: "nucleus0".to_string(),
            subnet: "10.0.42.0/24".to_string(),
            container_ip: None,
            // Empty by default – production services must configure DNS explicitly.
            // Agent mode callers can use BridgeConfig::with_public_dns() for convenience.
            dns: Vec::new(),
            port_forwards: Vec::new(),
            nat_backend: NatBackend::Auto,
        }
    }
}

impl BridgeConfig {
    /// Convenience: populate with public Google DNS resolvers.
    /// Suitable for agent/sandbox workloads, NOT for production services.
    pub fn with_public_dns(mut self) -> Self {
        self.dns = vec!["8.8.8.8".to_string(), "8.8.4.4".to_string()];
        self
    }

    pub fn with_dns(mut self, servers: Vec<String>) -> Self {
        self.dns = servers;
        self
    }

    pub fn with_nat_backend(mut self, backend: NatBackend) -> Self {
        self.nat_backend = backend;
        self
    }

    pub fn selected_nat_backend(&self, host_is_root: bool, rootless: bool) -> NatBackend {
        match self.nat_backend {
            NatBackend::Auto if host_is_root && !rootless => NatBackend::Kernel,
            NatBackend::Auto => NatBackend::Userspace,
            explicit => explicit,
        }
    }

    pub fn contains_ipv4(&self, ip: Ipv4Addr) -> Result<bool, String> {
        let (network, prefix) = parse_ipv4_cidr(&self.subnet)?;
        Ok(ipv4_in_cidr(ip, network, prefix))
    }

    /// Return the host-side bridge gateway address for this subnet.
    pub fn gateway_ipv4(&self) -> Result<Ipv4Addr, String> {
        let (network, prefix) = parse_ipv4_cidr(&self.subnet)?;
        let network_u32 = u32::from(network) & ipv4_mask(prefix);
        let gateway = if prefix >= 31 {
            network_u32
        } else {
            network_u32
                .checked_add(1)
                .ok_or_else(|| format!("CIDR '{}' overflowed", self.subnet))?
        };
        Ok(Ipv4Addr::from(gateway))
    }

    /// Validate all fields to prevent argument injection into ip/iptables commands.
    pub fn validate(&self) -> crate::error::Result<()> {
        // Bridge name: alphanumeric, dash, underscore; max 15 chars (Linux IFNAMSIZ)
        if self.bridge_name.is_empty() || self.bridge_name.len() > 15 {
            return Err(crate::error::NucleusError::NetworkError(format!(
                "Bridge name must be 1-15 characters, got '{}'",
                self.bridge_name
            )));
        }
        if !self
            .bridge_name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
        {
            return Err(crate::error::NucleusError::NetworkError(format!(
                "Bridge name contains invalid characters (allowed: a-zA-Z0-9_-): '{}'",
                self.bridge_name
            )));
        }

        // Subnet: must be valid IPv4 CIDR
        validate_ipv4_cidr(&self.subnet).map_err(crate::error::NucleusError::NetworkError)?;

        // Container IP (if specified)
        if let Some(ref ip) = self.container_ip {
            validate_ipv4_addr(ip).map_err(crate::error::NucleusError::NetworkError)?;
        }

        // DNS servers
        for dns in &self.dns {
            validate_ipv4_addr(dns).map_err(crate::error::NucleusError::NetworkError)?;
        }

        Ok(())
    }
}

/// Validate that a string is a valid IPv4 address (no leading dashes, proper octets).
fn validate_ipv4_addr(s: &str) -> Result<(), String> {
    let parts: Vec<&str> = s.split('.').collect();
    if parts.len() != 4 {
        return Err(format!("Invalid IPv4 address: '{}'", s));
    }
    for part in &parts {
        if part.is_empty() {
            return Err(format!("Invalid IPv4 address: '{}'", s));
        }
        if part.len() > 1 && part.starts_with('0') {
            return Err(format!(
                "Invalid IPv4 address: '{}' – octet '{}' has leading zero",
                s, part
            ));
        }
        match part.parse::<u8>() {
            Ok(_) => {}
            Err(_) => return Err(format!("Invalid IPv4 address: '{}'", s)),
        }
    }
    Ok(())
}

fn parse_ipv4_cidr(s: &str) -> Result<(Ipv4Addr, u8), String> {
    let (addr, prefix) = s
        .split_once('/')
        .ok_or_else(|| format!("Invalid CIDR (missing /prefix): '{}'", s))?;
    validate_ipv4_addr(addr)?;
    let addr = addr
        .parse::<Ipv4Addr>()
        .map_err(|_| format!("Invalid IPv4 address: '{}'", addr))?;
    let prefix: u8 = prefix
        .parse()
        .map_err(|_| format!("Invalid CIDR prefix: '{}'", s))?;
    if prefix > 32 {
        return Err(format!("CIDR prefix must be 0-32, got {}", prefix));
    }
    Ok((addr, prefix))
}

fn ipv4_mask(prefix: u8) -> u32 {
    if prefix == 0 {
        0
    } else {
        u32::MAX << (32 - prefix)
    }
}

fn ipv4_in_cidr(ip: Ipv4Addr, network: Ipv4Addr, prefix: u8) -> bool {
    let mask = ipv4_mask(prefix);
    (u32::from(ip) & mask) == (u32::from(network) & mask)
}

/// Validate that a string is a valid IPv4 CIDR (e.g., "10.0.42.0/24").
fn validate_ipv4_cidr(s: &str) -> Result<(), String> {
    parse_ipv4_cidr(s).map(|_| ())
}

/// Validate that a string is a valid IPv4 CIDR for egress rules.
pub fn validate_egress_cidr(s: &str) -> Result<(), String> {
    validate_ipv4_cidr(s)
}

/// Validate that a string is an exact DNS name for egress domain rules.
pub fn validate_egress_domain(s: &str) -> Result<(), String> {
    if s.is_empty() {
        return Err("Egress domain cannot be empty".to_string());
    }
    if s.parse::<Ipv4Addr>().is_ok() || s.parse::<Ipv6Addr>().is_ok() {
        return Err(format!(
            "Egress domain '{}' is an IP address; use --egress-allow with a CIDR",
            s
        ));
    }

    let domain = s.strip_suffix('.').unwrap_or(s);
    if domain.is_empty() {
        return Err("Egress domain cannot be only '.'".to_string());
    }
    if domain.len() > 253 {
        return Err(format!("Egress domain '{}' is longer than 253 bytes", s));
    }
    if !domain.contains('.') {
        return Err(format!(
            "Egress domain '{}' must be a fully-qualified name with at least one dot",
            s
        ));
    }

    for label in domain.split('.') {
        if label.is_empty() {
            return Err(format!("Egress domain '{}' contains an empty label", s));
        }
        if label.len() > 63 {
            return Err(format!(
                "Egress domain '{}' contains a label longer than 63 bytes",
                s
            ));
        }
        if label.starts_with('-') || label.ends_with('-') {
            return Err(format!(
                "Egress domain '{}' contains a label starting or ending with '-'",
                s
            ));
        }
        if !label
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'-')
        {
            return Err(format!("Egress domain '{}' contains invalid characters", s));
        }
    }

    Ok(())
}

/// Egress policy for audited outbound network access.
///
/// When set, iptables OUTPUT chain rules restrict which destinations the
/// container process can connect to. Use [`EgressPolicy::deny_all`] when no
/// outbound connections, including DNS, should be permitted.
#[derive(Debug, Clone)]
pub struct EgressPolicy {
    /// Allowed destination CIDRs (e.g., "10.0.0.0/8", "192.168.1.0/24").
    pub allowed_cidrs: Vec<String>,
    /// Allowed exact DNS names. These are resolved to IPv4 /32 rules at startup.
    pub allowed_domains: Vec<String>,
    /// Allowed destination TCP ports. Empty means all ports on allowed CIDRs.
    pub allowed_tcp_ports: Vec<u16>,
    /// Allowed destination UDP ports.
    pub allowed_udp_ports: Vec<u16>,
    /// Whether to log denied egress attempts (rate-limited).
    pub log_denied: bool,
    /// Whether to add implicit DNS (port 53 UDP/TCP) allow rules for configured
    /// resolvers. Defaults to `true` for explicit allowlist usability.
    pub allow_dns: bool,
}

impl Default for EgressPolicy {
    fn default() -> Self {
        Self {
            allowed_cidrs: Vec::new(),
            allowed_domains: Vec::new(),
            allowed_tcp_ports: Vec::new(),
            allowed_udp_ports: Vec::new(),
            log_denied: true,
            allow_dns: true,
        }
    }
}

impl EgressPolicy {
    /// Create a strict deny-all egress policy, including DNS.
    pub fn deny_all() -> Self {
        Self {
            allow_dns: false,
            ..Self::default()
        }
    }

    /// Allow egress to the given CIDRs on any port.
    pub fn with_allowed_cidrs(mut self, cidrs: Vec<String>) -> Self {
        self.allowed_cidrs = cidrs;
        self
    }

    /// Allow egress to the given exact DNS names.
    pub fn with_allowed_domains(mut self, domains: Vec<String>) -> Self {
        self.allowed_domains = domains;
        self
    }

    pub fn with_allowed_tcp_ports(mut self, ports: Vec<u16>) -> Self {
        self.allowed_tcp_ports = ports;
        self
    }

    pub fn with_allowed_udp_ports(mut self, ports: Vec<u16>) -> Self {
        self.allowed_udp_ports = ports;
        self
    }

    /// Create a deny-by-default policy that only permits TCP egress to the
    /// configured host-side credential broker.
    pub fn credential_broker_only(broker: &CredentialBrokerConfig) -> Self {
        Self::deny_all()
            .with_allowed_cidrs(vec![broker.broker_cidr()])
            .with_allowed_tcp_ports(vec![broker.broker_port])
    }

    /// Return true when this policy permits no direct outbound route except
    /// the configured credential broker endpoint.
    pub fn is_credential_broker_only(&self, broker: &CredentialBrokerConfig) -> bool {
        self.allowed_cidrs == vec![broker.broker_cidr()]
            && self.allowed_domains.is_empty()
            && self.allowed_tcp_ports == vec![broker.broker_port]
            && self.allowed_udp_ports.is_empty()
            && !self.allow_dns
    }
}

/// Host-side credential broker endpoint for broker-only egress.
///
/// The broker process runs outside the sandbox and holds the real credential.
/// Nucleus only allows the sandbox to reach this endpoint; the broker is
/// responsible for injecting credentials, constraining upstream destinations,
/// rate limiting, and auditing authenticated requests.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CredentialBrokerConfig {
    /// Host-side bridge IP reachable from the container namespace.
    pub broker_ip: Ipv4Addr,
    /// TCP port where the host-side broker listens.
    pub broker_port: u16,
    /// Whether Nucleus should inject standard proxy environment variables
    /// pointing at the broker. Disable this when using a provider-specific
    /// base URL environment variable instead.
    pub inject_proxy_env: bool,
}

impl CredentialBrokerConfig {
    /// Create a credential broker config from a host-side bridge IP and port.
    pub fn new(broker_ip: Ipv4Addr, broker_port: u16) -> Self {
        Self {
            broker_ip,
            broker_port,
            inject_proxy_env: true,
        }
    }

    /// Parse an endpoint in `IPv4:PORT` form.
    pub fn parse_endpoint(endpoint: &str) -> Result<Self, String> {
        let socket = SocketAddrV4::from_str(endpoint).map_err(|_| {
            format!(
                "Invalid credential broker endpoint '{}', expected IPv4:PORT",
                endpoint
            )
        })?;
        let config = Self::new(*socket.ip(), socket.port());
        config.validate()?;
        Ok(config)
    }

    pub fn with_proxy_env(mut self, inject_proxy_env: bool) -> Self {
        self.inject_proxy_env = inject_proxy_env;
        self
    }

    pub fn validate(&self) -> Result<(), String> {
        if self.broker_port == 0 {
            return Err("Credential broker port must be non-zero".to_string());
        }
        if self.broker_ip.is_unspecified() {
            return Err("Credential broker IP must not be 0.0.0.0".to_string());
        }
        if self.broker_ip.is_loopback() {
            return Err(
                "Credential broker IP must be reachable on the bridge, not container loopback"
                    .to_string(),
            );
        }
        if self.broker_ip.is_multicast() || self.broker_ip == Ipv4Addr::BROADCAST {
            return Err("Credential broker IP must be a unicast IPv4 address".to_string());
        }
        Ok(())
    }

    pub fn validate_for_bridge(&self, bridge: &BridgeConfig) -> Result<(), String> {
        self.validate()?;
        let gateway = bridge.gateway_ipv4()?;
        if self.broker_ip != gateway {
            return Err(format!(
                "Credential broker IP must be the host-side bridge address {} for subnet {}, got {}",
                gateway, bridge.subnet, self.broker_ip
            ));
        }
        Ok(())
    }

    pub fn broker_cidr(&self) -> String {
        format!("{}/32", self.broker_ip)
    }

    pub fn proxy_url(&self) -> String {
        format!("http://{}:{}", self.broker_ip, self.broker_port)
    }

    /// Standard proxy variables for HTTP API CLIs that honor proxy settings.
    ///
    /// These values are not secrets; they only point at the broker endpoint.
    pub fn proxy_environment(&self) -> Vec<(String, String)> {
        if !self.inject_proxy_env {
            return Vec::new();
        }

        let url = self.proxy_url();
        ["HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy"]
            .into_iter()
            .map(|key| (key.to_string(), url.clone()))
            .collect()
    }

    pub fn egress_policy(&self) -> EgressPolicy {
        EgressPolicy::credential_broker_only(self)
    }
}

/// Network protocol for port forwarding rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Protocol {
    Tcp,
    Udp,
}

impl Protocol {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Tcp => "tcp",
            Self::Udp => "udp",
        }
    }
}

impl std::fmt::Display for Protocol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Port forwarding rule
#[derive(Debug, Clone)]
pub struct PortForward {
    /// Optional host bind IP address. When omitted, match all local addresses.
    pub host_ip: Option<Ipv4Addr>,
    /// Host port
    pub host_port: u16,
    /// Container port
    pub container_port: u16,
    /// Protocol (tcp/udp)
    pub protocol: Protocol,
}

impl PortForward {
    /// Parse a port forward spec like:
    /// - "8080:80"
    /// - "8080:80/udp"
    /// - "127.0.0.1:8080:80"
    /// - "127.0.0.1:8080:80/udp"
    pub fn parse(spec: &str) -> crate::error::Result<Self> {
        let (ports, protocol) = if let Some((p, proto)) = spec.rsplit_once('/') {
            let protocol = match proto {
                "tcp" => Protocol::Tcp,
                "udp" => Protocol::Udp,
                _ => {
                    return Err(crate::error::NucleusError::ConfigError(format!(
                        "Invalid protocol '{}', must be tcp or udp",
                        proto
                    )))
                }
            };
            (p, protocol)
        } else {
            (spec, Protocol::Tcp)
        };

        let parts: Vec<&str> = ports.split(':').collect();
        let (host_ip, host_port, container_port) = match parts.as_slice() {
            [host_port, container_port] => (None, *host_port, *container_port),
            [host_ip, host_port, container_port] => {
                validate_ipv4_addr(host_ip).map_err(crate::error::NucleusError::ConfigError)?;
                let host_ip = host_ip.parse::<Ipv4Addr>().map_err(|_| {
                    crate::error::NucleusError::ConfigError(format!(
                        "Invalid host IP address: {}",
                        host_ip
                    ))
                })?;
                (Some(host_ip), *host_port, *container_port)
            }
            _ => {
                return Err(crate::error::NucleusError::ConfigError(format!(
                    "Invalid port forward format '{}', expected HOST:CONTAINER or HOST_IP:HOST:CONTAINER",
                    spec
                )))
            }
        };

        let host_port: u16 = host_port.parse().map_err(|_| {
            crate::error::NucleusError::ConfigError(format!("Invalid host port: {}", host_port))
        })?;
        let container_port: u16 = container_port.parse().map_err(|_| {
            crate::error::NucleusError::ConfigError(format!(
                "Invalid container port: {}",
                container_port
            ))
        })?;

        Ok(Self {
            host_ip,
            host_port,
            container_port,
            protocol,
        })
    }
}

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

    #[test]
    fn test_port_forward_parse() {
        let pf = PortForward::parse("8080:80").unwrap();
        assert_eq!(pf.host_ip, None);
        assert_eq!(pf.host_port, 8080);
        assert_eq!(pf.container_port, 80);
        assert_eq!(pf.protocol, Protocol::Tcp);

        let pf = PortForward::parse("5353:53/udp").unwrap();
        assert_eq!(pf.host_ip, None);
        assert_eq!(pf.host_port, 5353);
        assert_eq!(pf.container_port, 53);
        assert_eq!(pf.protocol, Protocol::Udp);

        let pf = PortForward::parse("127.0.0.1:8080:80").unwrap();
        assert_eq!(pf.host_ip, Some(Ipv4Addr::new(127, 0, 0, 1)));
        assert_eq!(pf.host_port, 8080);
        assert_eq!(pf.container_port, 80);
        assert_eq!(pf.protocol, Protocol::Tcp);

        let pf = PortForward::parse("10.0.0.5:5353:53/udp").unwrap();
        assert_eq!(pf.host_ip, Some(Ipv4Addr::new(10, 0, 0, 5)));
        assert_eq!(pf.host_port, 5353);
        assert_eq!(pf.container_port, 53);
        assert_eq!(pf.protocol, Protocol::Udp);
    }

    #[test]
    fn test_port_forward_parse_invalid() {
        assert!(PortForward::parse("8080").is_err());
        assert!(PortForward::parse("abc:80").is_err());
        assert!(PortForward::parse("8080:abc").is_err());
        assert!(PortForward::parse("127.0.0.1:abc:80").is_err());
        assert!(PortForward::parse("999.0.0.1:8080:80").is_err());
    }

    #[test]
    fn test_validate_ipv4_addr_rejects_leading_zeros() {
        assert!(validate_ipv4_addr("10.0.42.1").is_ok());
        assert!(validate_ipv4_addr("0.0.0.0").is_ok());
        assert!(
            validate_ipv4_addr("010.0.0.1").is_err(),
            "leading zero in first octet must be rejected"
        );
        assert!(
            validate_ipv4_addr("10.01.0.1").is_err(),
            "leading zero in second octet must be rejected"
        );
        assert!(
            validate_ipv4_addr("10.0.01.1").is_err(),
            "leading zero in third octet must be rejected"
        );
        assert!(
            validate_ipv4_addr("10.0.0.01").is_err(),
            "leading zero in fourth octet must be rejected"
        );
    }
}