httpjail 0.6.2

Monitor and restrict HTTP/HTTPS requests from processes
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! Docker container execution wrapped in Linux jail network isolation

use super::LinuxJail;
use crate::jail::{Jail, JailConfig};
use crate::sys_resource::{ManagedResource, SystemResource};
use anyhow::{Context, Result};
use std::process::{Command, ExitStatus};
use tracing::{debug, info, warn};

/// Docker network resource that gets cleaned up on drop
struct DockerNetwork {
    network_name: String,
}

impl DockerNetwork {
    const NETWORK_PREFIX: &'static str = "httpjail_";

    /// Generate network name from jail ID
    fn network_name_from_jail_id(jail_id: &str) -> String {
        format!("{}{}", Self::NETWORK_PREFIX, jail_id)
    }

    /// Check if a Docker command failed due to resource not existing
    fn is_not_found_error(stderr: &str) -> bool {
        stderr.contains("not found")
            || stderr.contains("No such")
            || stderr.contains("does not exist")
    }

    /// Check if a Docker command failed due to resource already existing
    fn is_already_exists_error(stderr: &str) -> bool {
        stderr.contains("already exists")
    }
}

/// Docker routing nftables resource that gets cleaned up on drop
struct DockerRoutingTable {
    #[allow(dead_code)]
    jail_id: String,
    table_name: String,
}

impl DockerRoutingTable {
    /// Generate table name from jail ID
    fn table_name_from_jail_id(jail_id: &str) -> String {
        format!("httpjail_docker_{}", jail_id)
    }
}

impl SystemResource for DockerRoutingTable {
    fn create(jail_id: &str) -> Result<Self> {
        Ok(Self {
            jail_id: jail_id.to_string(),
            table_name: Self::table_name_from_jail_id(jail_id),
        })
    }

    fn cleanup(&mut self) -> Result<()> {
        debug!("Cleaning up Docker routing table: {}", self.table_name);

        for family in ["inet", "ip"] {
            let output = Command::new("/usr/sbin/nft")
                .args(["delete", "table", family, &self.table_name])
                .output()
                .context("Failed to delete Docker routing table")?;
            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                if !DockerNetwork::is_not_found_error(&stderr) {
                    anyhow::bail!("Failed to delete Docker routing table: {}", stderr);
                }
            } else {
                info!(
                    "Removed Docker {} routing table {}",
                    family, self.table_name
                );
            }
        }

        Ok(())
    }

    fn for_existing(jail_id: &str) -> Self {
        Self {
            jail_id: jail_id.to_string(),
            table_name: Self::table_name_from_jail_id(jail_id),
        }
    }
}

// The nftables guard is installed on this host, so Docker must use its local daemon.
// Ignore caller-selected contexts; a remote daemon would bypass the bridge guard.
fn local_docker_command() -> Command {
    let mut cmd = Command::new("/usr/bin/docker");
    cmd.env_remove("DOCKER_HOST")
        .env_remove("DOCKER_CONTEXT")
        .env_remove("DOCKER_CONFIG")
        .arg("--host=unix:///var/run/docker.sock");
    cmd
}

impl SystemResource for DockerNetwork {
    fn create(jail_id: &str) -> Result<Self> {
        let network_name = Self::network_name_from_jail_id(jail_id);

        // Create Docker network with no default gateway (isolated)
        // Using a /24 subnet in the 172.20.x.x range
        let subnet = Self::compute_docker_subnet(jail_id);

        let output = local_docker_command()
            .args([
                "network",
                "create",
                "--driver",
                "bridge",
                "--subnet",
                &subnet,
                "--opt",
                "com.docker.network.bridge.enable_ip_masquerade=false",
                &network_name,
            ])
            .output()
            .context("Failed to create Docker network")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if Self::is_already_exists_error(&stderr) {
                info!("Docker network {} already exists", network_name);
            } else {
                anyhow::bail!("Failed to create Docker network: {}", stderr);
            }
        } else {
            info!(
                "Created Docker network {} with subnet {}",
                network_name, subnet
            );
        }

        Ok(Self { network_name })
    }

    fn cleanup(&mut self) -> Result<()> {
        debug!("Cleaning up Docker network: {}", self.network_name);

        let output = local_docker_command()
            .args(["network", "rm", &self.network_name])
            .output()
            .context("Failed to remove Docker network")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if Self::is_not_found_error(&stderr) {
                debug!("Docker network {} already removed", self.network_name);
            } else {
                anyhow::bail!("Failed to remove Docker network: {}", stderr);
            }
        } else {
            info!("Removed Docker network {}", self.network_name);
        }

        Ok(())
    }

    fn for_existing(jail_id: &str) -> Self {
        Self {
            network_name: Self::network_name_from_jail_id(jail_id),
        }
    }
}

impl DockerNetwork {
    /// Compute a unique Docker subnet for this jail (172.20.x.0/24)
    fn compute_docker_subnet(jail_id: &str) -> String {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        jail_id.hash(&mut hasher);
        let h = hasher.finish();
        let third_octet = ((h % 256) as u8).max(1); // 1-255
        format!("172.20.{}.0/24", third_octet)
    }

    /// Get the Docker bridge interface name for this network
    fn get_bridge_name(&self) -> Result<String> {
        let output = local_docker_command()
            .args(["network", "inspect", &self.network_name, "-f", "{{.Id}}"])
            .output()
            .context("Failed to inspect Docker network")?;

        if !output.status.success() {
            anyhow::bail!("Failed to get Docker network ID");
        }

        let network_id = String::from_utf8_lossy(&output.stdout)
            .trim()
            .chars()
            .take(12)
            .collect::<String>();

        let bridge = format!("br-{}", network_id);
        anyhow::ensure!(
            std::path::Path::new("/sys/class/net")
                .join(&bridge)
                .exists(),
            "Docker bridge {bridge} is not present on the local host"
        );
        Ok(bridge)
    }
}

/// DockerLinux jail implementation that combines Docker containers with Linux jail isolation
///
/// This jail wraps the standard LinuxJail to provide network isolation for Docker containers.
/// Unlike the previous approach, this implementation:
///
/// 1. Creates a complete Linux jail with network namespace
/// 2. Creates an isolated Docker network with no default connectivity
/// 3. Uses nftables on the host to route traffic from Docker network to jail
/// 4. Runs containers in the isolated Docker network
///
/// The implementation reuses all LinuxJail networking, nftables, and resource management
/// while adding Docker-specific network creation and routing.
pub struct DockerLinux {
    /// The underlying Linux jail that provides network isolation
    inner_jail: LinuxJail,
    /// Configuration for the jail
    config: JailConfig,
    /// The Docker network resource
    docker_network: Option<ManagedResource<DockerNetwork>>,
    /// The Docker routing table resource
    docker_routing: Option<ManagedResource<DockerRoutingTable>>,
}

impl DockerLinux {
    /// Create a new DockerLinux jail
    pub fn new(config: JailConfig) -> Result<Self> {
        let inner_jail = LinuxJail::new(config.clone())?;
        Ok(Self {
            inner_jail,
            config,
            docker_network: None,
            docker_routing: None,
        })
    }

    fn container_name(&self) -> String {
        format!("httpjail_{}_run", self.config.jail_id)
    }

    /// Only options that cannot change the container's network, privileges, or host access.
    const SAFE_VALUE_FLAGS: &'static [&'static str] = &[
        "-e",
        "--env",
        "--entrypoint",
        "-w",
        "--workdir",
        "-u",
        "--user",
        "--pull",
        "--memory",
        "--cpus",
        "--label",
    ];
    const SAFE_BOOL_FLAGS: &'static [&'static str] = &[
        "--rm",
        "--read-only",
        "--init",
        "--no-healthcheck",
        "-i",
        "-t",
    ];

    /// Build the docker command with isolated network
    #[allow(clippy::collapsible_if)]
    fn build_docker_command(
        &self,
        docker_args: &[String],
        extra_env: &[(String, String)],
    ) -> Result<(Command, Option<tempfile::TempDir>)> {
        let network_name = DockerNetwork::network_name_from_jail_id(&self.config.jail_id);
        let image_idx = Self::find_image_index(docker_args)?;
        let docker_opts = &docker_args[..image_idx];
        let image = &docker_args[image_idx];
        let user_command = &docker_args[image_idx + 1..];

        // Build the docker run command
        let mut cmd = local_docker_command();
        cmd.arg("run");

        // Route DNS to the dummy resolver, never Docker's host-configured resolver.
        let host_ip =
            super::format_ip(LinuxJail::compute_host_ip_for_jail_id(&self.config.jail_id));
        let container_name = self.container_name();
        cmd.args([
            "--network",
            &network_name,
            "--dns",
            &host_ip,
            "--name",
            &container_name,
            "--cap-drop=NET_RAW",
            "--rm",
        ]);

        // Mount only a public certificate snapshot, never the CA key directory.
        let mut ca_cert_path = None;
        for (key, value) in extra_env {
            if key == "SSL_CERT_DIR" {
                continue; // That directory also holds the CA private key.
            }
            cmd.arg("-e").arg(format!("{}={}", key, value));
            if key == "SSL_CERT_FILE" && ca_cert_path.is_none() {
                ca_cert_path = Some(value.clone());
            }
        }

        // Docker resolves bind sources later. A separate local actor could change
        // the original path in between, so mount a root-private regular-file copy.
        let mut cert_dir = None;
        if let Some(cert_path) = ca_cert_path {
            use std::io::Read;
            use std::os::unix::fs::OpenOptionsExt;
            let cert = std::fs::OpenOptions::new()
                .read(true)
                .custom_flags(libc::O_NOFOLLOW)
                .open(&cert_path)
                .context("Failed to open public CA certificate")?;
            let metadata = cert.metadata()?;
            anyhow::ensure!(
                metadata.is_file() && metadata.len() < 1024 * 1024,
                "Invalid public CA certificate"
            );
            let mut pem = String::new();
            cert.take(1024 * 1024).read_to_string(&mut pem)?;
            anyhow::ensure!(
                pem.trim_start().starts_with("-----BEGIN CERTIFICATE-----")
                    && pem.contains("-----END CERTIFICATE-----")
                    && !pem.contains("PRIVATE KEY"),
                "Invalid public CA certificate"
            );
            let dir = if unsafe { libc::geteuid() == 0 } {
                let root_dir = std::path::Path::new("/var/lib/httpjail");
                crate::jail::ensure_trusted_root_dir(root_dir)?;
                tempfile::Builder::new()
                    .prefix("cert-")
                    .tempdir_in(root_dir)
            } else {
                tempfile::tempdir()
            }
            .context("Failed to create protected CA mount directory")?;
            let snapshot = dir.path().join("ca-cert.pem");
            std::fs::write(&snapshot, pem).context("Failed to write public CA snapshot")?;
            cmd.arg("-v")
                .arg(format!("{}:{}:ro", snapshot.display(), cert_path));
            cert_dir = Some(dir);
            cmd.env_remove("SSL_CERT_DIR");
        }

        // Add user's docker options
        for opt in docker_opts {
            cmd.arg(opt);
        }

        // Add the image
        cmd.arg(image);

        // Add user command if provided
        for arg in user_command {
            cmd.arg(arg);
        }

        Ok((cmd, cert_dir))
    }

    /// Find the image while rejecting Docker options that could bypass isolation.
    /// Everything after the image belongs to the container command, not Docker.
    fn find_image_index(args: &[String]) -> Result<usize> {
        let mut i = 0;
        while let Some(arg) = args.get(i) {
            if !arg.starts_with('-') {
                anyhow::ensure!(!arg.is_empty(), "Docker image must not be empty");
                return Ok(i);
            }
            if Self::SAFE_BOOL_FLAGS.contains(&arg.as_str()) {
                i += 1;
                continue;
            }
            let (flag, inline_value) = arg
                .split_once('=')
                .map_or((arg.as_str(), None), |(name, value)| (name, Some(value)));
            anyhow::ensure!(
                Self::SAFE_VALUE_FLAGS.contains(&flag),
                "Docker option not permitted in an isolated jail: {}",
                arg
            );
            if let Some(value) = inline_value {
                anyhow::ensure!(
                    !value.is_empty(),
                    "Missing value for Docker option {}",
                    flag
                );
                i += 1;
            } else {
                let value = args.get(i + 1).context("Missing Docker option value")?;
                anyhow::ensure!(
                    !value.starts_with('-'),
                    "Invalid value for Docker option {}",
                    flag
                );
                i += 2;
            }
        }
        anyhow::bail!("Could not find Docker image in arguments")
    }

    /// Setup nftables rules to route Docker network traffic to jail
    fn setup_docker_routing(&mut self) -> Result<()> {
        let docker_network = self
            .docker_network
            .as_ref()
            .context("Docker network not created")?;

        if let Some(network) = docker_network.inner() {
            let bridge_name = network.get_bridge_name()?;

            // Get the jail's veth host IP
            let host_ip = LinuxJail::compute_host_ip_for_jail_id(&self.config.jail_id);
            let host_ip_str = super::format_ip(host_ip);

            info!(
                "Setting up routing from Docker bridge {} to jail at {}",
                bridge_name, host_ip_str
            );

            // Add nftables rules to:
            // 1. Allow traffic from Docker network to jail's proxy ports
            // 2. DNAT HTTP/HTTPS traffic to the proxy
            let table_name = DockerRoutingTable::table_name_from_jail_id(&self.config.jail_id);

            // HTTP(S) is DNATed to a local host listener (INPUT). Forwarded
            // bridge traffic is denied by the single inet guard below.
            let nft_rules = format!(
                r#"table ip {table_name} {{
                    chain prerouting {{
                        type nat hook prerouting priority -100;
                        iifname "{bridge_name}" tcp dport 80 dnat to {host_ip_str}:{http_port};
                        iifname "{bridge_name}" tcp dport 443 dnat to {host_ip_str}:{https_port};
                    }}
                }}"#,
                http_port = self.config.http_proxy_port,
                https_port = self.config.https_proxy_port,
            );

            // Docker bridge traffic to its host gateway takes INPUT, not FORWARD.
            // This inet guard blocks both IPv4 and IPv6 outside proxy and dummy DNS.
            let guard = format!(
                r#"table inet {table_name} {{
                    chain input {{
                        type filter hook input priority -5; policy accept;
                        iifname "{bridge_name}" ip saddr {docker_subnet} ip daddr {host_ip_str} tcp dport {{ {http_port}, {https_port} }} accept
                        iifname "{bridge_name}" ip saddr {docker_subnet} ip daddr {host_ip_str} udp dport 53 accept
                        iifname "{bridge_name}" drop
                    }}
                    chain forward {{
                        type filter hook forward priority -5; policy accept;
                        iifname "{bridge_name}" drop
                    }}
                }}"#,
                table_name = table_name,
                bridge_name = bridge_name,
                docker_subnet = DockerNetwork::compute_docker_subnet(&self.config.jail_id),
                host_ip_str = host_ip_str,
                http_port = self.config.http_proxy_port,
                https_port = self.config.https_proxy_port,
            );
            let nft_rules = format!("{nft_rules}\n{guard}");

            // Apply the rules
            let mut nft_cmd = Command::new("/usr/sbin/nft");
            nft_cmd.arg("-f").arg("-");
            nft_cmd.stdin(std::process::Stdio::piped());

            let mut child = nft_cmd.spawn().context("Failed to spawn nft command")?;

            if let Some(mut stdin) = child.stdin.take() {
                use std::io::Write;
                stdin
                    .write_all(nft_rules.as_bytes())
                    .context("Failed to write nftables rules")?;
            }

            let status = child.wait().context("Failed to wait for nft command")?;

            if !status.success() {
                anyhow::bail!("Failed to apply nftables rules for Docker routing");
            }

            info!("Docker routing rules applied successfully");

            // Store the routing table as a managed resource for cleanup
            // Note: We create the resource AFTER applying the rules
            self.docker_routing = Some(ManagedResource::<DockerRoutingTable>::create(
                &self.config.jail_id,
            )?);
        }

        Ok(())
    }
}

impl Jail for DockerLinux {
    fn setup(&mut self, proxy_port: u16) -> Result<()> {
        // A missing canary cannot prove an older jail is dead: it may live
        // beneath another sudo-preserved HOME. Never delete its network here.

        // First setup the inner Linux jail
        self.inner_jail.setup(proxy_port)?;

        // Create the Docker network
        self.docker_network = Some(ManagedResource::<DockerNetwork>::create(
            &self.config.jail_id,
        )?);

        // Setup routing from Docker network to jail
        self.setup_docker_routing()?;

        info!("DockerLinux jail setup complete with Docker network isolation");
        Ok(())
    }

    fn execute(&self, command: &[String], extra_env: &[(String, String)]) -> Result<ExitStatus> {
        info!("Executing Docker container in isolated network");

        // Build and execute the docker command
        let (cmd, _cert_dir) = self.build_docker_command(command, extra_env)?;

        debug!("Docker command: {:?}", cmd);

        // Execute docker run and wait for it to complete
        crate::jail::run_command(cmd, None).context("Failed to execute docker run command")
    }

    fn execute_with_timeout(
        &self,
        command: &[String],
        extra_env: &[(String, String)],
        timeout: std::time::Duration,
    ) -> Result<ExitStatus> {
        let (cmd, _cert_dir) = self.build_docker_command(command, extra_env)?;
        let status = crate::jail::run_command(cmd, Some(timeout))
            .context("Failed to execute docker run command")?;
        if status.code() == Some(124) {
            // Killing the Docker CLI does not necessarily stop its daemon-owned container.
            let output = local_docker_command()
                .args(["rm", "-f", &self.container_name()])
                .output()
                .context("Failed to stop timed-out Docker container")?;
            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                anyhow::ensure!(
                    DockerNetwork::is_not_found_error(&stderr),
                    "Failed to stop timed-out Docker container: {}",
                    stderr
                );
            }
        }
        Ok(status)
    }

    fn cleanup(&self) -> Result<()> {
        // The command may exit via process::exit and a signal handler holds a
        // clone, so do not rely on Drop to clean up daemon-owned resources.
        // Remove the network before its guard; on failure retain both and the
        // canary so a later orphan cleanup can retry without exposing traffic.
        Self::cleanup_orphaned(&self.config.jail_id)
    }

    fn jail_id(&self) -> &str {
        self.inner_jail.jail_id()
    }

    fn cleanup_orphaned(jail_id: &str) -> Result<()>
    where
        Self: Sized,
    {
        // Keep the bridge firewall until Docker confirms the network is gone.
        DockerNetwork::for_existing(jail_id).cleanup()?;
        DockerRoutingTable::for_existing(jail_id).cleanup()?;
        LinuxJail::cleanup_orphaned(jail_id)
    }
}

impl Drop for DockerLinux {
    fn drop(&mut self) {
        // Never remove the firewall guard while Docker still reports an active
        // network: a surviving container would regain direct host/Internet access.
        if let Some(mut network) = self.docker_network.take() {
            let result = network.inner_mut().map_or(Ok(()), SystemResource::cleanup);
            if let Err(error) = result {
                warn!(
                    "Retaining Docker bridge firewall after network cleanup failure: {}",
                    error
                );
                if let Some(guard) = self.docker_routing.take() {
                    std::mem::forget(guard);
                }
            }
        }
        // Otherwise the routing guard is removed after the network.
    }
}

impl Clone for DockerLinux {
    fn clone(&self) -> Self {
        Self {
            inner_jail: self.inner_jail.clone(),
            config: self.config.clone(),
            docker_network: None,
            docker_routing: None,
        }
    }
}

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

    #[test]
    fn docker_mounts_only_public_ca_file() {
        let dir = tempfile::tempdir().unwrap();
        let cert = dir.path().join("ca-cert.pem");
        std::fs::write(
            &cert,
            b"-----BEGIN CERTIFICATE-----\npublic\n-----END CERTIFICATE-----\n",
        )
        .unwrap();
        std::fs::write(dir.path().join("ca-key.pem"), b"dummy private key").unwrap();
        let cert_path = cert.to_string_lossy().to_string();
        let parent = dir.path().to_string_lossy().to_string();
        let jail = DockerLinux::new(crate::jail::JailConfig::new()).unwrap();
        let (cmd, _cert_dir) = jail
            .build_docker_command(
                &["alpine:latest".to_string()],
                &[
                    ("SSL_CERT_FILE".to_string(), cert_path.clone()),
                    ("SSL_CERT_DIR".to_string(), parent.clone()),
                ],
            )
            .unwrap();
        let args = cmd
            .get_args()
            .map(|arg| arg.to_string_lossy().into_owned())
            .collect::<Vec<_>>();
        let mount = args
            .iter()
            .find(|arg| arg.ends_with(&format!(":{cert_path}:ro")))
            .unwrap();
        let source = mount.strip_suffix(&format!(":{cert_path}:ro")).unwrap();
        assert_ne!(source, cert_path);
        assert!(
            std::fs::read_to_string(source)
                .unwrap()
                .contains("BEGIN CERTIFICATE")
        );
        assert!(!args.contains(&format!("{parent}:{parent}:ro")));
        assert!(!args.iter().any(|arg| arg.starts_with("SSL_CERT_DIR=")));

        std::fs::remove_file(&cert).unwrap();
        std::os::unix::fs::symlink("ca-key.pem", &cert).unwrap();
        assert!(
            std::fs::read_to_string(source)
                .unwrap()
                .contains("BEGIN CERTIFICATE")
        );
        assert!(
            jail.build_docker_command(
                &["alpine:latest".to_string()],
                &[("SSL_CERT_FILE".to_string(), cert_path)]
            )
            .is_err()
        );
    }

    #[test]
    fn docker_options_cannot_override_isolation() {
        let args = |parts: &[&str]| parts.iter().map(|s| (*s).to_string()).collect::<Vec<_>>();
        assert_eq!(
            DockerLinux::find_image_index(&args(&[
                "--rm",
                "-e",
                "FOO=bar",
                "alpine",
                "--net=other"
            ]))
            .unwrap(),
            3
        );
        for option in [
            "--net=other",
            "--network=host",
            "--privileged",
            "--dns=8.8.8.8",
            "-v",
        ] {
            assert!(
                DockerLinux::find_image_index(&args(&[option, "alpine"])).is_err(),
                "unsafe Docker option {option} was accepted"
            );
        }
    }
}