mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
fuzzy_domain is a substring match, so an allow rule meant to cover a
site and its subdomains (".example.com") also admits names that merely
contain it, such as "a.example.com.other.net", whose owner chooses where
it resolves. There was no kind for "this domain and its subdomains" on a
label boundary.
domain_suffix matches the domain itself and any name ending in
"." + domain; "*.example.com" and ".example.com" are stored as
"example.com". This is the semantics the sandbox egress policy already
applies to scope domains, and taskSandboxRunInfo already read a
domain_suffix allow rule. fuzzy_domain is unchanged, so existing rules
keep their meaning.
Migration 0026 widens the kind CHECK constraint of asset_intercept_rules
and task_intercept_rules; schema.sql carries the same list for new
databases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
3.1 KiB
Go
80 lines
3.1 KiB
Go
package db
|
|
|
|
import "time"
|
|
|
|
// AssetInterceptRule is one row of asset_intercept_rules — a global asset
|
|
// blocklist entry. Unlike intercept_rules (which matches tool name / input),
|
|
// these match the *target asset*: an exact/fuzzy domain·ip·url, or a CIDR range.
|
|
// This layer only stores rules; the matching/enforcement logic lives elsewhere.
|
|
type AssetInterceptRule struct {
|
|
ID int64 `json:"id"`
|
|
Enabled bool `json:"enabled"`
|
|
Kind string `json:"kind"` // exact_domain|exact_ip|exact_url|domain_suffix|fuzzy_domain|fuzzy_ip|fuzzy_url|cidr
|
|
Pattern string `json:"pattern"`
|
|
Note string `json:"note"`
|
|
Builtin bool `json:"builtin"`
|
|
// Action is only used by task-level rules: 'block'=intercept, 'allow'=allow (whitelist).
|
|
// Global rules (asset_intercept_rules) do not have this column; it is always empty and treated as intercept.
|
|
Action string `json:"action,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
const assetInterceptRuleCols = `id, enabled, kind, pattern, note, builtin, created_at, updated_at`
|
|
|
|
func scanAssetInterceptRule(row interface{ Scan(...any) error }) (AssetInterceptRule, error) {
|
|
var r AssetInterceptRule
|
|
err := row.Scan(&r.ID, &r.Enabled, &r.Kind, &r.Pattern, &r.Note, &r.Builtin, &r.CreatedAt, &r.UpdatedAt)
|
|
return r, err
|
|
}
|
|
|
|
// ListAssetInterceptRules returns all rules, built-ins first then newest first.
|
|
func (d *DB) ListAssetInterceptRules() ([]AssetInterceptRule, error) {
|
|
rows, err := d.Query(`SELECT ` + assetInterceptRuleCols + ` FROM asset_intercept_rules ORDER BY builtin DESC, id DESC`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []AssetInterceptRule
|
|
for rows.Next() {
|
|
r, err := scanAssetInterceptRule(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// CreateAssetInterceptRule inserts a new user rule (builtin is always false here).
|
|
func (d *DB) CreateAssetInterceptRule(kind, pattern, note string, enabled bool) (AssetInterceptRule, error) {
|
|
row := d.QueryRow(`
|
|
INSERT INTO asset_intercept_rules(enabled, kind, pattern, note, builtin)
|
|
VALUES ($1, $2, $3, $4, false)
|
|
RETURNING `+assetInterceptRuleCols,
|
|
enabled, kind, pattern, note)
|
|
return scanAssetInterceptRule(row)
|
|
}
|
|
|
|
// UpdateAssetInterceptRule replaces the editable fields of an existing rule.
|
|
func (d *DB) UpdateAssetInterceptRule(id int64, kind, pattern, note string, enabled bool) (AssetInterceptRule, error) {
|
|
row := d.QueryRow(`
|
|
UPDATE asset_intercept_rules
|
|
SET enabled=$2, kind=$3, pattern=$4, note=$5
|
|
WHERE id=$1
|
|
RETURNING `+assetInterceptRuleCols,
|
|
id, enabled, kind, pattern, note)
|
|
return scanAssetInterceptRule(row)
|
|
}
|
|
|
|
// DeleteAssetInterceptRule removes a rule (built-in rules are deletable too).
|
|
func (d *DB) DeleteAssetInterceptRule(id int64) error {
|
|
_, err := d.Exec(`DELETE FROM asset_intercept_rules WHERE id=$1`, id)
|
|
return err
|
|
}
|
|
|
|
// ToggleAssetInterceptRule flips the enabled state of a rule.
|
|
func (d *DB) ToggleAssetInterceptRule(id int64, enabled bool) error {
|
|
_, err := d.Exec(`UPDATE asset_intercept_rules SET enabled=$2 WHERE id=$1`, id, enabled)
|
|
return err
|
|
}
|