Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix: review round — spec 409 shape, revision guard, curl portability, comment/ordering fixes
高然43c19af2a5d9
diff --git a/components/egress/fastsandbox.go b/components/egress/fastsandbox.goindex 39ebd0459..145aa5514 100644--- a/components/egress/fastsandbox.go+++ b/components/egress/fastsandbox.go@@ -116,12 +116,31 @@ func runFastSandboxProfile(ctx context.Context, upstreamSpec *mitmproxy.Upstreamreg := subject.NewRegistry(alwaysDeny, alwaysAllow)fastSandboxSrv := newFastSandboxPolicyServer(ctx, reg, podNft)+ // Register the configured state-directory basenames BEFORE either+ // constructor runs: the volume-root restriction must recognize the+ // configured sibling as ours even on custom-named deployments.+ vaultDir := strings.TrimSpace(os.Getenv(constants.EnvEgressVaultDir))+ policyDir := strings.TrimSpace(os.Getenv(constants.EnvEgressPolicyDir))+ if policyDir == "" {+ log.Fatalf("fast-sandbox policy persistence: OPENSANDBOX_EGRESS_POLICY_DIR is required (the recorded policy is egress-held state)")+ }+ registerStateDirName(vaultDir)+ registerStateDirName(policyDir)+ // The two state directories are siblings holding same-named per-subject+ // files (<uid>.json) and seal markers: aliasing them would make each+ // persister clobber the other's state (a restart-time rehydrate of one+ // kind quarantines the other kind's file). Reject before any state is+ // written.+ if vaultDir != "" && policyDir != "" && sameStateDir(vaultDir, policyDir) {+ log.Fatalf("fast-sandbox policy persistence: OPENSANDBOX_EGRESS_POLICY_DIR must differ from OPENSANDBOX_EGRESS_VAULT_DIR (both resolve to %s)", policyDir)+ }+// Vault persistence (OSEP-0022 stage 2): per-subject vault files under// OPENSANDBOX_EGRESS_VAULT_DIR so an egress restart recovers locally at// SET_BINDING replay. Empty/unset keeps the vault memory-only. Fail fast// on an unusable directory: silently serving memory-only would break the// acked ⇒ durable contract.- if vaultDir := strings.TrimSpace(os.Getenv(constants.EnvEgressVaultDir)); vaultDir != "" {+ if vaultDir != "" {vp, err := newVaultPersistence(vaultDir)if err != nil {log.Fatalf("fast-sandbox vault persistence: %v", err)@@ -130,35 +149,6 @@ func runFastSandboxProfile(ctx context.Context, upstreamSpec *mitmproxy.Upstreamlog.Infof("fast-sandbox vault persistence enabled at %s (one file per subject, atomic commit protocol)", vaultDir)}- // Policy persistence: REQUIRED for the fast-sandbox profile. Per-subject- // policy files under OPENSANDBOX_EGRESS_POLICY_DIR make the recorded- // policy egress-held state — an egress restart recovers it locally at- // SET_BINDING replay (the committed file supersedes the replayed binding- // input) and the POLICY_EXPORT / POLICY_IMPORT snapshot transfer carries- // it across pause/resume. There is no memory-only posture: the binding- // input must never become a policy recovery source, so an unusable- // directory fails startup.- policyDir := strings.TrimSpace(os.Getenv(constants.EnvEgressPolicyDir))- if policyDir == "" {- log.Fatalf("fast-sandbox policy persistence: OPENSANDBOX_EGRESS_POLICY_DIR is required (the recorded policy is egress-held state)")- }- // Register the configured state-directory basenames BEFORE either- // constructor runs: the volume-root restriction must recognize the- // configured sibling as ours even on custom-named deployments.- registerStateDirName(policyDir)- if vaultDir := strings.TrimSpace(os.Getenv(constants.EnvEgressVaultDir)); vaultDir != "" {- registerStateDirName(vaultDir)- }- // The two state directories are siblings holding same-named per-subject- // files (<uid>.json) and seal markers: aliasing them would make each- // persister clobber the other's state (a restart-time rehydrate of one- // kind quarantines the other kind's file). Reject before any state is- // written.- if vaultDir := strings.TrimSpace(os.Getenv(constants.EnvEgressVaultDir)); vaultDir != "" {- if sameStateDir(vaultDir, policyDir) {- log.Fatalf("fast-sandbox policy persistence: OPENSANDBOX_EGRESS_POLICY_DIR must differ from OPENSANDBOX_EGRESS_VAULT_DIR (both resolve to %s)", policyDir)- }- }pp, err := newPolicyPersistence(policyDir)if err != nil {log.Fatalf("fast-sandbox policy persistence: %v", err)diff --git a/components/egress/fastsandbox_server.go b/components/egress/fastsandbox_server.goindex c53b1ee90..5a979e0cc 100644--- a/components/egress/fastsandbox_server.go+++ b/components/egress/fastsandbox_server.go@@ -91,8 +91,9 @@ type fastSandboxPolicyServer struct {// pendingPolicies (denying subjects); this entry carries only the// bookkeeping the vault entry carries for vault state.policies map[subject.Subject]*policyEntry- // policyFiles owns the per-subject policy files; nil keeps the policy- // memory-only (the default).+ // policyFiles owns the per-subject policy files. Always non-nil in the+ // fast-sandbox profile (startup fails without it); nil only in unit+ // tests that assemble the server directly.policyFiles *policyPersistence// policyRecovery is the policy persistence recovery latch (same// mechanics as vaultRecovery).diff --git a/components/egress/tests/smoke-fast-sandbox.sh b/components/egress/tests/smoke-fast-sandbox.shindex 2f535758c..5dfca3bed 100755--- a/components/egress/tests/smoke-fast-sandbox.sh+++ b/components/egress/tests/smoke-fast-sandbox.sh@@ -474,7 +474,7 @@ case "${http_code}" in301|307|308) ;; # redirects into the /policy/{uid} relay subtree*) fail "bare /policy must redirect into the relay subtree, got ${http_code}" ;;esac-location="$(curl -s -o /dev/null -w '%{header_json}' "http://127.0.0.1:${POLICY_PORT}/policy" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("location","")[0])' 2>/dev/null || true)"+location="$(curl -s -D - -o /dev/null "http://127.0.0.1:${POLICY_PORT}/policy" | tr -d '\r' | sed -n 's/^[Ll]ocation: //p')"[ "${location}" = "/policy/" ] || fail "bare /policy redirect target must be /policy/, got ${location:-none}"pass "superseded proxy-route surfaces removed (relay surfaces only)"diff --git a/server/opensandbox_server/services/fast_sandbox/policy_relay.py b/server/opensandbox_server/services/fast_sandbox/policy_relay.pyindex d5cd1c314..509928186 100644--- a/server/opensandbox_server/services/fast_sandbox/policy_relay.py+++ b/server/opensandbox_server/services/fast_sandbox/policy_relay.py@@ -134,6 +134,11 @@ def _relay_get_full(service, sandbox_id: str) -> dict:status.HTTP_503_SERVICE_UNAVAILABLE,"Network policy relay record is missing the policy body.",)+ if not isinstance(state.get("revision"), int):+ raise policy_relay_error(+ status.HTTP_503_SERVICE_UNAVAILABLE,+ "Network policy relay record is missing the revision.",+ )return statediff --git a/specs/sandbox-lifecycle.yml b/specs/sandbox-lifecycle.ymlindex 51e6df2e9..d9c1a142a 100644--- a/specs/sandbox-lifecycle.yml+++ b/specs/sandbox-lifecycle.yml@@ -80,8 +80,7 @@ paths:'404':$ref: '#/components/responses/NotFound''409':- description: Fsb - the sandbox is paused; its recorded policy is- sealed until resume.+ $ref: '#/components/responses/Conflict''500':$ref: '#/components/responses/InternalServerError''503':