Files
ai_academy/internal/localsandbox/seatbelt/compiler.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

181 lines
6.5 KiB
Go

// Package seatbelt implements the sandbox backend for macOS on top of
// /usr/bin/sandbox-exec. The policy compiler in this package carries no build
// tag so it stays testable on every platform.
package seatbelt
import (
_ "embed"
"fmt"
"strings"
"academy/internal/localsandbox/core"
)
//go:embed base.sbpl
var seatbeltBasePolicy string
// seatbeltProgram is a compiled policy ready to hand to sandbox-exec.
type seatbeltProgram struct {
// Profile is the sbpl text. It never contains a caller-supplied path.
Profile string
// Params are the -DNAME=VALUE arguments carrying those paths.
Params []string
}
// compileSeatbelt turns a core.Policy into sbpl text plus -D parameters.
//
// Every path travels as a parameter and is referenced from the profile as
// (param "NAME"). Inlining paths would require escaping them into both string
// and regex contexts, which is exactly the class of bug this avoids.
func compileSeatbelt(p core.Policy) (seatbeltProgram, error) {
if err := p.Validate(); err != nil {
return seatbeltProgram{}, err
}
var params []string
// Four ordered tiers. sbpl gives precedence to the rule written later, so
// the order below *is* the policy:
//
// writeAllows workspace writes, network
// privateDenies the user's home, denied for reading as a whole
// readAllows workspace + toolchains, re-opened inside that deny
// denies credentials and root anchors, final say
//
// Reads cannot simply sit in the first tier: the base profile grants
// blanket file-read*, so the private deny has to come after every broad
// allow, and the paths it must not cover have to come after it in turn.
var writeAllows []string
var privateDenies []string
var readAllows []string
var denies []string
for i, root := range p.WritableRoots {
rootParam := fmt.Sprintf("WRITABLE_ROOT_%d", i)
params = append(params, fmt.Sprintf("-D%s=%s", rootParam, root.Path))
clauses := []string{fmt.Sprintf(`(subpath (param %q))`, rootParam)}
for j, sub := range root.ReadOnlySubpaths {
subParam := fmt.Sprintf("%s_EXCLUDED_%d", rootParam, j)
params = append(params, fmt.Sprintf("-D%s=%s", subParam, sub))
// Both forms are required: subpath alone still permits creating
// the protected directory itself.
clauses = append(clauses,
fmt.Sprintf(`(require-not (literal (param %q)))`, subParam),
fmt.Sprintf(`(require-not (subpath (param %q)))`, subParam),
)
}
writeAllows = append(writeAllows,
fmt.Sprintf("(allow file-write* (require-all %s))", strings.Join(clauses, " ")),
)
readAllows = append(readAllows,
fmt.Sprintf("(allow file-read* (subpath (param %q)))", rootParam),
)
// The writable root is the authority boundary the next policy build
// reuses; it must not be replaceable by a symlink.
denies = append(denies, fmt.Sprintf(
`(deny file-write-unlink (require-all (literal (param %q)) (vnode-type DIRECTORY)))`,
rootParam))
for j := range root.ReadOnlySubpaths {
subParam := fmt.Sprintf("%s_EXCLUDED_%d", rootParam, j)
// Renaming the parent of a protected subtree would move it out of
// its carve-out, so deny unlinking the carve-out itself too.
denies = append(denies, fmt.Sprintf(
`(deny file-write-unlink (require-all (literal (param %q)) (vnode-type DIRECTORY)))`,
subParam))
}
}
for i, private := range p.PrivateRoots {
param := fmt.Sprintf("PRIVATE_ROOT_%d", i)
params = append(params, fmt.Sprintf("-D%s=%s", param, private))
// Read only. Writing is already confined to the writable roots, and
// denying writes here would revoke the workspace along with them.
privateDenies = append(privateDenies,
fmt.Sprintf("(deny file-read* (subpath (param %q)))", param))
}
for i, root := range p.ReadableRoots {
param := fmt.Sprintf("READABLE_ROOT_%d", i)
params = append(params, fmt.Sprintf("-D%s=%s", param, root))
// A readable root may be a single file — a shell startup script —
// which subpath does not match on its own.
readAllows = append(readAllows,
fmt.Sprintf("(allow file-read* (subpath (param %q)))", param),
fmt.Sprintf("(allow file-read* (literal (param %q)))", param),
)
}
if len(p.WritableRoots) > 0 {
// Host temp opens with the first writable root, for chat and install
// alike. A policy with none (Ask mode before approval) must not write
// anywhere. /tmp is a symlink of /private/tmp and Seatbelt matches the
// resolved vnode, so both names are required. /var/folders stays closed.
writeAllows = append(writeAllows,
`(allow file-write* (subpath "/tmp") (subpath "/private/tmp"))`)
}
networkRules, err := seatbeltNetworkRules(p)
if err != nil {
return seatbeltProgram{}, err
}
writeAllows = append(writeAllows, networkRules...)
for i, deny := range p.DenyRead {
param := fmt.Sprintf("DENY_READ_%d", i)
params = append(params, fmt.Sprintf("-D%s=%s", param, deny))
denies = append(denies,
fmt.Sprintf("(deny file-read* (subpath (param %q)))", param),
fmt.Sprintf("(deny file-read* (literal (param %q)))", param),
fmt.Sprintf("(deny file-write* (subpath (param %q)))", param),
fmt.Sprintf("(deny file-write* (literal (param %q)))", param),
)
}
sections := []string{seatbeltBasePolicy}
sections = append(sections, writeAllows...)
sections = append(sections, privateDenies...)
sections = append(sections, readAllows...)
// Credential denies and root anchors come last so nothing above can
// reopen them.
sections = append(sections, denies...)
return seatbeltProgram{
Profile: strings.Join(sections, "\n") + "\n",
Params: params,
}, nil
}
func seatbeltNetworkRules(p core.Policy) ([]string, error) {
switch p.Network {
case core.NetworkDenied:
// No allow rule at all; (deny default) in the base policy covers it.
return nil, nil
case core.NetworkUnrestricted:
return []string{
"(allow network-outbound)",
"(allow network-inbound)",
"(allow network-bind)",
`(allow system-socket)`,
}, nil
case core.NetworkLoopback:
if len(p.LoopbackPorts) == 0 {
// Fail closed rather than silently widening to every port.
return nil, fmt.Errorf(
"localsandbox: loopback network mode requires at least one port")
}
rules := []string{`(allow system-socket)`}
for _, port := range p.LoopbackPorts {
if port < 1 || port > 65535 {
return nil, fmt.Errorf("localsandbox: invalid loopback port %d", port)
}
// sandbox-exec rejects 127.0.0.1 here: "host must be * or localhost".
rules = append(rules,
fmt.Sprintf(`(allow network-outbound (remote ip "localhost:%d"))`, port))
}
return rules, nil
default:
return nil, fmt.Errorf("localsandbox: unknown network mode %d", p.Network)
}
}