From 93fa316d760b5fc10589aaba12ad85efa68cfe95 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Tue, 1 Dec 2026 02:59:58 +0100 Subject: [PATCH] fix(db): revoke materialized company egress with the scope row Deleting an operator-approved company scope row left its task_scope_egress entries behind, so the task's egress proxy stayed open after the operator revoked the company. DeleteTaskScope now reads the deleted row and drops the matching snapshot entries in the same transaction; the regression test asserts that the sandbox scope is empty after revocation. --- db/task_scope.go | 29 +++++++++++++++++++++++---- server/sandbox_company_egress_test.go | 23 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/db/task_scope.go b/db/task_scope.go index 5452b884..20392b31 100644 --- a/db/task_scope.go +++ b/db/task_scope.go @@ -2,6 +2,7 @@ package db import ( "database/sql" + "errors" "fmt" "net" "sort" @@ -351,14 +352,34 @@ func scopeRowCovers(existing, cand TaskScope) bool { } // DeleteTaskScope removes a single scope row by id, scoped to the given task. -// Returns whether a row was actually deleted. +// Returns whether a row was actually deleted. Deleting a company grant also +// drops its materialized task_scope_egress entries: leaving them would keep the +// task's egress proxy open after the operator revoked the company. func (s *AssetStore) DeleteTaskScope(taskID, scopeID int64) (bool, error) { - res, err := s.db.Exec(`DELETE FROM task_scope WHERE id=$1 AND task_id=$2`, scopeID, taskID) + tx, err := s.db.Begin() if err != nil { return false, err } - n, _ := res.RowsAffected() - return n > 0, nil + defer func() { _ = tx.Rollback() }() + var kind string + var companyID sql.NullInt64 + err = tx.QueryRow(`DELETE FROM task_scope WHERE id=$1 AND task_id=$2 RETURNING kind, company_id`, scopeID, taskID). + Scan(&kind, &companyID) + if errors.Is(err, sql.ErrNoRows) { + return false, nil + } + if err != nil { + return false, err + } + if kind == "company" && companyID.Valid { + if _, err := tx.Exec(`DELETE FROM task_scope_egress WHERE task_id=$1 AND company_id=$2`, taskID, companyID.Int64); err != nil { + return false, err + } + } + if err := tx.Commit(); err != nil { + return false, err + } + return true, nil } // ListTaskScope returns all scope rows for a task. diff --git a/server/sandbox_company_egress_test.go b/server/sandbox_company_egress_test.go index 0e182a28..ad32a2d8 100644 --- a/server/sandbox_company_egress_test.go +++ b/server/sandbox_company_egress_test.go @@ -109,4 +109,27 @@ func TestSandboxCompanyEgressFrozenAtApproval(t *testing.T) { if !scopeHas(domains, approvedDom) || !scopeHas(domains, injectedDom) || !scopeHas(cidrs, approvedCIDR) { t.Fatalf("approval snapshot incomplete: domains=%v cidrs=%v", domains, cidrs) } + + // Removing the company grant revokes the materialized egress, so the proxy + // does not stay open after the operator deletes the scope row. + rows, err := as.ListTaskScope(task2.ID) + if err != nil { + t.Fatalf("ListTaskScope: %v", err) + } + companyScopeID := int64(0) + for _, r := range rows { + if r.Kind == "company" && r.CompanyID != nil && *r.CompanyID == compID { + companyScopeID = r.ID + } + } + if companyScopeID == 0 { + t.Fatal("company scope row not found") + } + if ok, err := as.DeleteTaskScope(task2.ID, companyScopeID); err != nil || !ok { + t.Fatalf("DeleteTaskScope: ok=%v err=%v", ok, err) + } + domains, cidrs = runInfo(t, task2.ID) + if len(domains) != 0 || len(cidrs) != 0 { + t.Fatalf("revoked company grant still grants egress: domains=%v cidrs=%v", domains, cidrs) + } }