Respect filesystem permissions during capability discovery (#36124)

## Why

Executor capability discovery can traverse plugin and skill roots, including
symlinks. Under restricted filesystem permissions, discovery must not expose
files outside the permitted paths.

## What changed

- Pass each environment's filesystem sandbox context through capability root
  discovery and apply it to metadata, directory walks, and file reads.
- Enable discovery for restricted sessions so permitted executor skills remain
  available while inaccessible roots and symlink targets are omitted.
- Key discovery caches by sandbox context and reject sandboxed discovery on
  executors that do not advertise support for it.
- Split requests with more than 128 roots into supported-size batches.

## Testing

- Cover permitted and denied external symlink targets.
- Verify restricted skill listing excludes inaccessible skills.
- Verify cache separation across permission contexts and discovery of 129 roots.

GitOrigin-RevId: 44d16468ca003403bdb8b71a04ae8c9ff94ed494
This commit is contained in:
jif authored and copyberry committed 2026-07-30 10:01:06 +00:00
1 parent 3d805abdf0
commit 5decb399ae
14 files changed
+510 -70

No files matched your search

@@ -10,6 +10,7 @@ use codex_exec_server_protocol::DISCOVERABLE_PLUGIN_MANIFEST_PATHS;
use codex_exec_server_protocol::DiscoveredPluginFiles;
use codex_exec_server_protocol::DiscoveredSkillFiles;
use codex_file_system::ExecutorFileSystem;
use codex_file_system::FileSystemSandboxContext;
use codex_file_system::WalkEntryKind;
use codex_file_system::WalkOptions;
use codex_utils_path_uri::PathUri;
@@ -17,7 +18,7 @@ use futures::StreamExt;
use serde::Deserialize;
use serde_json::Value;
const MAX_ROOTS_PER_REQUEST: usize = 128;
pub(crate) const MAX_ROOTS_PER_REQUEST: usize = 128;
const MAX_SCAN_DEPTH: usize = 6;
const MAX_DIRECTORIES_PER_ROOT: usize = 2_000;
const MAX_ENTRIES_PER_ROOT: usize = 20_000;
@@ -63,7 +64,8 @@ async fn discover_root(
file_system: &dyn ExecutorFileSystem,
request: CapabilityRootDiscoverRequest,
) -> CapabilityRootDiscovery {
let CapabilityRootDiscoverRequest { id, path } = request;
let CapabilityRootDiscoverRequest { id, path, sandbox } = request;
let sandbox = sandbox.as_ref();
let mut discovery = CapabilityRootDiscovery {
id,
path: path.clone(),
@@ -74,7 +76,17 @@ async fn discover_root(
error: None,
};
match file_system.get_metadata(&path, /*sandbox*/ None).await {
#[cfg(target_os = "windows")]
if sandbox.is_some_and(|context| {
context.should_run_in_sandbox()
&& context.windows_sandbox_level
== codex_protocol::config_types::WindowsSandboxLevel::Disabled
}) {
discovery.error = Some("filesystem sandbox is unavailable on this executor".to_string());
return discovery;
}
match file_system.get_metadata(&path, sandbox).await {
Ok(metadata) if metadata.is_directory => {}
Ok(_) => {
discovery.error = Some(format!("capability root {path} is not a directory"));
@@ -96,7 +108,7 @@ async fn discover_root(
follow_directory_symlinks: true,
prune_hidden_directories: false,
},
/*sandbox*/ None,
sandbox,
)
.await
{
@@ -143,14 +155,26 @@ async fn discover_root(
});
let mut budget = BundleBudget::default();
let root_manifest =
read_first_plugin_manifest(file_system, &path, &mut budget, &mut discovery.warnings).await;
let root_manifest = read_first_plugin_manifest(
file_system,
&path,
sandbox,
&mut budget,
&mut discovery.warnings,
)
.await;
let inherited_manifest = match root_manifest.as_ref() {
Some(manifest) => Some(manifest.clone()),
None => {
read_nearest_ancestor_manifest(file_system, &path, &mut budget, &mut discovery.warnings)
.await
read_nearest_ancestor_manifest(
file_system,
&path,
sandbox,
&mut budget,
&mut discovery.warnings,
)
.await
}
};
let mut seen_namespace_roots = HashSet::new();
@@ -170,6 +194,7 @@ async fn discover_root(
if let Some(manifest) = read_optional_text_file(
file_system,
manifest_path,
sandbox,
&mut budget,
&mut discovery.warnings,
)
@@ -190,15 +215,27 @@ async fn discover_root(
};
let mcp_config = match mcp_path {
Some(path) => {
read_optional_text_file(file_system, path, &mut budget, &mut discovery.warnings)
.await
read_optional_text_file(
file_system,
path,
sandbox,
&mut budget,
&mut discovery.warnings,
)
.await
}
None => None,
};
let apps_config = match declarations.apps_config {
Some(path) => {
read_optional_text_file(file_system, path, &mut budget, &mut discovery.warnings)
.await
read_optional_text_file(
file_system,
path,
sandbox,
&mut budget,
&mut discovery.warnings,
)
.await
}
None => None,
};
@@ -213,6 +250,7 @@ async fn discover_root(
let Some(instructions) = read_optional_text_file(
file_system,
skill_path.clone(),
sandbox,
&mut budget,
&mut discovery.warnings,
)
@@ -228,6 +266,7 @@ async fn discover_root(
read_optional_text_file(
file_system,
metadata_path,
sandbox,
&mut budget,
&mut discovery.warnings,
)
@@ -247,6 +286,7 @@ async fn discover_root(
async fn read_first_plugin_manifest(
file_system: &dyn ExecutorFileSystem,
root: &PathUri,
sandbox: Option<&FileSystemSandboxContext>,
budget: &mut BundleBudget,
warnings: &mut Vec<String>,
) -> Option<CapabilityTextFile> {
@@ -254,7 +294,9 @@ async fn read_first_plugin_manifest(
let Ok(path) = root.join(relative_path) else {
continue;
};
if let Some(manifest) = read_optional_text_file(file_system, path, budget, warnings).await {
if let Some(manifest) =
read_optional_text_file(file_system, path, sandbox, budget, warnings).await
{
return Some(manifest);
}
}
@@ -264,13 +306,14 @@ async fn read_first_plugin_manifest(
async fn read_nearest_ancestor_manifest(
file_system: &dyn ExecutorFileSystem,
root: &PathUri,
sandbox: Option<&FileSystemSandboxContext>,
budget: &mut BundleBudget,
warnings: &mut Vec<String>,
) -> Option<CapabilityTextFile> {
let mut ancestor = root.parent();
while let Some(path) = ancestor {
if let Some(manifest) =
read_first_plugin_manifest(file_system, &path, budget, warnings).await
read_first_plugin_manifest(file_system, &path, sandbox, budget, warnings).await
{
return Some(manifest);
}
@@ -282,10 +325,11 @@ async fn read_nearest_ancestor_manifest(
async fn read_optional_text_file(
file_system: &dyn ExecutorFileSystem,
path: PathUri,
sandbox: Option<&FileSystemSandboxContext>,
budget: &mut BundleBudget,
warnings: &mut Vec<String>,
) -> Option<CapabilityTextFile> {
let metadata = match file_system.get_metadata(&path, /*sandbox*/ None).await {
let metadata = match file_system.get_metadata(&path, sandbox).await {
Ok(metadata) if metadata.is_file => metadata,
Ok(_) => return None,
Err(error) if error.kind() == io::ErrorKind::NotFound => return None,
@@ -310,32 +354,49 @@ async fn read_optional_text_file(
));
return None;
}
let mut stream = match file_system.read_file_stream(&path, /*sandbox*/ None).await {
Ok(stream) => stream,
Err(error) => {
warnings.push(format!("failed to read capability file {path}: {error}"));
return None;
let contents = if sandbox.is_some_and(FileSystemSandboxContext::should_run_in_sandbox) {
match file_system.read_file(&path, sandbox).await {
Ok(contents) if contents.len() <= MAX_FILE_BYTES && budget.can_add(contents.len()) => {
contents
}
Ok(_) => {
warnings.push(format!("capability file {path} exceeded its read limit"));
return None;
}
Err(error) => {
warnings.push(format!("failed to read capability file {path}: {error}"));
return None;
}
}
};
let mut contents = Vec::with_capacity(size);
while let Some(chunk) = stream.next().await {
let chunk = match chunk {
Ok(chunk) => chunk,
} else {
let mut stream = match file_system.read_file_stream(&path, sandbox).await {
Ok(stream) => stream,
Err(error) => {
warnings.push(format!("failed to read capability file {path}: {error}"));
return None;
}
};
let Some(new_len) = contents.len().checked_add(chunk.len()) else {
warnings.push(format!("capability file {path} exceeded its read limit"));
return None;
};
if new_len > MAX_FILE_BYTES || !budget.can_add(new_len) {
warnings.push(format!("capability file {path} exceeded its read limit"));
return None;
let mut contents = Vec::with_capacity(size);
while let Some(chunk) = stream.next().await {
let chunk = match chunk {
Ok(chunk) => chunk,
Err(error) => {
warnings.push(format!("failed to read capability file {path}: {error}"));
return None;
}
};
let Some(new_len) = contents.len().checked_add(chunk.len()) else {
warnings.push(format!("capability file {path} exceeded its read limit"));
return None;
};
if new_len > MAX_FILE_BYTES || !budget.can_add(new_len) {
warnings.push(format!("capability file {path} exceeded its read limit"));
return None;
}
contents.extend_from_slice(&chunk);
}
contents.extend_from_slice(&chunk);
}
contents
};
let contents = match String::from_utf8(contents) {
Ok(contents) => contents,
Err(error) => {