ARTEX/server/sandbox_empty_egress_warning_test.go
simple-minded f69f94e658
fix(sandbox): warn when a task's egress allowlist comes up empty
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
2026-12-02 17:44:25 +05:30

91 lines
3.1 KiB
Go

package server
import (
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/db"
)
// TestEmptyEgressScopeWarning covers the chat-spawn scope trap of issue #830:
// spawn_task stamps the company link approved_by_operator=false, so the task
// starts with an empty sandbox egress allowlist and every connection from the
// sandbox is denied, with nothing in the UI saying why. The engine must warn
// loudly when a task carries scope rows whose allowlist still comes up empty.
func TestEmptyEgressScopeWarning(t *testing.T) {
dsn := strings.TrimSpace(os.Getenv("ARTEX_PG_DSN"))
if dsn == "" {
t.Skip("ARTEX_PG_DSN not set")
}
pg, err := db.Open(dsn)
if err != nil {
db.SkipPGUnavailable(t, err)
}
t.Cleanup(func() { _ = pg.Close() })
suffix := strconv.FormatInt(time.Now().UnixNano(), 36)
compID, _, _, _, _, err := pg.Companies().CreateCompanyWithScope(
"EmptyEgressCo-"+suffix, "",
[]db.ScopeInput{{Kind: "cidr", Value: "203.0.113.0/24"}},
"test fixture",
true,
)
if err != nil {
t.Fatalf("CreateCompanyWithScope: %v", err)
}
warn := func(t *testing.T, taskID int64) string {
t.Helper()
info := taskSandboxRunInfo(pg, t.TempDir(), strconv.FormatInt(taskID, 10))
return emptyEgressScopeWarning(pg, taskID, info)
}
// Case 1, the trap: a chat-spawned task carries the company link stamped by
// the agent (approved_by_operator=false). The allowlist is empty and the
// operator must hear about it.
trap, err := pg.CreateTaskWithOptions("empty-egress-trap-"+suffix, "goal", db.TaskCreateOptions{
CompanyIDs: []int64{compID},
CompanyScopeByAgent: true,
})
if err != nil {
t.Fatalf("CreateTaskWithOptions(trap): %v", err)
}
t.Cleanup(func() { _ = pg.DeleteTask(trap.ID) })
trapInfo := taskSandboxRunInfo(pg, t.TempDir(), strconv.FormatInt(trap.ID, 10))
if len(trapInfo.ScopeDomains)+len(trapInfo.ScopeCIDRs) != 0 {
t.Fatalf("precondition: agent-stamped company must grant no egress, got domains=%v cidrs=%v",
trapInfo.ScopeDomains, trapInfo.ScopeCIDRs)
}
if w := warn(t, trap.ID); w == "" {
t.Fatal("task with scope rows and an empty allowlist must warn (issue #830): got no warning")
} else if !strings.Contains(w, "approv") {
t.Fatalf("warning must point the operator at approval, got %q", w)
}
// Case 2: the same company selected by the operator at creation. The
// allowlist is non-empty, so there is nothing to warn about.
okTask, err := pg.CreateTaskWithOptions("empty-egress-ok-"+suffix, "goal", db.TaskCreateOptions{
CompanyIDs: []int64{compID},
})
if err != nil {
t.Fatalf("CreateTaskWithOptions(ok): %v", err)
}
t.Cleanup(func() { _ = pg.DeleteTask(okTask.ID) })
if w := warn(t, okTask.ID); w != "" {
t.Fatalf("operator-approved scope must not warn, got %q", w)
}
// Case 3: a scopeless task. The allowlist is empty by design; no rows, no
// warning.
bare, err := pg.CreateTaskWithOptions("empty-egress-bare-"+suffix, "goal", db.TaskCreateOptions{})
if err != nil {
t.Fatalf("CreateTaskWithOptions(bare): %v", err)
}
t.Cleanup(func() { _ = pg.DeleteTask(bare.ID) })
if w := warn(t, bare.ID); w != "" {
t.Fatalf("scopeless task must not warn, got %q", w)
}
}