File size: 9,549 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 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 | //! Contracts for layer-owned denial facts, parser scoping, and policy conversion.
use super::ConfigPathContext;
use crate::FilesystemConstraints;
use crate::FilesystemDenyReadPattern;
use crate::RequirementSource;
use crate::RequirementsLayerEntry;
use crate::compose_requirements_for_hostname;
use codex_protocol::permissions::FileSystemAccessMode;
use codex_protocol::permissions::FileSystemPath;
use codex_protocol::permissions::FileSystemSandboxEntry;
use codex_protocol::permissions::FileSystemSandboxKind;
use codex_protocol::permissions::FileSystemSandboxPolicy;
use codex_utils_absolute_path::AbsolutePathBufGuard;
use codex_utils_path_uri::PathConvention;
use codex_utils_path_uri::PathUri;
use pretty_assertions::assert_eq;
use tempfile::tempdir;
#[test]
fn composition_applies_each_layers_base_and_home_to_policy() {
let native_home = tempdir().expect("native home");
let first = RequirementsLayerEntry::from_toml(
RequirementSource::Unknown,
"[permissions.filesystem]\ndeny_read = ['private', '~/secret', 'private', 'C:\\', '\\\\server\\share\\', 'private/*.key']",
)
.with_path_context(ConfigPathContext::new(
PathConvention::Windows,
Some(PathUri::parse("file:///C:/first").expect("first base")),
Some(PathUri::parse("file:///C:/Users/first").expect("first home")),
));
let second = RequirementsLayerEntry::from_toml_value(
RequirementSource::Unknown,
toml::toml! { [permissions.filesystem] deny_read = ["private", "~/secret"] }.into(),
)
.with_path_context(ConfigPathContext::new(
PathConvention::Windows,
Some(PathUri::parse("file:///D:/second").expect("second base")),
Some(PathUri::parse("file:///D:/Users/second").expect("second home")),
));
let composed = AbsolutePathBufGuard::with_home_directory(native_home.path(), || {
compose_requirements_for_hostname([first, second], /*hostname*/ None)
.expect("compose layers")
.expect("requirements present")
});
let constraints = FilesystemConstraints::from(composed.permissions.expect("permissions").value);
let mut expected = FileSystemSandboxPolicy {
kind: FileSystemSandboxKind::Restricted,
glob_scan_max_depth: Some(7),
entries: vec![FileSystemSandboxEntry::new(
PathUri::parse("file:///C:/allowed")
.expect("existing path")
.into(),
FileSystemAccessMode::Write,
)],
};
let mut policy = expected.clone();
expected.entries.extend(
[
"file:///D:/second/private",
"file:///D:/Users/second/secret",
"file:///C:/first/private",
"file:///C:/Users/first/secret",
"file:///C:/",
"file://server/share",
]
.map(|path| {
FileSystemSandboxEntry::new(
PathUri::parse(path).expect("expected denial").into(),
FileSystemAccessMode::Deny,
)
}),
);
expected.entries.push(FileSystemSandboxEntry::new(
FileSystemPath::GlobPattern {
pattern: r"C:\first\private/*.key".to_string(),
},
FileSystemAccessMode::Deny,
));
constraints
.apply_to_policy(&mut policy, PathConvention::Windows)
.expect("apply denials");
constraints
.apply_to_policy(&mut policy, PathConvention::Windows)
.expect("deduplicate existing denials");
assert_eq!(policy, expected);
assert!(
constraints
.apply_to_policy(&mut policy, PathConvention::Posix)
.is_err(),
"a foreign denial cannot be reinterpreted using the host convention",
);
assert_eq!(policy, expected);
}
#[test]
fn common_parser_uses_supplied_windows_drive_and_glob_separators() {
let context = ConfigPathContext::new(
PathConvention::Windows,
Some(PathUri::parse("file:///C:/base/cwd").expect("base")),
/*user_home_dir*/ None,
);
let _guard = context.enter();
for (input, expected) in [
(r"C:private\*.key", r"C:\base\cwd\private/*.key"),
(r"D:\*.key", r"D:\/*.key"),
(r"\private\*.key", r"C:\private/*.key"),
] {
assert_eq!(
FilesystemDenyReadPattern::from_input(input)
.expect("resolve pattern")
.as_str(),
expected,
);
}
}
#[test]
fn windows_denial_globs_reject_ambiguous_roots_and_streams() {
let context = ConfigPathContext::new(
PathConvention::Windows,
Some(PathUri::parse("file://server/share/base").expect("UNC base")),
/*user_home_dir*/ None,
);
let _guard = context.enter();
for input in [
r"\\?\C:\private",
r"\\*\share\private",
r"D:*.key",
r"private\*\file:stream",
] {
assert!(
FilesystemDenyReadPattern::from_input(input).is_err(),
"{input}"
);
}
}
#[test]
fn failed_nested_composition_restores_outer_context_and_then_native_resolution() {
let base = tempdir().expect("native base");
let _native_guard = AbsolutePathBufGuard::new(base.path());
let native = FilesystemDenyReadPattern::from_input("private");
{
let outer = ConfigPathContext::new(
PathConvention::Posix,
Some(PathUri::parse("file:///outer").expect("outer base")),
/*user_home_dir*/ None,
);
let _outer_guard = outer.enter();
let inner = RequirementsLayerEntry::from_toml(
RequirementSource::Unknown,
"[permissions.filesystem]\ndeny_read = ['relative']",
)
.with_path_context(ConfigPathContext::new(
PathConvention::Windows,
/*base_dir*/ None,
/*user_home_dir*/ None,
));
assert!(compose_requirements_for_hostname([inner], /*hostname*/ None).is_err());
assert_eq!(
FilesystemDenyReadPattern::from_input("private")
.expect("outer context restored")
.as_str(),
"/outer/private",
);
}
assert_eq!(FilesystemDenyReadPattern::from_input("private"), native);
}
#[test]
fn native_guard_and_supplied_native_facts_use_the_same_parser() {
let base = tempdir().expect("base");
let home = tempdir().expect("home");
let _base_guard = AbsolutePathBufGuard::new(base.path());
AbsolutePathBufGuard::with_home_directory(home.path(), || {
let inputs = ["./private", "~/secret/*.txt"];
let native = inputs.map(FilesystemDenyReadPattern::from_input);
let context = ConfigPathContext::new(
PathConvention::native(),
Some(PathUri::from_host_native_path(base.path()).expect("native base URI")),
Some(PathUri::from_host_native_path(home.path()).expect("native home URI")),
);
let _context_guard = context.enter();
assert_eq!(inputs.map(FilesystemDenyReadPattern::from_input), native);
});
}
#[test]
fn missing_supplied_home_does_not_fall_back_to_native_home() {
let home = tempdir().expect("native home");
let layer = RequirementsLayerEntry::from_toml(
RequirementSource::Unknown,
"[permissions.filesystem]\ndeny_read = ['~/secret']",
)
.with_path_context(ConfigPathContext::new(
PathConvention::Windows,
Some(PathUri::parse("file:///C:/base").expect("base")),
/*user_home_dir*/ None,
));
AbsolutePathBufGuard::with_home_directory(home.path(), || {
assert!(compose_requirements_for_hostname([layer], /*hostname*/ None).is_err());
});
}
#[test]
fn denial_resolution_rejects_glob_syntax_in_supplied_facts() {
for directory in ["file:///home/sam[1]", "file:///C:/Users/sam[1]"] {
let directory = PathUri::parse(directory).unwrap();
let context = ConfigPathContext::new(
directory.infer_path_convention().unwrap(),
Some(directory.clone()),
Some(directory),
);
// Literal denials also reject metacharacters introduced by path facts.
for input in [
"private/*.key",
"~/private/*.key",
"private/key",
"~/private/key",
] {
let layer = RequirementsLayerEntry::from_toml(
RequirementSource::Unknown,
format!("[permissions.filesystem]\ndeny_read = ['{input}']"),
)
.with_path_context(context.clone());
assert!(compose_requirements_for_hostname([layer], /*hostname*/ None).is_err());
}
}
}
#[test]
fn native_and_supplied_denials_reject_the_same_unsafe_facts() {
let parent = tempdir().unwrap();
let directory = parent.path().join("sam[1]");
let _base_guard = AbsolutePathBufGuard::new(&directory);
AbsolutePathBufGuard::with_home_directory(&directory, || {
let inputs = [
"private/*.key",
"~/private/*.key",
"private/key",
"~/private/key",
];
let native = inputs.map(FilesystemDenyReadPattern::from_input);
assert!(native.iter().all(Result::is_err));
let directory = PathUri::from_host_native_path(&directory).unwrap();
let context = ConfigPathContext::new(
PathConvention::native(),
Some(directory.clone()),
Some(directory),
);
let _context_guard = context.enter();
assert_eq!(inputs.map(FilesystemDenyReadPattern::from_input), native);
});
}
|