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

@@ -74,7 +74,7 @@ async fn remote_file_system_sends_path_and_sandbox_cwd_uris_without_native_conve
for path in &paths {
assert_eq!(
file_system
.read_file(path, Some(&sandbox))
.read_file(path, Default::default(), Some(&sandbox))
.await
.expect("remote read should succeed"),
Vec::<u8>::new()
@@ -85,6 +85,7 @@ async fn remote_file_system_sends_path_and_sandbox_cwd_uris_without_native_conve
.into_iter()
.map(|path| FsReadFileParams {
path,
follow_symlinks: None,
sandbox: Some(sandbox.clone()),
})
.collect::<Vec<_>>();
@@ -114,8 +115,8 @@ async fn concurrent_remote_metadata_requests_share_only_in_flight_results() {
let path = PathUri::parse("file:///workspace/project/AGENTS.md").expect("valid path URI");
let (first, second) = tokio::join!(
file_system.get_metadata(&path, /*sandbox*/ None),
file_system.get_metadata(&path, /*sandbox*/ None),
file_system.get_metadata(&path, Default::default(), /*sandbox*/ None),
file_system.get_metadata(&path, Default::default(), /*sandbox*/ None),
);
let expected = FileMetadata {
is_directory: false,
@@ -133,14 +134,18 @@ async fn concurrent_remote_metadata_requests_share_only_in_flight_results() {
let initializing_path = path.clone();
let initializer = tokio::spawn(async move {
initializing_file_system
.get_metadata(&initializing_path, /*sandbox*/ None)
.get_metadata(
&initializing_path,
Default::default(),
/*sandbox*/ None,
)
.await
});
abandoned_request_rx
.await
.expect("server should receive the abandoned metadata request");
let follower = file_system.get_metadata(&path, /*sandbox*/ None);
let follower = file_system.get_metadata(&path, Default::default(), /*sandbox*/ None);
tokio::pin!(follower);
assert!(futures::poll!(follower.as_mut()).is_pending());
assert_eq!(
@@ -175,6 +180,7 @@ async fn concurrent_remote_metadata_requests_share_only_in_flight_results() {
vec![
FsGetMetadataParams {
path: path.clone(),
follow_symlinks: None,
sandbox: None,
};
3
@@ -204,8 +210,8 @@ async fn concurrent_remote_metadata_errors_are_shared_but_retried() {
let path = PathUri::parse("file:///workspace/project/AGENTS.md").expect("valid path URI");
let (first, second) = tokio::join!(
file_system.get_metadata(&path, /*sandbox*/ None),
file_system.get_metadata(&path, /*sandbox*/ None),
file_system.get_metadata(&path, Default::default(), /*sandbox*/ None),
file_system.get_metadata(&path, Default::default(), /*sandbox*/ None),
);
assert_eq!(
first.expect_err("first metadata error").kind(),
@@ -217,7 +223,7 @@ async fn concurrent_remote_metadata_errors_are_shared_but_retried() {
);
assert_eq!(
file_system
.get_metadata(&path, /*sandbox*/ None)
.get_metadata(&path, Default::default(), /*sandbox*/ None)
.await
.expect("failed metadata request should be retried")
.size,
@@ -228,10 +234,12 @@ async fn concurrent_remote_metadata_errors_are_shared_but_retried() {
vec![
FsGetMetadataParams {
path: path.clone(),
follow_symlinks: None,
sandbox: None,
},
FsGetMetadataParams {
path,
follow_symlinks: None,
sandbox: None,
},
]
@@ -279,21 +287,30 @@ async fn remote_metadata_starts_fresh_after_intervening_filesystem_mutation() {
.await
.expect("remote filesystem client should connect");
let stale_request = file_system.get_metadata(&path, /*sandbox*/ None);
let stale_request =
file_system.get_metadata(&path, Default::default(), /*sandbox*/ None);
tokio::pin!(stale_request);
assert!(futures::poll!(stale_request.as_mut()).is_pending());
let result = match mutation {
MetadataMutation::Write => {
file_system
.write_file(&path, b"updated".to_vec(), /*sandbox*/ None)
.write_file(
&path,
b"updated".to_vec(),
Default::default(),
/*sandbox*/ None,
)
.await
}
MetadataMutation::CreateDirectory => {
file_system
.create_directory(
&path,
CreateDirectoryOptions { recursive: true },
CreateDirectoryOptions {
recursive: true,
follow_symlinks: true,
},
/*sandbox*/ None,
)
.await
@@ -305,6 +322,7 @@ async fn remote_metadata_starts_fresh_after_intervening_filesystem_mutation() {
RemoveOptions {
recursive: true,
force: true,
follow_symlinks: true,
},
/*sandbox*/ None,
)
@@ -333,7 +351,7 @@ async fn remote_metadata_starts_fresh_after_intervening_filesystem_mutation() {
}
let (refreshed, stale) = tokio::join!(
file_system.get_metadata(&path, /*sandbox*/ None),
file_system.get_metadata(&path, Default::default(), /*sandbox*/ None),
stale_request.as_mut(),
);
assert_eq!(stale.expect("original metadata request").size, 42);
@@ -343,6 +361,7 @@ async fn remote_metadata_starts_fresh_after_intervening_filesystem_mutation() {
vec![
FsGetMetadataParams {
path: path.clone(),
follow_symlinks: None,
sandbox: None,
};
2
@@ -386,15 +405,15 @@ async fn remote_metadata_requests_do_not_cross_path_or_sandbox_boundaries() {
);
let (first, second) = tokio::join!(
file_system.get_metadata(&first_path, /*sandbox*/ None),
file_system.get_metadata(&second_path, /*sandbox*/ None),
file_system.get_metadata(&first_path, Default::default(), /*sandbox*/ None),
file_system.get_metadata(&second_path, Default::default(), /*sandbox*/ None),
);
first.expect("metadata for first path");
second.expect("metadata for second path");
let (first, second) = tokio::join!(
file_system.get_metadata(&first_path, Some(&sandbox)),
file_system.get_metadata(&first_path, Some(&sandbox)),
file_system.get_metadata(&first_path, Default::default(), Some(&sandbox)),
file_system.get_metadata(&first_path, Default::default(), Some(&sandbox)),
);
first.expect("first sandboxed metadata request");
second.expect("second sandboxed metadata request");