Scope extension filesystem access to callback permissions (#49898)

## Why

Executor skill reads need to use the environments and permissions captured for the current step, including newly approved grants. Cached continuation pages must remain bound to the filesystem and permissions that produced them.

## What changed

- Supply borrowed `EnvironmentAccess` to turn-input contributors and extension tools, with session and originating-turn grants already applied.
- Require executor skill reads to use filesystem access from an environment available to the callback, and replace separate optional read contexts with `SkillReadContext`.
- Bind skill pagination snapshots to `EnvironmentAccessKey`, which identifies the source filesystem and callback permissions.
- Route image-generation filesystem operations through the same accessor.

## Testing

Extend skill-read integration coverage to verify that a denied reference becomes readable after a turn-scoped grant is captured by the next model step. Update Windows coverage to verify that restricted reads fail when the executor cannot enforce a sandbox, and add a test requiring turn environment selection before exposing capability roots.

GitOrigin-RevId: 825b49302319965835d61c77fc5bcdb951e10543
This commit is contained in:
Adam Perry @ OpenAI
2026-10-01 06:29:03 +00:00
committed by copyberry
parent 08a7031b6a
commit da2e174a66
22 changed files with 559 additions and 294 deletions
-18
View File
@@ -3774,24 +3774,6 @@ impl Session {
extension_data.insert(selected_capability_roots.clone());
if let Some(discovery) = &executor_capability_discovery {
extension_data.insert(discovery.as_ref().clone());
if !discovery.sandbox_contexts().is_empty() {
extension_data.insert(discovery.sandbox_contexts().clone());
}
} else if !turn_context
.permission_profile_for_environments(&environments)
.file_system_sandbox_policy()
.has_full_disk_read_access()
{
let sandbox_contexts = environments
.turn_environments()
.map(|environment| {
(
environment.selection.environment_id.clone(),
environment.sandbox_context(/*additional_permissions*/ None),
)
})
.collect::<HashMap<_, _>>();
extension_data.insert(sandbox_contexts);
}
let (mcp, prepared_recommendations) = tokio::join!(
// MCP refresh can be large; keep it off the sampling request's stack.
+15
View File
@@ -8,9 +8,11 @@ use crate::environment_selection::TurnEnvironmentSnapshot;
use crate::realtime_conversation::RealtimeConversationSnapshot;
use crate::session::step_settings::ResolvedStepSettings;
use crate::session::turn_context::TurnContext;
use crate::session::turn_context::TurnEnvironment;
use crate::tools::router::ToolRouter;
use codex_exec_server::ExecutorCapabilityDiscoverySnapshot;
use codex_exec_server::ResolvedSelectedCapabilityRoot;
use codex_file_system::EnvironmentAccess;
use codex_mcp::McpBinding;
use codex_otel::SessionTelemetry;
use codex_protocol::items::ModelInvocationContext;
@@ -44,6 +46,19 @@ pub(crate) struct StepContext {
}
impl StepContext {
/// Pairs the step's environments with access using current session and originating-turn grants.
pub(crate) fn environments(&self) -> Vec<(&TurnEnvironment, impl EnvironmentAccess + '_)> {
self.environments
.turn_environments()
.map(|environment| {
let grants = self
.turn
.granted_permissions(&environment.selection.environment_id);
(environment, environment.fs_accessor(grants))
})
.collect()
}
/// Persist the context captured for this request, even after a live update.
pub(crate) fn to_turn_context_item(&self) -> TurnContextItem {
let mut item = self.turn.to_turn_context_item();
+34
View File
@@ -9935,6 +9935,40 @@ async fn mcp_refresh_detects_shared_auth_manager_changes() {
);
}
/// A ready registry entry alone must not expose roots outside the turn's environment selection.
#[tokio::test]
async fn capability_roots_require_turn_environment_selection() {
let (mut session, turn_context) = make_session_and_context().await;
let environment = turn_context
.initial_environments
.primary()
.expect("ready local environment");
let root = codex_protocol::capabilities::SelectedCapabilityRoot {
id: "selected-root".to_string(),
location: codex_protocol::capabilities::CapabilityRootLocation::Environment {
environment_id: environment.selection.environment_id.clone(),
path: environment.cwd().clone(),
},
};
session.services.selected_capability_roots = vec![root.clone()];
let unselected_roots = session
.resolve_selected_capability_roots_for_step(&TurnEnvironmentSnapshot::default())
.await;
assert!(unselected_roots.is_empty());
let selected_roots = session
.resolve_selected_capability_roots_for_step(&turn_context.initial_environments)
.await;
assert_eq!(
selected_roots
.iter()
.map(|root| root.selected_root().clone())
.collect::<Vec<_>>(),
vec![root]
);
}
#[tokio::test]
#[tracing_test::traced_test]
async fn conflicting_ready_environment_root_ids_keep_first_location() {
+5 -6
View File
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::atomic::Ordering;
@@ -1186,15 +1185,15 @@ async fn build_extension_turn_input_items(
return Some(Vec::new());
}
let environments = step_context
.environments
.turn_environments()
let environment_accessors = step_context.environments();
let environments = environment_accessors
.iter()
.enumerate()
.map(|(index, environment)| TurnInputEnvironment {
_lifetime: PhantomData,
.map(|(index, (environment, fs))| TurnInputEnvironment {
environment_id: environment.selection.environment_id.clone(),
cwd: environment.cwd().clone(),
is_primary: index == 0,
fs,
})
.collect::<Vec<_>>();
+13
View File
@@ -17,6 +17,8 @@ use codex_core_plugins::ResolvedPluginMetricsOperation;
use codex_core_plugins::TrustedPluginRoots;
use codex_exec_server::ExecutorFileSystem;
use codex_extension_api::SelectedPluginSnapshot;
use codex_file_system::EnvironmentAccess;
use codex_file_system::FileSystemEnvironmentAccessor;
use codex_file_system::FileSystemSandboxContext;
use codex_model_provider::SharedModelProvider;
use codex_protocol::SessionId;
@@ -215,6 +217,17 @@ impl TurnEnvironment {
self.config().permission_profile.permission_profile()
}
/// Borrows this snapshot's filesystem using its permissions and any additional grants.
pub(crate) fn fs_accessor(
&self,
additional_permissions: Option<AdditionalPermissionProfile>,
) -> impl EnvironmentAccess + '_ {
FileSystemEnvironmentAccessor::new(
self.environment.filesystem_ref(),
self.sandbox_context(additional_permissions),
)
}
/// Sandbox context for this environment, including any additional permission grants.
pub(crate) fn sandbox_context(
&self,
@@ -1,4 +1,3 @@
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::Weak;
@@ -18,12 +17,10 @@ use codex_tools::TurnItemEmissionFuture;
use codex_tools::TurnItemEmitter;
use codex_utils_string::to_ascii_json_string;
use crate::sandboxing::SandboxPermissions;
use crate::session::session::Session;
use crate::session::turn_context::TurnContext;
use crate::tools::context::ToolInvocation;
use crate::tools::context::ToolPayload;
use crate::tools::handlers::apply_granted_turn_permissions;
use crate::tools::lifecycle::extension_tool_call_source;
use crate::tools::registry::CoreToolRuntime;
use crate::tools::registry::ToolExecutor;
@@ -66,7 +63,22 @@ impl ToolExecutor<ToolInvocation> for ExtensionToolAdapter {
where
ToolInvocation: 'a,
{
Box::pin(async move { self.0.handle(to_extension_call(&invocation).await).await })
Box::pin(async move {
let environment_accessors = invocation.step_context.environments();
let environments = environment_accessors
.iter()
.map(|(environment, fs)| {
ToolEnvironment::new(
environment.selection.environment_id.clone(),
environment.cwd().clone(),
fs,
)
})
.collect();
self.0
.handle(to_extension_call(&invocation, environments).await)
.await
})
}
}
@@ -164,7 +176,10 @@ impl TurnItemEmitter for CoreTurnItemEmitter {
}
}
async fn to_extension_call(invocation: &ToolInvocation) -> ExtensionToolCall<'_> {
async fn to_extension_call<'a>(
invocation: &ToolInvocation,
environments: Vec<ToolEnvironment<'a>>,
) -> ExtensionToolCall<'a> {
let history = invocation
.session
.clone_history()
@@ -184,25 +199,6 @@ async fn to_extension_call(invocation: &ToolInvocation) -> ExtensionToolCall<'_>
&invocation.step_context.settings,
))
.and_then(|metadata| to_ascii_json_string(&metadata).ok());
let mut environments = Vec::new();
for environment in invocation.step_context.environments.turn_environments() {
let additional_permissions = apply_granted_turn_permissions(
&invocation.step_context,
environment,
environment.cwd(),
SandboxPermissions::UseDefault,
/*additional_permissions*/ None,
)
.additional_permissions;
let file_system_sandbox_context = environment.sandbox_context(additional_permissions);
environments.push(ToolEnvironment {
_lifetime: PhantomData,
environment_id: environment.selection.environment_id.clone(),
cwd: environment.cwd().clone(),
file_system: environment.environment.get_filesystem(),
file_system_sandbox_context,
});
}
ExtensionToolCall {
turn_id: invocation.turn.sub_id.clone(),
call_id: invocation.call_id.clone(),
@@ -355,7 +351,7 @@ mod tests {
*self.captured_sandbox_cwds.lock().await = call
.environments
.iter()
.map(|environment| environment.file_system_sandbox_context.cwd.clone())
.map(|environment| environment.cwd.clone())
.collect();
let call = codex_tools::ToolCall {
environments: Vec::new(),