diff --git a/internal/commands/agenthooks/cx/hooks.go b/internal/commands/agenthooks/cx/hooks.go index dbe0b0d65..91fa7a811 100644 --- a/internal/commands/agenthooks/cx/hooks.go +++ b/internal/commands/agenthooks/cx/hooks.go @@ -140,8 +140,8 @@ func cxBeforeFileEdit(ev agenthooks.FileEditEvent) agenthooks.FileEditVerdict { // Each engine below is skipped (not just its verdict ignored) when the developer switched it off in // ~/.checkmarx/cx-onboarding.properties; an unreadable config leaves every engine on. // cx_severity_threshold (same file): a deny whose highest severity is below the threshold is let - // through for ASCA, KICS, SCA and secrets. Malicious packages and an unknown severity - // are never relaxed. A blank or invalid threshold means low (nothing filtered). + // through for ASCA, KICS, SCA and secrets. Malicious packages and an empty or unexpected severity + // are never relaxed; an unrated (None/Unknown) severity is always let through, low threshold included. A blank or invalid threshold means low (nothing filtered). if scannerOn(ev.Agent, scannerconfig.ASCA) { if blocked, reason, context, severity := asca.ScanFileEdit(&ev, telemetryWrapper, agent); blocked && !belowThreshold(ev.Agent, severity) { sessiontally.Add(ev.SessionID, engineAsca, 1, 1) @@ -311,20 +311,20 @@ 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. Claude, Cursor, Gemini and Codex are excluded for now: they keep +// 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 (GitHub Copilot, Droid, Windsurf, ...) honours it. Add an agent to +// machine. 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 { - case agenthooks.AgentClaude, agenthooks.AgentCursor, agenthooks.AgentGemini, agenthooks.AgentCodex: + case agenthooks.AgentCursor, agenthooks.AgentGemini, agenthooks.AgentCodex: return false } return true } -// secretsApply reports whether the secret-detection guardrail runs for this agent. Claude, Cursor, Gemini -// and Codex skip it entirely and keep the ASCA / KICS / SCA guardrails; every other agent runs it. It +// secretsApply reports whether the secret-detection guardrail runs for this agent. Cursor, Gemini and +// Codex skip it entirely and keep the ASCA / KICS / SCA guardrails; every other agent runs it. It // shares switchesApply's agent list so the two cannot drift apart. func secretsApply(agent agenthooks.AgentID) bool { return switchesApply(agent) @@ -355,16 +355,28 @@ func selfHealingOff(agent agenthooks.AgentID, hook string) bool { return true } +// 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. +func defaultThresholdRank(agent agenthooks.AgentID) int { + if !switchesApply(agent) { + return scannerconfig.RankLow + } + return scannerconfig.RankHigh +} + // belowThreshold is scannerconfig.BelowThreshold, limited to the agents the switches apply to. GitHub -// Copilot inside VS Code has no threshold setting, so it uses the IDE file's threshold, low when unset. +// 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) { return false } + defaultRank := defaultThresholdRank(agent) if agent == agenthooks.AgentCopilotCLI && hostenv.InVSCode() { - return scannerconfig.IDEBelowThreshold(severity) + return scannerconfig.IDEBelowThreshold(severity, defaultRank) } - return scannerconfig.BelowThreshold(severity) + return scannerconfig.BelowThreshold(severity, defaultRank) } // agentToString converts agenthooks.AgentID enum to string representation for telemetry. diff --git a/internal/commands/agenthooks/cx/scanner_switch_test.go b/internal/commands/agenthooks/cx/scanner_switch_test.go index 81d105028..2752de578 100644 --- a/internal/commands/agenthooks/cx/scanner_switch_test.go +++ b/internal/commands/agenthooks/cx/scanner_switch_test.go @@ -13,6 +13,7 @@ import ( "github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails/kics" "github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails/secrets" + "github.com/checkmarx/ast-cli/internal/commands/agenthooks/scannerconfig" "github.com/checkmarx/ast-cli/internal/services/realtimeengine" "github.com/checkmarx/ast-cli/internal/services/realtimeengine/iacrealtime" "github.com/checkmarx/ast-cli/internal/services/realtimeengine/secretsrealtime" @@ -109,14 +110,17 @@ func TestCxBeforeFileEdit_SeverityThreshold(t *testing.T) { severity string wantBlock bool }{ - {"no config blocks low", "", "LOW", true}, - {"blank threshold blocks low", "cx_severity_threshold=\n", "LOW", true}, - {"invalid threshold blocks low", "cx_severity_threshold=hihg\n", "LOW", true}, + {"no config defaults to high, lets low through", "", "LOW", false}, + {"blank threshold defaults to high, lets low through", "cx_severity_threshold=\n", "LOW", false}, + {"invalid threshold defaults to high, lets low through", "cx_severity_threshold=hihg\n", "LOW", false}, + {"explicit low threshold blocks low", "cx_severity_threshold=low\n", "LOW", true}, {"high threshold lets low through", "cx_severity_threshold=high\n", "LOW", false}, {"high threshold lets medium through", "cx_severity_threshold=high\n", "MEDIUM", false}, {"high threshold blocks high", "cx_severity_threshold=high\n", "HIGH", true}, {"high threshold blocks critical", "cx_severity_threshold=high\n", "CRITICAL", true}, - {"unknown severity is never relaxed", "cx_severity_threshold=critical\n", "", true}, + {"empty severity is never relaxed", "cx_severity_threshold=critical\n", "", true}, + {"unrated severity never blocks at low", "cx_severity_threshold=low\n", "None", false}, + {"unrated severity never blocks at critical", "cx_severity_threshold=critical\n", "None", false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -138,7 +142,7 @@ func TestCxBeforeFileEdit_SeverityThreshold_Secrets(t *testing.T) { severity string wantBlock bool }{ - {"no config blocks low", "", "Low", true}, + {"no config defaults to high, lets low through", "", "Low", false}, {"high threshold lets medium through", "cx_severity_threshold=high\n", "Medium", false}, {"high threshold blocks high", "cx_severity_threshold=high\n", "High", true}, {"critical threshold lets low through", "cx_severity_threshold=critical\n", "Low", false}, @@ -181,12 +185,12 @@ func TestCxBeforeFileEdit_SeverityThreshold_SecretsMixed(t *testing.T) { } } -// The switches and secret detection are features of every agent except Claude, Cursor, Gemini and Codex: those four skip secret +// 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. func TestSwitches_IgnoredForOtherAgents(t *testing.T) { const secret = "ghp_other_agent_secret_value" others := []agenthooks.AgentID{ - agenthooks.AgentClaude, agenthooks.AgentCursor, agenthooks.AgentGemini, agenthooks.AgentCodex, + agenthooks.AgentCursor, agenthooks.AgentGemini, agenthooks.AgentCodex, } for _, agent := range others { t.Run(agentToString(agent)+"/secrets are skipped", func(t *testing.T) { @@ -216,11 +220,11 @@ func TestSwitches_IgnoredForOtherAgents(t *testing.T) { } } -func TestSwitchesApply_ExcludesFourAgents(t *testing.T) { +func TestSwitchesApply_ExcludesThreeAgents(t *testing.T) { for agent, want := range map[agenthooks.AgentID]bool{ agenthooks.AgentCopilotCLI: true, agenthooks.AgentCopilot: true, agenthooks.AgentDroid: true, agenthooks.AgentWindsurf: true, - agenthooks.AgentClaude: false, agenthooks.AgentCursor: false, agenthooks.AgentGemini: false, + agenthooks.AgentClaude: true, agenthooks.AgentCursor: false, agenthooks.AgentGemini: false, agenthooks.AgentCodex: false, } { if got := switchesApply(agent); got != want { @@ -229,11 +233,11 @@ func TestSwitchesApply_ExcludesFourAgents(t *testing.T) { } } -func TestSecretsApply_ExcludesFourAgents(t *testing.T) { +func TestSecretsApply_ExcludesThreeAgents(t *testing.T) { for agent, want := range map[agenthooks.AgentID]bool{ agenthooks.AgentCopilotCLI: true, agenthooks.AgentCopilot: true, agenthooks.AgentDroid: true, agenthooks.AgentWindsurf: true, - agenthooks.AgentClaude: false, agenthooks.AgentCursor: false, + agenthooks.AgentClaude: true, agenthooks.AgentCursor: false, agenthooks.AgentGemini: false, agenthooks.AgentCodex: false, } { if got := secretsApply(agent); got != want { @@ -241,3 +245,58 @@ func TestSecretsApply_ExcludesFourAgents(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) { + 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, + } { + if got := defaultThresholdRank(agent); got != want { + t.Errorf("%s: got %d, want %d", agentToString(agent), got, want) + } + } +} + +// 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" + for _, agent := range []agenthooks.AgentID{agenthooks.AgentClaude, agenthooks.AgentCopilotCLI} { + t.Run(agentToString(agent)+"/secrets block by default", func(t *testing.T) { + resetHookGlobals(t) + useScannerConfig(t, "") + secretsScanner = stubSecretsScanner(secret) + if v := cxBeforeFileEdit(secretsEdit(secret, agent)); v.Permit { + t.Fatal("secret detection must run for this agent") + } + }) + t.Run(agentToString(agent)+"/secrets switch off allows", func(t *testing.T) { + resetHookGlobals(t) + useScannerConfig(t, "cx_secret_scanner_enabled=false\n") + secretsScanner = stubSecretsScanner(secret) + if v := cxBeforeFileEdit(secretsEdit(secret, agent)); !v.Permit { + t.Fatal("secrets switch must apply to this agent") + } + }) + t.Run(agentToString(agent)+"/kics switch off allows", func(t *testing.T) { + resetHookGlobals(t) + useScannerConfig(t, "cx_kics_scanner_enabled=false\n") + kicsScanner = kicsFinding("HIGH") + if v := cxBeforeFileEdit(kicsEdit(agent)); !v.Permit { + t.Fatal("kics switch must apply to this agent") + } + }) + t.Run(agentToString(agent)+"/threshold relaxes", func(t *testing.T) { + resetHookGlobals(t) + useScannerConfig(t, "cx_severity_threshold=critical\n") + kicsScanner = kicsFinding("LOW") + if v := cxBeforeFileEdit(kicsEdit(agent)); !v.Permit { + t.Fatal("severity threshold must apply to this agent") + } + }) + } +} diff --git a/internal/commands/agenthooks/cx/vscode_self_healing_test.go b/internal/commands/agenthooks/cx/vscode_self_healing_test.go index 61a15c9a9..e9cd7d875 100644 --- a/internal/commands/agenthooks/cx/vscode_self_healing_test.go +++ b/internal/commands/agenthooks/cx/vscode_self_healing_test.go @@ -102,18 +102,18 @@ func TestScannerOn_VSCodeUsesIDEFile(t *testing.T) { } } -func TestBelowThreshold_VSCodeDefaultsToLow(t *testing.T) { - const cliHigh = "cx_severity_threshold=high\n" +func TestBelowThreshold_VSCodeDefaultsToHigh(t *testing.T) { + const cliLow = "cx_severity_threshold=low\n" cases := []struct { name string root string ide, cli string want bool }{ - {"VS Code without a threshold blocks Medium (low)", rootVSCodeFromCLIInstall, "cx_schema_version=1\n", cliHigh, false}, - {"VS Code without an IDE file blocks Medium (low)", rootVSCodeFromMarketplace, "", cliHigh, false}, - {"VS Code honours a threshold in the IDE file", rootVSCodeFromCLIInstall, "cx_schema_version=1\n" + cliHigh, "", true}, - {"terminal keeps the terminal threshold", rootCLI, "cx_schema_version=1\n", cliHigh, true}, + {"VS Code without a threshold relaxes Medium (default high)", rootVSCodeFromCLIInstall, "cx_schema_version=1\n", "", true}, + {"VS Code without an IDE file relaxes Medium (default high)", rootVSCodeFromMarketplace, "", "", true}, + {"VS Code IDE file can still lower the threshold to low", rootVSCodeFromCLIInstall, "cx_schema_version=1\n" + cliLow, "", false}, + {"terminal file is ignored by VS Code even when it sets low", rootVSCodeFromCLIInstall, "cx_schema_version=1\n", cliLow, true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { diff --git a/internal/commands/agenthooks/sca/sca.go b/internal/commands/agenthooks/sca/sca.go index 641906fd4..9c566aa4e 100644 --- a/internal/commands/agenthooks/sca/sca.go +++ b/internal/commands/agenthooks/sca/sca.go @@ -87,7 +87,7 @@ func denyFrom(malicious, vulnerable []ossrealtime.OssPackage, workDir, agent, se } // aboveThreshold drops CVEs below the developer's severity threshold, and any package left with -// none. Malicious packages never go through here. A CVE with an unknown severity is kept. +// none. Malicious packages never go through here. A CVE with no severity rating (None/Unknown) is dropped at every threshold. func aboveThreshold(pkgs []ossrealtime.OssPackage) []ossrealtime.OssPackage { if BelowThreshold == nil { return pkgs diff --git a/internal/commands/agenthooks/scannerconfig/scannerconfig.go b/internal/commands/agenthooks/scannerconfig/scannerconfig.go index 2e96c3972..b36887344 100644 --- a/internal/commands/agenthooks/scannerconfig/scannerconfig.go +++ b/internal/commands/agenthooks/scannerconfig/scannerconfig.go @@ -173,52 +173,70 @@ const thresholdKey = "cx_severity_threshold" // package status such as "Malicious", an unexpected label) has no rank and is never relaxed. var severityRank = map[string]int{"low": 1, "medium": 2, "high": 3, "critical": 4} +// unratedSeverity lists the labels for a finding that has no severity rating (a CVE without a score). +// Such a finding is unknown, not dangerous, so it is below every threshold, low included, and never blocks. +var unratedSeverity = map[string]bool{"none": true, "unknown": true} + +// RankLow and RankHigh are the severity ranks callers pass as a default when cx_severity_threshold is +// blank, missing or invalid. RankLow filters nothing; RankHigh lets low/medium findings through. +const ( + RankLow = 1 + RankHigh = 3 +) + // Threshold returns the minimum severity rank (1 low … 4 critical) a finding needs to block. -// Blank, missing or invalid means low, so nothing is filtered. -func Threshold() int { - return thresholdIn(Path()) +// Blank, missing or invalid falls back to defaultRank (pass RankLow or RankHigh). +func Threshold(defaultRank int) int { + return thresholdIn(Path(), defaultRank) } -func thresholdIn(path string) int { +func thresholdIn(path string, defaultRank int) int { value, _ := valueIn(path, thresholdKey) - return thresholdRank(value) + return thresholdRank(value, defaultRank) } -// thresholdRank is the rank of a cx_severity_threshold value; blank or invalid means low (1). -func thresholdRank(value string) int { +// thresholdRank is the rank of a cx_severity_threshold value; blank or invalid falls back to defaultRank. +func thresholdRank(value string, defaultRank int) int { if rank, ok := severityRank[strings.ToLower(strings.TrimSpace(value))]; ok { return rank } - return 1 + return defaultRank } // IDEThreshold is Threshold for events from VS Code. The VS Code extension writes cx_severity_threshold=low // to the VS Code state file, so only that file's cx_severity_threshold counts; a missing file, key or invalid -// value is low (nothing filtered). The terminal's ~/.checkmarx/cx-onboarding.properties threshold never applies to VS Code. -func IDEThreshold() int { - return ideThresholdIn(IDEPath()) +// value falls back to defaultRank. The terminal's ~/.checkmarx/cx-onboarding.properties threshold never +// applies to VS Code. +func IDEThreshold(defaultRank int) int { + return ideThresholdIn(IDEPath(), defaultRank) } -func ideThresholdIn(path string) int { +func ideThresholdIn(path string, defaultRank int) int { props, ok := ideProps(path) if !ok { - return 1 + return defaultRank } - return thresholdRank(props[thresholdKey]) + return thresholdRank(props[thresholdKey], defaultRank) } // BelowThreshold reports whether a deny whose highest severity is `severity` should be let through -// because it is under cx_severity_threshold. An unranked severity returns false (keep blocking). -func BelowThreshold(severity string) bool { - return belowRank(severity, Threshold()) +// because it is under cx_severity_threshold. An unrated severity ("None", "Unknown") is under every +// threshold and returns true; any other unranked severity returns false (keep blocking). +// defaultRank is used when the threshold is blank, missing or invalid. +func BelowThreshold(severity string, defaultRank int) bool { + return belowRank(severity, Threshold(defaultRank)) } // IDEBelowThreshold is BelowThreshold for events from VS Code, against IDEThreshold. -func IDEBelowThreshold(severity string) bool { - return belowRank(severity, IDEThreshold()) +func IDEBelowThreshold(severity string, defaultRank int) bool { + return belowRank(severity, IDEThreshold(defaultRank)) } func belowRank(severity string, threshold int) bool { - rank, ok := severityRank[strings.ToLower(strings.TrimSpace(severity))] + label := strings.ToLower(strings.TrimSpace(severity)) + if unratedSeverity[label] { + return true + } + rank, ok := severityRank[label] return ok && rank < threshold } diff --git a/internal/commands/agenthooks/scannerconfig/scannerconfig_test.go b/internal/commands/agenthooks/scannerconfig/scannerconfig_test.go index 78aecd9cf..66089b4a3 100644 --- a/internal/commands/agenthooks/scannerconfig/scannerconfig_test.go +++ b/internal/commands/agenthooks/scannerconfig/scannerconfig_test.go @@ -103,13 +103,16 @@ func TestThresholdIn(t *testing.T) { "cx_severity_threshold=high\ncx_severity_threshold=low\n": 1, // last wins } for body, want := range cases { - if got := thresholdIn(write(t, body)); got != want { + if got := thresholdIn(write(t, body), RankLow); got != want { t.Errorf("%q: got %d, want %d", body, got, want) } } - if thresholdIn(filepath.Join(t.TempDir(), "missing")) != 1 || thresholdIn("") != 1 { + if thresholdIn(filepath.Join(t.TempDir(), "missing"), RankLow) != 1 || thresholdIn("", RankLow) != 1 { t.Error("missing file / path must mean low") } + if got := thresholdIn(filepath.Join(t.TempDir(), "missing"), RankHigh); got != RankHigh { + t.Errorf("missing file with a high default must mean high: got %d", got) + } } func TestBelowThreshold(t *testing.T) { @@ -126,14 +129,21 @@ func TestBelowThreshold(t *testing.T) { for sev, want := range map[string]bool{ "Low": true, "MEDIUM": true, "High": false, "Critical": false, "": false, "Malicious": false, "weird": false, // unranked is never relaxed + "None": true, "unknown": true, // unrated is below every threshold } { - if got := BelowThreshold(sev); got != want { + if got := BelowThreshold(sev, RankLow); got != want { t.Errorf("high threshold, %q: got %v, want %v", sev, got, want) } } set("cx_severity_threshold=\n") - if BelowThreshold("Low") { - t.Error("blank threshold must filter nothing") + if BelowThreshold("Low", RankLow) { + t.Error("blank threshold must filter nothing with a low default") + } + if !BelowThreshold("Low", RankHigh) { + t.Error("blank threshold must filter Low with a high default") + } + if BelowThreshold("High", RankHigh) { + t.Error("blank threshold must not filter High with a high default") } } @@ -241,11 +251,11 @@ func TestIDEThresholdIn(t *testing.T) { "cx_schema_version=2\ncx_severity_threshold=high\n": 1, // unknown schema: file ignored } for body, want := range cases { - if got := ideThresholdIn(writeIDE(t, body)); got != want { + if got := ideThresholdIn(writeIDE(t, body), RankLow); got != want { t.Errorf("%q: got %d, want %d", body, got, want) } } - if ideThresholdIn(filepath.Join(t.TempDir(), "missing")) != 1 || ideThresholdIn("") != 1 { + if ideThresholdIn(filepath.Join(t.TempDir(), "missing"), RankLow) != 1 || ideThresholdIn("", RankLow) != 1 { t.Error("missing IDE file / path must mean low") } } @@ -257,10 +267,10 @@ func TestIDEBelowThresholdIgnoresTerminalThreshold(t *testing.T) { _ = os.MkdirAll(filepath.Join(dir, ".checkmarx"), 0o755) _ = os.WriteFile(Path(), []byte("cx_severity_threshold=critical\n"), 0o600) _ = os.WriteFile(IDEPath(), []byte("cx_schema_version=1\ncx_self_healing_enabled=true\n"), 0o600) - if !BelowThreshold("High") { + if !BelowThreshold("High", RankLow) { t.Error("terminal: a critical threshold must relax High") } - if IDEBelowThreshold("Low") || IDEBelowThreshold("High") { - t.Error("VS Code without a threshold must block at every severity (low)") + if IDEBelowThreshold("Low", RankLow) || IDEBelowThreshold("High", RankLow) { + t.Error("VS Code without a threshold must block at every severity (low default)") } }