fix(agent): refuse insert_assets when intercept rules cannot be loaded

insert_assets swallowed ListAssetInterceptRules and
TaskInterceptRulesSplit errors and evaluated an empty rule set, which
allowed every asset. A load failure now refuses the whole call. The
tool result is generic; the driver error stays in the process log.
This commit is contained in:
Coff0xc
2026-11-18 17:22:36 +01:00
committed by BaaRai
parent 74c9c17626
commit 20b278cf98
3 changed files with 70 additions and 6 deletions
+1
View File
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **insert_assets fail-closed**: when intercept or task allow/block rules cannot be loaded, `insert_assets` refuses the whole call instead of evaluating an empty rule set (which allowed every asset). The tool result does not include the underlying database error.
- **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`.
- **PostgreSQL text[] literals escape backslashes**: `marshalStringArray` quoted double quotes but not backslashes, so a value ending in `\"` could close the current array element and inject another. It now doubles backslashes before quotes, matching `marshalJSONBArray`.
- **Browser MCP env null no longer crashes startup**: a stored MCP `env` of JSON `null` unmarshaled into a nil map, and `syncBrowserMCPProxy` then assigned `NODE_EXTRA_CA_CERTS` when traffic capture was on, panicking the process on every start. `decodeStrMap` now always returns a writable map.
+47
View File
@@ -3,6 +3,7 @@ package agent
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
@@ -449,3 +450,49 @@ func TestInsertAssetsRejectsHostnameIPPerItem(t *testing.T) {
t.Fatalf("rejected hostname still stored in assets.ip (%d rows)", stored)
}
}
func TestInsertAssetsRuleLoadFailure(t *testing.T) {
if out, refused := insertAssetsRuleLoadFailure(nil); refused || out.IsError {
t.Fatalf("nil error must not refuse: refused=%v IsError=%v %q", refused, out.IsError, out.Flatten())
}
out, refused := insertAssetsRuleLoadFailure(errors.New("sql: database is closed"))
text := out.Flatten()
if !refused || !out.IsError || !strings.Contains(text, "asset intercept rules are unavailable") {
t.Fatalf("want fail-closed refusal, refused=%v IsError=%v %q", refused, out.IsError, text)
}
for _, leak := range []string{"sql:", "database is closed"} {
if strings.Contains(text, leak) {
t.Fatalf("tool result leaked the load error: %q", text)
}
}
}
func TestInsertAssetsRefusesWhenInterceptRulesUnavailable(t *testing.T) {
d := testDB(t)
ts := NewToolSet(nil, "")
ts.SetAssetStore(d.Assets(), d.Companies())
d.Close()
raw, err := json.Marshal(map[string]any{
"assets": []any{
map[string]any{"type": "ip", "ip": "203.0.113.50"},
},
})
if err != nil {
t.Fatal(err)
}
res, err := ts.insertAssets().Call(context.Background(), raw, nil)
if err != nil {
t.Fatalf("Call error: %v", err)
}
text := res.Flatten()
if !res.IsError || !strings.Contains(text, "asset intercept rules are unavailable") {
t.Fatalf("want fail-closed refusal, IsError=%v %q", res.IsError, text)
}
for _, leak := range []string{"sql:", "database is closed", "pq:", "connection"} {
if strings.Contains(strings.ToLower(text), strings.ToLower(leak)) {
t.Fatalf("tool result leaked the load error: %q", text)
}
}
}
+22 -6
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"net/url"
"strconv"
@@ -121,6 +122,17 @@ type assetInputItem struct {
Params []map[string]any `json:"params"`
}
// insertAssetsRuleLoadFailure refuses the whole insert_assets call when
// intercept rules cannot be loaded. The underlying error stays in the process
// log so the model never sees a driver string.
func insertAssetsRuleLoadFailure(err error) (actool.Result, bool) {
if err == nil {
return actool.Result{}, false
}
log.Printf("[insert_assets] intercept rules unavailable: %v", err)
return actool.Errorf("asset intercept rules are unavailable; insertion refused"), true
}
// insertAssets is the unified insert_assets agent tool.
func (t *ToolSet) insertAssets() actool.CoreTool {
return writeTool(
@@ -233,16 +245,20 @@ func (t *ToolSet) insertAssets() actool.CoreTool {
var results []result
var errs []errEntry
// Asset gate rules are loaded once; on read failure the check is skipped
// (insertion not blocked).
// Interception rules = global ∪ task-level block; allow rules = task-level allow.
blockRules, _ := t.as.ListAssetInterceptRules()
// Fail closed: a rule-load error must not evaluate as an empty set (allow all).
blockRules, err := t.as.ListAssetInterceptRules()
if out, refused := insertAssetsRuleLoadFailure(err); refused {
return out, nil
}
var allowRules []db.AssetInterceptRule
if t.taskID > 0 {
if tb, ta, err := t.as.TaskInterceptRulesSplit(t.taskID); err == nil {
blockRules = append(blockRules, tb...)
allowRules = ta
tb, ta, err := t.as.TaskInterceptRulesSplit(t.taskID)
if out, refused := insertAssetsRuleLoadFailure(err); refused {
return out, nil
}
blockRules = append(blockRules, tb...)
allowRules = ta
}
for i, item := range a.Assets {