ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
fix(guard): record allow only after intercept decides
Coff0xceddc4ba33979
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 35c377e..c424fb5 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+- **Intercept audit ring**: `preToolUse` records allow only after intercept and the target gate decide, so a blocked call no longer leaves a leftover allow row in `Guard.Audit()`.- **DSN password redaction**: `config.Redact` now masks libpq `password=` keywords and `?password=` query values as well as URL userinfo, so a ping-failure log cannot print the database password.- **Asset DSL nesting**: `ParseDSL` refuses queries longer than 4 KiB or nested more than 32 parentheses deep, so a crafted `dsl` cannot grow the goroutine stack without bound.- **Retry-After overflow**: a 429/503 `Retry-After` of huge seconds no longer wraps `time.Duration` into a tiny delay. Values above `MaxDelay` are not retried.diff --git a/guard/guard.go b/guard/guard.goindex 42aaa49..3663722 100644--- a/guard/guard.go+++ b/guard/guard.go@@ -82,31 +82,40 @@ func (g *Guard) setGate(ic *intercept.Interceptor) {func (g *Guard) Hooks() *hook.Registry { return g.reg }func (g *Guard) preToolUse(ctx context.Context, ev hook.Event) hook.Result {+ cmd := toolAuditCommand(ev)+ res := g.applyIntercept(ctx, ev, cmd)if g.targetOnly {- // Not recorded: the audit ring is for the gated worker and chat calls.- return g.applyIntercept(ctx, ev)+ // Not recorded on a pass: the audit ring is for the gated worker and chat+ // calls. A block still writes through block().+ return res}- // Extract the shell-command surface for the audit log: Bash + the interactive-shell- // tools (shell_open's command, shell_send's text). Destructive/exfil gating is no- // longer hard-coded here — it now lives in the DB intercept rules, evaluated by- // applyIntercept below. Other tools record an empty command.- var cmd string+ // Record the final decision only. An allow written before applyIntercept+ // stayed in the ring when the call was later blocked, so Audit() showed a+ // call as allowed that never ran.+ if res.Decision != "block" {+ g.record(ev.ToolName, "allow", "", cmd)+ }+ return res+}++// toolAuditCommand is the shell-command surface for the audit log: Bash and+// shell_open's command, shell_send's text. Other tools record an empty command.+func toolAuditCommand(ev hook.Event) string {switch ev.ToolName {case "Bash", "shell_open":var in struct {Command string `json:"command"`}_ = json.Unmarshal(ev.Input, &in)- cmd = in.Command+ return in.Commandcase "shell_send":var in struct {Text string `json:"text"`}_ = json.Unmarshal(ev.Input, &in)- cmd = in.Text+ return in.Text}- g.record(ev.ToolName, "allow", "", cmd)- return g.applyIntercept(ctx, ev)+ return ""}// targetGate is the approval gate for asset targets, as the guard uses it.@@ -138,7 +147,7 @@ func (g *Guard) gateScope(ctx context.Context) (intercept.TargetScope, bool) {// is on: a deny blocks first, before any target request exists; an allow by a// rule or by the judge does not satisfy the gate; an ask and an unpinned target// share one approval request.-func (g *Guard) applyIntercept(ctx context.Context, ev hook.Event) hook.Result {+func (g *Guard) applyIntercept(ctx context.Context, ev hook.Event, cmd string) hook.Result {if g.targetOnly && g.interceptor != nil {// The planner and the main agent run Bash too (#124, F11): the built-in// deny rules apply to them, before the target gate. User rules and the@@ -146,7 +155,7 @@ func (g *Guard) applyIntercept(ctx context.Context, ev hook.Event) hook.Result {if dec, ok := g.interceptor.MatchBuiltin(ev.ToolName, ev.Input); ok {ctx = intercept.WithCall(ctx, ev.ToolName, ev.Input)g.interceptor.Log(ctx, intercept.ConvIDFromContext(ctx), dec, ev.ToolName, ev.Input, "denied")- return g.block(ev.ToolName, systemBlockMessage(dec.Message), "")+ return g.block(ev.ToolName, systemBlockMessage(dec.Message), cmd)}}scope, gated := g.gateScope(ctx)@@ -168,13 +177,13 @@ func (g *Guard) applyIntercept(ctx context.Context, ev hook.Event) hook.Result {if dec.Action == "deny" {// Note: a deny hit does not block for approval; it directly logs a denied entry (visible on the history / task interception pages).g.interceptor.Log(ctx, intercept.ConvIDFromContext(ctx), dec, ev.ToolName, ev.Input, "denied")- return g.block(ev.ToolName, systemBlockMessage(dec.Message), "")+ return g.block(ev.ToolName, systemBlockMessage(dec.Message), cmd)}var unpinned []intercept.TargetRequestif gated {var res intercept.TargetResultif unpinned, res = g.gate.CheckTargets(ctx, scope, ev.ToolName, ev.Input); res.Code != "" {- return g.block(ev.ToolName, TargetBlockMessage(res), "")+ return g.block(ev.ToolName, TargetBlockMessage(res), cmd)}}var ask *intercept.Decision@@ -187,14 +196,14 @@ func (g *Guard) applyIntercept(ctx context.Context, ev hook.Event) hook.Result {// immediately without creating a pending record — avoids orphaned DB entries// and makes execOne complete fast, reducing the race against drainSynthetic.if ctx.Err() != nil {- return g.block(ev.ToolName, systemBlockMessage("work cancelled; platform safety controls blocked execution"), "")+ return g.block(ev.ToolName, systemBlockMessage("work cancelled; platform safety controls blocked execution"), cmd)}ask = &dec}if len(unpinned) > 0 {res, merged := g.requestTargets(ctx, scope, ev, unpinned, ask)if res.Code != "" {- return g.block(ev.ToolName, TargetBlockMessage(res), "")+ return g.block(ev.ToolName, TargetBlockMessage(res), cmd)}if merged {// The approval of the target request answered the ask as well.@@ -204,7 +213,7 @@ func (g *Guard) applyIntercept(ctx context.Context, ev hook.Event) hook.Result {if ask != nil {convID := intercept.ConvIDFromContext(ctx)if !g.interceptor.HandleAsk(ctx, convID, dec, ev.ToolName, ev.Input) {- return g.block(ev.ToolName, systemBlockMessage("manual approval not granted (user rejected or approval timed out)"), "")+ return g.block(ev.ToolName, systemBlockMessage("manual approval not granted (user rejected or approval timed out)"), cmd)}}return hook.Result{}diff --git a/guard/guard_test.go b/guard/guard_test.goindex f4d5801..e49dced 100644--- a/guard/guard_test.go+++ b/guard/guard_test.go@@ -41,6 +41,34 @@ func TestPreToolUsePassthrough(t *testing.T) {}}+// A deny must not leave a prior allow row in the audit ring.+func TestPreToolUseAuditIsFinalDecision(t *testing.T) {+ ic := intercept.New(nil)+ ic.SetRulesForTest([]db.InterceptRule{{+ ID: 1, Name: "deny nmap", Enabled: true, Priority: 10,+ MatchTarget: "tool_input", MatchType: "string", Action: "deny",+ Pattern: "nmap", Message: "no nmap",+ }})+ g := NewWithInterceptor(ic)+ input, _ := json.Marshal(map[string]string{"command": "nmap -sV x"})+ block, _, _ := g.Hooks().PreToolUse(context.Background(), "Bash", input)+ if !block {+ t.Fatal("expected block")+ }+ audit := g.Audit()+ if len(audit) != 1 || audit[0].Action != "block" || audit[0].Tool != "Bash" {+ t.Fatalf("audit=%+v, want a single block", audit)+ }+ if audit[0].Command != "nmap -sV x" {+ t.Fatalf("block command=%q, want the denied command", audit[0].Command)+ }+ for _, e := range audit {+ if e.Action == "allow" {+ t.Fatalf("blocked call still has an allow row: %+v", audit)+ }+ }+}+// The target-only guard (planner, main agent) runs the built-in deny rules and// the target gate: no user intercept rule, no fallback judge, no audit-ring entry// for a call that passes (in production the built-in rules come from the store,