diff --git a/internal/commands/agenthooks/cx/hooks.go b/internal/commands/agenthooks/cx/hooks.go index 91fa7a81..8125aaa5 100644 --- a/internal/commands/agenthooks/cx/hooks.go +++ b/internal/commands/agenthooks/cx/hooks.go @@ -310,10 +310,10 @@ func logRemediationTelemetry(agent, engine, severity, sessionID string) { } } -// switchesApply reports whether the ~/.checkmarx/cx-onboarding.properties scanner switches and severity -// threshold are honoured for this agent. Cursor, Gemini and Codex are excluded for now: they keep -// scanning with ASCA / KICS / SCA and blocking at any severity even when the shared file exists on the -// machine. Every other agent (Claude, GitHub Copilot, Droid, Windsurf, ...) honours it. Add an agent to +// switchesApply reports whether the ~/.checkmarx/cx-onboarding.properties scanner switches (and, through +// thresholdApplies, the severity threshold) are honoured for this agent. Cursor, Gemini and Codex are +// excluded for now: they keep scanning with ASCA / KICS / SCA even when the shared file exists on the +// machine (Codex does honour the severity threshold, see thresholdApplies). Every other agent (Claude, GitHub Copilot, Droid, Windsurf, ...) honours it. Add an agent to // the case list below to hold it back, or remove one to roll the feature out to it. func switchesApply(agent agenthooks.AgentID) bool { switch agent { @@ -355,21 +355,29 @@ func selfHealingOff(agent agenthooks.AgentID, hook string) bool { return true } +// thresholdApplies reports whether cx_severity_threshold is honoured for this agent: every agent +// switchesApply covers, plus Codex. It is separate from switchesApply so Codex gets the severity threshold +// without the scanner switches and the secrets engine. Add an agent here to give it the threshold only. +func thresholdApplies(agent agenthooks.AgentID) bool { + return switchesApply(agent) || agent == agenthooks.AgentCodex +} + // defaultThresholdRank is the severity rank cx_severity_threshold falls back to when it is blank, -// missing or invalid. Every agent switchesApply covers (Claude, Copilot, Droid, Windsurf) defaults to -// high, so only high/critical findings still block until the developer configures a lower threshold. +// missing or invalid. Every agent thresholdApplies covers (Claude, Copilot, Droid, Windsurf, Codex) +// defaults to high, so only high/critical findings still block until the developer configures a lower +// threshold. func defaultThresholdRank(agent agenthooks.AgentID) int { - if !switchesApply(agent) { + if !thresholdApplies(agent) { return scannerconfig.RankLow } return scannerconfig.RankHigh } -// belowThreshold is scannerconfig.BelowThreshold, limited to the agents the switches apply to. GitHub +// belowThreshold is scannerconfig.BelowThreshold, limited to the agents the threshold applies to. GitHub // Copilot inside VS Code has no threshold setting, so it uses the IDE file's threshold, defaulting per // defaultThresholdRank when unset. func belowThreshold(agent agenthooks.AgentID, severity string) bool { - if !switchesApply(agent) { + if !thresholdApplies(agent) { return false } defaultRank := defaultThresholdRank(agent) diff --git a/internal/commands/agenthooks/cx/scanner_switch_test.go b/internal/commands/agenthooks/cx/scanner_switch_test.go index 2752de57..3ccfedf0 100644 --- a/internal/commands/agenthooks/cx/scanner_switch_test.go +++ b/internal/commands/agenthooks/cx/scanner_switch_test.go @@ -186,7 +186,7 @@ func TestCxBeforeFileEdit_SeverityThreshold_SecretsMixed(t *testing.T) { } // The switches and secret detection are features of every agent except Cursor, Gemini and Codex: those three skip secret -// detection and, with the shared properties file present, keeps ASCA/KICS/SCA blocking at any severity. +// detection and ignore the scanner switches. Cursor and Gemini also keep ASCA/KICS/SCA blocking at any severity. func TestSwitches_IgnoredForOtherAgents(t *testing.T) { const secret = "ghp_other_agent_secret_value" others := []agenthooks.AgentID{ @@ -209,6 +209,9 @@ func TestSwitches_IgnoredForOtherAgents(t *testing.T) { t.Fatal("kics switch must not apply to this agent") } }) + if agent == agenthooks.AgentCodex { + continue // Codex honours the threshold (TestCxBeforeFileEdit_SeverityThreshold_Codex) + } t.Run(agentToString(agent)+"/threshold does not relax", func(t *testing.T) { resetHookGlobals(t) useScannerConfig(t, "cx_severity_threshold=critical\n") @@ -246,15 +249,14 @@ func TestSecretsApply_ExcludesThreeAgents(t *testing.T) { } } -// Every agent switchesApply covers defaults cx_severity_threshold to high when unset; the three -// excluded agents (Cursor, Gemini, Codex) never relax, so their default rank is immaterial but still low. -func TestDefaultThresholdRank_IsHighForSwitchesApplyAgents(t *testing.T) { +// Every agent thresholdApplies covers defaults cx_severity_threshold to high when unset; the two +// excluded agents (Cursor, Gemini) never relax, so their default rank is immaterial but still low. +func TestDefaultThresholdRank_IsHighForThresholdAgents(t *testing.T) { for agent, want := range map[agenthooks.AgentID]int{ agenthooks.AgentClaude: scannerconfig.RankHigh, agenthooks.AgentCopilot: scannerconfig.RankHigh, agenthooks.AgentCopilotCLI: scannerconfig.RankHigh, agenthooks.AgentDroid: scannerconfig.RankHigh, - agenthooks.AgentWindsurf: scannerconfig.RankHigh, - agenthooks.AgentCursor: scannerconfig.RankLow, agenthooks.AgentGemini: scannerconfig.RankLow, - agenthooks.AgentCodex: scannerconfig.RankLow, + agenthooks.AgentWindsurf: scannerconfig.RankHigh, agenthooks.AgentCodex: scannerconfig.RankHigh, + agenthooks.AgentCursor: scannerconfig.RankLow, agenthooks.AgentGemini: scannerconfig.RankLow, } { if got := defaultThresholdRank(agent); got != want { t.Errorf("%s: got %d, want %d", agentToString(agent), got, want) @@ -262,6 +264,50 @@ func TestDefaultThresholdRank_IsHighForSwitchesApplyAgents(t *testing.T) { } } +func TestThresholdApplies_AddsCodex(t *testing.T) { + for agent, want := range map[agenthooks.AgentID]bool{ + agenthooks.AgentClaude: true, agenthooks.AgentCopilotCLI: true, agenthooks.AgentCopilot: true, + agenthooks.AgentDroid: true, agenthooks.AgentWindsurf: true, agenthooks.AgentCodex: true, + agenthooks.AgentCursor: false, agenthooks.AgentGemini: false, + } { + if got := thresholdApplies(agent); got != want { + t.Errorf("%s: got %v, want %v", agentToString(agent), got, want) + } + } +} + +// Codex honours cx_severity_threshold (default high) but not the scanner switches or secret detection, +// which TestSwitches_IgnoredForOtherAgents still checks. +func TestCxBeforeFileEdit_SeverityThreshold_Codex(t *testing.T) { + cases := []struct { + name string + config string + severity string + wantBlock bool + }{ + {"no config blocks critical", "", "CRITICAL", true}, + {"no config blocks high", "", "HIGH", true}, + {"no config lets medium through", "", "MEDIUM", false}, + {"no config lets low through", "", "LOW", false}, + {"blank threshold defaults to high", "cx_severity_threshold=\n", "MEDIUM", false}, + {"invalid threshold defaults to high", "cx_severity_threshold=hihg\n", "LOW", false}, + {"critical threshold lets high through", "cx_severity_threshold=critical\n", "HIGH", false}, + {"medium threshold blocks medium", "cx_severity_threshold=medium\n", "MEDIUM", true}, + {"low threshold blocks low", "cx_severity_threshold=low\n", "LOW", true}, + {"unknown severity is never relaxed", "", "", true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + resetHookGlobals(t) + useScannerConfig(t, tc.config) + kicsScanner = kicsFinding(tc.severity) + if v := cxBeforeFileEdit(kicsEdit(agenthooks.AgentCodex)); v.Permit == tc.wantBlock { + t.Fatalf("Permit=%v, wantBlock=%v", v.Permit, tc.wantBlock) + } + }) + } +} + // Claude Code and GitHub Copilot CLI honour secret detection, the scanner switches and the severity threshold. func TestSwitches_HonouredForClaudeAndCopilot(t *testing.T) { const secret = "ghp_supported_agent_secret_value"