github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scanners/json/scanner_test.go (about) 1 package json 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/khulnasoft-lab/defsec/pkg/framework" 8 "github.com/khulnasoft-lab/defsec/pkg/scanners/options" 9 10 "github.com/khulnasoft-lab/defsec/pkg/scan" 11 12 "github.com/khulnasoft-lab/defsec/test/testutil" 13 14 "github.com/stretchr/testify/assert" 15 16 "github.com/stretchr/testify/require" 17 ) 18 19 func Test_BasicScan(t *testing.T) { 20 21 fs := testutil.CreateFS(t, map[string]string{ 22 "/code/data.json": `{ "x": { "y": 123, "z": ["a", "b", "c"]}}`, 23 "/rules/rule.rego": `package builtin.json.lol 24 25 __rego_metadata__ := { 26 "id": "ABC123", 27 "avd_id": "AVD-AB-0123", 28 "title": "title", 29 "short_code": "short", 30 "severity": "CRITICAL", 31 "type": "JSON Check", 32 "description": "description", 33 "recommended_actions": "actions", 34 "url": "https://example.com", 35 } 36 37 __rego_input__ := { 38 "combine": false, 39 "selector": [{"type": "json"}], 40 } 41 42 deny[res] { 43 input.x.y == 123 44 res := { 45 "msg": "oh no", 46 "startline": 1, 47 "endline": 2, 48 } 49 } 50 51 `, 52 }) 53 54 scanner := NewScanner(options.ScannerWithPolicyDirs("rules")) 55 56 results, err := scanner.ScanFS(context.TODO(), fs, "code") 57 require.NoError(t, err) 58 59 require.Len(t, results.GetFailed(), 1) 60 61 assert.Equal(t, scan.Rule{ 62 AVDID: "AVD-AB-0123", 63 Aliases: []string{"ABC123"}, 64 ShortCode: "short", 65 Summary: "title", 66 Explanation: "description", 67 Impact: "", 68 Resolution: "actions", 69 Provider: "json", 70 Service: "general", 71 Links: []string{"https://example.com"}, 72 Severity: "CRITICAL", 73 Terraform: &scan.EngineMetadata{}, 74 CloudFormation: &scan.EngineMetadata{}, 75 CustomChecks: scan.CustomChecks{ 76 Terraform: (*scan.TerraformCustomCheck)(nil), 77 }, 78 RegoPackage: "data.builtin.json.lol", 79 Frameworks: map[framework.Framework][]string{}, 80 }, results.GetFailed()[0].Rule()) 81 }