Skip to content

Commit 3c64e68

Browse files
committed
fix(gate): align alias and schema validation
1 parent f6f39fb commit 3c64e68

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎internal/runner/runner.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,10 +2410,13 @@ func jsonGatePathValues(document any, path string) ([]any, bool, bool, bool) {
24102410
}
24112411
if array {
24122412
child, ok := object[keys[0]]
2413-
if !ok || child == nil {
2413+
if !ok {
24142414
continue
24152415
}
24162416
rootKeyPresent = true
2417+
if child == nil {
2418+
continue
2419+
}
24172420
items, ok := child.([]any)
24182421
if ok {
24192422
present = true
@@ -2424,7 +2427,7 @@ func jsonGatePathValues(document any, path string) ([]any, bool, bool, bool) {
24242427

24252428
for _, key := range keys {
24262429
child, ok := object[key]
2427-
if !ok || child == nil {
2430+
if !ok {
24282431
continue
24292432
}
24302433
rootKeyPresent = true

‎internal/runner/runner_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,7 @@ func TestRunDeclarativeJSONRuleCanPreferAnExistingRoot(t *testing.T) {
18451845
for _, raw := range []json.RawMessage{
18461846
json.RawMessage(`{"preferred":[{}],"legacy":[{"severity":"critical"}]}`),
18471847
json.RawMessage(`{"preferred":{},"legacy":[{"severity":"critical"}]}`),
1848+
json.RawMessage(`{"preferred":null,"legacy":[{"severity":"critical"}]}`),
18481849
} {
18491850
artifact, err := Run(Options{
18501851
Target: t.TempDir(), Scanners: []string{"skillspector"}, Sandbox: SandboxOptions{Mode: SandboxModeOff},
@@ -1864,6 +1865,27 @@ func TestRunDeclarativeJSONRuleCanPreferAnExistingRoot(t *testing.T) {
18641865
}
18651866
}
18661867

1868+
func TestRunDeclarativeJSONRulePreservesExplicitNullFieldAlias(t *testing.T) {
1869+
artifact, err := Run(Options{
1870+
Target: t.TempDir(), Scanners: []string{"skillspector"}, Sandbox: SandboxOptions{Mode: SandboxModeOff},
1871+
GateRules: map[string]ScannerGatePolicy{"skillspector": {JSONRules: []JSONGateRule{{
1872+
ID: "critical-finding", Paths: []string{"preferred.severity|level", "legacy.severity"},
1873+
Equals: json.RawMessage(`"critical"`), Action: "block",
1874+
}}}},
1875+
}, RunContext{Env: map[string]string{}, ScannerRunner: &gateScannerRunner{results: map[string]ScannerResult{
1876+
"skillspector": {
1877+
Status: "completed",
1878+
Raw: json.RawMessage(`{"preferred":{"severity":null,"level":"low"},"legacy":{"severity":"critical"}}`),
1879+
},
1880+
}}})
1881+
if err != nil {
1882+
t.Fatal(err)
1883+
}
1884+
if artifact.Gate != "block" || len(artifact.GateRules) != 1 || artifact.GateRules[0].Path != "legacy.severity" {
1885+
t.Fatalf("gate = %q, rules = %#v", artifact.Gate, artifact.GateRules)
1886+
}
1887+
}
1888+
18671889
func TestRunDeclarativeJSONRuleFallsBackFromEmptyScalarValues(t *testing.T) {
18681890
for _, raw := range []json.RawMessage{
18691891
json.RawMessage(`{"preferred":"","legacy":"critical"}`),

‎schemas/clawscan.schema.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@
215215
"oneOf": [
216216
{
217217
"type": "string",
218-
"minLength": 1
218+
"pattern": "^(?:[^.\\[\\]|]+(?:\\|[^.\\[\\]|]+)*|[^.\\[\\]|]+\\[\\])(?:\\.(?:[^.\\[\\]|]+(?:\\|[^.\\[\\]|]+)*|[^.\\[\\]|]+\\[\\]))*$"
219219
},
220220
{
221221
"type": "array",
222222
"minItems": 1,
223223
"uniqueItems": true,
224224
"items": {
225225
"type": "string",
226-
"minLength": 1
226+
"pattern": "^(?:[^.\\[\\]|]+(?:\\|[^.\\[\\]|]+)*|[^.\\[\\]|]+\\[\\])(?:\\.(?:[^.\\[\\]|]+(?:\\|[^.\\[\\]|]+)*|[^.\\[\\]|]+\\[\\]))*$"
227227
}
228228
}
229229
]

‎schemas/clawscan.schema_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ func TestClawScanSchemaRejectsInvalidGateRules(t *testing.T) {
125125
path: []
126126
exists: true
127127
action: block
128+
`,
129+
"indexed scalar path": `
130+
- id: risky
131+
path: "findings[0].severity"
132+
equals: critical
133+
action: block
134+
`,
135+
"indexed path in list": `
136+
- id: risky
137+
path: [result.risk, "findings[0].severity"]
138+
equals: critical
139+
action: block
128140
`,
129141
}
130142

0 commit comments

Comments
 (0)