firebrick-daemon 0.4.0

The fbkd daemon that manages Firebrick microVM sandboxes for 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
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
//! Network settings of sandboxes: removes the network device of a sandbox whose network is
//! disabled, and otherwise turns the egress rules of a spec into microsandbox's network policy,
//! with TLS interception and an HTTP deny response that tells the user how to allow a blocked
//! host.

use firebrick_spec::{NetworkRule, NetworkSpec};
use microsandbox::MicrosandboxError;
use microsandbox::sandbox::{SandboxBuilder, SandboxSpec};
use microsandbox_network::policy::{
    Action, NetworkPolicy, Rule, RuleBuilder, RuleDestinationBuilder,
};
use std::net::IpAddr;
use url::Host;

/// Body of the `403 Forbidden` response to a denied HTTP or HTTPS request. microsandbox
/// replaces `{host}` with the denied host.
pub const DENY_MESSAGE: &str = "firebrick blocked the connection to {host}: the network policy \
     of this sandbox doesn't allow it. To allow it, run `fbk network allow {host}` on the host, \
     outside the sandbox.";

/// Label that records the network settings a sandbox was created with, so an update that
/// doesn't change them can leave the sandbox alone.
pub const RULES_LABEL: &str = "firebrick.network";

/// Returns the value of [`RULES_LABEL`] for the network settings. A disabled network ignores
/// its rules, so it is recorded as `disabled`. Rules that aren't enforced don't change the
/// sandbox, so they are all recorded as `off`.
pub fn rules_label(network: &NetworkSpec) -> String {
    if !network.is_enabled() {
        return "disabled".to_string();
    }

    if !network.enforce {
        return "off".to_string();
    }

    let join = |rules: &[NetworkRule]| {
        rules
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(",")
    };

    format!(
        "allow={};deny={}",
        join(&network.allow),
        join(&network.deny)
    )
}

/// Returns the egress rules of a sandbox from its stored config. A sandbox whose label can't be
/// parsed, or one created before [`RULES_LABEL`] existed that has a network policy, counts as
/// enforced without allow rules, so callers that check a host against the rules fail closed.
pub fn rules_of(spec: &SandboxSpec) -> NetworkSpec {
    let unknown = || NetworkSpec {
        enforce: true,
        ..NetworkSpec::default()
    };

    match spec.labels.get(RULES_LABEL) {
        Some(label) => rules_from_label(label).unwrap_or_else(unknown),
        None if spec.network.policy.is_some() => unknown(),
        None => NetworkSpec::default(),
    }
}

/// Parses a value of [`RULES_LABEL`] back into rules. A disabled network comes back disabled
/// and enforced without rules, so it allows no host. Returns `None` for a value that
/// [`rules_label`] can't have written.
pub fn rules_from_label(value: &str) -> Option<NetworkSpec> {
    match value {
        "off" => return Some(NetworkSpec::default()),
        "disabled" => {
            return Some(NetworkSpec {
                enabled: Some(false),
                enforce: true,
                ..NetworkSpec::default()
            });
        }
        _ => {}
    }

    let (allow, deny) = value.strip_prefix("allow=")?.split_once(";deny=")?;

    Some(NetworkSpec {
        enforce: true,
        allow: parse_rules(allow)?,
        deny: parse_rules(deny)?,
        ..NetworkSpec::default()
    })
}

/// Parses a comma-separated list of rules, which may be empty.
fn parse_rules(value: &str) -> Option<Vec<NetworkRule>> {
    value
        .split(',')
        .filter(|rule| !rule.is_empty())
        .map(|rule| rule.parse().ok())
        .collect()
}

/// Whether the rules let the sandbox connect to the host, matched like [`policy`]: a matching
/// deny rule wins, then a matching allow rule, and anything else is denied. Rules that aren't
/// enforced allow every host. Domain rules only match host names and IP and CIDR rules only IP
/// addresses, because fbkd doesn't resolve names.
pub fn allows_host(network: &NetworkSpec, host: &Host<&str>) -> bool {
    let matches = |rule: &NetworkRule| rule_matches(rule, host);

    !network.enforce || (!network.deny.iter().any(matches) && network.allow.iter().any(matches))
}

/// Whether the rule's destination is the host.
fn rule_matches(rule: &NetworkRule, host: &Host<&str>) -> bool {
    match (rule, host) {
        (NetworkRule::Domain(domain), Host::Domain(name)) => {
            name.trim_end_matches('.').eq_ignore_ascii_case(domain)
        }
        (NetworkRule::DomainSuffix(suffix), Host::Domain(name)) => {
            is_same_or_subdomain(name.trim_end_matches('.'), suffix)
        }
        (NetworkRule::Ip(address), Host::Ipv4(ip)) => *address == IpAddr::V4(*ip),
        (NetworkRule::Ip(address), Host::Ipv6(ip)) => *address == IpAddr::V6(*ip),
        (NetworkRule::Cidr { address, prefix }, Host::Ipv4(ip)) => {
            in_cidr(*address, *prefix, IpAddr::V4(*ip))
        }
        (NetworkRule::Cidr { address, prefix }, Host::Ipv6(ip)) => {
            in_cidr(*address, *prefix, IpAddr::V6(*ip))
        }
        _ => false,
    }
}

/// Whether the name is the domain or one of its subdomains.
fn is_same_or_subdomain(name: &str, domain: &str) -> bool {
    let name = name.to_ascii_lowercase();
    let domain = domain.to_ascii_lowercase();

    name == domain || name.ends_with(&format!(".{domain}"))
}

/// Whether the IP address is in the network `address/prefix` of the same family.
fn in_cidr(address: IpAddr, prefix: u8, ip: IpAddr) -> bool {
    match (address, ip) {
        (IpAddr::V4(network), IpAddr::V4(ip)) => {
            same_prefix(network.to_bits().into(), ip.to_bits().into(), prefix, 32)
        }
        (IpAddr::V6(network), IpAddr::V6(ip)) => {
            same_prefix(network.to_bits(), ip.to_bits(), prefix, 128)
        }
        _ => false,
    }
}

/// Whether the first `prefix` of the `width` bits of both addresses are equal.
fn same_prefix(network: u128, ip: u128, prefix: u8, width: u32) -> bool {
    let shift = width.saturating_sub(u32::from(prefix));

    network.checked_shr(shift).unwrap_or(0) == ip.checked_shr(shift).unwrap_or(0)
}

/// Adds the network settings to a sandbox that is being created. A disabled network removes
/// the sandbox's network device and ignores the rules. Without `enforce`, the sandbox keeps
/// microsandbox's default policy and no TLS interception.
pub fn add_to_builder(
    builder: SandboxBuilder,
    network: &NetworkSpec,
) -> Result<SandboxBuilder, MicrosandboxError> {
    if !network.is_enabled() {
        return Ok(builder.disable_network());
    }

    if !network.enforce {
        return Ok(builder);
    }

    let policy = policy(network)?;

    // `tls(|t| t)` intercepts TLS on port 443, so HTTPS requests can get the deny response too.
    Ok(builder.network(|n| {
        n.policy(policy)
            .tls(|t| t)
            .http(|h| h.deny_response(true).deny_message(DENY_MESSAGE))
    }))
}

/// Returns the policy that enforces the rules: all deny rules, then all allow rules, then DNS
/// to the sandbox's resolver, and deny for any other egress. Rules match first-come, so a deny
/// rule wins over an allow rule regardless of where either is written. microsandbox also
/// matches domain rules against DNS queries, so a host denied by a domain rule doesn't
/// resolve, while every other name does.
pub fn policy(network: &NetworkSpec) -> Result<NetworkPolicy, MicrosandboxError> {
    let mut policy = NetworkPolicy::builder()
        .default_egress(Action::Deny)
        .egress(|r| {
            for rule in &network.deny {
                add_rule(r.deny(), rule);
            }
            for rule in &network.allow {
                add_rule(r.allow(), rule);
            }
            r
        })
        .build()?;

    policy.rules.push(Rule::allow_dns());

    Ok(policy)
}

/// Commits a rule with the destination of the spec's rule.
fn add_rule<'a>(
    destination: RuleDestinationBuilder<'a>,
    rule: &NetworkRule,
) -> &'a mut RuleBuilder {
    match rule {
        NetworkRule::Domain(domain) => destination.domain(domain),
        NetworkRule::DomainSuffix(domain) => destination.domain_suffix(domain),
        NetworkRule::Ip(address) => destination.ip(address.to_string()),
        NetworkRule::Cidr { .. } => destination.cidr(rule.to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::secrets::Secret;
    use microsandbox::sandbox::{Sandbox, SandboxConfig};
    use microsandbox_network::policy::{Destination, Direction};

    fn rules(values: &[&str]) -> Vec<NetworkRule> {
        values.iter().map(|value| value.parse().unwrap()).collect()
    }

    fn enforced_without_rules() -> NetworkSpec {
        enforced(&[], &[])
    }

    fn enforced(allow: &[&str], deny: &[&str]) -> NetworkSpec {
        NetworkSpec {
            enforce: true,
            allow: rules(allow),
            deny: rules(deny),
            ..NetworkSpec::default()
        }
    }

    fn disabled(network: NetworkSpec) -> NetworkSpec {
        NetworkSpec {
            enabled: Some(false),
            ..network
        }
    }

    /// Returns the config microsandbox would create a sandbox with after `add_to_builder` and the
    /// secrets.
    async fn config_with(network: &NetworkSpec, secrets: &[Secret]) -> SandboxConfig {
        let builder = Sandbox::builder("fbk-unit-network").image("alpine");
        let builder = add_to_builder(builder, network).unwrap();

        crate::secrets::add_to_builder(builder, secrets)
            .build()
            .await
            .unwrap()
    }

    /// Describes an egress rule as `<action> <destination>`.
    fn describe(rule: &Rule) -> String {
        assert_eq!(rule.direction, Direction::Egress);

        let action = match rule.action {
            Action::Allow => "allow",
            Action::Deny => "deny",
        };
        let destination = match &rule.destination {
            Destination::Domain(domain) => format!("domain {domain}"),
            Destination::DomainSuffix(domain) => format!("suffix {domain}"),
            Destination::Cidr(network) => format!("cidr {network}"),
            other => format!("{other:?}"),
        };

        format!("{action} {destination}")
    }

    fn allows(network: &NetworkSpec, host: &str) -> bool {
        let url = url::Url::parse(&format!("http://{host}/")).unwrap();
        allows_host(network, &url.host().unwrap())
    }

    #[test]
    fn rules_from_label_reads_what_rules_label_writes() {
        let enforced = enforced(&["github.com", "*.npmjs.org", "10.0.0.0/8"], &["1.2.3.4"]);
        let no_rules = enforced_without_rules();

        for network in [enforced, no_rules, NetworkSpec::default()] {
            assert_eq!(rules_from_label(&rules_label(&network)), Some(network));
        }
    }

    #[test]
    fn rules_from_label_rejects_other_values() {
        assert_eq!(rules_from_label(""), None);
        assert_eq!(rules_from_label("allow=github.com"), None);
        assert_eq!(rules_from_label("allow=not a rule;deny="), None);
    }

    #[test]
    fn disabled_network_allows_no_host() {
        let network = rules_from_label(&rules_label(&disabled(NetworkSpec::default()))).unwrap();

        assert!(!network.is_enabled());
        assert!(!allows(&network, "example.com"));
    }

    #[test]
    fn allows_every_host_when_not_enforced() {
        assert!(allows(&NetworkSpec::default(), "anything.example"));
        assert!(allows(&NetworkSpec::default(), "10.1.2.3"));
    }

    #[test]
    fn allows_hosts_that_match_an_allow_rule_only() {
        let network = enforced(&["github.com", "*.npmjs.org"], &[]);

        assert!(allows(&network, "github.com"));
        assert!(allows(&network, "GitHub.com."));
        assert!(allows(&network, "npmjs.org"));
        assert!(allows(&network, "registry.npmjs.org"));
        assert!(!allows(&network, "api.github.com"));
        assert!(!allows(&network, "evilnpmjs.org"));
        assert!(!allows(&network, "attacker.example"));
    }

    #[test]
    fn deny_rules_win_over_allow_rules() {
        let network = enforced(&["*.github.com"], &["gist.github.com"]);

        assert!(allows(&network, "api.github.com"));
        assert!(!allows(&network, "gist.github.com"));
    }

    #[test]
    fn ip_rules_match_ip_hosts_only() {
        let network = enforced(&["140.82.112.0/20", "2001:db8::1"], &["140.82.112.4"]);

        assert!(allows(&network, "140.82.113.1"));
        assert!(!allows(&network, "140.82.112.4"));
        assert!(!allows(&network, "140.82.128.1"));
        assert!(allows(&network, "[2001:db8::1]"));
        assert!(!allows(&network, "[2001:db8::2]"));
    }

    #[test]
    fn rules_label_lists_enforced_rules() {
        assert_eq!(
            rules_label(&enforced(&["github.com", "*.npmjs.org"], &["10.0.0.0/8"])),
            "allow=github.com,*.npmjs.org;deny=10.0.0.0/8"
        );
        assert_eq!(rules_label(&enforced(&[], &[])), "allow=;deny=");
    }

    #[test]
    fn rules_label_ignores_rules_that_are_not_enforced() {
        let off = NetworkSpec {
            enforce: false,
            ..enforced(&["github.com"], &[])
        };

        assert_eq!(rules_label(&off), "off");
        assert_eq!(rules_label(&NetworkSpec::default()), "off");
    }

    #[test]
    fn rules_label_records_a_disabled_network_without_rules() {
        assert_eq!(
            rules_label(&disabled(enforced(&["github.com"], &[]))),
            "disabled"
        );
        assert_eq!(rules_label(&disabled(NetworkSpec::default())), "disabled");
    }

    #[test]
    fn rules_label_of_an_explicitly_enabled_network_matches_the_default() {
        let enabled = |network: NetworkSpec| NetworkSpec {
            enabled: Some(true),
            ..network
        };

        assert_eq!(rules_label(&enabled(NetworkSpec::default())), "off");
        assert_eq!(
            rules_label(&enabled(enforced(&["github.com"], &[]))),
            "allow=github.com;deny="
        );
    }

    #[tokio::test]
    async fn disabled_network_removes_the_device_and_ignores_the_rules() {
        let config = config_with(&disabled(enforced(&["github.com"], &[])), &[]).await;

        assert!(!config.spec.network.enabled);
    }

    #[tokio::test]
    async fn secrets_do_not_enable_a_disabled_network() {
        let secret = Secret::new("GH_TOKEN".to_string(), "abc".to_string(), vec![]).unwrap();

        let config = config_with(&disabled(NetworkSpec::default()), &[secret]).await;

        assert!(!config.spec.network.enabled);
    }

    #[tokio::test]
    async fn enabled_network_keeps_the_device() {
        let config = config_with(&enforced(&["github.com"], &[]), &[]).await;

        assert!(config.spec.network.enabled);
    }

    #[test]
    fn policy_denies_egress_by_default() {
        let policy = policy(&enforced(&[], &[])).unwrap();

        assert_eq!(policy.default_egress, Action::Deny);
    }

    #[test]
    fn policy_allows_dns_last() {
        let policy = policy(&enforced(&["github.com"], &["gist.github.com"])).unwrap();

        assert_eq!(policy.rules.len(), 3);
        assert_eq!(
            format!("{:?}", policy.rules[2]),
            format!("{:?}", Rule::allow_dns())
        );
    }

    #[test]
    fn policy_puts_deny_rules_before_allow_rules() {
        let policy = policy(&enforced(
            &["*.github.com", "10.0.0.0/8"],
            &["gist.github.com", "10.1.2.3"],
        ))
        .unwrap();

        let (_dns, rules) = policy.rules.split_last().unwrap();
        let described: Vec<String> = rules.iter().map(describe).collect();

        assert_eq!(
            described,
            [
                "deny domain gist.github.com",
                "deny cidr 10.1.2.3/32",
                "allow suffix github.com",
                "allow cidr 10.0.0.0/8",
            ]
        );
    }

    #[test]
    fn policy_maps_each_rule_form_to_its_destination() {
        let policy = policy(&enforced(
            &[
                "github.com",
                "*.githubusercontent.com",
                "140.82.112.4",
                "2001:db8::1",
                "192.168.10.0/24",
            ],
            &[],
        ))
        .unwrap();

        let (_dns, rules) = policy.rules.split_last().unwrap();
        let described: Vec<String> = rules.iter().map(describe).collect();

        assert_eq!(
            described,
            [
                "allow domain github.com",
                "allow suffix githubusercontent.com",
                "allow cidr 140.82.112.4/32",
                "allow cidr 2001:db8::1/128",
                "allow cidr 192.168.10.0/24",
            ]
        );
    }

    #[test]
    fn deny_message_names_the_host_and_the_command() {
        assert_eq!(
            DENY_MESSAGE.replace("{host}", "example.org"),
            "firebrick blocked the connection to example.org: the network policy of this \
             sandbox doesn't allow it. To allow it, run `fbk network allow example.org` on the \
             host, outside the sandbox."
        );
    }
}