Make filesystem policy matching URI-native (#41001)

## Why

Filesystem policies can describe paths using a convention that differs from the
host running Codex. Native path comparisons can therefore mis-handle cases such
as case-variant Windows paths or ambiguous encoded components.

## What changed

- Resolve policy entries and special roots as `PathUri` values using the
  executor's path convention.
- Use validated URI components for containment, overlap, and precedence, and
  fail closed when component boundaries are ambiguous.
- Restrict relative joins to descendants and apply the same matching rules to
  protected metadata paths and permission-profile intersections.

## Testing

Added coverage for Windows case variants, encoded and opaque paths, repeated
separators, descendant joins, special roots, metadata protection, and preserved
deny entries.

GitOrigin-RevId: fb09d44d11df25faaac806fe00457e6f6b0d8596
This commit is contained in:
iceweasel-oai authored and copyberry committed 2026-08-26 23:49:05 +00:00
1 parent f3741880f6
commit 2926014075
5 files changed
+640 -113

No files matched your search

+105
View File
@@ -800,6 +800,57 @@ fn join_normalizes_relative_uri_segments() {
}
}
#[test]
fn join_descendant_uses_the_base_path_convention() {
for (base, relative, expected) in [
(
"file:///workspace",
"docs/../public",
"file:///workspace/public",
),
(
"file:///C:/workspace",
r"docs\..\public",
"file:///C:/workspace/public",
),
(
"file://server/share/workspace",
r"docs\..\public",
"file://server/share/workspace/public",
),
] {
let base = PathUri::parse(base).expect("valid base URI");
let expected = PathUri::parse(expected).expect("valid expected URI");
assert_eq!(
base.join_descendant(relative),
Ok(expected),
"joining {relative}"
);
}
}
#[test]
fn join_descendant_rejects_non_descendant_paths() {
for (base, path) in [
("file:///workspace", "/workspace/docs"),
("file:///workspace", "../outside"),
("file:///C:/workspace", r"\workspace\docs"),
("file:///C:/workspace", r"C:\workspace\docs"),
("file:///C:/workspace", r"C:docs"),
("file:///C:/workspace", r"docs\file:stream"),
("file://server/share/workspace", r"..\outside"),
] {
let base = PathUri::parse(base).expect("valid base URI");
assert_eq!(
base.join_descendant(path),
Err(PathUriParseError::JoinPathMustBeDescendant(
path.to_string()
)),
"joining {path}"
);
}
}
#[test]
fn join_replaces_posix_absolute_path() {
let base = PathUri::parse("file:///workspace").expect("valid base URI");
@@ -1157,6 +1208,12 @@ fn starts_with_uses_uri_segment_boundaries() {
"file:///workspace/plugin",
false,
),
(
"file:///workspace/pri%76ate/file",
"file:///workspace/%70rivate",
true,
),
("file:///workspace/%ff/file", "file:///workspace/%FF", true),
(
"file:///workspace/plugin/%5C..%5Coutside",
"file:///workspace/plugin",
@@ -1174,6 +1231,54 @@ fn starts_with_uses_uri_segment_boundaries() {
}
}
#[test]
fn overlaps_uses_lexical_containment() {
for (left, right, expected) in [
("file:///workspace", "file:///workspace/src", Some(true)),
(
"file:///C:/WORKSPACE",
"file:///c:/workspace/src",
Some(true),
),
(
"file:///workspace/src",
"file:///workspace/tests",
Some(false),
),
("file:///WORKSPACE", "file:///workspace/src", Some(false)),
] {
let left = PathUri::parse(left).expect("valid left URI");
let right = PathUri::parse(right).expect("valid right URI");
assert_eq!(left.overlaps(&right), expected, "{left} and {right}");
assert_eq!(right.overlaps(&left), expected, "{right} and {left}");
}
let opaque = PathUri::from_opaque_path_bytes(b"/workspace/private");
let lexical = PathUri::parse("file:///workspace").expect("valid lexical URI");
assert_eq!(opaque.overlaps(&opaque), Some(true));
assert_eq!(opaque.overlaps(&lexical), None);
assert_eq!(lexical.overlaps(&opaque), None);
}
#[test]
fn lexical_depth_counts_validated_nonempty_segments() {
for (path, expected) in [
("file:///", Some(0)),
("file:///workspace////", Some(1)),
("file:///workspace/%70rivate", Some(2)),
("file:///workspace/private%2Fsecret", None),
] {
let path = PathUri::parse(path).expect("valid path URI");
assert_eq!(path.lexical_depth(), expected, "lexical depth for {path}");
}
assert_eq!(
PathUri::from_opaque_path_bytes(b"/workspace").lexical_depth(),
None
);
}
#[test]
fn to_url_returns_the_validated_url() {
let uri = PathUri::parse("file://localhost/workspace/a%20file.rs").expect("valid file URI");