Resolve permission profiles with explicit execution-host path context (#44676)

## Why

Permission paths need to follow the execution host's path conventions and home directory. Literal directory names containing glob syntax must not change the meaning of deny patterns, and profile availability checks need to account for configured workspace roots.

## What changed

- Use `ConfigPathContext` to compile built-in and custom profiles, returning the resolved profile and deduplicated `PathUri` workspace roots. Materialize configured roots while retaining runtime workspace symbols.
- Use the same compiler for configuration loading, persisted profile validation, and profile catalogs. Resolve roots against the requested `cwd` when listing profiles.
- Resolve home-relative scoped rules using the supplied home directory and reject unsafe directory prefixes when constructing globs.
- Share workspace-root materialization across native paths and URIs. Deny the affected root when a workspace glob cannot be safely resolved, and clear grants for legacy home-relative workspace denials whose target is unknown.

## Testing

Add coverage for POSIX, Windows, and UNC path resolution, inherited workspace roots, scoped home denials, missing home context, and conservative denial behavior for unsafe globs. Add an app-server test verifying that profile availability reflects the requested `cwd`.

GitOrigin-RevId: ca259434742365c16d0b72629cabfbab41513a80
This commit is contained in:
Sean Huang authored and copyberry committed 2026-09-11 00:03:40 +00:00
1 parent 935ac7710d
commit 9e22e74e8d
17 files changed
+1453 -428

No files matched your search

+61 -88
View File
@@ -1173,6 +1173,18 @@ impl FileSystemSandboxPolicy {
},
None => (context.cwd, pattern.as_str()),
};
let convention = if is_windows {
PathConvention::Windows
} else {
PathConvention::Posix
};
if LegacyAppPathString::from_string(pattern)
.to_path_uri(convention)
.is_err()
&& let Err(error) = root.validate_glob_directory(convention)
{
return Some(Err(error.to_string()));
}
Some(
root
.join(pattern)
@@ -1188,73 +1200,27 @@ impl FileSystemSandboxPolicy {
/// Replaces symbolic `:workspace_roots` entries with concrete entries for
/// each workspace root.
pub fn materialize_project_roots_with_workspace_roots(
mut self,
self,
workspace_roots: &[AbsolutePathBuf],
) -> Self {
let mut entries = Vec::with_capacity(self.entries.len());
for entry in self.entries {
match entry.path {
FileSystemPath::Special {
value: FileSystemSpecialPath::ProjectRoots { subpath },
} => {
entries.extend(workspace_roots.iter().map(|root| FileSystemSandboxEntry {
path: FileSystemPath::from(match subpath.as_ref() {
Some(subpath) => {
AbsolutePathBuf::resolve_path_against_base(subpath, root.as_path())
}
None => root.clone(),
}),
access: entry.access,
missing_path_behavior: entry.missing_path_behavior,
}));
}
FileSystemPath::GlobPattern { pattern } => {
if let Some(subpath) = parse_project_roots_glob_pattern(&pattern) {
entries.extend(workspace_roots.iter().map(|root| FileSystemSandboxEntry {
path: FileSystemPath::GlobPattern {
pattern: resolve_project_roots_glob_pattern(subpath, root),
},
access: entry.access,
missing_path_behavior: entry.missing_path_behavior,
}));
} else {
entries.push(FileSystemSandboxEntry {
path: FileSystemPath::GlobPattern { pattern },
access: entry.access,
missing_path_behavior: entry.missing_path_behavior,
});
}
}
FileSystemPath::Path { path } => {
entries.push(FileSystemSandboxEntry {
path: path.into(),
access: entry.access,
missing_path_behavior: entry.missing_path_behavior,
});
}
FileSystemPath::Special { value } => {
entries.push(FileSystemSandboxEntry {
path: FileSystemPath::Special { value },
access: entry.access,
missing_path_behavior: entry.missing_path_behavior,
});
}
}
}
self.entries = entries;
self
let roots = workspace_roots
.iter()
.map(PathUri::from_abs_path)
.collect::<Vec<_>>();
self.materialize_project_roots_with_path_uris(&roots)
}
/// Materializes workspace-root entries without projecting executor paths onto the host.
pub fn materialize_project_roots_with_path_uris(mut self, workspace_roots: &[PathUri]) -> Self {
if let Ok(native_workspace_roots) = workspace_roots
.iter()
.map(PathUri::to_abs_path)
.collect::<Result<Vec<_>, _>>()
{
return self.materialize_project_roots_with_workspace_roots(&native_workspace_roots);
}
/// Legacy home-relative workspace denials clear all grants when their target is unknown.
pub fn materialize_project_roots_with_path_uris(self, workspace_roots: &[PathUri]) -> Self {
self.try_materialize_project_roots_with_path_uris(workspace_roots)
.unwrap_or_else(|| Self::restricted(Vec::new()))
}
fn try_materialize_project_roots_with_path_uris(
mut self,
workspace_roots: &[PathUri],
) -> Option<Self> {
let mut entries = Vec::with_capacity(self.entries.len());
for entry in self.entries {
let (subpath, is_glob) = match &entry.path {
@@ -1274,11 +1240,34 @@ impl FileSystemSandboxPolicy {
continue;
}
};
if entry.access == FileSystemAccessMode::Deny
&& subpath.is_some_and(|subpath| {
workspace_roots.iter().any(|root| {
root.infer_path_convention().is_some_and(|convention| {
convention.home_relative_suffix(subpath).is_some()
})
})
})
{
// Older policies could expand these rules outside the workspace.
// Without a home fact, denying only the workspace would weaken them.
return None;
}
entries.extend(workspace_roots.iter().filter_map(|root| {
let path = subpath.map_or_else(
|| Some(root.clone()),
|subpath| resolve_scoped_workspace_path(root, subpath),
);
let path = if is_glob
&& root
.infer_path_convention()
.is_none_or(|convention| root.validate_glob_directory(convention).is_err())
{
// An infallible materializer cannot return an unsafe glob.
// Use the existing deny-root fallback for unresolvable rules.
None
} else {
subpath.map_or_else(
|| Some(root.clone()),
|subpath| resolve_scoped_workspace_path(root, subpath),
)
};
let (path, access) = match path {
Some(path) if is_glob => (
FileSystemPath::GlobPattern {
@@ -1301,24 +1290,20 @@ impl FileSystemSandboxPolicy {
}));
}
self.entries = entries;
self
Some(self)
}
/// Preserves symbolic `:workspace_roots` entries while also adding concrete
/// entries for each provided workspace root.
pub fn with_materialized_project_roots_for_workspace_roots(
mut self,
self,
workspace_roots: &[AbsolutePathBuf],
) -> Self {
let materialized = self
.clone()
.materialize_project_roots_with_workspace_roots(workspace_roots);
for entry in materialized.entries {
if !self.entries.contains(&entry) {
self.entries.push(entry);
}
}
self
let roots = workspace_roots
.iter()
.map(PathUri::from_abs_path)
.collect::<Vec<_>>();
self.with_materialized_project_roots_for_path_uris(&roots)
}
pub fn with_additional_readable_roots(
@@ -1939,18 +1924,6 @@ fn resolve_entry_path(
}
}
fn parse_project_roots_glob_pattern(pattern: &str) -> Option<&Path> {
pattern
.strip_prefix(PROJECT_ROOTS_GLOB_PATTERN_PREFIX)
.map(Path::new)
}
fn resolve_project_roots_glob_pattern(subpath: &Path, root: &AbsolutePathBuf) -> String {
AbsolutePathBuf::resolve_path_against_base(subpath, root.as_path())
.to_string_lossy()
.into_owned()
}
fn resolve_candidate_path(path: &Path, cwd: &Path) -> Option<AbsolutePathBuf> {
if path.is_absolute() {
AbsolutePathBuf::from_absolute_path(path).ok()