File size: 14,824 Bytes
afa0cbf | 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 | //! Regression coverage for remote path matching and managed-denial enforcement.
use super::*;
use crate::permissions::FileSystemAccessMode;
use crate::permissions::FileSystemSandboxEntry;
use crate::permissions::PROTECTED_METADATA_PATH_NAMES;
use pretty_assertions::assert_eq;
fn uri(input: &str) -> PathUri {
PathUri::parse(input).unwrap()
}
fn deny(path: FileSystemPath) -> FileSystemSandboxEntry {
FileSystemSandboxEntry::new(path, FileSystemAccessMode::Deny)
}
#[test]
fn read_denials_use_target_paths_and_native_glob_semantics() {
for base in [
"file:///work/repo",
"file:///Users/agent/repo",
"file:///C:/work/repo",
"file://server/share/repo",
] {
let cwd = uri(base);
let context = FileSystemSandboxPolicyContext {
cwd: &cwd,
workspace_roots: std::slice::from_ref(&cwd),
user_home_dir: Some(&cwd),
temporary_directories: Some(&[]),
};
let policy = FileSystemSandboxPolicy::restricted(vec![
deny(FileSystemPath::Path {
path: cwd.join("private").unwrap(),
}),
deny(FileSystemPath::GlobPattern {
pattern: "~/secrets/*.key".into(),
}),
deny(FileSystemPath::GlobPattern {
pattern: "nested/**/*.env".into(),
}),
]);
let matcher = ReadDenyMatcher::try_new_with_context(&policy, &context)
.unwrap()
.unwrap();
for (path, expected) in [
("private", true),
("private/file", true),
("private-other/file", false),
("secrets/token.key", true),
("secrets/nested/token.key", false),
("nested/.env", true),
("nested/deep/token.env", true),
("public/file", false),
] {
assert_eq!(
matcher.is_read_denied_uri(&cwd.join(path).unwrap(), &context),
expected,
"{base}: {path}"
);
}
assert_eq!(
matcher.is_read_denied_uri(&cwd.join("PRIVATE/file").unwrap(), &context),
cwd.infer_path_convention() == Some(PathConvention::Windows),
);
assert!(matcher.is_read_denied_uri(&uri("file:///C:/foreign%2Fpath"), &context));
}
}
#[test]
fn managed_denials_reject_concrete_read_and_write_grants() {
for base in [
"file:///work/repo",
"file:///C:/work/repo",
"file://server/share/repo",
] {
let cwd = uri(base);
let private = cwd.join("private").unwrap();
let required = FileSystemSandboxPolicy::restricted(vec![deny(FileSystemPath::Path {
path: private.clone(),
})]);
let context = FileSystemSandboxPolicyContext {
cwd: &cwd,
workspace_roots: std::slice::from_ref(&cwd),
user_home_dir: None,
temporary_directories: Some(&[]),
};
let mut policy = FileSystemSandboxPolicy::workspace_write_with_path_uris(
&[],
/*exclude_tmpdir_env_var*/ true,
/*exclude_slash_tmp*/ true,
);
policy.entries.extend(required.entries.clone());
assert_eq!(
policy.validate_managed_deny_read(&required, &context),
Ok(())
);
for access in [FileSystemAccessMode::Read, FileSystemAccessMode::Write] {
let mut carveout = policy.clone();
carveout.entries.push(FileSystemSandboxEntry::new(
FileSystemPath::Path {
path: private.join("carveout").unwrap(),
},
access,
));
assert_eq!(
carveout.validate_managed_deny_read(&required, &context),
Err(SHADOWED.into())
);
}
}
}
#[test]
fn managed_denials_preserve_native_metadata_read_rejections() {
for base in [
"file:///executor/home",
"file:///C:/Users/executor",
"file://server/share/executor",
] {
let extra_root = uri(base);
let cwd = extra_root.join("workspace").unwrap();
let context = FileSystemSandboxPolicyContext {
cwd: &cwd,
workspace_roots: std::slice::from_ref(&cwd),
user_home_dir: Some(&extra_root),
temporary_directories: Some(&[]),
};
let legacy = FileSystemSandboxPolicy::workspace_write_with_path_uris(
std::slice::from_ref(&extra_root),
/*exclude_tmpdir_env_var*/ true,
/*exclude_slash_tmp*/ true,
);
for name in [".codex", ".git"] {
let denied_root = extra_root.join(name).unwrap();
let child = denied_root.join("reopened").unwrap();
for (path, expected_root_access) in [
(
FileSystemPath::Path {
path: denied_root.clone(),
},
FileSystemAccessMode::Deny,
),
(
FileSystemPath::GlobPattern {
pattern: denied_root.join("*").unwrap().inferred_native_path_string(),
},
FileSystemAccessMode::Read,
),
] {
let required = FileSystemSandboxPolicy::restricted(vec![deny(path)]);
let mut policy = legacy.clone();
policy.preserve_deny_read_restrictions_from(&required);
assert_eq!(
(
policy.resolve_access(&denied_root, &context),
policy.validate_managed_deny_read(&required, &context),
),
(
expected_root_access,
if expected_root_access == FileSystemAccessMode::Deny {
Err(SHADOWED.into())
} else {
Ok(())
},
),
"{base}: {name}",
);
let matcher = ReadDenyMatcher::try_new_with_context(&required, &context)
.unwrap()
.unwrap();
assert!(matcher.is_read_denied_uri(&child, &context));
for access in [FileSystemAccessMode::Read, FileSystemAccessMode::Write] {
let mut reopened = policy.clone();
reopened.entries.push(FileSystemSandboxEntry::new(
FileSystemPath::Path {
path: child.clone(),
},
access,
));
assert_eq!(
reopened.validate_managed_deny_read(&required, &context),
Err(SHADOWED.into()),
"{base}: {name}: {access}",
);
}
}
}
}
}
#[test]
fn malformed_or_unresolvable_denials_fail_closed() {
let cwd = uri("file:///work/repo");
let context = FileSystemSandboxPolicyContext {
cwd: &cwd,
workspace_roots: &[],
user_home_dir: None,
temporary_directories: Some(&[]),
};
for pattern in ["~/secrets/**", "secrets/[z-a]"] {
let policy = FileSystemSandboxPolicy::restricted(vec![deny(FileSystemPath::GlobPattern {
pattern: pattern.into(),
})]);
assert!(ReadDenyMatcher::try_new_with_context(&policy, &context).is_err());
}
let policy = FileSystemSandboxPolicy::restricted(vec![deny(FileSystemPath::Path {
path: uri("file:///C:/private"),
})]);
assert!(ReadDenyMatcher::try_new_with_context(&policy, &context).is_err());
}
#[test]
fn slash_tmp_denials_follow_the_executor_convention() {
let policy = FileSystemSandboxPolicy::restricted(vec![deny(FileSystemPath::Special {
value: FileSystemSpecialPath::SlashTmp,
})]);
for (base, active) in [("file:///work", true), ("file:///C:/work", false)] {
let cwd = uri(base);
let context = FileSystemSandboxPolicyContext {
cwd: &cwd,
workspace_roots: &[],
user_home_dir: None,
temporary_directories: None,
};
let matcher = ReadDenyMatcher::try_new_with_context(&policy, &context).unwrap();
assert_eq!(matcher.is_some(), active);
if let Some(matcher) = matcher {
assert!(matcher.is_read_denied_uri(&cwd.join("/tmp/file").unwrap(), &context));
}
}
}
#[test]
fn remote_workspace_write_keeps_metadata_protected() {
let cwd = uri("file:///C:/repo");
let extra = uri("file:///D:/extra");
let policy = FileSystemSandboxPolicy::workspace_write_with_path_uris(
std::slice::from_ref(&extra),
/*exclude_tmpdir_env_var*/ true,
/*exclude_slash_tmp*/ true,
);
let context = FileSystemSandboxPolicyContext {
cwd: &cwd,
workspace_roots: std::slice::from_ref(&cwd),
user_home_dir: None,
temporary_directories: Some(&[]),
};
for name in PROTECTED_METADATA_PATH_NAMES {
assert!(
policy
.entries
.contains(&FileSystemSandboxEntry::skip_missing_path(
FileSystemPath::Path {
path: extra.join(name).unwrap()
},
FileSystemAccessMode::Read,
))
);
}
for root in [cwd.clone(), extra] {
assert!(policy.can_write_path(&root.join("file").unwrap(), &context));
for name in PROTECTED_METADATA_PATH_NAMES {
let path = root.join(name).unwrap().join("file").unwrap();
assert!(policy.can_read_path(&path, &context));
assert!(!policy.can_write_path(&path, &context));
}
}
}
#[test]
fn read_globs_reject_metacharacters_in_home_and_cwd_facts() {
for prefix in ["file:///home", "file:///C:/Users"] {
let parent = uri(prefix);
let directory = parent.join("sam[1]").unwrap();
for (pattern, cwd, home) in [
("~/private/*.key", &parent, Some(&directory)),
("private/*.key", &directory, None),
] {
let policy =
FileSystemSandboxPolicy::restricted(vec![deny(FileSystemPath::GlobPattern {
pattern: pattern.into(),
})]);
let context = FileSystemSandboxPolicyContext {
cwd,
workspace_roots: std::slice::from_ref(&directory),
user_home_dir: home,
temporary_directories: None,
};
assert!(ReadDenyMatcher::try_new_with_context(&policy, &context).is_err());
let matcher = ReadDenyMatcher::from_context(&policy, &context).unwrap();
assert!(
matcher.is_read_denied_uri(&directory.join("private/a.key").unwrap(), &context)
);
}
}
}
#[test]
fn workspace_globs_fail_closed_on_literal_root_metacharacters_on_every_host() {
for prefix in ["file:///home", "file:///C:/Users"] {
let root = uri(prefix).join("sam[1]").unwrap();
let policy = FileSystemSandboxPolicy::restricted(vec![deny(FileSystemPath::GlobPattern {
pattern: crate::permissions::project_roots_glob_pattern(std::path::Path::new(
"private/*.key",
)),
})]);
let materialized = policy
.clone()
.materialize_project_roots_with_path_uris(std::slice::from_ref(&root));
assert_eq!(
materialized,
FileSystemSandboxPolicy::restricted(vec![deny(root.clone().into())]),
);
if let Ok(native_root) = root.to_abs_path() {
assert_eq!(
policy.materialize_project_roots_with_workspace_roots(&[native_root]),
materialized,
);
}
}
}
#[test]
fn materializing_legacy_home_relative_workspace_denials_removes_all_grants() {
for (root, home) in [
("file:///work/repo", "file:///home/sam"),
("file:///C:/work/repo", "file:///D:/Users/sam"),
] {
let root = uri(root);
let home = uri(home);
let convention = root.infer_path_convention().unwrap();
for subpath in ["~", "~/private/*.env", r"~\private", r"~\private\*.env"] {
if convention.home_relative_suffix(subpath).is_none() {
continue;
}
let denied_path = if subpath.contains('*') {
FileSystemPath::GlobPattern {
pattern: crate::permissions::project_roots_glob_pattern(std::path::Path::new(
subpath,
)),
}
} else {
FileSystemPath::Special {
value: FileSystemSpecialPath::ProjectRoots {
subpath: Some(subpath.into()),
},
}
};
let policy = FileSystemSandboxPolicy::restricted(vec![
FileSystemSandboxEntry::new(
FileSystemPath::Special {
value: FileSystemSpecialPath::Root,
},
FileSystemAccessMode::Read,
),
FileSystemSandboxEntry::new(
FileSystemPath::Special {
value: FileSystemSpecialPath::ProjectRoots { subpath: None },
},
FileSystemAccessMode::Write,
),
FileSystemSandboxEntry::new(home.clone().into(), FileSystemAccessMode::Write),
deny(denied_path),
]);
let mut materialized = vec![
policy
.clone()
.materialize_project_roots_with_path_uris(std::slice::from_ref(&root)),
policy
.clone()
.with_materialized_project_roots_for_path_uris(std::slice::from_ref(&root)),
];
if let Ok(native_root) = root.to_abs_path() {
materialized.push(
policy
.clone()
.materialize_project_roots_with_workspace_roots(std::slice::from_ref(
&native_root,
)),
);
materialized.push(policy.with_materialized_project_roots_for_workspace_roots(
std::slice::from_ref(&native_root),
));
}
for policy in materialized {
assert_eq!(policy, FileSystemSandboxPolicy::restricted(Vec::new()));
}
}
}
}
|