File size: 2,507 Bytes
bb5a19b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! Convert resolved filesystem denials into portable policy entries.

use crate::FilesystemConstraints;
use codex_protocol::permissions::FileSystemAccessMode;
use codex_protocol::permissions::FileSystemPath;
use codex_protocol::permissions::FileSystemSandboxEntry;
use codex_protocol::permissions::FileSystemSandboxPolicy;
use codex_utils_path_uri::LegacyAppPathString;
use codex_utils_path_uri::LegacyAppPathStringError;
use codex_utils_path_uri::PathConvention;
use codex_utils_path_uri::PathUri;
use std::io;

impl FilesystemConstraints {
    /// Adds denials without changing existing entries or other policy settings.
    /// `convention` must match the executor facts used to resolve these requirements.
    /// Literal paths become URIs; glob spellings and entry order are preserved.
    /// Invalid denials return an error before the policy is changed.
    pub fn apply_to_policy(
        &self,
        policy: &mut FileSystemSandboxPolicy,
        convention: PathConvention,
    ) -> io::Result<()> {
        let entries = self
            .deny_read
            .iter()
            .map(|deny_read| {
                PathUri::validate_config_path_text(deny_read.as_str(), convention)?;
                let path = if deny_read.contains_glob() {
                    FileSystemPath::GlobPattern {
                        pattern: deny_read.as_str().to_string(),
                    }
                } else {
                    let mut path = LegacyAppPathString::from_string(deny_read.as_str())
                        .to_path_uri(convention)?;
                    path.validate_config_path(convention)?;
                    // Match Core's existing URI spelling for UNC share roots.
                    if path.to_url().host_str().is_some() {
                        path = path.join(".")?;
                    }
                    path.into()
                };
                Ok(FileSystemSandboxEntry::new(
                    path,
                    FileSystemAccessMode::Deny,
                ))
            })
            .collect::<Result<Vec<_>, LegacyAppPathStringError>>()
            .map_err(|error| {
                io::Error::new(
                    io::ErrorKind::InvalidInput,
                    format!("invalid permissions.filesystem.deny_read path: {error}"),
                )
            })?;
        for entry in entries {
            if !policy.entries.contains(&entry) {
                policy.entries.push(entry);
            }
        }
        Ok(())
    }
}