diff --git a/CHANGELOG.md b/CHANGELOG.md index 13ecdf0..6bbd988 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/agent/insert_assets_test.go b/agent/insert_assets_test.go index d9f12e0..a852f24 100644 --- a/agent/insert_assets_test.go +++ b/agent/insert_assets_test.go @@ -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) + } + } +} diff --git a/agent/tools_insert.go b/agent/tools_insert.go index 2bad333..2112d82 100644 --- a/agent/tools_insert.go +++ b/agent/tools_insert.go @@ -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 {