Skip to content
Open
32 changes: 22 additions & 10 deletions internal/commands/agenthooks/cx/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
81 changes: 70 additions & 11 deletions internal/commands/agenthooks/cx/scanner_switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -229,15 +233,70 @@ 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 {
t.Errorf("%s: got %v, want %v", agentToString(agent), got, want)
}
}
}

// 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")
}
})
}
}
12 changes: 6 additions & 6 deletions internal/commands/agenthooks/cx/vscode_self_healing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/agenthooks/sca/sca.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 38 additions & 20 deletions internal/commands/agenthooks/scannerconfig/scannerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading
Loading