读见

GitHub - microsoft/mxc: 基于策略的、分层隔离与封锁

原题GitHub - microsoft/mxc: Policy-driven, layered isolation and containment

约 4 分钟约 807 字原文英文已翻译

读前速览

4 个要点

MXC 是一款跨平台沙箱式代码执行系统,通过统一的策略驱动接口提供多种封锁后端(从操作系统原生进程沙箱到完整虚拟机),用于在 Windows、Linux 和 macOS 上安全执行不受信任的代码。

  1. 跨平台兼容性

    MXC 支持 Windows、Linux 和 macOS,并利用平台特定的封锁后端,确保在不同操作系统上的一致安全性。

  2. 策略驱动配置

    该系统使用基于 JSON 的配置模型来定义安全策略,从而可以精细控制文件系统访问、网络出口和用户界面交互。

  3. 多种封锁后端

    MXC 提供多种隔离选项,从轻量级进程容器到重量级微虚拟机,允许用户根据具体工作负载选择适当的安全性级别。

  4. 开发友好的 SDK

    该项目提供 Rust、.NET 和 Node.js SDK,使得开发者可以轻松将沙盒功能集成到现有应用中。

完整摘要

Microsoft eXecution Container (MXC) 是一款用于在隔离环境中运行不受信任代码的通用型 SDK。它支持多种封锁后端,包括进程容器、Windows 沙箱、LXC 和微虚拟机,全部通过 JSON 策略系统进行配置。该框架对文件系统、网络和 UI 访问的控制非常严密,并提供用于诊断沙箱故障的工具。MXC 提供 Rust、.NET 和 Node SDK,便于将安全执行环境集成到各种应用中。

目录 · 16 节
查看原文

Microsoft eXecution Container (MXC)

MXC 是一个沙箱化代码执行系统,可在 Windows、Linux 和 macOS 上运行不受信任的代码(模型输出、插件和工具)。它提供多种隔离后端,从操作系统原生进程沙箱到完整虚拟机,并通过统一的隔离模型和类型化 SDK 提供服务。MXC is a sandboxed code execution system for running untrusted code (model output, plugins, and tools) on Windows, Linux, and macOS. It provides multiple containment backends, from OS-native process sandboxes to full VMs, behind a unified containment model and typed SDKs.

功能Features

  • 跨平台:支持 Windows、Linux 和 macOS,并采用适合各平台的隔离后端。
    Cross-platform: Windows, Linux, and macOS support with platform-appropriate containment backends
  • 基于 JSON 的配置:版本化的容器创建请求与安全策略
    JSON-based configuration: Versioned container-creation requests and security policies
  • Multiple containment backends: ProcessContainer, Windows Sandbox, LXC, Bubblewrap, Seatbelt, MicroVM (Nanvix), Hyperlight, IsolationSession, and WSLC
  • Policy-driven sandboxing:
    • Filesystem policy: Read-only, read-write, and denied path lists
    • Network policy: Proxy support, outbound controls, and backend-dependent host filtering
    • UI policy: Clipboard, display, and GUI access controls
  • 状态感知型生命周期:持久化容器的预配、启动、执行、停止与取消预配
    State-aware lifecycle: Provision, start, execute, stop, and deprovision persistent containers
  • Rust、.NET 和 Node SDK:适用于一次性执行和状态感知执行的版本化 API
    Rust, .NET, and Node SDKs: Versioned APIs for one-shot and state-aware execution
  • 诊断:用于了解容器中访问被拒绝故障的工具
    Diagnostics: Tools to understand access-denied failures in a container

MXC 是什么?What is MXC?

MXC 是一项会随应用一起构建的 SDK 依赖项。MXC is an SDK dependency that builds into your app.

flowchart LR
    App["Your application<br/>Launch API"] --> SDK["MXC SDK<br/>Rust / .NET / Node<br/>(in process)"]
    SDK --> Backend["Selected backend<br/>(in process)"]
    Backend --> Container["Isolated workload<br/>ProcessContainer / WSLC / Bubblewrap / ..."]
Loading

您的应用程序指定:Your application specifies:

  • 容器类型
    The container type
  • 遏制规则
    The containment rules
  • 工作负载命令
    The workload command

MXC 验证请求、选择后端,并在生成的容器中启动工作负载。MXC validates the request, selects the backend, and launches the workload in the resulting container.

支持哪些容器类型?What container types are supported?

MXC 会在 Windows、Linux 和 macOS 上通过适用于各平台的容器后端运行工作负载。MXC runs workloads through platform-appropriate container backends on Windows, Linux, and macOS.

Runtime platform Default backend Other backends Minimum host OS
Windows 11 x64 / ARM64 processcontainer windows_sandbox*, wslc, microvm*, hyperlight*, isolation_session Windows OS-version support
Linux x64 / ARM64 bubblewrap lxc, microvm, hyperlight -
macOS ARM64 / x64 seatbelt - -

* 这些后端尚处于实验阶段。* These backends are experimental.

如何使用 MXC?How do I use MXC?

通过包管理器安装 SDK。你无需克隆此代码仓库。Install an SDK through your package manager. You do not need to clone this repository.

SDK Package
Rust https://crates.io/crates/mxc-sdk
.NET https://www.nuget.org/packages/Microsoft.Mxc.Sdk
Node https://www.npmjs.com/package/@microsoft/mxc-sdk

Node 和 .NET 包含原生运行时资源。Rust crate 会将 MXC SDK、引擎和所选后端构建到使用该 crate 的应用程序中。The Node and .NET packages include the native runtime assets. The Rust crate builds the MXC SDK, engine, and selected backends into the consuming application.

非 SDK 使用方式:平台专用的执行器二进制文件(例如 wxc-exec.exe)接受符合稳定架构定义的 JSON 容器创建请求。可用于测试,或在无法将 SDK 嵌入应用时使用。Non-SDK consumption: Platform-specific executor binaries, such as wxc-exec.exe, accept JSON container-creation requests defined by the stable schema. Use for testing or when the SDK cannot be embedded in your app.

运行受限工作负载Running a contained workload

完整的 SDK 示例请参阅 Rust、.NET 和 Node 示例。For complete SDK samples, see the Rust, .NET, and Node samples.

Node 示例代码片段Sample Node snippet

import { spawn, type ContainerRequest } from '@microsoft/mxc-sdk/v1';

const request: ContainerRequest = {
  command: 'node -e "console.log(\'hello from container\')"',
  network: { egress: { default: 'deny' } },
  timeoutMs: 30_000,
};

const child = await spawn(request);

请参阅可运行的流式标准输入/输出示例和 SDK API 参考。See the runnable streaming standard-I/O sample and the SDK API reference.

我的应用无法在沙盒中运行!My application won't run in the sandbox!

在你有时间调整隔离规则之前,应用在沙盒中运行时可能会遇到访问问题。我们随时为你提供帮助。Your application will hit access issues when running in a sandbox, until you've had time to tune your containment rules. We're here to help.

调试控制台模式Debug console mode

原生执行器通常将标准输入、标准输出和标准错误保留给工作负载。使用 --debug 输出 MXC 诊断信息:Native executors normally reserve standard input, output, and error for the workload. Use --debug for MXC diagnostic output:

wxc-exec.exe --debug config.json

请参阅 MXC 诊断,获取完整的开发者参考资料。See MXC diagnostics for the complete developer reference.

审计模式Audit mode

警告:--audit 会关闭正在分析的工作负载的所有沙箱安全保护。切勿使用它运行不受信任的代码。

Warning: --audit turns off all sandbox security for the workload being analyzed. Never use it to run untrusted code.

审计模式可帮助策略作者查找访问被拒绝的故障,并重建 ProcessContainer 策略,以授予可信工具实际需要的文件访问权限和功能。在受支持的 Windows 版本上运行:Audit mode helps a policy author find access-denied failures and reconstruct a ProcessContainer policy that grants the files and capabilities a trusted tool actually needs. On supported Windows releases, run:

wxc-exec.exe --audit policy.json

MXC 会记录观察到的访问,并生成策略编写工件。有关安全的拒绝并记录诊断、审计输出和受支持的工作流,请参阅“记录被拒绝的访问”。MXC records the observed accesses and produces policy-authoring artifacts. See logging access denied for safe deny-and-record diagnostics, audit outputs, and supported workflows.

遥测Telemetry

官方 Microsoft 构建版本可能会向 Microsoft 发送可选诊断遥测数据。除非单次运行选择加入、Windows 用户已明确同意、管理策略允许收集,且应用程序在受限工作负载请求中启用了遥测选项,否则遥测处于关闭状态。管理员可以阻止遥测,但不能代替用户授予同意。Official Microsoft builds can send optional diagnostic telemetry to Microsoft. Telemetry is off unless the individual run opts in, the Windows user has explicitly consented, administrative policy permits collection, and your application enables the telemetry option in a contained workload request. An administrator can block telemetry but cannot grant consent for the user.

本地开源构建不会配置为将遥测数据发送至 Microsoft,且遥测在非 Windows 平台上不起作用。有关控制选项和隐私详情,请参阅遥测政策和同意说明。Local open-source builds are not configured to route telemetry to Microsoft, and telemetry is a no-op on non-Windows platforms. See telemetry policy and consent for controls and privacy details.

从源代码构建Building from source

在开发 MXC、更改原生运行时,或使用独立执行器二进制文件而非打包的 SDK 时,请从源代码构建。仓库构建会生成平台原生运行时和执行器,并暂存 Node SDK 使用的原生资源。Build from source when developing MXC, changing the native runtime, or using the standalone executor binaries instead of a packaged SDK. Repository builds produce the platform-native runtime and executors and stage the native assets used by the Node SDK.

构建前提条件如下:Build prerequisites are:

  • Rust, pinned to version 1.93 by src/rust-toolchain.toml
  • Node.js 24 或更高版本以及 npm
    Node.js 24 or later and npm
  • 所选后端描述的平台工具链和先决条件
    The platform toolchain and prerequisites described by the selected backend

构建Build

WindowsWindows

build.bat --all             # Release build for current architecture

LinuxLinux

./build.sh --all            # Release build

macOSmacOS

./build-mac.sh --all        # Release build for native architecture

文档Documentation

消费者文档Consumer documentation

Document Repository location Purpose
SDK samples samples/ Runnable Rust, .NET, and Node scenarios
SDK API reference docs/api-reference/ Supported V1 operations and types
Container lifecycle docs/container-lifecycle.md Persistent container lifecycle overview
Logging access denied docs/logging-access-denied.md Diagnose blocked accesses and author policy
Telemetry docs/telemetry.md Consent and administrative controls
Backend guides docs/backends/ Platform and backend prerequisites and behavior

仓库贡献者应从 MXC 开发文档开始。Repository contributors should start with the MXC development documentation.

贡献者指南Contributing

贡献指南请参阅 CONTRIBUTING.md。See CONTRIBUTING.md for contribution guidelines.

许可证License

详情请参阅 LICENSE.md。See LICENSE.md for details.

值得记下

MXC 是一个用于在 Windows、Linux 和 macOS 上运行不受信任代码(模型输出、插件和工具)的沙箱式代码执行系统。
该框架在统一的封锁模型和类型化 SDK 后端提供多种封锁后端,从操作系统原生进程沙箱到完整虚拟机。
您的应用将指定容器类型、封锁规则和工作负载命令。MXC 将验证请求、选择后端并启动一个容器内的相应工作负载。