Secure, Fast, and Extensible Sandbox runtime for AI agents.
feat(server): relay fsb policy CRUD to the egress-held record; harden relay fencing
高然c454d47a6991
diff --git a/components/egress/fastsandbox_actions.go b/components/egress/fastsandbox_actions.goindex 68d31ef32..a595e29ff 100644--- a/components/egress/fastsandbox_actions.go+++ b/components/egress/fastsandbox_actions.go@@ -113,9 +113,10 @@ func (s *fastSandboxPolicyServer) handleActionsStatus(w http.ResponseWriter, r *// Capability signal for the Fastlet's policy transfer: without policy// persistence there is no recorded policy to hand off, so the// transfer degrades (skips) rather than failing the checkpoint.- PolicyTransfer: s.policyFiles != nil,- PolicyRecovery: policyRecovery,- DegradedVaults: degraded,+ PolicyTransfer: s.policyFiles != nil,+ PolicyRecovery: policyRecovery,+ DegradedPolicies: s.degradedPolicyUIDs(),+ DegradedVaults: degraded,}if s.mitmGate != nil {resp.MitmCaFingerprint = s.mitmGate.CAFingerprint()@@ -236,7 +237,7 @@ func (s *fastSandboxPolicyServer) applySetBinding(env *actionhandler.Envelope) (if err != nil {return http.StatusInternalServerError, fmt.Errorf("deny-first install: %w", err)}- s.recordBindingState(subj, att)+ s.recordBindingState(subj, att, fence)if env.Binding.IsRemoval() {// Binding removed from a still-live sandbox: drop the pending policy// (a re-added binding gets a fresh input — never apply the removeddiff --git a/components/egress/fastsandbox_policy.go b/components/egress/fastsandbox_policy.goindex 9b09391e1..9f2b154ed 100644--- a/components/egress/fastsandbox_policy.go+++ b/components/egress/fastsandbox_policy.go@@ -41,7 +41,6 @@ import ("net/http""strings"- "github.com/alibaba/opensandbox/egress/pkg/actionhandler""github.com/alibaba/opensandbox/egress/pkg/constants""github.com/alibaba/opensandbox/egress/pkg/log""github.com/alibaba/opensandbox/egress/pkg/policy"@@ -61,6 +60,10 @@ type policyEntry struct {// sealed marks a paused subject whose policy file was exported into the// snapshot bundle: every relay op answers 409; enforcement is unchanged.sealed bool+ // degraded is non-empty while the persisted policy could not be+ // reloaded: the record stays default-deny (fail closed) until a+ // successful relay write heals it.+ degraded string}// policyRelayDocument is the GET response and PUT body of the relay@@ -119,6 +122,9 @@ func (s *fastSandboxPolicyServer) selectRelayPolicy(r *http.Request, subj subjecpol := s.reg.UserPolicy(subj)s.mu.Lock()defer s.mu.Unlock()+ if !s.fenceMatchesLocked(subj, fence) {+ return nil, http.StatusNotFound, "unknown subject"+ }if e := s.policies[subj]; e != nil && e.sealed {return nil, http.StatusConflict, "subject policy is sealed (paused); the bundle copy is already taken"}@@ -210,6 +216,11 @@ func (s *fastSandboxPolicyServer) handlePolicyRelayWrite(w http.ResponseWriter,// Guards + decision under one s.mu hold, serialized against rebinds and// terminal purges (the whole write already holds policyCommitMu).s.mu.Lock()+ if !s.fenceMatchesLocked(subj, fence) {+ s.mu.Unlock()+ http.Error(w, "unknown subject", http.StatusNotFound)+ return+ }entry := s.policies[subj]if entry != nil && entry.sealed {s.mu.Unlock()@@ -225,7 +236,6 @@ func (s *fastSandboxPolicyServer) handlePolicyRelayWrite(w http.ResponseWriter,if entry != nil {cur = entry.revision}- att, attOK := s.subjAtt[subj]s.mu.Unlock()if r.Method == http.MethodDelete {@@ -234,7 +244,7 @@ func (s *fastSandboxPolicyServer) handlePolicyRelayWrite(w http.ResponseWriter,return}// Deletion is an authoritative tombstone at the successor revision.- s.deleteRelayPolicy(w, subj, fence, att, attOK, cur+1)+ s.deleteRelayPolicy(w, subj, fence, cur+1)return}@@ -303,6 +313,8 @@ func (s *fastSandboxPolicyServer) putRelayPolicy(w http.ResponseWriter, r *http.if state != subject.StateActive {s.pendingPolicies[subj] = pol}+ // A fresh entry also clears a degraded mark: the successful write+ // IS the heal.s.policies[subj] = &policyEntry{revision: newRev, fence: fence}s.mu.Unlock()return nil@@ -370,12 +382,13 @@ func putResponseStatus(created bool) int {}// deleteRelayPolicy commits the deletion as an authoritative default-deny-// tombstone: enforcement reverts to deny-first AND the record persists at-// the successor revision (memory + file), so an egress restart reloads the-// deletion instead of letting the replayed bootstrap binding resurrect the-// deleted policy. GET serves the deny record; a later PUT replaces it at-// the successor revision. The caller holds policyCommitMu.-func (s *fastSandboxPolicyServer) deleteRelayPolicy(w http.ResponseWriter, subj subject.Subject, fence subject.Fencing, att actionhandler.NetworkAttachment, attOK bool, newRev int64) {+// tombstone: the record persists at the successor revision (memory + file)+// and enforcement becomes default-deny — in place on an active subject+// (which stays active, so a later relay PUT re-enables immediately), held+// pending on a denying one. An egress restart reloads the tombstone+// instead of letting the replayed bootstrap binding resurrect the deleted+// policy. GET serves the deny record. The caller holds policyCommitMu.+func (s *fastSandboxPolicyServer) deleteRelayPolicy(w http.ResponseWriter, subj subject.Subject, fence subject.Fencing, newRev int64) {deny := policy.DefaultDenyPolicy()apply := func() error {state, err := s.policyCommitTarget(subj, fence)@@ -383,19 +396,22 @@ func (s *fastSandboxPolicyServer) deleteRelayPolicy(w http.ResponseWriter, subjreturn err}if state == subject.StateActive {- // Enforcement first: nft deny-first commits before the registry- // state (fail-closed transition), mirroring SET_BINDING removal.- if !attOK {- return fmt.Errorf("subject %s has no recorded attachment for the deny-first revert", subj)- }- if err := s.revertToDenyFirst(subj, att); err != nil {+ // Enforcement first: the deny tombstone applies IN PLACE — the+ // subject stays active with default-deny enforced (the operator+ // always overlay keeps applying), so a later relay PUT+ // re-enables traffic immediately; the generic handler relay+ // never replays a data-plane-ready hook to activate a+ // pending-only replacement. nft commits before the registry+ // state (fail-closed transition).+ if err := s.applyPolicy(subj, deny); err != nil {return err}}s.mu.Lock()// The activation barrier keeps working across a hook replay: a// replayed data-plane-ready activates the deny record, never a- // stale policy.+ // stale policy. On an active subject the pending deny is+ // idempotent with what was just enforced.s.pendingPolicies[subj] = denys.policies[subj] = &policyEntry{revision: newRev, fence: fence}s.mu.Unlock()diff --git a/components/egress/fastsandbox_policy_persist.go b/components/egress/fastsandbox_policy_persist.goindex 9c60fa9b3..e27ea92ce 100644--- a/components/egress/fastsandbox_policy_persist.go+++ b/components/egress/fastsandbox_policy_persist.go@@ -29,6 +29,7 @@ import ("fmt""os""path/filepath"+ "sort""sync""github.com/alibaba/opensandbox/egress/pkg/log"@@ -315,12 +316,12 @@ func (s *fastSandboxPolicyServer) clearPolicyRecord(subj subject.Subject) {// recorded since — and the record materializes from the file// - mismatch → stale, purged (a reset never inherits the predecessor's// policy)-// - unreadable, a newer schema, or content that fails to parse → the file-// is quarantined and the binding input seeds the subject. Unlike the-// vault (whose content has no fallback source), a policy this egress-// cannot read has a safe fallback: the replayed binding input is the-// create-time bootstrap, so it can never grant post-create relay-// mutations+// - unreadable, a newer schema, or content that fails to parse → the+// subject DEGRADES fail-closed: the pending policy becomes default-deny+// and the degraded mark surfaces on the actions status (a successful+// relay PUT heals it). The file may carry an acknowledged runtime+// change this egress can no longer read — the bootstrap input could be+// more permissive, so it must not seed//// Removal SET_BINDINGS skip the reload (clearPolicyRecord handles them).// A live record for this exact fence wins (duplicate SET_BINDING); one left@@ -343,12 +344,12 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fe}fs, err := s.policyFiles.storeFor(subj)if err != nil {- log.Warnf("subject %s: policy file store unavailable (%v); the binding input seeds the subject", subj, err)+ s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file store unavailable: %v", err))return}snap, err := fs.Snapshot()if err != nil {- log.Warnf("subject %s: policy file unreadable (%v); the binding input seeds the subject", subj, err)+ s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file unreadable: %v", err))return}if !snap.Exists() {@@ -357,11 +358,17 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fe}pf, body, decErr := policy.DecodePolicyFile(snap.Data())if decErr != nil {- if _, delErr := fs.Delete(); delErr != nil {- log.Errorf("subject %s: unreadable policy file quarantine failed (file left in place): %v", subj, delErr)- return+ // Fail closed: the file may carry an acknowledged runtime change+ // (e.g. a tightening) that this egress can no longer read — seeding+ // the bootstrap input would silently undo it. Corrupt files are+ // quarantined; newer-schema files stay for the newer writer. A+ // relay PUT heals the subject.+ if !errors.Is(decErr, policy.ErrUnsupportedPolicyFileVersion) {+ if _, delErr := fs.Delete(); delErr != nil {+ log.Errorf("subject %s: unreadable policy file quarantine failed (file left in place): %v", subj, delErr)+ }}- log.Warnf("subject %s: policy file unusable (%v); quarantined, the binding input seeds the subject", subj, decErr)+ s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file unusable (%v)", decErr))return}recorded := subject.Fencing{RuntimeInstanceID: pf.Incarnation, AttachmentID: pf.AttachmentID}@@ -379,9 +386,10 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, feif parseErr != nil {if _, delErr := fs.Delete(); delErr != nil {log.Errorf("subject %s: unparseable policy file quarantine failed (file left in place): %v", subj, delErr)+ s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file quarantine failed: %v", delErr))return}- log.Warnf("subject %s: policy file content rejected (%v); quarantined, the binding input seeds the subject", subj, parseErr)+ s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file content rejected (%v)", parseErr))return}entry := &policyEntry{revision: pf.Revision, fence: fence}@@ -471,3 +479,35 @@ func (s *fastSandboxPolicyServer) applyBindingPolicy(subj subject.Subject, fenceapply,)}++// degradePolicyReloadLocked materializes the subject's record as a degraded+// default-deny entry: the pending policy is default-deny (the+// data-plane-ready barrier activates deny, never a stale or permissive+// policy) and the degraded mark surfaces on the actions status until a+// successful relay write heals it. Callers hold s.mu — the reload runs+// inside rehydratePolicyReload, before RegisterAndEnforce (same point as+// the healthy reload).+func (s *fastSandboxPolicyServer) degradePolicyReloadLocked(subj subject.Subject, fence subject.Fencing, reason string) {+ entry := &policyEntry{fence: fence, degraded: reason}+ if s.policyFiles.isSealed(subj) {+ entry.sealed = true+ }+ s.policies[subj] = entry+ s.pendingPolicies[subj] = policy.DefaultDenyPolicy()+ log.Warnf("subject %s: policy degraded fail-closed (%s); default-deny pending until a relay write heals it", subj, reason)+}++// degradedPolicyUIDs lists the subjects with a degraded policy record, as+// raw sandbox UIDs, sorted for stable surfacing.+func (s *fastSandboxPolicyServer) degradedPolicyUIDs() []string {+ s.mu.Lock()+ defer s.mu.Unlock()+ var out []string+ for subj, e := range s.policies {+ if e.degraded != "" {+ out = append(out, vaultFileUID(subj))+ }+ }+ sort.Strings(out)+ return out+}diff --git a/components/egress/fastsandbox_policy_persist_test.go b/components/egress/fastsandbox_policy_persist_test.goindex cc75e0e51..78d09cb30 100644--- a/components/egress/fastsandbox_policy_persist_test.go+++ b/components/egress/fastsandbox_policy_persist_test.go@@ -292,7 +292,7 @@ func TestPolicyPersistStaleIncarnationDiscarded(t *testing.T) {assert.Equal(t, http.StatusPreconditionFailed, rec.Code)}-func TestPolicyPersistUnusableFilesFallBackToInput(t *testing.T) {+func TestPolicyPersistUnusableFilesDegradeFailClosed(t *testing.T) {cases := []struct {name stringdata []byte@@ -311,18 +311,33 @@ func TestPolicyPersistUnusableFilesFallBackToInput(t *testing.T) {f.data = tc.dataf.exists = true- // Registration succeeds; the binding input seeds the subject.+ // Registration succeeds; the subject degrades FAIL CLOSED — the+ // bootstrap input must not seed (it may be more permissive than+ // the unreadable runtime change), the pending policy is+ // default-deny.h.readySubject(t, policyTestUID, vaultTestIP)rec := doPolicy(t, h.srv, http.MethodGet, policyTestUID, "", "")rev, doc := mustPolicyRevision(t, rec)+ assert.Equal(t, int64(0), rev)+ assert.Contains(t, string(doc.Policy), `"defaultAction":"deny"`)++ // The degraded mark surfaces on the actions status.+ status := mustStatus(t, h.srv)+ assert.Equal(t, []string{policyTestUID}, status.DegradedPolicies)+ assert.True(t, status.Ready, "a per-subject degradation never flips process readiness")++ // A relay PUT heals the subject: the degraded mark clears and+ // the written policy takes over at revision 1.+ // The degraded record exists at revision 0, so the healing PUT+ // is a replace (200), not a create.+ rec = doPolicy(t, h.srv, http.MethodPut, policyTestUID, policyPutDoc(policyAllow(), 0, false, false), "")+ require.Equal(t, http.StatusOK, rec.Code)+ rec = doPolicy(t, h.srv, http.MethodGet, policyTestUID, "", "")+ rev, doc = mustPolicyRevision(t, rec)assert.Equal(t, int64(1), rev)assert.Contains(t, string(doc.Policy), "api.example.com")-- // The unusable file was quarantined, then replaced by the- // seeded record's own file.- assert.True(t, f.exists, "the seeded record persists its own file")- pf, _ := decodePolicyFile(t, f)- assert.Equal(t, int64(1), pf.Revision)+ status = mustStatus(t, h.srv)+ assert.Empty(t, status.DegradedPolicies)})}}diff --git a/components/egress/fastsandbox_policy_relay_test.go b/components/egress/fastsandbox_policy_relay_test.goindex 8f02f817f..8ed73ba18 100644--- a/components/egress/fastsandbox_policy_relay_test.go+++ b/components/egress/fastsandbox_policy_relay_test.go@@ -197,18 +197,23 @@ func TestPolicyRelayInvalidPolicyKeepsLastGood(t *testing.T) {assert.Equal(t, "allow", reg.UserPolicy(s).Evaluate("other.example.com"), "last-good policy untouched")}-func TestPolicyRelayDeleteRevertsDenyFirst(t *testing.T) {+func TestPolicyRelayDeleteEnforcesDenyTombstone(t *testing.T) {srv, reg, nft := fastSandboxTestServer(t)s := setBindingAndReady(t, srv, reg, policyTestUID, vaultTestIP, strPtr(policyAllow()))require.Equal(t, subject.StateActive, mustState(reg.Get(s)))// DELETE removes the seeded policy (revision 1 → tombstone at 2) and- // reverts the subject to deny-first.+ // enforces default-deny IN PLACE: the subject stays active (a later+ // relay PUT re-enables immediately — the generic relay never replays a+ // data-plane-ready hook).rec := doPolicy(t, srv, http.MethodDelete, policyTestUID, "", "")require.Equal(t, http.StatusNoContent, rec.Code)- assert.Equal(t, subject.StateDenying, mustState(reg.Get(s)))- assert.Nil(t, reg.UserPolicy(s))- assert.Nil(t, reg.EffectivePolicy(s))+ assert.Equal(t, subject.StateActive, mustState(reg.Get(s)))+ assert.Equal(t, "deny", reg_UserEvaluate(srv, s, "api.example.com"))+ nft.mu.Lock()+ require.NotNil(t, nft.lastPolicy)+ assert.Equal(t, "deny", nft.lastPolicy.Evaluate("api.example.com"), "the deny tombstone is enforced")+ nft.mu.Unlock()// The tombstone is authoritative: GET serves the default-deny record at// the successor revision, and a binding replay cannot resurrect the@@ -225,17 +230,6 @@ func TestPolicyRelayDeleteRevertsDenyFirst(t *testing.T) {assert.Equal(t, int64(2), rev, "the tombstone survives the binding replay")assert.NotContains(t, string(doc.Policy), "api.example.com")- // A replayed data-plane-ready activates the deny record (the barrier- // consumes the tombstone's pending policy) — never a stale policy.- rec = doAction(t, srv, hookBody(t, policyTestUID, testFenceR, testFenceA, constants.HookDataPlaneReady))- require.Equal(t, http.StatusOK, rec.Code)- assert.Equal(t, subject.StateActive, mustState(reg.Get(s)))- nft.mu.Lock()- last := nft.lastPolicy- nft.mu.Unlock()- require.NotNil(t, last)- assert.Equal(t, "deny", last.Evaluate("api.example.com"), "the enforced policy is the deny tombstone")-// DELETE is idempotent (the tombstone replaces itself at the successor// revision).rec = doPolicy(t, srv, http.MethodDelete, policyTestUID, "", "")@@ -243,16 +237,12 @@ func TestPolicyRelayDeleteRevertsDenyFirst(t *testing.T) {rev, _ = mustPolicyRevision(t, doPolicy(t, srv, http.MethodGet, policyTestUID, "", ""))assert.Equal(t, int64(3), rev)- // Re-add goes through the relay: the PUT replaces the tombstone. The- // subject was reverted to deny-first by the second DELETE, so the write- // is held pending until a data-plane-ready replay activates it.+ // Re-add goes through the relay: the PUT replaces the tombstone and+ // re-enables immediately on the still-active subject.rec = doPolicy(t, srv, http.MethodPut, policyTestUID, policyPutDoc(policyAllow(), 3, false, true), "")require.Equal(t, http.StatusOK, rec.Code)rev, doc = mustPolicyRevision(t, rec)assert.Equal(t, int64(4), rev)- assert.Equal(t, subject.StateDenying, mustState(reg.Get(s)))- rec = doAction(t, srv, hookBody(t, policyTestUID, testFenceR, testFenceA, constants.HookDataPlaneReady))- require.Equal(t, http.StatusOK, rec.Code)assert.Equal(t, subject.StateActive, mustState(reg.Get(s)))assert.Equal(t, "allow", reg_UserEvaluate(srv, s, "api.example.com"))}diff --git a/components/egress/fastsandbox_policy_snapshot.go b/components/egress/fastsandbox_policy_snapshot.goindex a3dcbfdb9..df75732e7 100644--- a/components/egress/fastsandbox_policy_snapshot.go+++ b/components/egress/fastsandbox_policy_snapshot.go@@ -171,6 +171,14 @@ func (s *fastSandboxPolicyServer) policyExportSourceImageLocked(subj subject.Subif !regFence.Matches(fence) {return nil, http.StatusConflict, fmt.Errorf("stale policy export (fence mismatch) for subject %s", subj)}+ if !s.fenceMatchesLocked(subj, fence) {+ // Purged between the pre-read and the lock — the stale envelope+ // lost the race with a rebind.+ return nil, http.StatusNotFound, fmt.Errorf("policy export for unknown subject %s", subj)+ }+ if !regFence.Matches(fence) {+ return nil, http.StatusConflict, fmt.Errorf("stale policy export (fence mismatch) for subject %s", subj)+ }if recovery := s.policyRecovery.Load(); recovery != nil {return nil, http.StatusServiceUnavailable, fmt.Errorf("policy persistence recovery required: %s", recovery.reason)}@@ -257,9 +265,12 @@ func (s *fastSandboxPolicyServer) applyPolicyImport(env *actionhandler.Envelope)s.policyCommitMu.Lock()defer s.policyCommitMu.Unlock()- // Registry reads before s.mu (lock order: registry → policyCommitMu →- // s.mu, see applyPolicyExport).- regFence, registered := s.reg.Fence(subj)+ // The registration fence re-validation happens under s.mu against our+ // own recorded fence (subjFence) — no registry read is needed in this+ // section, so the registry → policyCommitMu → s.mu order is preserved+ // trivially (see applyPolicyExport). An unknown fence (no record) means+ // the subject is not registered: the bundle lands for the next+ // registration.s.mu.Lock()// The latch freezes every policy file mutation; only a restart// reconciles (relay-write parity).@@ -282,7 +293,8 @@ func (s *fastSandboxPolicyServer) applyPolicyImport(env *actionhandler.Envelope)}}}- if registered && !regFence.Matches(fence) {+ registered := s.fenceMatchesLocked(subj, fence)+ if _, known := s.subjFence[subj]; known && !registered {s.mu.Unlock()return http.StatusConflict, fmt.Errorf("stale policy import (fence mismatch) for subject %s", subj)}diff --git a/components/egress/fastsandbox_server.go b/components/egress/fastsandbox_server.goindex d5a02097e..c9a6378fd 100644--- a/components/egress/fastsandbox_server.go+++ b/components/egress/fastsandbox_server.go@@ -69,6 +69,13 @@ type fastSandboxPolicyServer struct {// cleanup (gateway refcounts) even when the REMOVE_BINDING envelope omits// the attachment block.subjAtt map[subject.Subject]actionhandler.NetworkAttachment+ // subjFence records the registered fence per subject. It is the+ // commit-time re-validation for the relay paths, whose registry reads+ // happen BEFORE s.mu (lock order: registry → s.mu): a rebind that+ // completed between the pre-read and the lock updates this map, so the+ // in-lock comparison rejects the stale request without touching the+ // registry under s.mu.+ subjFence map[subject.Subject]subject.Fencing// vaultFiles owns the per-subject vault files; nil keeps the vault// memory-only (the default, and the sidecar posture).@@ -136,6 +143,7 @@ func newFastSandboxPolicyServer(ctx context.Context, reg *subject.MemoryRegistrypendingPolicies: make(map[subject.Subject]*policy.NetworkPolicy),policies: make(map[subject.Subject]*policyEntry),subjAtt: make(map[subject.Subject]actionhandler.NetworkAttachment),+ subjFence: make(map[subject.Subject]subject.Fencing),gatewayDNSRefs: make(map[subject.Subject]netip.Addr),dnsRedirectInstall: iptables.SetupGatewayDNSRedirect,dnsRedirectRemove: iptables.RemoveGatewayDNSRedirect,@@ -561,20 +569,32 @@ func (s *fastSandboxPolicyServer) dropSubjectState(subj subject.Subject) {delete(s.policies, subj)delete(s.pendingPolicies, subj)delete(s.subjAtt, subj)+ delete(s.subjFence, subj)s.purgeVaultFile(subj)s.purgePolicyFile(subj)s.mu.Unlock()}// recordBindingState stores the SET_BINDING bookkeeping for a registered-// subject: the network attachment (terminal cleanup). Called after the+// subject: the network attachment (terminal cleanup) and the registered+// fence (relay commit-time re-validation, see subjFence). Called after the// registry lock is released.-func (s *fastSandboxPolicyServer) recordBindingState(subj subject.Subject, att actionhandler.NetworkAttachment) {+func (s *fastSandboxPolicyServer) recordBindingState(subj subject.Subject, att actionhandler.NetworkAttachment, fence subject.Fencing) {s.mu.Lock()s.subjAtt[subj] = att+ s.subjFence[subj] = fences.mu.Unlock()}+// fenceMatchesLocked reports whether the subject's recorded registration+// fence equals the request fence. Callers hold s.mu. A false return means+// the pre-read (registry → s.mu window) went stale: the subject was purged+// or rebound, and the request must not touch its state.+func (s *fastSandboxPolicyServer) fenceMatchesLocked(subj subject.Subject, fence subject.Fencing) bool {+ got, ok := s.subjFence[subj]+ return ok && got == fence+}+// attachment returns the last observed network attachment for a registered// subject (used by terminal cleanup when the REMOVE_BINDING envelope omits// the attachment block).diff --git a/components/egress/fastsandbox_vault.go b/components/egress/fastsandbox_vault.goindex 2045ecc9d..111be5b2e 100644--- a/components/egress/fastsandbox_vault.go+++ b/components/egress/fastsandbox_vault.go@@ -135,11 +135,30 @@ func (s *fastSandboxPolicyServer) handleVaultRelayWrite(w http.ResponseWriter, rreturn}effective := s.reg.EffectivePolicy(subj)+ if effective == nil {+ // Pre-read the pending policy and merge the always overlay BEFORE+ // s.mu (registry → s.mu order): a vault op held to the+ // data-plane-ready barrier must validate against the SAME policy+ // the hook will enforce, operator always-rules included.+ s.mu.Lock()+ pending := s.pendingPolicies[subj]+ s.mu.Unlock()+ if pending != nil {+ effective = s.reg.EffectiveOf(pending)+ }+ }// The rest of the mutation holds s.mu, serializing per-subject writers- // and fencing rebind/terminal purges.+ // and fencing rebind/terminal purges. The validation policy was fully+ // pre-computed above — nothing inside the lock touches the registry.s.mu.Lock()defer s.mu.Unlock()+ // Commit-time fence re-validation: the pre-read above cannot see a+ // rebind that completed after it (see subjFence).+ if !s.fenceMatchesLocked(subj, fence) {+ http.Error(w, "unknown subject", http.StatusNotFound)+ return+ }if s.vaultSealedLocked(subj) {http.Error(w, "subject vault is sealed (paused); the bundle copy is already taken", http.StatusConflict)return@@ -149,7 +168,7 @@ func (s *fastSandboxPolicyServer) handleVaultRelayWrite(w http.ResponseWriter, rreturn}store := s.vaultForLocked(subj, fence)- pol := s.vaultValidationPolicyLocked(effective, subj)+ pol := effectivevar (candidate *credentialvault.MutationCandidate@@ -283,6 +302,9 @@ func (s *fastSandboxPolicyServer) selectRelayVault(r *http.Request, subj subject}s.mu.Lock()defer s.mu.Unlock()+ if !s.fenceMatchesLocked(subj, fence) {+ return nil, http.StatusNotFound, "unknown subject"+ }if s.vaultSealedLocked(subj) {return nil, http.StatusConflict, "subject vault is sealed (paused); the bundle copy is already taken"}@@ -296,19 +318,24 @@ func (s *fastSandboxPolicyServer) vaultSealedLocked(subj subject.Subject) bool {return ok && e.sealed}-// vaultValidationPolicyLocked returns the policy a vault op validates-// against: the effective policy of an active subject, or — while the subject-// is still denying — its pending binding policy (the op is held to the-// data-plane-ready barrier, so validation must not depend on hook timing).-// The effective policy is PRE-READ by the caller before s.mu (lock order:-// registry → s.mu, see handleVaultRelayWrite); the pending fallback uses-// the raw user policy — the always overlay is enforcement-side and-// validation only needs the explicit allow cover. Callers hold s.mu.-func (s *fastSandboxPolicyServer) vaultValidationPolicyLocked(effective *policy.NetworkPolicy, subj subject.Subject) *policy.NetworkPolicy {+// vaultValidationPolicy returns the policy a vault op validates against:+// the effective policy of an active subject, or — while the subject is+// still denying — its pending binding policy MERGED with the always+// overlay (the op is held to the data-plane-ready barrier, so validation+// must not depend on hook timing). Call this BEFORE s.mu — it reads the+// registry (lock order: registry → s.mu, see handleVaultRelayWrite).+func (s *fastSandboxPolicyServer) vaultValidationPolicy(subj subject.Subject) *policy.NetworkPolicy {+ effective := s.reg.EffectivePolicy(subj)if effective != nil {return effective}- return s.pendingPolicies[subj]+ s.mu.Lock()+ pending := s.pendingPolicies[subj]+ s.mu.Unlock()+ if pending != nil {+ return s.reg.EffectiveOf(pending)+ }+ return nil}// writeVaultRelayError maps store errors onto the relay contract: revisiondiff --git a/components/egress/fastsandbox_vault_snapshot.go b/components/egress/fastsandbox_vault_snapshot.goindex f2035cf25..1d5e441e3 100644--- a/components/egress/fastsandbox_vault_snapshot.go+++ b/components/egress/fastsandbox_vault_snapshot.go@@ -177,6 +177,14 @@ func (s *fastSandboxPolicyServer) exportSourceImageLocked(subj subject.Subject,if !regFence.Matches(fence) {return nil, http.StatusConflict, fmt.Errorf("stale vault export (fence mismatch) for subject %s", subj)}+ if !s.fenceMatchesLocked(subj, fence) {+ // Purged between the pre-read and the lock — the stale envelope+ // lost the race with a rebind.+ return nil, http.StatusNotFound, fmt.Errorf("vault export for unknown subject %s", subj)+ }+ if !regFence.Matches(fence) {+ return nil, http.StatusConflict, fmt.Errorf("stale vault export (fence mismatch) for subject %s", subj)+ }if recovery := s.vaultRecovery.Load(); recovery != nil {return nil, http.StatusServiceUnavailable, fmt.Errorf("vault persistence recovery required: %s", recovery.reason)}@@ -255,8 +263,9 @@ func (s *fastSandboxPolicyServer) applyVaultImport(env *actionhandler.Envelope)}// Registry reads before s.mu (lock order: registry → s.mu, see- // handleVaultRelayWrite).- effective := s.reg.EffectivePolicy(subj)+ // handleVaultRelayWrite); the validation policy merges the always+ // overlay for the pending fallback (barrier semantics).+ validation := s.vaultValidationPolicy(subj)s.mu.Lock()defer s.mu.Unlock()// The latch freezes every vault file mutation; only a restart@@ -278,14 +287,17 @@ func (s *fastSandboxPolicyServer) applyVaultImport(env *actionhandler.Envelope)}}}- regFence, registered := s.reg.Fence(subj)+ // Commit-time fence re-validation against our own record (subjFence —+ // see fenceMatchesLocked): an unknown fence means unregistered (the+ // bundle lands for the next registration), a mismatched one is stale.+ registered := s.fenceMatchesLocked(subj, fence)+ if _, known := s.subjFence[subj]; known && !registered {+ return http.StatusConflict, fmt.Errorf("stale vault import (fence mismatch) for subject %s", subj)+ }if registered {- if !regFence.Matches(fence) {- return http.StatusConflict, fmt.Errorf("stale vault import (fence mismatch) for subject %s", subj)- }// Validate before any durable effect — a permanently broken bundle// is a 400 with last-good kept, never half-open.- if err := validateImportedVault(vf, body, s.vaultValidationPolicyLocked(effective, subj)); err != nil {+ if err := validateImportedVault(vf, body, validation); err != nil {return http.StatusBadRequest, fmt.Errorf("vault bundle rejected: %v", err)}}@@ -299,7 +311,7 @@ func (s *fastSandboxPolicyServer) applyVaultImport(env *actionhandler.Envelope)// Before SET_BINDING: registration reloads and validates.return s.vaultFiles.unseal(subj)}- return s.installImportedVaultLocked(subj, fence, vf, body, effective)+ return s.installImportedVaultLocked(subj, fence, vf, body, validation)},)if err != nil {@@ -324,10 +336,10 @@ func (s *fastSandboxPolicyServer) applyVaultImport(env *actionhandler.Envelope)// per-subject slice under the restored fence, replacing any stale entry.// The slice is only replaced once Reload accepts — a failure leaves the live// entry untouched while the commit protocol rolls the file back.-// effective is the caller's pre-read of the subject's effective policy-// (lock order: registry → s.mu, see handleVaultRelayWrite).-func (s *fastSandboxPolicyServer) installImportedVaultLocked(subj subject.Subject, fence subject.Fencing, vf credentialvault.VaultFile, body credentialvault.VaultFileBody, effective *policy.NetworkPolicy) error {- pol := s.vaultValidationPolicyLocked(effective, subj)+// validation is the caller's pre-computed vault validation policy+// (lock order: registry → s.mu, see vaultValidationPolicy).+func (s *fastSandboxPolicyServer) installImportedVaultLocked(subj subject.Subject, fence subject.Fencing, vf credentialvault.VaultFile, body credentialvault.VaultFileBody, validation *policy.NetworkPolicy) error {+ pol := validationentry, ok := s.vaults[subj]if !ok || entry.fence != fence {entry = vaultEntry{store: credentialvault.NewStore(nil, func() bool { return true }), fence: fence}diff --git a/components/egress/pkg/actionhandler/actionhandler.go b/components/egress/pkg/actionhandler/actionhandler.goindex a6c82a602..28c637420 100644--- a/components/egress/pkg/actionhandler/actionhandler.go+++ b/components/egress/pkg/actionhandler/actionhandler.go@@ -307,6 +307,9 @@ type StatusResponse struct {PolicyTransfer bool `json:"policyTransfer,omitempty"`// PolicyRecovery is true while the policy persistence recovery latch// freezes policy relay writes until restart (same mechanics as- // vaultRecovery).- PolicyRecovery bool `json:"policyRecovery,omitempty"`+ // vaultRecovery); DegradedPolicies lists subjects whose persisted+ // policy failed to load (default-deny fail closed for them; a+ // successful relay write clears it).+ PolicyRecovery bool `json:"policyRecovery,omitempty"`+ DegradedPolicies []string `json:"degradedPolicies,omitempty"`}diff --git a/server/opensandbox_server/services/fast_sandbox/policy_relay.py b/server/opensandbox_server/services/fast_sandbox/policy_relay.pynew file mode 100644index 000000000..6eb84ca55--- /dev/null+++ b/server/opensandbox_server/services/fast_sandbox/policy_relay.py@@ -0,0 +1,193 @@+# Copyright 2026 The OpenSandbox Authors+# Licensed 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++"""Server-side network policy relay for the fast-sandbox profile.++The recorded policy is egress-held: the create-time binding input seeds it+once, and every runtime change goes over the FastPath handler relay+(``InvokeHandlerAction`` → Fastlet → egress ``/policy/{sandboxUid}``) with+the relay response as the synchronous ack. The Sandbox CR's+``spec.actionBindings`` stays the create-time bootstrap only — runtime+mutations through ``replace_action_bindings`` would be superseded by the+egress at the next binding replay.++Responsibilities owned here (the egress owns validation, persistence, and+the revision state machine):++- shape validation of the public request bodies (``normalized_policy`` —+ the egress re-validates authoritatively and NACKs with 400 keeping+ last-good);+- ``PATCH`` composed server-side: relay ``GET`` → merge in memory → relay+ ``PUT`` carrying the read revision as ``expectedRevision`` (the+ per-sandbox in-process lock serializes local merges; the revision+ precondition is the cross-replica backstop — a stale writer gets 412).++No policy state is persisted server-side; reads reflect exactly what the+egress enforces.+"""++from __future__ import annotations++import json++from fastapi import HTTPException+from starlette import status++from opensandbox_server.api.schema import NetworkRule+from opensandbox_server.services.constants import SandboxErrorCodes+from opensandbox_server.services.fast_sandbox.fastpath_client import (+ FastPathError,+ FastPathHandlerError,+)+from opensandbox_server.services.fast_sandbox.network_policy import (+ delete_policy_rules,+ merge_policy_rules,+ normalized_policy,+ policy_status,+)+from opensandbox_server.services.fast_sandbox.vault import EGRESS_HANDLER++# Relay loopback path prefix on the egress handler (components/egress+# constants.PolicyRelayPathPrefix). The subject identity is the Sandbox UID —+# the same value the Fastlet delivers in the action envelope's sandbox.uid.+_POLICY_PATH_PREFIX = "/policy/"+++def policy_relay_error(status_code: int, message: str) -> HTTPException:+ """Map an egress relay status onto the public HTTP contract."""+ if status_code == 400:+ return HTTPException(+ status_code=status.HTTP_400_BAD_REQUEST,+ detail={"code": SandboxErrorCodes.INVALID_PARAMETER, "message": message},+ )+ if 500 <= status_code <= 599:+ return HTTPException(+ status_code=status.HTTP_503_SERVICE_UNAVAILABLE,+ detail={+ "code": SandboxErrorCodes.FSB_API_ERROR,+ "message": message or "Network policy backend unavailable.",+ },+ )+ mapped = status_code if 400 <= status_code <= 499 else status.HTTP_500_INTERNAL_SERVER_ERROR+ return HTTPException(+ status_code=mapped,+ detail={"code": SandboxErrorCodes.FSB_API_ERROR, "message": message},+ )+++def _resolve_subject(service, sandbox_id: str) -> tuple[str, str]:+ """Resolve (namespace, uid) for the relay target; 404 when unknown."""+ metadata = service._get_cr(sandbox_id)["metadata"]+ return metadata["namespace"], metadata["uid"]+++def _relay(+ service,+ sandbox_id: str,+ *,+ method: str,+ payload: bytes | None = None,+) -> tuple[int, bytes]:+ """Run one relay op and return (status, body) for any 2xx outcome."""+ namespace, uid = _resolve_subject(service, sandbox_id)+ try:+ result = service.fastpath_client.invoke_handler_action(+ namespace,+ sandbox_id,+ expected_uid=uid,+ handler=EGRESS_HANDLER,+ method=method,+ path=f"{_POLICY_PATH_PREFIX}{uid}",+ payload=payload,+ )+ except FastPathHandlerError as exc:+ raise policy_relay_error(exc.status_code, exc.message) from exc+ except FastPathError as exc:+ raise service._fastpath_http_error(exc) from exc+ return int(result.status), bytes(result.body)+++def _relay_get_full(service, sandbox_id: str) -> dict:+ """Relay GET of the recorded state: {"revision": N, "policy": {...}}."""+ _, body = _relay(service, sandbox_id, method="GET")+ try:+ state = json.loads(body.decode("utf-8"))+ except (UnicodeDecodeError, json.JSONDecodeError) as exc:+ raise policy_relay_error(+ status.HTTP_503_SERVICE_UNAVAILABLE,+ "Network policy relay returned an invalid record.",+ ) from exc+ if not isinstance(state.get("policy"), dict):+ raise policy_relay_error(+ status.HTTP_503_SERVICE_UNAVAILABLE,+ "Network policy relay record is missing the policy body.",+ )+ return state+++def relay_get(service, sandbox_id: str) -> dict:+ """Public read: the recorded policy in the policy_status shape."""+ state = _relay_get_full(service, sandbox_id)+ return policy_status(state["policy"])+++def relay_put(service, sandbox_id: str, normalized: dict) -> dict:+ """PUT a full replace (creates the record when absent)."""+ _, body = _relay(+ service,+ sandbox_id,+ method="PUT",+ payload=json.dumps({"policy": normalized}).encode("utf-8"),+ )+ try:+ ack = json.loads(body.decode("utf-8"))+ except (UnicodeDecodeError, json.JSONDecodeError):+ ack = None+ policy = (ack or {}).get("policy")+ return policy_status(policy if isinstance(policy, dict) else normalized)+++def relay_patch(service, sandbox_id: str, rules: list[NetworkRule]) -> dict:+ """PATCH composed server-side: GET → merge → PUT(expectedRevision).++ The per-sandbox in-process lock serializes local merges; the revision+ precondition inside the merged PUT is the cross-replica backstop (a+ concurrent writer commits first and the stale PATCH sees 412).+ """+ with service.policy_lock(sandbox_id):+ state = _relay_get_full(service, sandbox_id)+ merged = merge_policy_rules(state["policy"], rules)+ payload = json.dumps(+ {"policy": merged, "expectedRevision": state["revision"]}+ ).encode("utf-8")+ _, body = _relay(service, sandbox_id, method="PUT", payload=payload)+ try:+ ack = json.loads(body.decode("utf-8"))+ except (UnicodeDecodeError, json.JSONDecodeError):+ ack = None+ policy = (ack or {}).get("policy")+ return policy_status(policy if isinstance(policy, dict) else merged)+++def relay_delete_rules(service, sandbox_id: str, targets: list[str]) -> dict:+ """Remove rules by target (idempotent) through the relay."""+ with service.policy_lock(sandbox_id):+ state = _relay_get_full(service, sandbox_id)+ kept = delete_policy_rules(state["policy"], targets)+ payload = json.dumps(+ {"policy": kept, "expectedRevision": state["revision"]}+ ).encode("utf-8")+ _, body = _relay(service, sandbox_id, method="PUT", payload=payload)+ try:+ ack = json.loads(body.decode("utf-8"))+ except (UnicodeDecodeError, json.JSONDecodeError):+ ack = None+ policy = (ack or {}).get("policy")+ return policy_status(policy if isinstance(policy, dict) else kept)+++def normalized_for_create(policy) -> dict:+ """Create-path normalization (the binding input seed)."""+ return normalized_policy(policy)diff --git a/server/opensandbox_server/services/fast_sandbox/service.py b/server/opensandbox_server/services/fast_sandbox/service.pyindex c0962e161..f2a962d19 100644--- a/server/opensandbox_server/services/fast_sandbox/service.py+++ b/server/opensandbox_server/services/fast_sandbox/service.py@@ -91,11 +91,9 @@ from opensandbox_server.services.fast_sandbox.cr_reader import ()from opensandbox_server.services.fast_sandbox.cr_mapping import sandbox_from_crfrom opensandbox_server.services.fast_sandbox.network_policy import (- delete_policy_rules,- merge_policy_rules,normalized_policy,- policy_status,)+from opensandbox_server.services.fast_sandbox import policy_relayfrom opensandbox_server.services.fast_sandbox import vault as vault_relayfrom opensandbox_server.services.templates.template_service import FastSandboxTemplateServicefrom opensandbox_server.services.fast_sandbox.generated import fastpath_pb2 as pb2@@ -114,6 +112,11 @@ _SUPPORTED_EVENT_SCOPES = ("runtime", "all")# see FastSandboxService.__init__)._VAULT_LOCK_SHARDS = 64+# Same-shard-count locks for the policy PATCH/rule-delete composition+# (relay GET -> merge -> relay PUT); the revision precondition inside the+# merged PUT is the cross-replica backstop.+_POLICY_LOCK_SHARDS = 64+logger = logging.getLogger(__name__)@@ -149,11 +152,16 @@ class FastSandboxService(SandboxService, ExtensionService):# the serialization scope; the revision precondition inside the# merged PUT stays the cross-replica backstop either way.self._vault_locks = [Lock() for _ in range(_VAULT_LOCK_SHARDS)]+ self._policy_locks = [Lock() for _ in range(_POLICY_LOCK_SHARDS)]def vault_lock(self, sandbox_id: str) -> Lock:"""Return the process-local PATCH shard lock for one sandbox."""return self._vault_locks[hash(sandbox_id) % _VAULT_LOCK_SHARDS]+ def policy_lock(self, sandbox_id: str) -> Lock:+ """Return the process-local policy PATCH shard lock for one sandbox."""+ return self._policy_locks[hash(sandbox_id) % _POLICY_LOCK_SHARDS]+@propertydef cr_reader(self) -> SandboxCRReader:"""Informer-cached Sandbox CR reader, for background integrations."""@@ -728,68 +736,21 @@ class FastSandboxService(SandboxService, ExtensionService):# -- endpoints ---------------------------------------------------------def get_network_policy(self, sandbox_id: str) -> dict:- current = self._cr_reader.get(self._resolve_namespace(), sandbox_id)- binding = next(- (b for b in current["spec"].get("actionBindings", []) if b["handler"] == "egress"), None- )- try:- raw = binding.get("input") if binding else None- if isinstance(raw, str) and not raw.strip():- raw = None- policy = json.loads(raw) if raw is not None else None- if policy is not None:- policy = normalized_policy(NetworkPolicy.model_validate(policy))- except (ValueError, TypeError, HTTPException) as exc:- raise HTTPException(503, detail="Invalid persisted egress binding.") from exc- return policy_status(policy)+ """The recorded policy reflects exactly what the egress enforces."""+ return policy_relay.relay_get(self, sandbox_id)def replace_network_policy(self, sandbox_id: str, policy: NetworkPolicy) -> dict:- return self._commit_network_policy(sandbox_id, normalized_policy(policy))+ self._reject_network_policy_with_upstream_proxy()+ return policy_relay.relay_put(self, sandbox_id, normalized_policy(policy))def patch_network_policy(self, sandbox_id: str, rules: list[NetworkRule]) -> dict:- """Merge rules into the persisted egress binding (sidecar PATCH semantics)."""- current = self.get_network_policy(sandbox_id)- merged = merge_policy_rules(current["policy"], rules)- return self._commit_network_policy(sandbox_id, normalized_policy(NetworkPolicy.model_validate(merged)))+ """Merge rules into the recorded policy (sidecar PATCH semantics)."""+ self._reject_network_policy_with_upstream_proxy()+ return policy_relay.relay_patch(self, sandbox_id, rules)def delete_network_policy_rules(self, sandbox_id: str, targets: list[str]) -> dict:- """Remove rules by target from the persisted egress binding (idempotent)."""- current = self.get_network_policy(sandbox_id)- kept = delete_policy_rules(current["policy"], targets)- return self._commit_network_policy(- sandbox_id,- normalized_policy(NetworkPolicy.model_validate(kept)),- guard_upstream_proxy=False,- )-- def _commit_network_policy(- self, sandbox_id: str, normalized: dict, *, guard_upstream_proxy: bool = True- ) -> dict:- if guard_upstream_proxy:- self._reject_network_policy_with_upstream_proxy()- current = self._cr_reader.get(self._resolve_namespace(), sandbox_id)- metadata = current["metadata"]- bindings = [dict(b) for b in current["spec"].get("actionBindings", [])]- replacement = {"handler": "egress", "input": json.dumps(normalized)}- for index, binding in enumerate(bindings):- if binding["handler"] == "egress":- bindings[index] = replacement- break- else:- bindings.append(replacement)- try:- self._fastpath.replace_action_bindings(- metadata["namespace"],- sandbox_id,- bindings,- expected_uid=metadata["uid"],- expected_generation=metadata["generation"],- )- except FastPathError as exc:- raise self._fastpath_http_error(exc) from exc- finally:- self._cr_reader.invalidate(metadata["namespace"])- return policy_status(normalized)+ """Remove rules by target from the recorded policy (idempotent)."""+ return policy_relay.relay_delete_rules(self, sandbox_id, targets)def get_endpoint(self,diff --git a/server/tests/test_fsb_service.py b/server/tests/test_fsb_service.pyindex 499c58975..44dad9802 100644--- a/server/tests/test_fsb_service.py+++ b/server/tests/test_fsb_service.py@@ -83,6 +83,12 @@ class _FakeFastPathService(pb2_grpc.FastPathServiceServicer):self.diagnostic_runtime_state = pb2.RUNTIME_STATE_READYself.pool_has_resources = Trueself.get_error_by_namespace: dict[str, grpc.StatusCode] = {}+ # Per-sandbox recorded policy state, simulating the egress-held+ # /policy/{uid} surface: seeded at create from the egress binding+ # input (default deny), mutated by the relay, DELETE lands a+ # default-deny tombstone at the successor revision.+ self.policy_records: dict[str, dict] = {}+ self.last_relay: pb2.InvokeHandlerActionRequest | None = None@staticmethoddef _info(name: str, uid: str, namespace: str = "ns-1") -> pb2.SandboxInfo:@@ -137,6 +143,16 @@ class _FakeFastPathService(pb2_grpc.FastPathServiceServicer):}if self.abort_create_with is not None:context.abort(self.abort_create_with, "scripted post-persistence failure")+ seed = {"defaultAction": "deny", "egress": []}+ for binding in request.action_bindings:+ if binding.handler == "egress":+ try:+ parsed = json.loads(binding.input)+ if isinstance(parsed, dict):+ seed = parsed+ except json.JSONDecodeError:+ pass+ self.policy_records[request.request_id] = {"revision": 1, "policy": seed}info = self._info(request.request_id, uid, request.namespace)if self.create_pending:info.runtime.state = pb2.RUNTIME_STATE_CREATING@@ -148,6 +164,51 @@ class _FakeFastPathService(pb2_grpc.FastPathServiceServicer):completion=request.completion,)+ def InvokeHandlerAction(self, request, context):+ self.last_relay = request+ name = request.sandbox.namespaced_name.name+ record = self.policy_records.get(name)+ if request.path.startswith("/policy/"):+ if request.method == "GET":+ if record is None:+ return pb2.InvokeHandlerActionResponse(+ result=pb2.HandlerActionResult(status=404, body=b"no recorded policy")+ )+ return pb2.InvokeHandlerActionResponse(+ result=pb2.HandlerActionResult(status=200, body=json.dumps(record).encode())+ )+ if request.method == "PUT":+ document = json.loads(request.payload)+ expected = document.get("expectedRevision")+ current = record["revision"] if record else 0+ if expected is not None and expected != current:+ return pb2.InvokeHandlerActionResponse(+ result=pb2.HandlerActionResult(status=412, body=b"revision mismatch")+ )+ updated = {"revision": current + 1, "policy": document["policy"]}+ self.policy_records[name] = updated+ return pb2.InvokeHandlerActionResponse(+ result=pb2.HandlerActionResult(+ status=200 if record else 201, body=json.dumps(updated).encode()+ )+ )+ if request.method == "DELETE":+ if record is None:+ return pb2.InvokeHandlerActionResponse(+ result=pb2.HandlerActionResult(status=404, body=b"no recorded policy")+ )+ tombstone = {+ "revision": record["revision"] + 1,+ "policy": {"defaultAction": "deny", "egress": []},+ }+ self.policy_records[name] = tombstone+ return pb2.InvokeHandlerActionResponse(+ result=pb2.HandlerActionResult(status=204, body=b"")+ )+ return pb2.InvokeHandlerActionResponse(+ result=pb2.HandlerActionResult(status=404, body=b"unknown relay path")+ )+def GetSandbox(self, request, context):self.last_get = requestnamespace = request.sandbox.namespaced_name.namespace@@ -792,7 +853,7 @@ def test_upstream_proxy_config_allows_delete_network_policy_rules(persisted_fsb)response = client.request("DELETE", url, json=["a.com"])assert response.status_code == 200- assert fake.last_update is not None+ assert fake.last_relay is not None and fake.last_relay.path.startswith("/policy/")def test_upstream_proxy_config_allows_create_without_network_policy(http_fsb):@@ -817,36 +878,33 @@ def test_upstream_proxy_config_allows_create_without_network_policy(http_fsb):assert fake.last_create is not None-def test_http_policy_replace_preserves_bindings_and_fences_updates(persisted_fsb):+def test_http_policy_replace_relays_and_leaves_bindings_untouched(persisted_fsb):+ """Runtime policy changes ride the handler relay: the CRD binding input is+ the create-time bootstrap only, and a relay PUT never rewrites it."""client, fake, _, sandbox_id = persisted_fsburl = f"/v1/sandboxes/{sandbox_id}/networkpolicy"+ # The create-time egress binding seeds the record (default deny here).assert client.get(url).json()["policy"] == {"defaultAction": "deny", "egress": []}- bindings = [+ bindings_before = [{"handler": "audit", "input": '{"enabled":true}'},- {"handler": "egress", "input": " "},- {"handler": "other", "input": "{}"},+ {"handler": "egress", "input": '{"defaultAction":"deny"}'},]cr = fake.crs[("ns-1", sandbox_id)]- cr["spec"]["actionBindings"] = bindings- assert client.get(url).json()["mode"] == "deny_all"+ cr["spec"]["actionBindings"] = bindings_before+policy = {"defaultAction": "allow", "egress": []}response = client.put(url, json=policy)assert response.status_code == 200assert response.json()["policy"] == policyassert client.get(url).json()["policy"] == policy- assert list(fake.last_update.action_bindings.items)[0].input == bindings[0]["input"]- assert [b.handler for b in fake.last_update.action_bindings.items] == [- "audit",- "egress",- "other",- ]- assert fake.last_update.action_bindings.items[2].input == bindings[2]["input"]- assert json.loads(fake.last_update.action_bindings.items[1].input) == policy- assert fake.last_update.sandbox.expected_uid == cr["metadata"]["uid"]- assert fake.last_update.expected_generation == 1- fake.abort_update_with = grpc.StatusCode.ABORTED- assert client.put(url, json={"defaultAction": "deny"}).status_code == 409+ # The CRD binding input is untouched: the relay owned the change.+ assert cr["spec"]["actionBindings"] == bindings_before+ assert fake.last_update is None++ # The relayed policy is the record: the (stale) binding input never+ # resurfaces through a read.+ cr["spec"]["actionBindings"][1]["input"] = '{"defaultAction":"deny","egress":[{"action":"allow","target":"stale.com"}]}'assert client.get(url).json()["policy"] == policy@@ -890,9 +948,25 @@ def test_http_policy_patch_and_delete_match_sidecar_semantics(persisted_fsb):}assert client.request("DELETE", url, json=["a.com"]).status_code == 200- # Conflict protection still applies to merged commits.- fake.abort_update_with = grpc.StatusCode.ABORTED- assert client.patch(url, json=[{"action": "deny", "target": "d.com"}]).status_code == 409+ # Conflict protection still applies to merged commits: the merged PUT+ # carries the read revision as expectedRevision — a concurrent relay+ # write landing between the merge-read and the commit turns the stale+ # merge into a 412.+ from opensandbox_server.services.fast_sandbox import policy_relay as policy_relay_module++ real_get_full = policy_relay_module._relay_get_full++ def racy_get_full(service, sid):+ state = real_get_full(service, sid)+ fake.policy_records[sid]["revision"] += 1+ return state++ policy_relay_module._relay_get_full = racy_get_full+ try:+ response = client.patch(url, json=[{"action": "deny", "target": "d.com"}])+ finally:+ policy_relay_module._relay_get_full = real_get_full+ assert response.status_code == 412def test_policy_rejects_invalid_input_and_other_tenant(persisted_fsb):@@ -926,6 +1000,7 @@ def test_policy_rejects_port_scoped_rules(persisted_fsb):assert client.patch(url, json=[port_only]).status_code == 400assert client.patch(url, json=[scoped]).status_code == 400assert fake.last_update is None+ assert fake.last_relay is None or fake.last_relay.method == "GET"def test_legacy_policy_route_preserves_body_for_existing_proxy(http_fsb, monkeypatch):diff --git a/specs/sandbox-lifecycle.yml b/specs/sandbox-lifecycle.ymlindex 6378ebed3..04ce4c63c 100644--- a/specs/sandbox-lifecycle.yml+++ b/specs/sandbox-lifecycle.yml@@ -58,6 +58,10 @@ paths:tags: [Sandboxes]operationId: getSandboxNetworkPolicysummary: Read sandbox network policy+ description: |+ Fsb reads the egress-held record — the response reflects exactly what+ the egress enforces, not the create-time binding input. Other backends+ proxy the sandbox-side egress service.description: |For Fsb, reads persisted egress Action Binding intent in the tenant'sSandbox CR, not live enforcement state. An absent binding defaults to@@ -94,9 +98,12 @@ paths:summary: Replace the complete sandbox network policydescription: |Replaces defaultAction and all rules, rather than merging them. Fsb- commits the egress binding through FastPath, preserving unrelated bindings- with UID/generation conflict protection. Success does not imply handler- convergence. Other backends proxy the sandbox-side egress service.+ relays the replacement to the egress-held policy record through the+ FastPath handler relay — the ack means the egress applied it (and, with+ policy persistence, that it is durable). The create-time binding input+ is the bootstrap only; runtime changes never rewrite it, and a paused+ (sealed) sandbox rejects mutations until resume. Other backends proxy+ the sandbox-side egress service.requestBody:required: truecontent: