| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| use super::*; | |
| use maka_agent_api::contract::WorkspaceAccess; | |
| use maka_agent_facts::interaction::PermissionCommand; | |
| use maka_agent_facts::interaction::{PermissionRequest, PermissionSource}; | |
| use maka_plugin_core::shell_run::ShellOutput; | |
| use maka_plugin_core::shell_run::ShellPermissions; | |
| use maka_plugin_core::shell_run::ShellRun; | |
| use maka_plugin_core::shell_run::ShellState; | |
| use maka_plugin_core::shell_run::ShellVisibility; | |
| use maka_plugin_core::terminal::TerminalSize; | |
| use maka_plugin_core::terminal::display::Display; | |
| use maka_sandbox::filesystem::{Access, Rule}; | |
| pub(super) struct Captured { | |
| pub(super) executor: maka_process::ShellExecutor, | |
| pub(super) workspace: std::fs::File, | |
| ceiling: Sandbox, | |
| } | |
| pub(super) fn capture( | |
| configuration: &ResolvedConfiguration, | |
| settings: &Configuration, | |
| state_root: &std::path::Path, | |
| shell: &maka_agent_facts::configuration::policy::ShellPolicy, | |
| floor: &Sandbox, | |
| ) -> Result<Captured, ToolError> { | |
| let workspace = &configuration.workspace; | |
| if workspace.worktree.is_some() && !settings.isolated_workspaces { | |
| return Err(failed("Isolated workspace execution is not installed")); | |
| } | |
| let (path, directory, identity) = | |
| maka_workspace_support::capture_selected(workspace).map_err(failed)?; | |
| let workspace_directory = directory.try_clone().map_err(failed)?.into_std_file(); | |
| if configuration.permissions.access == WorkspaceAccess::DangerFullAccess { | |
| let ceiling = Sandbox::Disabled.intersect(floor).map_err(failed)?; | |
| // A managed floor comes from a Run captured below full access. It is that | |
| // Run's approval ceiling, so the command starts without network until a | |
| // grant applies, as it would in a managed Session. `prepare` keeps its | |
| // files within the Run's own ceiling. | |
| let sandbox = match &ceiling { | |
| Sandbox::Managed { filesystem, .. } => Sandbox::Managed { | |
| filesystem: filesystem.clone(), | |
| network: Network::Denied, | |
| }, | |
| unmanaged => unmanaged.clone(), | |
| }; | |
| let executor = | |
| maka_process::ShellExecutor::from_directory(path, directory.into_std_file(), sandbox)?; | |
| return Ok(Captured { | |
| executor: select_shell(platform(executor, settings, state_root), shell)?, | |
| workspace: workspace_directory, | |
| ceiling, | |
| }); | |
| } | |
| let (base, protections) = | |
| maka_workspace_support::sandbox(workspace, configuration.permissions.access, state_root) | |
| .map_err(failed)?; | |
| let mut installed_filesystem = settings.filesystem.clone(); | |
| if workspace.worktree.is_some() { | |
| installed_filesystem | |
| .rules | |
| .push(Rule::subtree(workspace.path.as_str(), Access::Write)); | |
| } | |
| let installation = Sandbox::Managed { | |
| filesystem: installed_filesystem, | |
| network: settings.network.clone(), | |
| }; | |
| let ceiling = installation | |
| .intersect(&protections) | |
| .map_err(failed)? | |
| .intersect(floor) | |
| .map_err(failed)?; | |
| let base = base.intersect(&ceiling).map_err(failed)?; | |
| maka_fs_tools::directory::open(&path, &identity).map_err(failed)?; | |
| let executor = | |
| maka_process::ShellExecutor::from_directory(path, directory.into_std_file(), base)?; | |
| let executor = select_shell(platform(executor, settings, state_root), shell)?; | |
| Ok(Captured { | |
| executor, | |
| workspace: workspace_directory, | |
| ceiling, | |
| }) | |
| } | |
| /// Platform bootstrap shared by Session and explicitly installed setup launches. | |
| pub(super) fn platform( | |
| executor: maka_process::ShellExecutor, | |
| settings: &Configuration, | |
| state_root: &std::path::Path, | |
| ) -> maka_process::ShellExecutor { | |
| let _ = (settings, state_root); | |
| #[cfg(target_os = "linux")] | |
| let executor = match &settings.helper { | |
| Some(helper) => executor.with_network_helper(helper.clone()), | |
| None => executor, | |
| }; | |
| #[cfg(windows)] | |
| let executor = | |
| executor.with_backend(Arc::new(maka_host_runtime::sandbox::windows::Backend::new( | |
| state_root, | |
| settings.helper.as_ref().expect("validated helper"), | |
| ))); | |
| executor | |
| } | |
| fn select_shell( | |
| executor: maka_process::ShellExecutor, | |
| shell: &maka_agent_facts::configuration::policy::ShellPolicy, | |
| ) -> Result<maka_process::ShellExecutor, ToolError> { | |
| let _ = shell; | |
| #[cfg(windows)] | |
| let executor = match shell.preference { | |
| maka_agent_facts::configuration::policy::ShellPreference::Auto => executor, | |
| maka_agent_facts::configuration::policy::ShellPreference::GitBash => { | |
| executor.with_git_bash(std::path::Path::new(&shell.executable))? | |
| } | |
| }; | |
| Ok(executor) | |
| } | |
| impl Processes { | |
| pub(super) async fn execute( | |
| &self, | |
| mut input: Execute, | |
| access: &dyn NativeCallAccess, | |
| configuration: ResolvedConfiguration, | |
| floor: &Sandbox, | |
| cancellation: &CancellationToken, | |
| ) -> Result<Value, ToolError> { | |
| let tool = access.call(); | |
| let requested = input | |
| .additional_permissions | |
| .take() | |
| .map(|permissions| PermissionRequest { | |
| reason: input.justification.take().expect("validated justification"), | |
| command: Some(PermissionCommand { | |
| command: input.command.clone(), | |
| cwd: configuration.workspace.path.clone(), | |
| }), | |
| permissions, | |
| source: PermissionSource::Processes, | |
| }); | |
| let captured = self | |
| .prepare( | |
| access, | |
| &configuration, | |
| requested, | |
| input.shell.take(), | |
| floor, | |
| cancellation, | |
| ) | |
| .await?; | |
| let captured = Captured { | |
| executor: captured.executor.with_login_shell(input.login), | |
| ..captured | |
| }; | |
| let compiler = captured.executor.clone(); | |
| let source = input.command.clone(); | |
| let terminal = input.pty; | |
| let command = tokio::task::spawn_blocking(move || { | |
| if terminal { | |
| compiler.command_pty(&source) | |
| } else { | |
| compiler.command_pipes(&source) | |
| } | |
| }) | |
| .await | |
| .map_err(failed)??; | |
| let current = access.current().await?; | |
| if current.permission_basis() != configuration.permission_basis() | |
| || current.workspace != configuration.workspace | |
| { | |
| return Err(failed( | |
| "Workspace permissions changed before process admission", | |
| )); | |
| } | |
| cancelled(cancellation)?; | |
| if !input.run_in_background { | |
| let output = command | |
| .run_shell(input.timeout_ms, cancellation.clone()) | |
| .await?; | |
| return Ok(output.into_output(captured.executor.cwd().to_owned(), input.command)); | |
| } | |
| let size = TerminalSize::new(80, 24).expect("terminal size"); | |
| let at = maka_agent_authority::now(); | |
| let record = ShellRun { | |
| id: format!("{}{}", self.prefix, uuid::Uuid::new_v4()), | |
| session_id: tool.execution.session_id.clone(), | |
| source_work_id: Some(tool.execution.run_id.to_string()), | |
| correlation: serde_json::json!({ "execution": tool.execution, "operationId": tool.operation_id, "toolUseId": tool.tool_use_id() }), | |
| visibility: ShellVisibility::Model, | |
| permissions: ShellPermissions { | |
| basis: configuration.permission_basis(), | |
| sandbox: captured.executor.sandbox().clone(), | |
| }, | |
| cwd: configuration.workspace.path, | |
| command: input.command, | |
| started_at: at, | |
| updated_at: at, | |
| timeout_ms: input.timeout_ms, | |
| revision: 1, | |
| state: ShellState::Starting, | |
| output: if input.pty { | |
| ShellOutput::Pty { | |
| screen: Arc::new(Display::new(size)), | |
| } | |
| } else { | |
| ShellOutput::Pipes { | |
| stdout: String::new(), | |
| stderr: String::new(), | |
| latest_stream: None, | |
| stdout_truncated: false, | |
| stderr_truncated: false, | |
| } | |
| }, | |
| }; | |
| let custody = tool.custody.clone(); | |
| let mut handle = if input.pty { | |
| self.resources | |
| .start_pty(record, command, size, custody, Some(captured.workspace)) | |
| } else { | |
| self.resources | |
| .start_pipes(record, command, custody, Some(captured.workspace)) | |
| } | |
| .map_err(|error| match error { | |
| ShellError::Process(error) => error, | |
| error => failed(error), | |
| })?; | |
| let record = tokio::select! { | |
| biased; | |
| _ = cancellation.cancelled() => { handle.stop(); handle.drained().await.map_err(worker_error)? }, | |
| ready = handle.ready() => ready.map_err(worker_error)?, | |
| }; | |
| let record = if cancellation.is_cancelled() { | |
| handle.stop(); | |
| handle.drained().await.map_err(worker_error)? | |
| } else { | |
| record | |
| }; | |
| self.project(tool, (*record).clone(), record.state.active()) | |
| .await | |
| } | |
| } | |
| impl Processes { | |
| pub(super) async fn prepare( | |
| &self, | |
| access: &dyn NativeCallAccess, | |
| configuration: &ResolvedConfiguration, | |
| requested: Option<PermissionRequest>, | |
| shell: Option<maka_agent_facts::configuration::policy::ShellPolicy>, | |
| floor: &Sandbox, | |
| cancellation: &CancellationToken, | |
| ) -> Result<Captured, ToolError> { | |
| let shell = match shell { | |
| Some(shell) => shell, | |
| None => { | |
| self.configuration | |
| .runtime_policy() | |
| .await | |
| .map_err(|e| failed(e.to_string()))? | |
| .policy | |
| .shell | |
| } | |
| }; | |
| let settings = self.settings.clone(); | |
| let state_root = self.state_root.clone(); | |
| let captured_configuration = configuration.clone(); | |
| let captured_floor = floor.clone(); | |
| let mut captured = tokio::task::spawn_blocking(move || { | |
| capture( | |
| &captured_configuration, | |
| &settings, | |
| &state_root, | |
| &shell, | |
| &captured_floor, | |
| ) | |
| }) | |
| .await | |
| .map_err(failed)??; | |
| let grants = access | |
| .permission_grants( | |
| access.target(), | |
| &configuration.permission_basis(), | |
| PermissionSource::Processes, | |
| ) | |
| .await?; | |
| // The floor bounds grants; without one the command holds no more than its Run. | |
| let mut sandbox = captured | |
| .executor | |
| .sandbox() | |
| .intersect(access.permission_ceiling()) | |
| .map_err(failed)?; | |
| for grant in grants { | |
| sandbox = sandbox | |
| .with_grant(&grant.permissions, &captured.ceiling) | |
| .map_err(failed)?; | |
| } | |
| if let Some(requested) = requested { | |
| let permissions = requested.permissions; | |
| let required = tokio::task::spawn_blocking(move || { | |
| maka_fs_tools::workspace::permissions::materialize(permissions) | |
| }) | |
| .await | |
| .map_err(failed)? | |
| .map_err(failed)?; | |
| if !captured.ceiling.permits(&required).map_err(failed)? { | |
| return Err(failed( | |
| "Requested access exceeds the process installation or protected workspace boundary", | |
| )); | |
| } | |
| if !sandbox.permits(&required).map_err(failed)? { | |
| let grant = maka_agent_authority::permissions::request( | |
| access, | |
| PermissionRequest { | |
| permissions: required.clone(), | |
| ..requested | |
| }, | |
| access.target(), | |
| cancellation, | |
| ) | |
| .await?; | |
| sandbox = sandbox | |
| .with_grant(&grant.permissions, &captured.ceiling) | |
| .map_err(failed)?; | |
| } | |
| if !sandbox.permits(&required).map_err(failed)? { | |
| return Err(failed( | |
| "Not all requested process permissions were approved; command was not started", | |
| )); | |
| } | |
| } | |
| let sandbox = sandbox.intersect(floor).map_err(failed)?; | |
| captured.executor = | |
| launch_executor(captured.executor, sandbox, self.capture_network_route()).await?; | |
| Ok(captured) | |
| } | |
| pub(super) async fn capture_network_route(&self) -> Result<maka_network::Policy, ToolError> { | |
| let network = self | |
| .configuration | |
| .network_configuration() | |
| .await | |
| .map_err(|_| failed("Process network configuration unavailable"))?; | |
| maka_network::Policy::from_host_settings(&network.proxy, network.password.as_deref()) | |
| .await | |
| .map_err(|_| failed("Process network route unavailable")) | |
| } | |
| } | |
| pub(super) async fn launch_executor( | |
| executor: maka_process::ShellExecutor, | |
| sandbox: Sandbox, | |
| route: impl Future<Output = Result<maka_network::Policy, ToolError>>, | |
| ) -> Result<maka_process::ShellExecutor, ToolError> { | |
| let executor = executor.with_sandbox(sandbox); | |
| if executor.sandbox().uses_network_route() { | |
| Ok(executor.with_network_route(route.await?)) | |
| } else { | |
| Ok(executor) | |
| } | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| use maka_agent_facts::configuration::policy::{ShellPolicy, ShellPreference}; | |
| #[tokio::test] | |
| async fn disabled_launch_runs_without_reading_network_configuration() { | |
| let executor = | |
| maka_process::ShellExecutor::new(std::env::current_dir().unwrap(), Sandbox::Disabled) | |
| .unwrap() | |
| .with_login_shell(false); | |
| let mut reads = 0; | |
| let executor = | |
| launch_executor(executor, Sandbox::Disabled, unavailable_network(&mut reads)) | |
| .await | |
| .unwrap(); | |
| #[cfg(unix)] | |
| let script = "printf proxy-not-needed"; | |
| #[cfg(windows)] | |
| let script = "echo proxy-not-needed"; | |
| let result = executor | |
| .command_pipes(script) | |
| .unwrap() | |
| .run_with_input(Vec::new(), 1024, 10_000, CancellationToken::new()) | |
| .await | |
| .unwrap(); | |
| assert_eq!(result.stdout.trim(), "proxy-not-needed"); | |
| assert_eq!(reads, 0); | |
| } | |
| #[tokio::test] | |
| async fn restricted_launch_is_blocked_if_network_configuration_is_unavailable() { | |
| let executor = | |
| maka_process::ShellExecutor::new(std::env::current_dir().unwrap(), Sandbox::Disabled) | |
| .unwrap(); | |
| let sandbox = Sandbox::Managed { | |
| filesystem: Policy::uniform(Access::Read), | |
| network: Network::Restricted { | |
| destinations: Default::default(), | |
| }, | |
| }; | |
| let mut reads = 0; | |
| let result = launch_executor(executor, sandbox, unavailable_network(&mut reads)).await; | |
| assert!( | |
| matches!(result, Err(ToolError::Failed(message)) if message == "Process network configuration unavailable") | |
| ); | |
| assert_eq!(reads, 1); | |
| } | |
| async fn unavailable_network(reads: &mut usize) -> Result<maka_network::Policy, ToolError> { | |
| *reads += 1; | |
| Err(failed("Process network configuration unavailable")) | |
| } | |
| #[cfg(windows)] | |
| #[test] | |
| fn managed_worktree_shell_keeps_writes_inside_its_workspace() { | |
| use maka_fs_tools::worktree::Worktrees; | |
| use std::{fs, process::Command, sync::atomic::AtomicBool}; | |
| let fixture = tempfile::tempdir().unwrap(); | |
| let source = fixture.path().join("source"); | |
| let state = fixture.path().join("state"); | |
| fs::create_dir(&source).unwrap(); | |
| for args in [ | |
| &["init", "--quiet", "--object-format=sha1"][..], | |
| &[ | |
| "-c", | |
| "user.name=Maka tests", | |
| "-c", | |
| "user.email=tests@maka.invalid", | |
| "-c", | |
| "commit.gpgsign=false", | |
| "commit", | |
| "--quiet", | |
| "--allow-empty", | |
| "-m", | |
| "base", | |
| ], | |
| ] { | |
| let result = Command::new("git") | |
| .current_dir(&source) | |
| .env("GIT_CONFIG_NOSYSTEM", "1") | |
| .env("GIT_CONFIG_GLOBAL", "NUL") | |
| .args(args) | |
| .output() | |
| .unwrap(); | |
| assert!( | |
| result.status.success(), | |
| "{}", | |
| String::from_utf8_lossy(&result.stderr) | |
| ); | |
| } | |
| let worktrees = Worktrees::open(&state.join("subagent-worktrees")).unwrap(); | |
| let cancel = Arc::new(AtomicBool::new(false)); | |
| let binding = worktrees | |
| .plan(&source, &"a".repeat(64), cancel.clone()) | |
| .unwrap(); | |
| worktrees.ensure(&binding, &cancel).unwrap(); | |
| let workspace = binding.directory().to_owned(); | |
| let configuration: ResolvedConfiguration = serde_json::from_value(serde_json::json!({ | |
| "title": "Worktree", "titleIsManual": false, "labels": [], | |
| "workspace": {"path": workspace, "worktree": binding}, | |
| "binding": {"id": uuid::Uuid::new_v4(), "permissionEpoch": 0}, | |
| "permissions": {"access": "workspace_write", "requestPermissions": false}, | |
| "boundary_revision": 0 | |
| })) | |
| .unwrap(); | |
| let settings = Configuration { | |
| isolated_workspaces: true, | |
| filesystem: Policy::uniform(Access::Read), | |
| network: Network::Allowed, | |
| helper: Some(std::env::current_exe().unwrap()), | |
| setup_directory: None, | |
| session_owner: None, | |
| }; | |
| let shell = ShellPolicy { | |
| preference: ShellPreference::Auto, | |
| executable: String::new(), | |
| titles: false, | |
| }; | |
| let state = fs::canonicalize(state).unwrap(); | |
| let captured = capture( | |
| &configuration, | |
| &settings, | |
| &state, | |
| &shell, | |
| &Sandbox::Disabled, | |
| ) | |
| .unwrap(); | |
| let Sandbox::Managed { filesystem, .. } = captured.executor.sandbox() else { | |
| panic!("expected a managed workspace sandbox"); | |
| }; | |
| let policy = filesystem.compile().unwrap(); | |
| assert_eq!( | |
| policy.access(&captured.executor.cwd().join("output")), | |
| Access::Write | |
| ); | |
| let state = maka_fs_tools::workspace::project::host_path(&state).unwrap(); | |
| assert_eq!( | |
| policy.access(&std::path::Path::new(state).join("private")), | |
| Access::Deny | |
| ); | |
| } | |
| /// A full-access Session can run a Run captured below full access, such as a | |
| /// read-only external recovery Run. Its commands start without network. | |
| #[test] | |
| fn full_access_commands_start_with_the_runs_base_network() { | |
| let directory = std::env::temp_dir().join(uuid::Uuid::new_v4().to_string()); | |
| std::fs::create_dir(&directory).unwrap(); | |
| let path = std::fs::canonicalize(&directory).unwrap(); | |
| let configuration: ResolvedConfiguration = serde_json::from_value(serde_json::json!({ | |
| "title": "Full access", "titleIsManual": false, "labels": [], | |
| "workspace": {"path": path}, | |
| "binding": {"id": uuid::Uuid::new_v4(), "permissionEpoch": 0}, | |
| "permissions": {"access": "danger_full_access", "requestPermissions": false}, | |
| "boundary_revision": 0 | |
| })) | |
| .unwrap(); | |
| let settings: Configuration = serde_json::from_value(serde_json::json!({ | |
| "filesystem": {"default": "write", "rules": [], "denyGlobs": []}, | |
| "network": "allowed", | |
| "helper": null | |
| })) | |
| .unwrap(); | |
| let shell = ShellPolicy { | |
| preference: ShellPreference::Auto, | |
| executable: String::new(), | |
| titles: false, | |
| }; | |
| let capped = Sandbox::Managed { | |
| filesystem: Policy { | |
| default: Access::Read, | |
| rules: Vec::new(), | |
| deny_globs: Vec::new(), | |
| }, | |
| // The Run's network approval ceiling, raised in the floor of process tools. | |
| network: Network::Allowed, | |
| }; | |
| let captured = capture(&configuration, &settings, &path, &shell, &capped).unwrap(); | |
| let networks = | |
| [captured.executor.sandbox(), &captured.ceiling].map(|sandbox| match sandbox { | |
| Sandbox::Managed { network, .. } => network.clone(), | |
| other => panic!("expected a managed sandbox, got {other:?}"), | |
| }); | |
| assert_eq!(networks, [Network::Denied, Network::Allowed]); | |
| let captured = | |
| capture(&configuration, &settings, &path, &shell, &Sandbox::Disabled).unwrap(); | |
| assert_eq!(captured.executor.sandbox(), &Sandbox::Disabled); | |
| std::fs::remove_dir_all(&directory).unwrap(); | |
| } | |
| } |