mod backend;
mod baseline;
mod browser_spawner;
mod callback_runner;
#[cfg(all(feature = "sandbox", any(target_os = "linux", target_os = "macos")))]
mod confinement;
#[cfg(not(all(feature = "sandbox", any(target_os = "linux", target_os = "macos"))))]
#[path = "confinement/unsupported.rs"]
mod confinement;
mod gpg;
mod host_path;
mod host_resources;
mod process_runner;
pub use backend::{
LiveProcessBackend, LocalProcessBackend, PreparedLiveReplace, SessionGrantClearPolicy,
UnrestrictedPermissionedProcessRunnerFactory,
};
pub use baseline::{default_development_path_rules, default_development_path_rules_with_env};
pub use browser_spawner::BrowserProcessSpawner;
pub use callback_runner::{
CallbackBackgroundSupervisor, CallbackProcessExecutor, CallbackProcessOutput,
CallbackProcessRequest, CallbackProcessResponder, CallbackProcessRunner,
CallbackProcessTerminalStatus,
};
#[cfg(all(feature = "sandbox", any(target_os = "linux", target_os = "macos")))]
pub use confinement::BrowserSandboxProfile;
pub use confinement::{probe_workspace_sandbox, sandbox_helper_main, sandbox_helper_requested};
pub use gpg::GpgAgentSockets;
pub use host_path::resolve_host_path;
pub use host_resources::{HostPathKind, HostPathMetadata, ssh_known_hosts};
pub use process_runner::{
DEFAULT_REMOVED_ENV_PREFIX, ProcessEnvIsolation, ProcessSessionPermissions, TokioProcessRunner,
action_process_path,
};
#[cfg(all(feature = "sandbox", any(target_os = "linux", target_os = "macos")))]
pub use process_runner::{
WorkspaceProcessEnvironment, WorkspaceProcessRunner, WorkspaceProcessRunnerFactory,
};
use merry_core::{CapabilityPolicy, HostPathPolicy};
use merry_runtime::{
AcceptedLocalWorkspaceProcessAdmission, HostIntegration, PathAccess, PathAccessRule,
PathAccessRuleSource, PermissionedProcessRunnerFactory, ProcessRunner, ProcessRunnerError,
ProcessSessionPermissionView,
};
use std::{
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProcessBackendMode {
Host,
Isolated,
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ProcessBackendError {
#[error("process backend configuration failed: {source}")]
Runner {
#[source]
source: ProcessRunnerError,
},
#[error(
"isolated local process backend is unavailable on target operating system `{target_os}`"
)]
UnsupportedIsolation {
target_os: &'static str,
},
}
impl From<ProcessRunnerError> for ProcessBackendError {
fn from(source: ProcessRunnerError) -> Self {
Self::Runner { source }
}
}
#[derive(Clone)]
pub struct ProcessSession {
admission: AcceptedLocalWorkspaceProcessAdmission,
runner: Arc<dyn ProcessRunner>,
permissioned_factory: Arc<dyn PermissionedProcessRunnerFactory>,
permission_view: Option<ProcessSessionPermissionView>,
session_permissions: Option<ProcessSessionPermissions>,
static_path_rules: Vec<PathAccessRule>,
workspace_writable: bool,
network_policy: CapabilityPolicy,
allowed_domains: Vec<String>,
browser_allow_local: bool,
live_policy: Option<merry_runtime::LivePolicyHandle>,
live_env_isolation: Option<Arc<std::sync::RwLock<ProcessEnvIsolation>>>,
env_isolation: ProcessEnvIsolation,
}
impl ProcessSession {
#[must_use]
pub fn from_parts(
admission: AcceptedLocalWorkspaceProcessAdmission,
runner: Arc<dyn ProcessRunner>,
permissioned_factory: Arc<dyn PermissionedProcessRunnerFactory>,
) -> Self {
Self {
admission,
runner,
permissioned_factory,
permission_view: None,
session_permissions: None,
static_path_rules: Vec::new(),
workspace_writable: true,
network_policy: CapabilityPolicy::Ask,
allowed_domains: Vec::new(),
browser_allow_local: true,
live_policy: None,
live_env_isolation: None,
env_isolation: ProcessEnvIsolation::default(),
}
}
#[must_use]
pub fn with_permission_view(mut self, view: ProcessSessionPermissionView) -> Self {
self.permission_view = Some(view);
self
}
#[must_use]
pub fn with_session_permissions(mut self, permissions: ProcessSessionPermissions) -> Self {
self.permission_view = Some(permissions.view());
self.session_permissions = Some(permissions);
self
}
#[must_use]
pub fn with_access_policy_inputs(
mut self,
static_path_rules: Vec<PathAccessRule>,
workspace_writable: bool,
) -> Self {
self.static_path_rules = static_path_rules;
self.workspace_writable = workspace_writable;
self
}
#[must_use]
pub fn with_network_policy_inputs(
mut self,
network_policy: CapabilityPolicy,
allowed_domains: Vec<String>,
browser_allow_local: bool,
) -> Self {
self.network_policy = network_policy;
self.allowed_domains = allowed_domains;
self.browser_allow_local = browser_allow_local;
self
}
#[must_use]
pub fn with_live_policy(mut self, policy: merry_runtime::LivePolicyHandle) -> Self {
self.live_policy = Some(policy);
self
}
#[must_use]
pub fn with_live_env_isolation(
mut self,
env_isolation: Arc<std::sync::RwLock<ProcessEnvIsolation>>,
) -> Self {
self.live_env_isolation = Some(env_isolation);
self
}
#[must_use]
pub fn with_env_isolation(mut self, env_isolation: ProcessEnvIsolation) -> Self {
self.env_isolation = env_isolation;
self
}
#[must_use]
pub const fn workspace_writable(&self) -> bool {
self.workspace_writable
}
#[must_use]
pub fn has_live_policy(&self) -> bool {
self.live_policy.is_some()
}
#[must_use]
pub fn env_isolation(&self) -> &ProcessEnvIsolation {
&self.env_isolation
}
#[must_use]
pub const fn admission(&self) -> AcceptedLocalWorkspaceProcessAdmission {
self.admission
}
#[must_use]
pub fn runner(&self) -> Arc<dyn ProcessRunner> {
Arc::clone(&self.runner)
}
#[must_use]
pub fn permissioned_factory(&self) -> Arc<dyn PermissionedProcessRunnerFactory> {
Arc::clone(&self.permissioned_factory)
}
#[must_use]
pub fn permission_view(&self) -> Option<ProcessSessionPermissionView> {
self.permission_view.clone()
}
#[must_use]
pub fn session_permissions(&self) -> Option<ProcessSessionPermissions> {
self.session_permissions.clone()
}
#[must_use]
pub fn workspace_access_policy(
&self,
workspace_root: &Path,
workspace_writable: bool,
) -> merry_runtime::WorkspaceAccessPolicy {
let mut policy = merry_runtime::WorkspaceAccessPolicy::new(
workspace_root,
self.workspace_writable && workspace_writable,
)
.with_static_rules(self.static_path_rules.iter().cloned());
if let Some(view) = self.permission_view.clone() {
policy = policy.with_session_permissions(view);
}
policy
}
#[must_use]
pub fn live_policy_handle(
&self,
workspace_root: &Path,
workspace_writable: bool,
) -> merry_runtime::LivePolicyHandle {
if let Some(live) = &self.live_policy {
return live.clone();
}
let restricted = self.admission.sandbox_profile()
!= merry_runtime::LocalWorkspaceProcessSandboxProfile::Host;
let workspace_access = restricted.then(|| {
merry_runtime::WorkspaceAccessPolicy::new(
workspace_root,
self.workspace_writable && workspace_writable,
)
.with_static_rules(self.static_path_rules.iter().cloned())
});
let baseline = merry_runtime::LivePolicyBaseline::new(
restricted,
workspace_access,
self.product_private_paths(),
self.network_access_policy(),
);
let mut handle = merry_runtime::LivePolicyHandle::new(baseline);
if let Some(view) = self.permission_view.clone() {
handle = handle.with_session_permissions(view);
}
handle
}
#[must_use]
pub fn network_access_policy(&self) -> merry_runtime::NetworkAccessPolicy {
let mut policy =
merry_runtime::NetworkAccessPolicy::new(self.network_policy, self.browser_allow_local)
.with_allowed_domains(&self.allowed_domains);
if let Some(view) = self.permission_view.clone() {
policy = policy.with_session_permissions(view);
}
policy
}
#[must_use]
pub fn product_private_paths(&self) -> Vec<PathBuf> {
merry_runtime::extract_product_private_paths(&self.static_path_rules)
}
#[must_use]
pub fn browser_spawner(&self, workspace_root: impl Into<PathBuf>) -> BrowserProcessSpawner {
let workspace_root = workspace_root.into();
let live_policy = self.live_policy_handle(&workspace_root, self.workspace_writable);
let live_env = self
.live_env_isolation
.clone()
.unwrap_or_else(|| Arc::new(std::sync::RwLock::new(self.env_isolation.clone())));
BrowserProcessSpawner::new(live_policy, workspace_root, live_env)
}
}
#[derive(Debug, Clone)]
pub struct ProcessSessionRequest {
pub(crate) workspace_root: Option<PathBuf>,
pub(crate) workspace_writable: bool,
pub(crate) inherited_permissions: Option<ProcessSessionPermissionView>,
pub(crate) extra_path_rules: Vec<PathAccessRule>,
}
impl Default for ProcessSessionRequest {
fn default() -> Self {
Self {
workspace_root: None,
workspace_writable: true,
inherited_permissions: None,
extra_path_rules: Vec::new(),
}
}
}
impl ProcessSessionRequest {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_workspace_root(mut self, workspace_root: impl Into<PathBuf>) -> Self {
self.workspace_root = Some(workspace_root.into());
self
}
#[must_use]
pub fn read_only_workspace(mut self) -> Self {
self.workspace_writable = false;
self
}
#[must_use]
pub fn with_workspace_writable(mut self, writable: bool) -> Self {
self.workspace_writable = writable;
self
}
#[must_use]
pub fn with_inherited_permissions(mut self, view: ProcessSessionPermissionView) -> Self {
self.inherited_permissions = Some(view);
self
}
#[must_use]
pub fn with_extra_path_rules(
mut self,
rules: impl IntoIterator<Item = PathAccessRule>,
) -> Self {
self.extra_path_rules = rules.into_iter().collect();
self
}
#[must_use]
pub fn workspace_root(&self) -> Option<&Path> {
self.workspace_root.as_deref()
}
#[must_use]
pub const fn workspace_writable(&self) -> bool {
self.workspace_writable
}
#[must_use]
pub fn inherited_permissions(&self) -> Option<&ProcessSessionPermissionView> {
self.inherited_permissions.as_ref()
}
#[must_use]
pub fn extra_path_rules(&self) -> &[PathAccessRule] {
&self.extra_path_rules
}
}
pub trait ProcessBackend: Send + Sync {
fn new_session(&self) -> ProcessSession;
fn new_session_with(
&self,
_request: ProcessSessionRequest,
) -> Result<ProcessSession, ProcessBackendError> {
Ok(self.new_session())
}
}
#[derive(Debug, Clone)]
pub struct ProcessBackendOptions {
pub(crate) path_rules: Vec<PathAccessRule>,
pub(crate) network_policy: CapabilityPolicy,
pub(crate) allowed_domains: Vec<String>,
pub(crate) browser_allow_local: bool,
pub(crate) workspace_writable: bool,
pub(crate) host_integrations: Vec<HostIntegration>,
pub(crate) denied_host_integrations: Vec<HostIntegration>,
pub(crate) gpg_agent_sockets: Option<GpgAgentSockets>,
pub(crate) environment_overrides: Vec<(OsString, OsString)>,
pub(crate) env_isolation: ProcessEnvIsolation,
}
impl Default for ProcessBackendOptions {
fn default() -> Self {
Self {
path_rules: Vec::new(),
network_policy: CapabilityPolicy::Ask,
allowed_domains: Vec::new(),
browser_allow_local: true,
workspace_writable: true,
host_integrations: Vec::new(),
denied_host_integrations: Vec::new(),
gpg_agent_sockets: None,
environment_overrides: Vec::new(),
env_isolation: ProcessEnvIsolation::default(),
}
}
}
impl ProcessBackendOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_path_rules(mut self, path_rules: impl IntoIterator<Item = PathAccessRule>) -> Self {
self.path_rules = path_rules.into_iter().collect();
self
}
#[must_use]
pub fn extend_path_rules(
mut self,
path_rules: impl IntoIterator<Item = PathAccessRule>,
) -> Self {
self.path_rules.extend(path_rules);
self
}
#[must_use]
pub fn with_default_development_baseline(self, home: &Path) -> Self {
self.extend_path_rules(default_development_path_rules(home))
}
#[must_use]
pub fn with_host_path_policy(mut self, policy: &HostPathPolicy) -> Self {
for path in policy.product_writable_config() {
self.path_rules.push(PathAccessRule::new(
path.clone(),
PathAccess::ReadWrite,
PathAccessRuleSource::ProductConfig,
));
}
if let Some(memory_root) = policy.durable_memory_root() {
self.path_rules.push(PathAccessRule::new(
memory_root.to_path_buf(),
PathAccess::ReadWrite,
PathAccessRuleSource::DurableMemory,
));
}
for path in policy.product_readonly() {
self.path_rules.push(PathAccessRule::new(
path.clone(),
PathAccess::ReadOnly,
PathAccessRuleSource::ProductReadOnly,
));
}
for path in policy.denied() {
self.path_rules.push(PathAccessRule::new(
path.clone(),
PathAccess::Deny,
PathAccessRuleSource::ProductPrivate,
));
}
self
}
#[must_use]
pub fn with_network_policy(mut self, policy: CapabilityPolicy) -> Self {
self.network_policy = policy;
self
}
#[must_use]
pub fn with_allowed_domains<I, S>(mut self, domains: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.allowed_domains = domains.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn with_browser_allow_local(mut self, allow_local: bool) -> Self {
self.browser_allow_local = allow_local;
self
}
#[must_use]
pub fn with_network_requests_allowed(mut self, allowed: bool) -> Self {
self.network_policy = if allowed {
CapabilityPolicy::Ask
} else {
CapabilityPolicy::Deny
};
self
}
#[must_use]
pub fn read_only_workspace(mut self) -> Self {
self.workspace_writable = false;
self
}
#[must_use]
pub fn with_host_integrations(
mut self,
host_integrations: impl IntoIterator<Item = HostIntegration>,
) -> Self {
self.host_integrations = host_integrations.into_iter().collect();
self.denied_host_integrations
.retain(|denied| !self.host_integrations.contains(denied));
self
}
#[must_use]
pub fn with_denied_host_integrations(
mut self,
denied: impl IntoIterator<Item = HostIntegration>,
) -> Self {
self.denied_host_integrations = denied.into_iter().collect();
self.host_integrations
.retain(|allowed| !self.denied_host_integrations.contains(allowed));
self
}
#[must_use]
pub fn with_host_integration_policy(
mut self,
integration: HostIntegration,
policy: CapabilityPolicy,
) -> Self {
self.host_integrations.retain(|item| *item != integration);
self.denied_host_integrations
.retain(|item| *item != integration);
match policy {
CapabilityPolicy::Allow => self.host_integrations.push(integration),
CapabilityPolicy::Deny => self.denied_host_integrations.push(integration),
CapabilityPolicy::Ask => {}
}
self
}
#[must_use]
pub fn with_gpg_agent_sockets(mut self, sockets: GpgAgentSockets) -> Self {
self.gpg_agent_sockets = Some(sockets);
self
}
#[must_use]
pub fn with_environment_overrides(
mut self,
environment_overrides: impl IntoIterator<Item = (OsString, OsString)>,
) -> Self {
self.environment_overrides = environment_overrides.into_iter().collect();
self
}
#[must_use]
pub fn with_env_isolation(mut self, env_isolation: ProcessEnvIsolation) -> Self {
self.env_isolation = env_isolation;
self
}
#[must_use]
pub fn path_rules(&self) -> &[PathAccessRule] {
&self.path_rules
}
#[must_use]
pub const fn network_policy(&self) -> CapabilityPolicy {
self.network_policy
}
#[must_use]
pub fn allowed_domains(&self) -> &[String] {
&self.allowed_domains
}
#[must_use]
pub const fn browser_allow_local(&self) -> bool {
self.browser_allow_local
}
#[must_use]
pub const fn network_requests_allowed(&self) -> bool {
self.network_policy.allows_requests()
}
#[must_use]
pub fn host_integrations(&self) -> &[HostIntegration] {
&self.host_integrations
}
#[must_use]
pub fn denied_host_integrations(&self) -> &[HostIntegration] {
&self.denied_host_integrations
}
#[must_use]
pub fn environment_overrides(&self) -> &[(OsString, OsString)] {
&self.environment_overrides
}
}
#[cfg(test)]
mod tests;