mxc-sdk 1.2.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Builds the policy preparation result for ProcessContainer: restricts the
//! parsed request, projects the restriction back onto the request JSON the
//! application authored, and checks that the candidate reproduces it.

use crate::mxc_common::models::{
    CaptureDenialsMode, ExecutionRequest, LearningModeCapability, NetworkAction,
};
use crate::mxc_common::mxc_error::{MxcError, MxcErrorCode};
use crate::mxc_common::policy_failures::PolicyFailures;
use crate::mxc_common::policy_preparation::{collect_changes, PreparedPolicyRequest};
use crate::mxc_common::splice::serialize_preserving_unchanged_members;
use crate::process_container_common::base_container_runner::BaseContainerRunner;
use crate::process_container_common::working_directory::{
    try_launch_working_directory, LaunchWorkingDirectory,
};
use serde_json::Value;
use std::collections::HashSet;

/// Prepare a candidate for `request`, which was parsed from `original_json`.
/// `reparse` parses candidate JSON through the same contract and options as
/// the original request.
pub(crate) fn prepare_candidate(
    original_json: &str,
    request: &ExecutionRequest,
    failures: &PolicyFailures,
    reparse: impl FnOnce(&str) -> Option<ExecutionRequest>,
) -> Result<PreparedPolicyRequest, MxcError> {
    let restricted = BaseContainerRunner::prepare_policy_request(request, failures)
        .map_err(backend_preparation_error)?;
    let original: Value = serde_json::from_str(original_json)
        .map_err(|_| MxcError::malformed_request("invalid policy preparation request JSON"))?;
    let (candidate, original_cwd) = project_request(&original, request, &restricted)?;
    let candidate_json = match (original.as_object(), candidate.as_object()) {
        (Some(original), Some(candidate)) => {
            serialize_preserving_unchanged_members(original_json, original, candidate)
        }
        _ => None,
    }
    .ok_or_else(projection_error)?;
    let reparsed = reparse(&candidate_json).ok_or_else(projection_error)?;
    BaseContainerRunner::verify_prepared_policy(&restricted, &reparsed)
        .map_err(backend_preparation_error)?;
    if resolve_launch_directory(&reparsed)?.path != original_cwd {
        return Err(projection_error());
    }
    let mut changes = Vec::new();
    collect_changes("", Some(&original), Some(&candidate), &mut changes)?;
    Ok(PreparedPolicyRequest {
        request_json: if changes.is_empty() {
            original_json.to_owned()
        } else {
            candidate_json
        },
        changes,
    })
}

// Backend messages can quote request data; preparation reports only the category.
fn backend_preparation_error(mut error: MxcError) -> MxcError {
    error.message = match error.code {
        MxcErrorCode::BackendUnavailable => {
            "native Windows ProcessContainer cannot enforce policy preparation on this host"
        }
        _ => "policy preparation cannot safely enforce the requested policy",
    }
    .to_owned();
    error.remediation = None;
    error
}

fn projection_error() -> MxcError {
    MxcError::policy_validation(
        "the restrictive policy cannot be represented safely in the request contract",
    )
}

fn resolve_launch_directory(
    request: &ExecutionRequest,
) -> Result<LaunchWorkingDirectory, MxcError> {
    try_launch_working_directory(request).map_err(|_| {
        MxcError::policy_validation(
            "policy preparation cannot resolve the launch directory safely; check filesystem availability",
        )
    })
}

fn is_reserved_learning_capability(entry: &str) -> bool {
    LearningModeCapability::from_capability_name(entry).is_some()
}

fn path_key(path: &str) -> String {
    path.trim_end_matches('\\').to_ascii_lowercase()
}

/// Returns the candidate request JSON and the launch directory of the
/// original request, which the candidate must keep.
fn project_request(
    original: &Value,
    before: &ExecutionRequest,
    after: &ExecutionRequest,
) -> Result<(Value, String), MxcError> {
    let mut json = original.clone();
    let process_container = if original.get("appContainer").is_some() {
        "appContainer"
    } else {
        "processContainer"
    };
    let action = |value| {
        Value::String(
            match value {
                NetworkAction::Allow => "allow",
                NetworkAction::Deny => "deny",
            }
            .to_owned(),
        )
    };
    let previous_denies: HashSet<_> = before.policy.denied_paths.iter().collect();
    let newly_denied: HashSet<_> = after
        .policy
        .denied_paths
        .iter()
        .filter(|path| !previous_denies.contains(path))
        .map(|path| path_key(path))
        .collect();
    let no_removals = HashSet::new();
    for (name, old, new) in [
        (
            "readwritePaths",
            &before.policy.readwrite_paths,
            &after.policy.readwrite_paths,
        ),
        (
            "readonlyPaths",
            &before.policy.readonly_paths,
            &after.policy.readonly_paths,
        ),
        (
            "deniedPaths",
            &before.policy.denied_paths,
            &after.policy.denied_paths,
        ),
    ] {
        let authored = original.pointer(&format!("/filesystem/{name}"));
        let removed = if name == "deniedPaths" {
            &no_removals
        } else {
            &newly_denied
        };
        let has_suppressed_removal = authored.and_then(Value::as_array).is_some_and(|paths| {
            paths
                .iter()
                .filter_map(Value::as_str)
                .any(|path| removed.contains(&path_key(path)))
        });
        if old != new || has_suppressed_removal {
            set(
                &mut json,
                &["filesystem", name],
                Some(project_path_list(authored, old, new, removed)?),
            )?;
        }
    }
    if before.policy.enumerate_paths != after.policy.enumerate_paths {
        set(
            &mut json,
            &[process_container, "filesystem", "enumeratePaths"],
            Some(project_path_list(
                original.pointer(&format!("/{process_container}/filesystem/enumeratePaths")),
                &before.policy.enumerate_paths,
                &after.policy.enumerate_paths,
                &no_removals,
            )?),
        )?;
    }
    let authorable_capabilities = |entries: &[String]| {
        entries
            .iter()
            .filter(|entry| !is_reserved_learning_capability(entry))
            .cloned()
            .collect::<Vec<_>>()
    };
    let old = authorable_capabilities(&before.policy.capabilities);
    let new = authorable_capabilities(&after.policy.capabilities);
    if old != new {
        set(
            &mut json,
            &[process_container, "capabilities"],
            Some(Value::from(new)),
        )?;
    }
    if after.policy.capture_denials.is_none()
        && !after.policy.capabilities.iter().any(|entry| {
            LearningModeCapability::from_capability_name(entry)
                == Some(LearningModeCapability::Learning)
        })
        && original
            .get(process_container)
            .and_then(|section| section.get("learningMode"))
            == Some(&Value::Bool(true))
    {
        set(
            &mut json,
            &[process_container, "learningMode"],
            Some(Value::Bool(false)),
        )?;
    }
    if before.policy.network_egress != after.policy.network_egress {
        let old = before
            .policy
            .network_egress
            .as_ref()
            .ok_or_else(projection_error)?;
        let new = after
            .policy
            .network_egress
            .as_ref()
            .ok_or_else(projection_error)?;
        if old.default != new.default {
            set(
                &mut json,
                &["network", "egress", "default"],
                Some(action(new.default)),
            )?;
        }
        if old.allow != new.allow {
            if !new.allow.is_empty() {
                return Err(projection_error());
            }
            set(
                &mut json,
                &["network", "egress", "allow"],
                Some(Value::Array(Vec::new())),
            )?;
        }
        if old.deny != new.deny {
            return Err(projection_error());
        }
    }
    if before.policy.network_ingress != after.policy.network_ingress {
        let old = before
            .policy
            .network_ingress
            .as_ref()
            .ok_or_else(projection_error)?;
        let new = after
            .policy
            .network_ingress
            .as_ref()
            .ok_or_else(projection_error)?;
        if old.default != new.default {
            set(
                &mut json,
                &["network", "ingress", "default"],
                Some(action(new.default)),
            )?;
        }
        if old.host_loopback != new.host_loopback {
            set(
                &mut json,
                &["network", "ingress", "hostLoopback"],
                Some(action(new.host_loopback)),
            )?;
        }
    }
    if before.policy.network_proxy.is_enabled() && !after.policy.network_proxy.is_enabled() {
        set(&mut json, &["runtimeConfig", "networkProxy"], None)?;
    }
    if before.policy.allowed_proxy_peer != after.policy.allowed_proxy_peer {
        set(
            &mut json,
            &[process_container, "network", "allowedProxyPeer"],
            after.policy.allowed_proxy_peer.clone().map(Value::String),
        )?;
    }
    match (
        &before.policy.capture_denials,
        &after.policy.capture_denials,
    ) {
        (Some(_), None) => set(&mut json, &[process_container, "captureDenials"], None)?,
        (Some(old), Some(new)) if old.mode != new.mode => {
            set(
                &mut json,
                &[process_container, "captureDenials", "mode"],
                Some(Value::String(
                    match new.mode {
                        CaptureDenialsMode::Block => "block",
                        CaptureDenialsMode::Allow => "allow",
                    }
                    .to_owned(),
                )),
            )?;
        }
        (None, Some(_)) => return Err(projection_error()),
        _ => {}
    }
    let original_cwd = resolve_launch_directory(before)?;
    let candidate_cwd = resolve_launch_directory(after)?;
    if original_cwd.path != candidate_cwd.path {
        set(
            &mut json,
            &["process", "cwd"],
            Some(Value::String(original_cwd.path.clone())),
        )?;
    }
    Ok((json, original_cwd.path))
}

fn project_path_list(
    authored: Option<&Value>,
    before: &[String],
    after: &[String],
    removed: &HashSet<String>,
) -> Result<Value, MxcError> {
    use std::collections::HashMap;
    fn counts(paths: &[String]) -> HashMap<&str, usize> {
        let mut counts = HashMap::new();
        for path in paths {
            *counts.entry(path.as_str()).or_insert(0usize) += 1;
        }
        counts
    }
    let old_counts = counts(before);
    let new_counts = counts(after);
    let mut remaining = new_counts.clone();
    let mut projected = Vec::new();
    if let Some(value) = authored.filter(|value| !value.is_null()) {
        for value in value.as_array().ok_or_else(projection_error)? {
            let path = value.as_str().ok_or_else(projection_error)?;
            // Normalization can suppress entries already covered by more
            // restrictive lists. Preserve that unrelated authored data.
            if !old_counts.contains_key(path) {
                if !removed.contains(&path_key(path)) {
                    projected.push(value.clone());
                }
            } else if let Some(count) = remaining.get_mut(path).filter(|count| **count > 0) {
                *count -= 1;
                projected.push(value.clone());
            }
        }
    }
    let mut seen = HashMap::new();
    for path in after {
        let count = seen.entry(path.as_str()).or_insert(0usize);
        *count += 1;
        if *count > old_counts.get(path.as_str()).copied().unwrap_or(0) {
            projected.push(Value::String(path.clone()));
        }
    }
    Ok(Value::Array(projected))
}

fn set(json: &mut Value, path: &[&str], value: Option<Value>) -> Result<(), MxcError> {
    let (last, parents) = path.split_last().ok_or_else(projection_error)?;
    let mut current = json;
    for parent in parents {
        let object = current.as_object_mut().ok_or_else(projection_error)?;
        current = object
            .entry((*parent).to_owned())
            .or_insert_with(|| Value::Object(Default::default()));
        if current.is_null() {
            *current = Value::Object(Default::default());
        }
    }
    let object = current.as_object_mut().ok_or_else(projection_error)?;
    match value {
        Some(value) => {
            object.insert((*last).to_owned(), value);
        }
        None => {
            object.remove(*last);
        }
    }
    Ok(())
}

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

    #[test]
    fn projection_keeps_suppressed_authoring_when_only_runtime_paths_change() {
        let before = vec!["work".into(), "work".into()];
        let after = vec!["work".into()];
        let authored = json!(["work", "denied", "work", "enumerated", "denied"]);
        assert_eq!(
            project_path_list(Some(&authored), &before, &after, &HashSet::new()).unwrap(),
            json!(["work", "denied", "enumerated", "denied"]),
        );
        let authored = json!(["read", "denied", "read"]);
        assert_eq!(
            project_path_list(
                Some(&authored),
                &["read".into(), "read".into()],
                &["read".into(), "work".into()],
                &HashSet::new(),
            )
            .unwrap(),
            json!(["read", "denied", "work"]),
        );
        assert_eq!(
            project_path_list(Some(&Value::Null), &[], &["work".into()], &HashSet::new()).unwrap(),
            json!(["work"]),
        );
    }
}