mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
CompanyEgressForTask errors no longer skip a company's grants: the caller returns TargetGateUnavailable instead of a silently narrowed set. The TestTaskEgressGrants doc no longer claims allow rules participate (grants-only by design since the gate test that pinned that invariant), and the orphan Security line under the Unreleased heading is gone.
159 lines
4.9 KiB
Go
159 lines
4.9 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"strings"
|
|
)
|
|
|
|
// egressExecer is satisfied by *DB, *sql.DB and *sql.Tx.
|
|
type egressExecer interface {
|
|
Exec(query string, args ...any) (sql.Result, error)
|
|
}
|
|
|
|
// ScopeEgress is one materialized egress entry an operator approval granted.
|
|
type ScopeEgress struct {
|
|
Kind string // domain|cidr
|
|
Value string
|
|
}
|
|
|
|
// snapshotCompanyEgressSQL freezes the domains and CIDRs a company grant covers
|
|
// right now: its operator-approved company_scope domain and IP/CIDR rules. The
|
|
// sandbox proxy reads task_scope_egress, so inventory written after this point
|
|
// cannot widen an approved task. Assets and agent-written company_scope rows
|
|
// (add_company_scope, ScopeSentry sync) are never copied: a worker controls
|
|
// both, and the next operator approval would otherwise promote them.
|
|
const snapshotCompanyEgressSQL = `
|
|
INSERT INTO task_scope_egress(task_id, kind, value, company_id)
|
|
SELECT $1, e.kind, e.value, e.company_id
|
|
FROM (
|
|
SELECT 'domain' AS kind, lower(trim(cs.domain)) AS value, cs.company_id
|
|
FROM company_scope cs
|
|
JOIN unnest($2::bigint[]) AS ids(id) ON ids.id = cs.company_id
|
|
WHERE cs.kind = 'domain' AND COALESCE(cs.domain, '') <> ''
|
|
AND cs.approved_by_operator = TRUE
|
|
UNION ALL
|
|
SELECT 'cidr', cs.net::text, cs.company_id
|
|
FROM company_scope cs
|
|
JOIN unnest($2::bigint[]) AS ids(id) ON ids.id = cs.company_id
|
|
WHERE cs.kind IN ('ip', 'cidr') AND cs.net IS NOT NULL
|
|
AND cs.approved_by_operator = TRUE
|
|
) e
|
|
WHERE e.value <> ''
|
|
ON CONFLICT DO NOTHING`
|
|
|
|
// snapshotCompanyEgress records the current inventory of every given company as
|
|
// task egress. Call it only from an operator-approval path.
|
|
func snapshotCompanyEgress(ex egressExecer, taskID int64, companyIDs []int64) error {
|
|
if taskID <= 0 || len(companyIDs) == 0 {
|
|
return nil
|
|
}
|
|
_, err := ex.Exec(snapshotCompanyEgressSQL, taskID, companyIDs)
|
|
return err
|
|
}
|
|
|
|
// CompanyEgressForTask returns the frozen entries one operator-approved company
|
|
// grant gave this task. A company without entries grants no egress.
|
|
func (d *DB) CompanyEgressForTask(taskID, companyID int64) ([]ScopeEgress, error) {
|
|
rows, err := d.Query(`
|
|
SELECT kind, value FROM task_scope_egress
|
|
WHERE task_id=$1 AND company_id=$2
|
|
ORDER BY kind, value`, taskID, companyID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []ScopeEgress
|
|
for rows.Next() {
|
|
var e ScopeEgress
|
|
if err := rows.Scan(&e.Kind, &e.Value); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, e)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// TaskEgressGrants returns the normalized egress entries an operator approved
|
|
// for this task: the task's own approved, unexpanded task_scope rows and the
|
|
// entries frozen into task_scope_egress by approved company grants (the same
|
|
// rows, normalized the same way, that feed the sandbox proxy's allowlist).
|
|
// Unapproved rows (agent-written, expanded) grant nothing, and task allow
|
|
// interception rules play no role here either: they satisfy neither the
|
|
// network nor the gate (TestAllowRuleDoesNotSatisfyGate). The target gate
|
|
// consults this set so it cannot demand an approval for a target whose
|
|
// address space the operator already granted.
|
|
func (d *DB) TaskEgressGrants(taskID int64) ([]ScopeEgress, error) {
|
|
if taskID <= 0 {
|
|
return nil, nil
|
|
}
|
|
seenDom := map[string]bool{}
|
|
seenCIDR := map[string]bool{}
|
|
var out []ScopeEgress
|
|
add := func(kind, value string) {
|
|
if kind == "domain" {
|
|
value = strings.ToLower(strings.TrimSpace(value))
|
|
value = strings.TrimPrefix(value, "*.")
|
|
value = strings.TrimPrefix(value, ".")
|
|
} else {
|
|
value = strings.TrimSpace(value)
|
|
}
|
|
if value == "" {
|
|
return
|
|
}
|
|
seen := seenCIDR
|
|
if kind == "domain" {
|
|
seen = seenDom
|
|
}
|
|
if !seen[value] {
|
|
seen[value] = true
|
|
out = append(out, ScopeEgress{Kind: kind, Value: value})
|
|
}
|
|
}
|
|
rows, err := d.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`, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var companyIDs []int64
|
|
seenComp := map[int64]bool{}
|
|
for rows.Next() {
|
|
var kind, dom, netStr, val string
|
|
var compID int64
|
|
if rows.Scan(&kind, &dom, &netStr, &val, &compID) != nil {
|
|
continue
|
|
}
|
|
switch kind {
|
|
case "company":
|
|
if compID > 0 && !seenComp[compID] {
|
|
seenComp[compID] = true
|
|
companyIDs = append(companyIDs, compID)
|
|
}
|
|
case "domain", "root_domain", "subdomain":
|
|
if dom != "" {
|
|
add("domain", dom)
|
|
} else {
|
|
add("domain", val)
|
|
}
|
|
case "cidr", "ip", "ip_range":
|
|
if netStr != "" {
|
|
add("cidr", netStr)
|
|
} else {
|
|
add("cidr", val)
|
|
}
|
|
}
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
rows.Close()
|
|
for _, cid := range companyIDs {
|
|
entries, err := d.CompanyEgressForTask(taskID, cid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, e := range entries {
|
|
add(e.Kind, e.Value)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|