File size: 2,453 Bytes
52a9af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::windows_codex_system_dir;
use std::path::Path;

/// Coarse compatibility result for the default Windows system-config namespace.
#[derive(Clone, Copy)]
pub enum WindowsSystemConfigNamespaceProbe {
    Missing,
    Expected,
    UnexpectedType,
    UnexpectedOwner,
    StandardUserMutationAcl,
    CheckError,
}

/// Observes the common user-preclaimed-directory shape without changing config loading.
/// This is not a trust decision: it only checks directory owner and broad
/// standard-user mutation ACLs.
pub fn probe_windows_system_config_namespace() -> WindowsSystemConfigNamespaceProbe {
    let codex_dir = windows_codex_system_dir();
    let has_system_file = match (
        codex_dir.join("config.toml").try_exists(),
        codex_dir.join("requirements.toml").try_exists(),
    ) {
        (Ok(true), _) | (_, Ok(true)) => true,
        (Ok(false), Ok(false)) => false,
        _ => return WindowsSystemConfigNamespaceProbe::CheckError,
    };
    if !has_system_file {
        return WindowsSystemConfigNamespaceProbe::Missing;
    }
    let Some(openai_dir) = codex_dir.parent() else {
        return WindowsSystemConfigNamespaceProbe::CheckError;
    };
    let result = probe_directory(
        openai_dir,
        codex_windows_sandbox::path_has_standard_user_mutation_allow,
    );
    if !matches!(result, WindowsSystemConfigNamespaceProbe::Expected) {
        return result;
    }
    let result = probe_directory(
        codex_dir.as_path(),
        codex_windows_sandbox::path_or_child_file_has_standard_user_mutation_allow,
    );
    if !matches!(result, WindowsSystemConfigNamespaceProbe::Expected) {
        return result;
    }
    WindowsSystemConfigNamespaceProbe::Expected
}

fn probe_directory(
    path: &Path,
    mutation_allow: fn(&Path) -> anyhow::Result<bool>,
) -> WindowsSystemConfigNamespaceProbe {
    if !path.is_dir() {
        return WindowsSystemConfigNamespaceProbe::UnexpectedType;
    }
    match codex_windows_sandbox::path_has_trusted_system_owner(path) {
        Ok(true) => {}
        Ok(false) => return WindowsSystemConfigNamespaceProbe::UnexpectedOwner,
        Err(_) => return WindowsSystemConfigNamespaceProbe::CheckError,
    }
    match mutation_allow(path) {
        Ok(false) => WindowsSystemConfigNamespaceProbe::Expected,
        Ok(true) => WindowsSystemConfigNamespaceProbe::StandardUserMutationAcl,
        Err(_) => WindowsSystemConfigNamespaceProbe::CheckError,
    }
}