Harden unsandboxed patch filesystem access (#39659)

## Why

An `apply_patch` path can be replaced with a symlink after verification, allowing an unsandboxed patch operation to reach a different file than the one that was approved.

## What changed

- Add `follow_symlinks` options to executor filesystem reads, writes, metadata lookups, directory creation, and removal, including the corresponding `followSymlinks` protocol fields.
- Implement no-follow filesystem operations on Unix and Windows that reject links in any path component and restrict file access to regular files.
- Run `apply_patch` with symlink traversal disabled when an otherwise-required sandbox is bypassed, while retaining the existing follow-symlink default for standalone callers.

## Testing

- Cover leaf and ancestor symlinks across patch add, update, delete, and move operations, including a path swap after verification.
- Exercise local and remote no-follow filesystem behavior, concurrent directory creation, special-file rejection, and Windows reparse points.

GitOrigin-RevId: 43fd479084891493ce13564fbd894b98f329c6dd
This commit is contained in:
pakrym-oai authored and copyberry committed 2026-08-20 08:10:08 +00:00
1 parent 4e1a772a7d
commit e3e5ad2847
88 files changed
+2782 -302

No files matched your search

+17 -3
View File
@@ -3,6 +3,8 @@ use std::io;
use codex_exec_server::CapabilityRootDiscovery;
use codex_exec_server::ExecutorFileSystem;
use codex_exec_server::GetMetadataOptions;
use codex_exec_server::ReadFileOptions;
use codex_protocol::protocol::Product;
use codex_skills::EnvironmentSkillMetadata;
use codex_skills::ParsedSkillFrontmatter;
@@ -326,7 +328,11 @@ async fn read_skill_contents(
skill_path: &PathUri,
) -> Result<String, String> {
file_system
.read_file_text(skill_path, /*sandbox*/ None)
.read_file_text(
skill_path,
ReadFileOptions::default(),
/*sandbox*/ None,
)
.await
.map_err(|err| format!("failed to read file: {err}"))
}
@@ -336,7 +342,11 @@ async fn probe_skill_metadata(
metadata_path: &PathUri,
) -> (Option<SkillDependencies>, Option<SkillPolicy>) {
match file_system
.get_metadata(metadata_path, /*sandbox*/ None)
.get_metadata(
metadata_path,
GetMetadataOptions::default(),
/*sandbox*/ None,
)
.await
{
Ok(metadata) if metadata.is_file => {}
@@ -355,7 +365,11 @@ async fn read_skill_metadata(
metadata_path: &PathUri,
) -> (Option<SkillDependencies>, Option<SkillPolicy>) {
let contents = match file_system
.read_file_text(metadata_path, /*sandbox*/ None)
.read_file_text(
metadata_path,
ReadFileOptions::default(),
/*sandbox*/ None,
)
.await
{
Ok(contents) => contents,
+11 -2
View File
@@ -2,6 +2,8 @@ use std::collections::HashMap;
use std::sync::Arc;
use codex_exec_server::ExecutorFileSystem;
use codex_exec_server::GetMetadataOptions;
use codex_exec_server::ReadFileOptions;
use codex_protocol::protocol::SkillScope;
use codex_skills::ParsedSkillFrontmatter;
use codex_skills::SkillError;
@@ -219,7 +221,14 @@ async fn load_skills_under_root(
);
return None;
}
match file_system.get_metadata(&path_uri, /*sandbox*/ None).await {
match file_system
.get_metadata(
&path_uri,
GetMetadataOptions::default(),
/*sandbox*/ None,
)
.await
{
Ok(metadata) if metadata.is_file => {}
Ok(_) => {
error!(
@@ -348,7 +357,7 @@ async fn parse_skill_file(
}
.unwrap_or(SkillMetadataDiscovery::Absent);
let (contents, loaded_metadata) = tokio::join!(
file_system.read_file_text(path_uri, /*sandbox*/ None),
file_system.read_file_text(path_uri, ReadFileOptions::default(), /*sandbox*/ None,),
load_host_skill_metadata(file_system, path, &metadata, plugin_root),
);
let contents = contents.map_err(|error| format!("failed to read file: {error}"))?;
@@ -10,10 +10,13 @@ use codex_exec_server::ExecutorFileSystemFuture;
use codex_exec_server::FileMetadata;
use codex_exec_server::FileSystemReadStream;
use codex_exec_server::FileSystemSandboxContext;
use codex_exec_server::GetMetadataOptions;
use codex_exec_server::ReadDirectoryEntry;
use codex_exec_server::ReadFileOptions;
use codex_exec_server::RemoveOptions;
use codex_exec_server::WalkOptions;
use codex_exec_server::WalkOutcome;
use codex_exec_server::WriteFileOptions;
use codex_utils_path_uri::PathUri;
use tokio::sync::Notify;
use tokio::sync::Semaphore;
@@ -96,6 +99,7 @@ impl ExecutorFileSystem for RecordingFileSystem<'_> {
fn read_file<'a>(
&'a self,
path: &'a PathUri,
options: ReadFileOptions,
sandbox: Option<&'a FileSystemSandboxContext>,
) -> ExecutorFileSystemFuture<'a, Vec<u8>> {
self.read_files
@@ -106,7 +110,7 @@ impl ExecutorFileSystem for RecordingFileSystem<'_> {
self.skill_read_started.store(true, Ordering::Release);
self.skill_read_started_notify.notify_waiters();
}
self.inner.read_file(path, sandbox)
self.inner.read_file(path, options, sandbox)
}
fn read_file_stream<'a>(
@@ -121,9 +125,10 @@ impl ExecutorFileSystem for RecordingFileSystem<'_> {
&'a self,
path: &'a PathUri,
contents: Vec<u8>,
options: WriteFileOptions,
sandbox: Option<&'a FileSystemSandboxContext>,
) -> ExecutorFileSystemFuture<'a, ()> {
self.inner.write_file(path, contents, sandbox)
self.inner.write_file(path, contents, options, sandbox)
}
fn create_directory<'a>(
@@ -138,6 +143,7 @@ impl ExecutorFileSystem for RecordingFileSystem<'_> {
fn get_metadata<'a>(
&'a self,
path: &'a PathUri,
options: GetMetadataOptions,
sandbox: Option<&'a FileSystemSandboxContext>,
) -> ExecutorFileSystemFuture<'a, FileMetadata> {
self.metadata_files
@@ -157,10 +163,10 @@ impl ExecutorFileSystem for RecordingFileSystem<'_> {
}
notified.await;
}
self.inner.get_metadata(path, sandbox).await
self.inner.get_metadata(path, options, sandbox).await
});
}
self.inner.get_metadata(path, sandbox)
self.inner.get_metadata(path, options, sandbox)
}
fn read_directory<'a>(
+11 -2
View File
@@ -1,6 +1,8 @@
use std::io;
use codex_exec_server::ExecutorFileSystem;
use codex_exec_server::GetMetadataOptions;
use codex_exec_server::ReadFileOptions;
use codex_protocol::protocol::Product;
use codex_skills::SkillDependencies;
use codex_skills::SkillInterface;
@@ -86,7 +88,10 @@ pub(super) async fn load_host_skill_metadata(
SkillMetadataDiscovery::Present(path) => path,
SkillMetadataDiscovery::Absent => return LoadedSkillMetadata::default(),
SkillMetadataDiscovery::Probe(path) => {
match file_system.get_metadata(path, /*sandbox*/ None).await {
match file_system
.get_metadata(path, GetMetadataOptions::default(), /*sandbox*/ None)
.await
{
Ok(metadata) if metadata.is_file => {}
Ok(_) => return LoadedSkillMetadata::default(),
Err(error) if error.kind() == io::ErrorKind::NotFound => {
@@ -105,7 +110,11 @@ pub(super) async fn load_host_skill_metadata(
};
let contents = match file_system
.read_file_text(metadata_path, /*sandbox*/ None)
.read_file_text(
metadata_path,
ReadFileOptions::default(),
/*sandbox*/ None,
)
.await
{
Ok(contents) => contents,