mxc-sdk 1.0.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use super::*;
use crate::mxc_common::logger::{Logger, Mode};
use crate::mxc_common::models::{
    ContainerPolicy, NetworkEnforcementMode, NetworkPolicy, ProxyAddress, ProxyConfig,
};

fn policy_with_proxy(host: &str, port: u16) -> ContainerPolicy {
    ContainerPolicy {
        network_enforcement_mode: NetworkEnforcementMode::Firewall,
        network_proxy: ProxyConfig {
            address: Some(ProxyAddress::new(host.to_string(), port)),
            builtin_test_server: false,
        },
        ..Default::default()
    }
}

fn apply_and_collect(
    container: &str,
    policy: &ContainerPolicy,
) -> (
    NetworkIptablesManager,
    Vec<Vec<String>>,
    Result<bool, String>,
) {
    let fake = super::test_firewall::install();
    let mut manager = NetworkIptablesManager::new(container, EgressHookPoint::ContainerNetns(4242));
    let mut logger = Logger::new(Mode::Buffer);
    let _ = fake.forget_issued();

    let result = manager.apply_firewall_rules(policy, &mut logger);
    let issued = fake.issued();
    (manager, issued, result)
}

fn appended_rules<'a>(
    issued: &'a [Vec<String>],
    binary: &str,
    chain: &str,
) -> Vec<&'a Vec<String>> {
    issued
        .iter()
        .filter(|argv| {
            argv.first().map(String::as_str) == Some(binary)
                && argv.get(1).map(String::as_str) == Some("-A")
                && argv.get(2).map(String::as_str) == Some(chain)
        })
        .collect()
}

fn action_of(rule: &[String]) -> Option<&str> {
    let index = rule.iter().position(|arg| arg == "-j")?;
    rule.get(index + 1).map(String::as_str)
}

fn has_pair(rule: &[String], flag: &str, value: &str) -> bool {
    rule.windows(2)
        .any(|pair| pair[0] == flag && pair[1] == value)
}

#[test]
fn proxy_mode_forces_a_drop_default_even_when_the_policy_says_allow() {
    assert_eq!(
        NetworkIptablesManager::default_policy_action(NetworkPolicy::Allow, true),
        "DROP"
    );
    assert_eq!(
        NetworkIptablesManager::default_policy_action(NetworkPolicy::Block, true),
        "DROP"
    );
}

#[test]
fn without_a_proxy_the_configured_default_policy_still_decides_the_catch_all() {
    assert_eq!(
        NetworkIptablesManager::default_policy_action(NetworkPolicy::Allow, false),
        "ACCEPT"
    );
    assert_eq!(
        NetworkIptablesManager::default_policy_action(NetworkPolicy::Block, false),
        "DROP"
    );
}

#[test]
fn the_terminal_rule_built_in_proxy_mode_drops() {
    let rule = NetworkIptablesManager::build_default_policy_rule_arg(
        "MXC-proxy-terminal",
        NetworkPolicy::Allow,
        true,
    );

    assert_eq!(action_of(&rule), Some("DROP"), "actual rule: {rule:?}");
}

#[test]
fn an_applied_proxy_chain_ends_in_drop_under_an_allow_default() {
    let mut policy = policy_with_proxy("10.9.8.7", 3128);
    policy.default_network_policy = NetworkPolicy::Allow;

    let (manager, issued, result) = apply_and_collect("proxy-allow-default", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "iptables", manager.chain_name());
    let last = rules.last().expect("the chain must have at least one rule");
    assert_eq!(
        action_of(last),
        Some("DROP"),
        "the last rule appended to a proxied chain must be the closing DROP; actual: {last:?}"
    );
}

#[test]
fn the_proxy_accept_names_the_proxy_address_port_and_protocol() {
    let policy = policy_with_proxy("10.9.8.7", 3128);

    let (manager, issued, result) = apply_and_collect("proxy-shape", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "iptables", manager.chain_name());
    let accepts: Vec<&&Vec<String>> = rules
        .iter()
        .filter(|rule| action_of(rule) == Some("ACCEPT") && !has_pair(rule, "-o", "lo"))
        .collect();

    assert_eq!(
        accepts.len(),
        1,
        "a proxied chain must carry exactly one ACCEPT reaching off-box, for the proxy; \
         actual: {rules:?}"
    );
    let accept = accepts[0];
    assert!(
        has_pair(accept, "-d", "10.9.8.7"),
        "the proxy ACCEPT must name the proxy address; actual: {accept:?}"
    );
    assert!(
        has_pair(accept, "--dport", "3128"),
        "the proxy ACCEPT must name the proxy port; actual: {accept:?}"
    );
    assert!(
        has_pair(accept, "-p", "tcp"),
        "the proxy ACCEPT must be scoped to TCP; actual: {accept:?}"
    );
}

#[test]
fn the_proxy_accept_is_appended_before_the_closing_drop() {
    let policy = policy_with_proxy("10.9.8.7", 3128);

    let (manager, issued, result) = apply_and_collect("proxy-order", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "iptables", manager.chain_name());
    let actions: Vec<Option<&str>> = rules.iter().map(|rule| action_of(rule)).collect();

    assert_eq!(
        actions,
        vec![Some("ACCEPT"), Some("ACCEPT"), Some("DROP")],
        "a proxied chain must read exactly 'keep loopback, accept the proxy, drop the rest'; \
         actual: {rules:?}"
    );
    assert!(
        has_pair(rules[0], "-o", "lo"),
        "loopback comes first; actual: {rules:?}"
    );
    assert!(
        has_pair(rules[1], "-d", "10.9.8.7"),
        "the proxy ACCEPT comes second; actual: {rules:?}"
    );
}

#[test]
fn every_resolved_proxy_address_is_opened() {
    let mut logger = Logger::new(Mode::Buffer);
    let policy = policy_with_proxy("localhost", 8888);

    let (endpoints, _pin) = NetworkIptablesManager::resolve_proxy_endpoints(&policy, &mut logger)
        .expect("localhost must resolve");

    assert!(
        !endpoints.is_empty(),
        "localhost must yield at least one endpoint"
    );
    assert!(
        endpoints.iter().all(|endpoint| endpoint.port == 8888),
        "every endpoint must carry the configured proxy port; actual: {endpoints:?}"
    );
}

#[test]
fn proxy_mode_opens_no_dns_port() {
    let policy = policy_with_proxy("10.9.8.7", 3128);

    let (manager, issued, result) = apply_and_collect("proxy-nodns", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "iptables", manager.chain_name());
    assert!(
        !rules.is_empty(),
        "the proxied chain must have been programmed at all; issued: {issued:?}"
    );

    for rule in rules {
        assert!(
            !has_pair(rule, "--dport", "53"),
            "a proxied chain must not open DNS; actual: {rule:?}"
        );
    }
}

#[test]
fn proxy_mode_keeps_loopback_and_drops_the_conntrack_exemption() {
    let policy = policy_with_proxy("10.9.8.7", 3128);

    let (manager, issued, result) = apply_and_collect("proxy-nobase", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "iptables", manager.chain_name());
    assert!(
        !rules.is_empty(),
        "the proxied chain must have been programmed at all; issued: {issued:?}"
    );

    assert!(
        rules.iter().any(|rule| has_pair(rule, "-o", "lo")),
        "a proxied container must still reach its own loopback; actual: {rules:?}"
    );

    for rule in rules {
        assert!(
            !has_pair(rule, "-i", "lo"),
            "this chain hangs off OUTPUT, where -i never matches; actual: {rule:?}"
        );
        assert!(
            !has_pair(rule, "--state", "ESTABLISHED,RELATED"),
            "a proxied chain must not carry the conntrack exemption; actual: {rule:?}"
        );
    }
}

#[test]
fn proxy_mode_does_not_program_the_allow_list() {
    let mut policy = policy_with_proxy("10.9.8.7", 3128);
    policy.allowed_hosts = vec!["10.1.1.1".to_string()];

    let (manager, issued, result) = apply_and_collect("proxy-nolists", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "iptables", manager.chain_name());
    assert!(
        !rules.is_empty(),
        "the proxied chain must have been programmed at all; issued: {issued:?}"
    );

    for rule in rules {
        assert!(
            !has_pair(rule, "-d", "10.1.1.1"),
            "a proxied chain must ignore the allow list; actual: {rule:?}"
        );
    }
}

#[test]
fn a_proxy_combined_with_a_block_list_is_refused() {
    let mut policy = policy_with_proxy("10.9.8.7", 3128);
    policy.blocked_hosts = vec!["10.2.2.2".to_string()];

    let (_manager, _issued, result) = apply_and_collect("proxy-blocked", &policy);

    let message = result.expect_err("a policy whose block list cannot be enforced must be refused");
    assert!(
        message.contains("blockedHosts"),
        "the refusal must name the setting that cannot be enforced, got: {message}"
    );
}

#[test]
fn the_ipv6_chain_allows_only_loopback_before_its_closing_drop_in_proxy_mode() {
    let policy = policy_with_proxy("10.9.8.7", 3128);

    let (manager, issued, result) = apply_and_collect("proxy-v6", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "ip6tables", manager.chain_name());
    let actions: Vec<Option<&str>> = rules.iter().map(|rule| action_of(rule)).collect();

    assert_eq!(
        actions,
        vec![Some("ACCEPT"), Some("DROP")],
        "the IPv6 chain of a proxied container must deny everything it can route; \
         actual: {rules:?}"
    );
    assert!(
        has_pair(rules[0], "-o", "lo"),
        "the only IPv6 ACCEPT must be the loopback one; actual: {rules:?}"
    );
}

#[test]
fn without_a_proxy_the_base_exemptions_and_host_lists_are_still_programmed() {
    let policy = ContainerPolicy {
        network_enforcement_mode: NetworkEnforcementMode::Firewall,
        allowed_hosts: vec!["10.1.1.1".to_string()],
        ..Default::default()
    };

    let (manager, issued, result) = apply_and_collect("proxy-control", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let rules = appended_rules(&issued, "iptables", manager.chain_name());
    assert!(
        rules.iter().any(|rule| has_pair(rule, "-o", "lo")),
        "a non-proxied chain must still carry the loopback exemption; actual: {rules:?}"
    );
    assert!(
        rules.iter().any(|rule| has_pair(rule, "--dport", "53")),
        "a non-proxied chain must still open DNS; actual: {rules:?}"
    );
    assert!(
        rules.iter().any(|rule| has_pair(rule, "-d", "10.1.1.1")),
        "a non-proxied chain must still program its allow list; actual: {rules:?}"
    );
}

#[test]
fn no_chain_opens_a_dhcp_port() {
    let filtered = ContainerPolicy {
        network_enforcement_mode: NetworkEnforcementMode::Firewall,
        default_network_policy: NetworkPolicy::Block,
        allowed_hosts: vec!["10.1.1.1".to_string()],
        ..Default::default()
    };

    for (name, policy) in [
        ("dhcp-filtered", filtered),
        ("dhcp-proxied", policy_with_proxy("10.9.8.7", 3128)),
    ] {
        let (manager, issued, result) = apply_and_collect(name, &policy);
        assert!(result.is_ok(), "apply must succeed, got {result:?}");

        let mut programmed = false;
        for binary in ["iptables", "ip6tables"] {
            for rule in appended_rules(&issued, binary, manager.chain_name()) {
                programmed = true;
                assert!(
                    !has_pair(rule, "--dport", "67") && !has_pair(rule, "--dport", "547"),
                    "{name} must not open a DHCP port; actual: {rule:?}"
                );
            }
        }
        assert!(
            programmed,
            "{name} never reached the rule builder, so this assertion would hold \
             vacuously; issued: {issued:?}"
        );
    }
}

#[test]
fn an_ipv6_proxy_literal_is_refused_rather_than_silently_dropped() {
    let mut logger = Logger::new(Mode::Buffer);

    for host in ["2001:db8::1", "[2001:db8::1]"] {
        let policy = policy_with_proxy(host, 3128);
        let err = NetworkIptablesManager::resolve_proxy_endpoints(&policy, &mut logger)
            .expect_err("an IPv6 proxy endpoint must be refused");

        assert!(
            err.to_lowercase().contains("ipv6"),
            "the refusal must say IPv6 is the reason, got: {err}"
        );
    }
}

#[test]
fn ipv6_literals_are_recognized_bracketed_or_bare() {
    assert!(
        NetworkIptablesManager::host_is_ipv6_literal("::1"),
        "a bare IPv6 literal must be recognized"
    );
    assert!(
        NetworkIptablesManager::host_is_ipv6_literal("[::1]"),
        "a bracketed IPv6 literal must be recognized"
    );
    assert!(
        NetworkIptablesManager::host_is_ipv6_literal("2001:db8::1"),
        "a full IPv6 literal must be recognized"
    );
    assert!(
        !NetworkIptablesManager::host_is_ipv6_literal("10.9.8.7"),
        "an IPv4 literal must not be classified as IPv6"
    );
    assert!(
        !NetworkIptablesManager::host_is_ipv6_literal("proxy.example.com"),
        "a hostname must not be classified as IPv6"
    );
}

#[test]
fn a_hostname_proxy_records_a_pin_naming_an_authorized_address() {
    let policy = policy_with_proxy("localhost", 8888);

    let (manager, _issued, result) = apply_and_collect("proxy-pin", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    let pin = manager
        .proxy_host_pin()
        .expect("a hostname proxy must record a pin");
    assert_eq!(pin.hostname(), "localhost");
    assert_eq!(pin.ip().to_string(), "127.0.0.1");
}

#[test]
fn an_ip_literal_proxy_records_no_pin() {
    let policy = policy_with_proxy("10.9.8.7", 3128);

    let (manager, _issued, result) = apply_and_collect("proxy-nopin", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    assert!(
        manager.proxy_host_pin().is_none(),
        "an IP-literal proxy needs no hosts entry"
    );
}

#[test]
fn a_policy_without_a_proxy_records_no_pin() {
    let policy = ContainerPolicy {
        network_enforcement_mode: NetworkEnforcementMode::Firewall,
        ..Default::default()
    };

    let (manager, _issued, result) = apply_and_collect("proxy-absent", &policy);
    assert!(result.is_ok(), "apply must succeed, got {result:?}");

    assert!(
        manager.proxy_host_pin().is_none(),
        "a policy without a proxy must not record a hosts-file pin"
    );
}

#[test]
fn a_zero_proxy_port_is_refused() {
    let mut logger = Logger::new(Mode::Buffer);
    let policy = policy_with_proxy("10.9.8.7", 0);

    let err = NetworkIptablesManager::resolve_proxy_endpoints(&policy, &mut logger)
        .expect_err("port 0 must be refused");

    assert!(
        err.to_lowercase().contains("port"),
        "the refusal must name the port as the reason, got: {err}"
    );
}

#[test]
fn an_unresolvable_proxy_host_is_refused() {
    let mut logger = Logger::new(Mode::Buffer);
    let policy = policy_with_proxy("proxy.invalid", 3128);

    let err = NetworkIptablesManager::resolve_proxy_endpoints(&policy, &mut logger)
        .expect_err("an unresolvable proxy host must be refused");

    assert!(
        err.contains("proxy.invalid"),
        "the refusal must name the host that failed, got: {err}"
    );
}

#[test]
fn a_policy_without_a_proxy_resolves_to_no_endpoints() {
    let mut logger = Logger::new(Mode::Buffer);
    let policy = ContainerPolicy::default();

    let (endpoints, pin) = NetworkIptablesManager::resolve_proxy_endpoints(&policy, &mut logger)
        .expect("a policy with no proxy must not be an error");

    assert!(endpoints.is_empty());
    assert!(pin.is_none());
}

#[test]
fn a_proxy_answer_larger_than_the_cap_contributes_no_more_than_the_cap() {
    let addresses: Vec<String> = (0..40).map(|n| format!("203.0.113.{n}")).collect();
    let mut logger = Logger::new(Mode::Buffer);

    let accepted =
        NetworkIptablesManager::bound_proxy_addresses("proxy.invalid", &addresses, &mut logger);

    assert_eq!(
        accepted.len(),
        NetworkIptablesManager::MAX_PROXY_ENDPOINTS,
        "a 40-address answer must not become 40 ACCEPT rules and 40 iptables \
         processes on the container-start path; actual count: {}",
        accepted.len()
    );

    assert_eq!(
        accepted.first().map(String::as_str),
        Some("203.0.113.0"),
        "the pinned address must survive the bound; actual: {:?}",
        accepted.first()
    );

    let buffer = logger.get_buffer();
    assert!(
        buffer.contains("proxy.invalid") && buffer.contains("40"),
        "a trimmed answer must say which host was trimmed and from what size; \
         actual buffer: {buffer:?}"
    );
}

#[test]
fn a_proxy_answer_within_the_cap_is_left_alone() {
    let addresses: Vec<String> = (0..3).map(|n| format!("203.0.113.{n}")).collect();
    let mut logger = Logger::new(Mode::Buffer);

    let accepted =
        NetworkIptablesManager::bound_proxy_addresses("proxy.invalid", &addresses, &mut logger);

    assert_eq!(
        accepted,
        addresses.as_slice(),
        "an answer within the cap must reach the chain intact"
    );
    assert!(
        logger.get_buffer().is_empty(),
        "an untrimmed answer must not warn; actual buffer: {:?}",
        logger.get_buffer()
    );
}