Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(egress): keep the vault file on reload-validation failure — degrade, don't quarantine
高然ff1b49d16894
diff --git a/components/egress/fastsandbox_actions.go b/components/egress/fastsandbox_actions.goindex fe6098c79..b45cb82f8 100644--- a/components/egress/fastsandbox_actions.go+++ b/components/egress/fastsandbox_actions.go@@ -231,12 +231,15 @@ func (s *fastSandboxPolicyServer) applySetBinding(env *actionhandler.Envelope) (if !env.Binding.IsRemoval() {// Policy first: its reload result is the validation policy for the// vault reload (a template clone's vault hosts are covered by the- // landed template policy, not by the replayed bootstrap input).- seed := s.rehydratePolicyReload(subj, fence)+ // landed template policy, not by the replayed bootstrap input). A+ // degraded policy record degrades the VAULT with it (fail-closed,+ // file kept) instead of seeding the deny fallback — a deny-seeded+ // vault validation would quarantine a healthy vault.+ seed, policyDegraded := s.rehydratePolicyReload(subj, fence)if seed == nil {seed = pol}- s.rehydrateVaultReload(subj, fence, seed)+ s.rehydrateVaultReload(subj, fence, seed, policyDegraded)}state, err := s.reg.RegisterAndEnforce(subj, subject.SubjectKey{SourceIP: att.IP}, fence, func() error {return s.OnRegistered(subj, att)diff --git a/components/egress/fastsandbox_policy_persist.go b/components/egress/fastsandbox_policy_persist.goindex 3bffea0e1..ae4327399 100644--- a/components/egress/fastsandbox_policy_persist.go+++ b/components/egress/fastsandbox_policy_persist.go@@ -378,24 +378,24 @@ func (s *fastSandboxPolicyServer) clearPolicyRecordLocked(subj subject.Subject)// Returns the policy the subject must run per the reload: the file-backed// policy, default-deny when degraded, or nil when the binding input seeds// (fresh subject).-func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fence subject.Fencing) *policy.NetworkPolicy {+func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fence subject.Fencing) (seed *policy.NetworkPolicy, degraded bool) {s.mu.Lock()defer s.mu.Unlock()if e := s.policies[subj]; e != nil {if e.fence == fence {- return nil+ return nil, false}delete(s.policies, subj)}fs, err := s.policyFiles.storeFor(subj)if err != nil {s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file store unavailable: %v", err))- return policy.DefaultDenyPolicy()+ return policy.DefaultDenyPolicy(), true}snap, err := fs.Snapshot()if err != nil {s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file unreadable: %v", err))- return policy.DefaultDenyPolicy()+ return policy.DefaultDenyPolicy(), true}if !snap.Exists() {// Fresh subject: the binding input seeds. A durable seal marker (an@@ -405,9 +405,9 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fes.policies[subj] = &policyEntry{fence: fence, sealed: true}s.pendingPolicies[subj] = policy.DefaultDenyPolicy()log.Infof("subject %s: seal restored from marker (file-less subject)", subj)- return policy.DefaultDenyPolicy()+ return policy.DefaultDenyPolicy(), true}- return nil+ return nil, false}pf, body, decErr := policy.DecodePolicyFile(snap.Data())if decErr != nil {@@ -422,7 +422,7 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fe}}s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file unusable (%v)", decErr))- return policy.DefaultDenyPolicy()+ return policy.DefaultDenyPolicy(), true}recorded := subject.Fencing{RuntimeInstanceID: pf.Incarnation, AttachmentID: pf.AttachmentID}if !recorded.Matches(fence) {@@ -433,17 +433,17 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fe} else {log.Infof("subject %s: stale policy file discarded (fence %s/%s ≠ %s/%s)", subj, pf.Incarnation, pf.AttachmentID, fence.RuntimeInstanceID, fence.AttachmentID)}- return nil+ return nil, false}pol, parseErr := policy.ParsePolicy(string(body))if 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 policy.DefaultDenyPolicy()+ return policy.DefaultDenyPolicy(), true}s.degradePolicyReloadLocked(subj, fence, fmt.Sprintf("policy file content rejected (%v)", parseErr))- return policy.DefaultDenyPolicy()+ return policy.DefaultDenyPolicy(), true}entry := &policyEntry{revision: pf.Revision, fence: fence}// A durable seal marker (export acked, teardown not yet delivered)@@ -456,7 +456,7 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fes.policies[subj] = entrys.pendingPolicies[subj] = pollog.Infof("subject %s: policy reloaded from file (revision %d, incarnation %s); binding input superseded", subj, pf.Revision, pf.Incarnation)- return pol+ return pol, false}// applyBindingPolicyLocked seeds the binding's policy for a freshlydiff --git a/components/egress/fastsandbox_vault_persist.go b/components/egress/fastsandbox_vault_persist.goindex ff2fa5e97..3036551a8 100644--- a/components/egress/fastsandbox_vault_persist.go+++ b/components/egress/fastsandbox_vault_persist.go@@ -463,7 +463,13 @@ func (s *fastSandboxPolicyServer) degradedVaultUIDsLocked() []string {// An existing slice for this exact fence wins (duplicate SET_BINDING); one// left under a different fence goes through the same file rules as a fresh// registration.-func (s *fastSandboxPolicyServer) rehydrateVaultReload(subj subject.Subject, fence subject.Fencing, pol *policy.NetworkPolicy) {+// policyDegraded reports that the recorded policy itself degraded at this+// restart: the vault then degrades WITHOUT validation and WITHOUT+// quarantine (a deny-seeded validation would quarantine a HEALTHY vault+// whose binding hosts the tightened/unknown policy no longer covers —+// silently destroying persisted credentials on every restart until a+// policy/vault relay write heals it).+func (s *fastSandboxPolicyServer) rehydrateVaultReload(subj subject.Subject, fence subject.Fencing, pol *policy.NetworkPolicy, policyDegraded bool) {if s.vaultFiles == nil {return}@@ -503,13 +509,27 @@ func (s *fastSandboxPolicyServer) rehydrateVaultReload(subj subject.Subject, fenentry.degraded = fmt.Sprintf("corrupt vault file: %v", decErr)s.quarantineVaultFile(subj, fs)log.Warnf("subject %s: %s", subj, entry.degraded)+ case policyDegraded:+ // The recorded policy degraded at this restart: hold the vault+ // degraded WITHOUT validating it against the deny fallback (a+ // deny-seeded validation would quarantine a HEALTHY vault) and+ // WITHOUT quarantining the file — a policy/vault relay write+ // heals the slice.+ entry.degraded = "held degraded: the recorded policy is degraded at this restart"+ log.Warnf("subject %s: %s (file kept)", subj, entry.degraded)case (subject.Fencing{RuntimeInstanceID: vf.Incarnation, AttachmentID: vf.AttachmentID}).Matches(fence):// Same-instance recovery: revalidate against the replayed binding- // policy, install at the exact file revision.+ // policy, install at the exact file revision. A validation+ // failure (e.g. the recorded policy was tightened and no longer+ // covers a binding host) DEGRADES the slice but KEEPS the file —+ // runtime relay commits never re-validate the vault, so+ // policy/vault divergence at restart is a normal state, and+ // quarantining here would silently destroy persisted+ // credentials. A policy/vault relay write heals the slice; a+ // policy heal + restart revives it from the kept file.if err := entry.store.Reload(vf.Revision, credentialvault.CreateRequest(body), pol); err != nil {entry.degraded = fmt.Sprintf("vault file reload rejected: %v", err)- s.quarantineVaultFile(subj, fs) // can never validate under this binding- log.Warnf("subject %s: %s", subj, entry.degraded)+ log.Warnf("subject %s: %s (file kept; a policy/vault relay write heals the slice)", subj, entry.degraded)} else {log.Infof("subject %s: vault reloaded from file (revision %d, incarnation %s)", subj, vf.Revision, vf.Incarnation)}diff --git a/components/egress/fastsandbox_vault_persist_test.go b/components/egress/fastsandbox_vault_persist_test.goindex 269f4779b..a2938de57 100644--- a/components/egress/fastsandbox_vault_persist_test.go+++ b/components/egress/fastsandbox_vault_persist_test.go@@ -547,9 +547,12 @@ func TestVaultPersistUnknownVersionDegradesButPreservesFile(t *testing.T) {func TestVaultPersistReloadValidationFailureDegrades(t *testing.T) {h := newVaultPersistHarness(t)// The file's binding host is not covered by the replayed binding policy:- // the content cannot be revalidated, so the slice degrades (fail closed)- // instead of injecting uncovered credentials — and the file is- // quarantined, since it can never validate under this binding.+ // the content cannot be revalidated, so the slice DEGRADES (fail closed)+ // instead of injecting uncovered credentials — and the file is KEPT: the+ // policy/vault divergence is a normal state (runtime relay commits never+ // re-validate the vault), so quarantining would silently destroy+ // persisted credentials. A policy heal + restart, or a vault relay+ // write, revives the slice.uncovered := `{"credentials":[{"name":"tok","source":{"type":"inline","value":"sekret"}}],` +`"bindings":[{"name":"api","match":{"hosts":["other.example.com"]},"auth":{"type":"bearer","credential":"tok"}}]}`h.seedVaultFile(vaultTestUID, testFenceR, testFenceA, 5, uncovered)@@ -563,7 +566,34 @@ func TestVaultPersistReloadValidationFailureDegrades(t *testing.T) {// The vault slice stays empty — interception fails closed.rec := doVault(t, h.srv, http.MethodGet, vaultTestUID, "", "")assert.Equal(t, http.StatusNotFound, rec.Code)- assert.False(t, h.fileFor(vaultTestUID).exists, "content rejected by the binding policy is quarantined")+ assert.True(t, h.fileFor(vaultTestUID).exists, "the uncovered-but-healthy vault file is kept (no quarantine)")++ // Revival: a policy heal (the record now covers the vault's binding+ // host) plus a restart reloads the kept vault at its preserved revision.+ healed := `{"defaultAction":"deny","egress":[{"action":"allow","target":"other.example.com"}]}`+ rec = doPolicy(t, h.srv, http.MethodPut, vaultTestUID, policyPutDoc(healed, 0, false, false), "")+ require.Equal(t, http.StatusOK, rec.Code)++ srv2, _, _ := fastSandboxTestServer(t)+ vp2, err := newVaultPersistence(h.dir)+ require.NoError(t, err)+ vp2.newFile = h.vp.newFile+ srv2.SetVaultFiles(vp2)++ // The replayed binding covers the vault's binding host (other.example.com).+ rec = doAction(t, srv2, bindBody(t, vaultTestUID, vaultTestIP, testFenceR, testFenceA, 2,+ strPtr(`{"defaultAction":"deny","egress":[{"action":"allow","target":"other.example.com"}]}`)))+ require.Equal(t, http.StatusOK, rec.Code)+ rec = doAction(t, srv2, hookBody(t, vaultTestUID, testFenceR, testFenceA, constants.HookRuntimeReady))+ require.Equal(t, http.StatusOK, rec.Code)+ rec = doAction(t, srv2, hookBody(t, vaultTestUID, testFenceR, testFenceA, constants.HookDataPlaneReady))+ require.Equal(t, http.StatusOK, rec.Code)++ rec = doVault(t, srv2, http.MethodGet, vaultTestUID, "", "")+ require.Equal(t, http.StatusOK, rec.Code)+ assert.Contains(t, rec.Body.String(), `"revision":5`)+ status = mustStatus(t, srv2)+ assert.Empty(t, status.DegradedVaults)}func TestVaultPersistDuplicateSetBindingKeepsLiveSlice(t *testing.T) {