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

//! macOS filesystem and environment discovery for Seatbelt profile generation.

use std::fs;
use std::path::{Path, PathBuf};

use crate::mxc_common::models::ExecutionRequest;
use crate::seatbelt_common::profile_builder::{ProfileError, ProfileInputs};

const DEVELOPER_DIR_LINKS: [&str; 2] = [
    "/private/var/db/xcode_select_link",
    "/private/var/select/developer_dir",
];

pub(crate) fn prepare_profile(request: &ExecutionRequest) -> Result<ProfileInputs, ProfileError> {
    if request
        .seatbelt
        .as_ref()
        .is_some_and(|config| config.profile_override.is_some())
    {
        return Ok(ProfileInputs::default());
    }

    let developer_dir = active_developer_dir()
        .map(|dir| developer_dir_grant_root(&dir))
        .and_then(|dir| dir.into_os_string().into_string().ok());
    let denied_paths = resolve_all(&request.policy.denied_paths)?;
    let readonly_paths = resolve_all(&request.policy.readonly_paths)?;
    let readwrite_paths = resolve_all(&request.policy.readwrite_paths)?;
    let user_keychains = if request.seatbelt.as_ref().is_some_and(|c| c.keychain_access) {
        let home = std::env::var("HOME").map_err(|_| {
            ProfileError::Host(
                "HOME environment variable not set; cannot expand '~/Library/Keychains' for \
                 keychainAccess"
                    .to_string(),
            )
        })?;
        Some(user_keychains_dir(&home)?)
    } else {
        None
    };

    Ok(ProfileInputs {
        readonly_paths,
        readwrite_paths,
        denied_paths,
        developer_dir,
        user_keychains,
        capture_tag: None,
    })
}

fn resolve_all(paths: &[String]) -> Result<Vec<String>, ProfileError> {
    paths.iter().map(|p| resolve_policy_path(p)).collect()
}

/// Seatbelt matches real paths, so a symlinked HOME must be resolved.
fn user_keychains_dir(home: &str) -> Result<String, ProfileError> {
    resolve_policy_path(&format!("{home}/Library/Keychains"))
}

/// Follow symlinks before applying `..`, retaining missing trailing names.
pub(crate) fn resolve_policy_path(path: &str) -> Result<String, ProfileError> {
    let expanded = expand_tilde(path).map_err(ProfileError::Host)?;
    let absolute = std::path::absolute(&expanded).map_err(|error| {
        ProfileError::Host(format!(
            "Filesystem path '{path}' could not be resolved: {error}"
        ))
    })?;
    let resolved = crate::mxc_common::filesystem_symlink::resolve_through_symlinks(&absolute)
        .ok_or_else(|| {
            ProfileError::Policy(format!("Filesystem path '{path}' has no resolved target"))
        })?;
    if let Some(error) = resolved.error {
        return Err(classify_io_error(
            format!("Filesystem path '{path}' could not be resolved: {error}"),
            &error,
        ));
    }
    resolved.path.into_os_string().into_string().map_err(|_| {
        ProfileError::Policy(format!(
            "Filesystem path '{path}' resolves to a non-UTF-8 path"
        ))
    })
}

/// Errors a different path would avoid blame the request; anything else is a host fault.
pub(crate) fn classify_io_error(message: String, error: &std::io::Error) -> ProfileError {
    if crate::mxc_common::filesystem_symlink::is_policy_path_error(error) {
        ProfileError::Policy(message)
    } else {
        ProfileError::Host(message)
    }
}

pub(crate) fn expand_tilde(path: &str) -> Result<String, String> {
    if path == "~" || path.starts_with("~/") {
        let home = std::env::var("HOME").map_err(|_| {
            format!("HOME environment variable not set; cannot expand '{path}' in seatbelt profile")
        })?;
        if path == "~" {
            Ok(home)
        } else {
            Ok(format!("{home}/{}", &path[2..]))
        }
    } else {
        Ok(path.to_string())
    }
}

/// Ignore caller-controlled DEVELOPER_DIR; only trust the system's selection.
fn active_developer_dir() -> Option<PathBuf> {
    DEVELOPER_DIR_LINKS
        .iter()
        .map(Path::new)
        .filter(|link| link_is_root_controlled(link))
        .filter_map(|link| fs::read_link(link).ok())
        .find(|target| target.is_absolute() && target.is_dir())
}

/// Replacing a link is controlled by its parent, not just the link's owner.
fn link_is_root_controlled(link: &Path) -> bool {
    use std::os::unix::fs::{MetadataExt as _, PermissionsExt as _};

    let Ok(link_meta) = fs::symlink_metadata(link) else {
        return false;
    };
    if !link_meta.file_type().is_symlink() || link_meta.uid() != 0 {
        return false;
    }
    link.ancestors().skip(1).all(|dir| {
        fs::metadata(dir)
            .is_ok_and(|meta| dir_is_root_controlled(meta.uid(), meta.permissions().mode()))
    })
}

fn dir_is_root_controlled(uid: u32, mode: u32) -> bool {
    uid == 0 && mode & 0o022 == 0
}

/// Xcode shims also load frameworks outside Contents/Developer.
fn developer_dir_grant_root(dir: &Path) -> PathBuf {
    enclosing_app_bundle(dir).map_or_else(|| dir.to_path_buf(), Path::to_path_buf)
}

fn enclosing_app_bundle(dir: &Path) -> Option<&Path> {
    if dir.file_name()? != "Developer" {
        return None;
    }
    let contents = dir.parent()?;
    if contents.file_name()? != "Contents" {
        return None;
    }
    let bundle = contents.parent()?;
    bundle
        .extension()?
        .eq_ignore_ascii_case("app")
        .then_some(bundle)
}

/// Expands `~`, validates the output path, and adds `.json` when extensionless.
pub(crate) fn prepare_capture_output_path(
    request: &ExecutionRequest,
) -> Result<Option<String>, ProfileError> {
    let Some(path) = request
        .seatbelt
        .as_ref()
        .and_then(|config| config.capture_denials.as_ref())
        .and_then(|capture| capture.output_path.as_deref())
    else {
        return Ok(None);
    };
    let mut expanded = expand_tilde(path).map_err(ProfileError::Host)?;
    crate::learning_mode_core::capture_output::validate_output_path(
        "seatbelt.captureDenials.outputPath",
        Path::new(&expanded),
    )
    .map_err(ProfileError::Policy)?;
    if Path::new(&expanded).extension().is_none() {
        expanded = Path::new(&expanded)
            .with_extension("json")
            .to_string_lossy()
            .into_owned();
    }
    Ok(Some(expanded))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_common::models::SeatbeltConfig;
    use crate::seatbelt_common::profile_builder::build_profile_with_proxy;

    #[test]
    fn lexical_spellings_normalize_to_the_same_rule() {
        for spelling in [
            "/tmp/secret",
            "//tmp/secret",
            "/./tmp/secret",
            "/tmp//secret",
            "/tmp/./secret/",
            "///tmp/secret//",
        ] {
            assert_eq!(
                resolve_policy_path(spelling).unwrap(),
                "/private/tmp/secret",
                "spelling {spelling} must normalize"
            );
        }
    }

    #[test]
    fn lexical_normalization_is_idempotent_and_preserves_root() {
        assert_eq!(resolve_policy_path("/").unwrap(), "/");
        assert_eq!(resolve_policy_path("//").unwrap(), "/");
        let once = resolve_policy_path("//tmp/./secret/").unwrap();
        assert_eq!(resolve_policy_path(&once).unwrap(), once);
    }

    #[test]
    fn parent_segments_follow_symlinked_roots() {
        for (path, expected) in [
            ("/private/var/../tmp", "/private/tmp"),
            ("/tmp/..", "/private"),
            ("/..", "/"),
            ("/a/b/../c", "/a/c"),
        ] {
            assert_eq!(resolve_policy_path(path).unwrap(), expected);
        }
        assert_eq!(
            resolve_policy_path("/tmp/..secret").unwrap(),
            "/private/tmp/..secret"
        );
    }

    #[test]
    fn user_symlinks_resolve_before_parent_segments_and_missing_tails() {
        let dir = tempfile::tempdir().unwrap();
        let root = fs::canonicalize(dir.path()).unwrap();
        fs::create_dir_all(root.join("real/nested")).unwrap();
        let link = root.join("link");
        std::os::unix::fs::symlink("real/nested", &link).unwrap();
        for suffix in ["../secret", "missing/../../secret"] {
            let path = link.join(suffix);
            assert_eq!(
                resolve_policy_path(path.to_str().unwrap()).unwrap(),
                root.join("real/secret").to_str().unwrap()
            );
        }
    }

    #[test]
    fn unresolved_symlinks_fail_before_parent_segments_can_hide_them() {
        let dir = tempfile::tempdir().unwrap();
        for (name, target) in [("dangling", "missing"), ("loop", "loop")] {
            let link = dir.path().join(name);
            std::os::unix::fs::symlink(target, &link).unwrap();
            for suffix in ["", "child", "../secret"] {
                let path = link.join(suffix);
                let error = resolve_policy_path(path.to_str().unwrap()).unwrap_err();
                assert!(
                    matches!(&error, ProfileError::Policy(message) if message.contains("could not be resolved")),
                    "{error:?}"
                );
            }
        }
    }

    #[test]
    fn prepared_aliases_keep_the_strictest_profile_rules() {
        let dir = tempfile::tempdir().unwrap();
        let target = fs::canonicalize(dir.path()).unwrap().join("target");
        fs::write(&target, "secret").unwrap();
        let link = dir.path().join("alias");
        std::os::unix::fs::symlink("target", &link).unwrap();
        let mut request = ExecutionRequest::default();
        request.policy.readonly_paths = vec![target.to_str().unwrap().into()];
        request.policy.readwrite_paths = vec![link.to_str().unwrap().into()];
        let inputs = prepare_profile(&request).unwrap();
        let profile = build_profile_with_proxy(&request, None, &inputs).unwrap();
        let filter = format!("(subpath \"{}\")", target.display());
        assert!(profile.contains(&filter));
        assert!(!profile.contains("(allow file-read* file-write* network-bind network-outbound"));

        request.policy.denied_paths = vec![link.to_str().unwrap().into()];
        let inputs = prepare_profile(&request).unwrap();
        let profile = build_profile_with_proxy(&request, None, &inputs).unwrap();
        assert!(!profile.contains(";; --- policy.readonlyPaths / policy.readwritePaths"));
        assert!(profile.contains(&filter));
    }

    #[test]
    fn denied_path_with_redundant_spelling_still_denies() {
        let mut request = ExecutionRequest::default();
        request.policy.readwrite_paths = vec!["/tmp".into()];
        request.policy.denied_paths = vec!["//tmp/./secret/".into()];
        let inputs = prepare_profile(&request).unwrap();
        let profile = build_profile_with_proxy(&request, None, &inputs).unwrap();
        let deny = profile
            .find("(deny file-read* file-read-metadata file-write* network-bind network-outbound")
            .unwrap();
        assert!(profile[deny..].contains("(subpath \"/private/tmp/secret\")"));
    }

    #[test]
    fn profile_override_does_not_resolve_ignored_paths() {
        let dir = tempfile::tempdir().unwrap();
        let link = dir.path().join("loop");
        std::os::unix::fs::symlink("loop", &link).unwrap();
        let mut request = ExecutionRequest {
            seatbelt: Some(SeatbeltConfig {
                profile_override: Some("(version 1)\n(allow default)".into()),
                keychain_access: true,
                ..Default::default()
            }),
            ..Default::default()
        };
        request.policy.denied_paths = vec![link.to_str().unwrap().into()];
        let inputs = prepare_profile(&request).unwrap();
        let profile = build_profile_with_proxy(&request, None, &inputs).unwrap();
        assert_eq!(profile, "(version 1)\n(allow default)");
    }

    #[test]
    fn keychains_under_a_symlinked_home_use_the_real_path() {
        let dir = tempfile::tempdir().unwrap();
        let root = fs::canonicalize(dir.path()).unwrap();
        fs::create_dir(root.join("real-home")).unwrap();
        let home = root.join("home");
        std::os::unix::fs::symlink("real-home", &home).unwrap();
        assert_eq!(
            user_keychains_dir(home.to_str().unwrap()).unwrap(),
            root.join("real-home/Library/Keychains").to_str().unwrap()
        );
    }

    #[test]
    fn only_a_root_owned_unwritable_dir_is_trusted() {
        assert!(dir_is_root_controlled(0, 0o755));
        assert!(dir_is_root_controlled(0, 0o700));
        for mode in [0o775, 0o757, 0o777, 0o1777] {
            assert!(!dir_is_root_controlled(0, mode), "mode {mode:o}");
        }
        for uid in [1, 501, 1000] {
            assert!(!dir_is_root_controlled(uid, 0o755), "uid {uid}");
        }
    }

    #[test]
    fn a_link_the_caller_controls_is_not_trusted() {
        use std::os::unix::fs::{symlink, MetadataExt as _};

        let dir = tempfile::tempdir().unwrap();
        fs::create_dir(dir.path().join("target")).unwrap();
        let link = dir.path().join("link");
        symlink(dir.path().join("target"), &link).unwrap();
        if fs::metadata(dir.path()).unwrap().uid() != 0 {
            assert!(!link_is_root_controlled(&link));
        }
        let plain = dir.path().join("plain");
        fs::write(&plain, "").unwrap();
        assert!(!link_is_root_controlled(&plain));
        assert!(!link_is_root_controlled(&dir.path().join("missing")));
    }

    #[test]
    fn the_real_xcode_select_links_are_trusted_when_present() {
        for link in DEVELOPER_DIR_LINKS.iter().map(Path::new) {
            if fs::symlink_metadata(link).is_ok() {
                assert!(
                    link_is_root_controlled(link),
                    "rejected a stock link: {}",
                    link.display()
                );
            }
        }
    }

    #[test]
    fn developer_directory_grants_cover_only_the_enclosing_xcode_bundle() {
        for (dir, expected) in [
            (
                "/Applications/Xcode_26.6.app/Contents/Developer",
                "/Applications/Xcode_26.6.app",
            ),
            (
                "/Library/Developer/CommandLineTools",
                "/Library/Developer/CommandLineTools",
            ),
            (
                "/Applications/Xcode.APP/Contents/Developer",
                "/Applications/Xcode.APP",
            ),
            (
                "/Applications/Xcode-beta.app/Contents/Developer",
                "/Applications/Xcode-beta.app",
            ),
            (
                "/Volumes/Build/Xcode.app/Contents/Developer",
                "/Volumes/Build/Xcode.app",
            ),
            (
                "/Applications/Evil.app/Contents/Developer/usr/bin",
                "/Applications/Evil.app/Contents/Developer/usr/bin",
            ),
            (
                "/Applications/Evil.app/a/b/Developer",
                "/Applications/Evil.app/a/b/Developer",
            ),
            (
                "/Applications/Evil.app/Contents/Developer2",
                "/Applications/Evil.app/Contents/Developer2",
            ),
            (
                "/Applications/Xcode.app/Contents/Developer/",
                "/Applications/Xcode.app",
            ),
            (
                "/Applications/Xcode.app/Contents//Developer",
                "/Applications/Xcode.app",
            ),
            (
                "/Applications/Xcode.app/Contents/./Developer",
                "/Applications/Xcode.app",
            ),
            (
                "/Applications/Xcode.app/Contents/Developer/..",
                "/Applications/Xcode.app/Contents/Developer/..",
            ),
        ] {
            assert_eq!(
                developer_dir_grant_root(Path::new(dir)),
                Path::new(expected)
            );
        }
    }

    #[test]
    fn baseline_profile_grants_the_active_developer_dir() {
        let Some(dir) = active_developer_dir() else {
            return;
        };
        let request = ExecutionRequest::default();
        let inputs = prepare_profile(&request).unwrap();
        let profile = build_profile_with_proxy(&request, None, &inputs).unwrap();
        let granted = developer_dir_grant_root(&dir);
        assert!(profile.contains(&format!("(subpath \"{}\")", granted.display())));
    }

    #[test]
    fn capture_output_path_expands_tilde_then_validates() {
        let with_path = |path: &str| ExecutionRequest {
            seatbelt: Some(SeatbeltConfig {
                capture_denials: Some(crate::mxc_common::models::CaptureDenialsConfig {
                    output_path: Some(path.to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let home = std::env::var("HOME").unwrap();

        assert_eq!(
            prepare_capture_output_path(&with_path("~/denials.json")).unwrap(),
            Some(format!("{home}/denials.json"))
        );
        for path in ["~/denials", "~/denials/"] {
            assert_eq!(
                prepare_capture_output_path(&with_path(path)).unwrap(),
                Some(format!("{home}/denials.json"))
            );
        }
        let directory = tempfile::tempdir().unwrap();
        let error = prepare_capture_output_path(&with_path(directory.path().to_str().unwrap()))
            .unwrap_err();
        assert!(
            matches!(&error, ProfileError::Policy(m) if m.contains("existing directory")),
            "{error}"
        );
        let error = prepare_capture_output_path(&with_path("denials.json")).unwrap_err();
        assert!(
            matches!(&error, ProfileError::Policy(m) if m.contains("must be an absolute path")),
            "{error}"
        );
        let error =
            prepare_capture_output_path(&with_path("~/mxc-missing-dir/denials.json")).unwrap_err();
        assert!(
            matches!(&error, ProfileError::Policy(m) if m.contains("parent directory does not exist")),
            "{error}"
        );
        assert_eq!(
            prepare_capture_output_path(&ExecutionRequest::default()).unwrap(),
            None
        );
    }
}