An open-source, faster Devin alternative
TypeScript
fix(secrets-proxy): register session for allowlist-only secret stores
motatoesf6614ebcc433
diff --git a/internal/qemu/manager.go b/internal/qemu/manager.goindex 2280f54b..3cbf3a34 100644--- a/internal/qemu/manager.go+++ b/internal/qemu/manager.go@@ -300,7 +300,7 @@ func (m *Manager) sealSandboxEnvs(ctx context.Context, sandboxID string, netCfg}return merged}- if len(cfg.Envs) == 0 && len(cfg.SecretEnvs) == 0 {+ if len(cfg.Envs) == 0 && len(cfg.SecretEnvs) == 0 && len(cfg.EgressAllowlist) == 0 {return cfg.Envs}sealed := m.secretsProxy.CreateSealedEnvs(sandboxID, netCfg.GuestIP, netCfg.HostIP, cfg.Envs, cfg.SecretEnvs, cfg.EgressAllowlist, cfg.SecretAllowedHosts)@@ -2709,10 +2709,11 @@ func (m *Manager) RestoreFromCheckpoint(ctx context.Context, sandboxID, checkpoilog.Printf("qemu: RestoreFromCheckpoint %s: clock sync failed: %v", sandboxID, err)}- // Re-register secrets proxy session from checkpoint metadata.- if m.secretsProxy != nil && len(cpMeta.SealedTokens) > 0 {+ // Re-register secrets proxy session from checkpoint metadata. An allowlist+ // alone is enough — without a session the proxy 407s every request.+ if m.secretsProxy != nil && (len(cpMeta.SealedTokens) > 0 || len(cpMeta.EgressAllowlist) > 0) {m.secretsProxy.ReregisterSession(sandboxID, netCfg.GuestIP, cpMeta.SealedTokens, cpMeta.EgressAllowlist, cpMeta.TokenHosts)- log.Printf("qemu: RestoreFromCheckpoint %s: re-registered secrets proxy session (%d tokens)", sandboxID, len(cpMeta.SealedTokens))+ log.Printf("qemu: RestoreFromCheckpoint %s: re-registered secrets proxy session (%d tokens, %d allowlist)", sandboxID, len(cpMeta.SealedTokens), len(cpMeta.EgressAllowlist))}// Step 8: Update VM instancediff --git a/internal/qemu/migration.go b/internal/qemu/migration.goindex afc31a17..6d172ad5 100644--- a/internal/qemu/migration.go+++ b/internal/qemu/migration.go@@ -748,11 +748,12 @@ func (m *Manager) PrepareIncomingMigrationWithS3(ctx context.Context, sandboxID,// returning so the source can issue the LiveMigrate RPC immediately// without a races window where the migrated VM is running but the// proxy has no substitution map.- if m.secretsProxy != nil && len(secrets.SealedTokens) > 0 {+ // An allowlist alone is enough — without a session the proxy 407s every request.+ if m.secretsProxy != nil && (len(secrets.SealedTokens) > 0 || len(secrets.EgressAllowlist) > 0) {if vm, getErr := m.getVM(sandboxID); getErr == nil && vm.network != nil && vm.network.GuestIP != "" {m.secretsProxy.ReregisterSession(sandboxID, vm.network.GuestIP, secrets.SealedTokens, secrets.EgressAllowlist, secrets.TokenHosts)- log.Printf("qemu: migration %s: re-registered secrets proxy session (%d tokens, guestIP=%s)",- sandboxID, len(secrets.SealedTokens), vm.network.GuestIP)+ log.Printf("qemu: migration %s: re-registered secrets proxy session (%d tokens, %d allowlist, guestIP=%s)",+ sandboxID, len(secrets.SealedTokens), len(secrets.EgressAllowlist), vm.network.GuestIP)}}diff --git a/internal/qemu/snapshot.go b/internal/qemu/snapshot.goindex ae3d3b39..81ee343e 100644--- a/internal/qemu/snapshot.go+++ b/internal/qemu/snapshot.go@@ -486,10 +486,11 @@ func (m *Manager) doWake(ctx context.Context, sandboxID, checkpointKey string, clog.Printf("qemu: wake %s: clock sync failed: %v", sandboxID, err)}- // Re-register secrets proxy session from persisted tokens.- if m.secretsProxy != nil && len(meta.SealedTokens) > 0 {+ // Re-register secrets proxy session from persisted tokens. An allowlist+ // alone is enough — without a session the proxy 407s every request.+ if m.secretsProxy != nil && (len(meta.SealedTokens) > 0 || len(meta.EgressAllowlist) > 0) {m.secretsProxy.ReregisterSession(sandboxID, netCfg.GuestIP, meta.SealedTokens, meta.EgressAllowlist, meta.TokenHosts)- log.Printf("qemu: wake %s: re-registered secrets proxy session (%d tokens)", sandboxID, len(meta.SealedTokens))+ log.Printf("qemu: wake %s: re-registered secrets proxy session (%d tokens, %d allowlist)", sandboxID, len(meta.SealedTokens), len(meta.EgressAllowlist))}// Refresh the proxy CA in the guest's trust store. Wake may land on a// different worker than the one that hibernated the sandbox, in whichdiff --git a/internal/secretsproxy/proxy.go b/internal/secretsproxy/proxy.goindex 652f86bb..7ad385f7 100644--- a/internal/secretsproxy/proxy.go+++ b/internal/secretsproxy/proxy.go@@ -198,12 +198,14 @@ func (p *SecretsProxy) ReregisterSession(sandboxID, guestIP string, tokens map[s// CreateSealedEnvs generates sealed tokens for the given env vars, registers a// proxy session, and returns the complete env map to inject into the VM.// Includes sealed tokens + proxy config vars (HTTP_PROXY, CA certs, etc.).-// Returns nil if envVars is empty.+// Returns nil only if there is nothing to enforce: no envs, no secrets, and no+// allowlist. An allowlist alone is still enough to require routing through the+// proxy so the allowlist actually gates egress.//// allowlist controls which hosts the sandbox can reach (nil = all).// secretAllowedHosts maps env var name → allowed hosts for that secret (nil = all allowed hosts).func (p *SecretsProxy) CreateSealedEnvs(sandboxID, guestIP, gatewayIP string, plaintextEnvs, secretEnvs map[string]string, allowlist []string, secretAllowedHosts map[string][]string) map[string]string {- if len(plaintextEnvs) == 0 && len(secretEnvs) == 0 {+ if len(plaintextEnvs) == 0 && len(secretEnvs) == 0 && len(allowlist) == 0 {return nil}@@ -237,8 +239,11 @@ func (p *SecretsProxy) CreateSealedEnvs(sandboxID, guestIP, gatewayIP string, pl}}- // Only register a proxy session if there's actually something to substitute.- if len(tokenMap) > 0 {+ // Register a proxy session whenever the sandbox has anything for the proxy+ // to act on: tokens to substitute OR an egress allowlist to enforce. Without+ // a session the proxy rejects every CONNECT with 407 no_session, so an+ // allowlist-only store would otherwise leave the sandbox with zero egress.+ if len(tokenMap) > 0 || len(allowlist) > 0 {p.RegisterSession(guestIP, &Session{SandboxID: sandboxID,Secrets: tokenMap,diff --git a/internal/secretsproxy/proxy_test.go b/internal/secretsproxy/proxy_test.gonew file mode 100644index 00000000..d15e1cb9--- /dev/null+++ b/internal/secretsproxy/proxy_test.go@@ -0,0 +1,108 @@+package secretsproxy++import (+ "io"+ "log"+ "reflect"+ "testing"+)++// newProxyForTest builds a SecretsProxy with no listener — sufficient for+// exercising session registration / CreateSealedEnvs without going on the wire.+//+// Sets the package-global log output to io.Discard for the duration of the+// test. bench_test.go leaves log.SetOutput(nil) behind on cleanup, which would+// otherwise panic any log.Printf in tests that run after it.+func newProxyForTest(t *testing.T) *SecretsProxy {+ t.Helper()+ log.SetOutput(io.Discard)+ return &SecretsProxy{+ sessions: make(map[string]*Session),+ closed: make(chan struct{}),+ }+}++func TestCreateSealedEnvs_AllowlistOnlyRegistersSession(t *testing.T) {+ p := newProxyForTest(t)++ allowlist := []string{"example.com", "api.openai.com"}+ envs := p.CreateSealedEnvs("sb-test", "10.0.0.5", "10.0.0.1",+ map[string]string{"FOO": "bar"}, // user envs+ nil, // no secret envs+ allowlist, // allowlist only+ nil,+ )++ if envs == nil {+ t.Fatal("CreateSealedEnvs returned nil; expected non-nil env map for allowlist-only sandbox")+ }+ if envs["HTTPS_PROXY"] == "" {+ t.Errorf("expected HTTPS_PROXY to be injected when allowlist is set; got %q", envs["HTTPS_PROXY"])+ }++ got := p.sessions["10.0.0.5"]+ if got == nil {+ t.Fatal("no session registered for guest IP; allowlist-only sandboxes must still get a session")+ }+ if !reflect.DeepEqual(got.Allowlist, allowlist) {+ t.Errorf("session allowlist = %v, want %v", got.Allowlist, allowlist)+ }+ if len(got.Secrets) != 0 {+ t.Errorf("session unexpectedly has secrets: %v", got.Secrets)+ }+}++func TestCreateSealedEnvs_NoEnvsNoSecretsNoAllowlistReturnsNil(t *testing.T) {+ p := newProxyForTest(t)+ envs := p.CreateSealedEnvs("sb-empty", "10.0.0.6", "10.0.0.1", nil, nil, nil, nil)+ if envs != nil {+ t.Errorf("expected nil env map when there is nothing to enforce; got %v", envs)+ }+ if _, exists := p.sessions["10.0.0.6"]; exists {+ t.Errorf("did not expect a registered session when there is nothing to enforce")+ }+}++func TestCreateSealedEnvs_AllowlistOnlyEnforcesViaSession(t *testing.T) {+ p := newProxyForTest(t)+ p.CreateSealedEnvs("sb-allow", "10.0.0.7", "10.0.0.1",+ map[string]string{"K": "v"},+ nil,+ []string{"example.com"},+ nil,+ )++ sess := p.sessions["10.0.0.7"]+ if sess == nil {+ t.Fatal("allowlist-only session should be registered")+ }+ if !hostAllowed("example.com", sess.Allowlist) {+ t.Errorf("example.com should be allowed by registered session")+ }+ if hostAllowed("evil.com", sess.Allowlist) {+ t.Errorf("evil.com should NOT be allowed by registered session")+ }+}++func TestCreateSealedEnvs_SecretsOnlyStillRegisters(t *testing.T) {+ // Regression guard: the pre-fix behavior (register only when secrets exist)+ // must still hold. Sandboxes with secrets but no allowlist get a session.+ p := newProxyForTest(t)+ p.CreateSealedEnvs("sb-secret", "10.0.0.8", "10.0.0.1",+ nil,+ map[string]string{"API_KEY": "real-secret"},+ nil,+ nil,+ )++ sess := p.sessions["10.0.0.8"]+ if sess == nil {+ t.Fatal("session must still register when only secrets are present")+ }+ if len(sess.Secrets) != 1 {+ t.Errorf("expected 1 sealed secret in session; got %d", len(sess.Secrets))+ }+ if len(sess.Allowlist) != 0 {+ t.Errorf("expected empty allowlist; got %v", sess.Allowlist)+ }+}