mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
An operator-approved company grant (task_scope.kind='company') was expanded at run time from live company_scope rows and company-attributed assets. The planner can write both with add_company_scope, so one approval followed by a scope write widened the task's sandbox egress without a new operator decision, including to internal addresses. The concrete domains and CIDRs are now materialized into task_scope_egress when the operator approves the company (task creation with CompanyIDs, or AddOperatorScope), and taskSandboxRunInfo reads only that snapshot. Migration 0024 backfills already-approved grants with their current inventory, so upgraded tasks keep their reach but gain no future widening. Re-approving the company refreshes the snapshot, which makes inventory changes an explicit operator decision again.
160 lines
4.9 KiB
Go
160 lines
4.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/agent"
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/sandbox"
|
|
)
|
|
|
|
// SandboxClient is the subset of *sandbox.Client used by the panel server & engine.
|
|
type SandboxClient interface {
|
|
Health(ctx context.Context) (sandbox.HealthResponse, error)
|
|
Exec(ctx context.Context, taskID string, req sandbox.ExecRequest) (sandbox.ExecResponse, error)
|
|
Kill(ctx context.Context, taskID string) (sandbox.KillResponse, error)
|
|
Delete(ctx context.Context, taskID string) (sandbox.DeleteResponse, error)
|
|
}
|
|
|
|
// WithSandbox configures the panel's sandbox execution mode ("required" or "off")
|
|
// and its artex-runner client (CONTRACT.md §6).
|
|
func WithSandbox(client SandboxClient, mode string) Option {
|
|
return func(s *Server) {
|
|
s.sandboxClient = client
|
|
s.sandboxMode = mode
|
|
}
|
|
}
|
|
|
|
// LogSandboxOffBanner logs one prominent startup warning when ARTEX_SANDBOX=off
|
|
// is explicitly selected (CONTRACT.md §6).
|
|
func LogSandboxOffBanner() {
|
|
bar := strings.Repeat("!", 78)
|
|
log.Print("[sandbox] " + bar)
|
|
log.Print("[sandbox] !! WARNING: SANDBOX IS OFF (ARTEX_SANDBOX=off) — EXPLICIT DEGRADED MODE.")
|
|
log.Print("[sandbox] !! Agent commands will execute locally on the panel container instead of")
|
|
log.Print("[sandbox] !! isolated per-task artex-worker containers. Do not use with untrusted targets.")
|
|
log.Print("[sandbox] " + bar)
|
|
}
|
|
|
|
// checkSandboxReady verifies artex-runner is reachable and connected to Docker
|
|
// before starting a task in ARTEX_SANDBOX=required mode.
|
|
func (s *Server) checkSandboxReady(ctx context.Context) error {
|
|
if s.sandboxMode != sandbox.ModeRequired {
|
|
return nil
|
|
}
|
|
if s.sandboxClient == nil {
|
|
return fmt.Errorf("%w: ARTEX_SANDBOX=required but no runner client is configured (set ARTEX_RUNNER_URL and ARTEX_RUNNER_TOKEN)", sandbox.ErrRunnerUnavailable)
|
|
}
|
|
hctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
h, err := s.sandboxClient.Health(hctx)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot start task: sandbox runner unavailable (%w)", err)
|
|
}
|
|
if !h.OK || !h.Docker {
|
|
return fmt.Errorf("cannot start task: %w (health ok=%v docker=%v)", sandbox.ErrDockerUnavailable, h.OK, h.Docker)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// taskSandboxRunInfo builds the per-task sandbox metadata (including scope
|
|
// domains and CIDRs for the per-task egress proxy) from PostgreSQL.
|
|
func taskSandboxRunInfo(pg *db.DB, dataDir, taskID string) agent.SandboxRunInfo {
|
|
info := agent.SandboxRunInfo{
|
|
TaskID: taskID,
|
|
TaskDir: filepath.Join(dataDir, "tasks", taskID),
|
|
}
|
|
if pg == nil {
|
|
return info
|
|
}
|
|
tid, err := strconv.ParseInt(taskID, 10, 64)
|
|
if err != nil || tid <= 0 {
|
|
return info
|
|
}
|
|
seenDom := map[string]bool{}
|
|
seenCIDR := map[string]bool{}
|
|
addDom := func(d string) {
|
|
d = strings.ToLower(strings.TrimSpace(d))
|
|
d = strings.TrimPrefix(d, "*.")
|
|
d = strings.TrimPrefix(d, ".")
|
|
if d != "" && !seenDom[d] {
|
|
seenDom[d] = true
|
|
info.ScopeDomains = append(info.ScopeDomains, d)
|
|
}
|
|
}
|
|
addCIDR := func(c string) {
|
|
c = strings.TrimSpace(c)
|
|
if c != "" && !seenCIDR[c] {
|
|
seenCIDR[c] = true
|
|
info.ScopeCIDRs = append(info.ScopeCIDRs, c)
|
|
}
|
|
}
|
|
var companyIDs []int64
|
|
seenComp := map[int64]bool{}
|
|
if rows, err := pg.Query(`SELECT kind, COALESCE(domain,''), COALESCE(net::text,''), COALESCE(value,''), COALESCE(company_id,0) FROM task_scope WHERE task_id=$1 AND approved_by_operator=true AND COALESCE(expanded,false)=false`, tid); err == nil {
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var kind, dom, netStr, val string
|
|
var compID int64
|
|
if rows.Scan(&kind, &dom, &netStr, &val, &compID) == nil {
|
|
switch kind {
|
|
case "company":
|
|
if compID > 0 && !seenComp[compID] {
|
|
seenComp[compID] = true
|
|
companyIDs = append(companyIDs, compID)
|
|
}
|
|
case "domain", "root_domain", "subdomain":
|
|
if dom != "" {
|
|
addDom(dom)
|
|
} else {
|
|
addDom(val)
|
|
}
|
|
case "cidr", "ip", "ip_range":
|
|
if netStr != "" {
|
|
addCIDR(netStr)
|
|
} else {
|
|
addCIDR(val)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Company grants are frozen at operator-approval time in task_scope_egress.
|
|
// Reading live company_scope/assets here let the planner's add_company_scope
|
|
// widen an approved task's egress without a new operator decision.
|
|
for _, cid := range companyIDs {
|
|
entries, err := pg.CompanyEgressForTask(tid, cid)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, e := range entries {
|
|
switch e.Kind {
|
|
case "domain":
|
|
addDom(e.Value)
|
|
case "cidr":
|
|
addCIDR(e.Value)
|
|
}
|
|
}
|
|
}
|
|
if rows, err := pg.Query(`SELECT kind, pattern FROM task_intercept_rules WHERE task_id=$1 AND enabled=true AND action='allow'`, tid); err == nil {
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var kind, pat string
|
|
if rows.Scan(&kind, &pat) == nil {
|
|
switch kind {
|
|
case "exact_domain", "domain_suffix":
|
|
addDom(pat)
|
|
case "cidr", "ip":
|
|
addCIDR(pat)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return info
|
|
}
|