Repository: Checkmarx/kics (language: python)

Issue:
panic: runtime error: slice bounds out of range [1:0] 
when scanning with the latest version on the following yaml file:
`
apiVersion: v1
customRules:
  - identifier: THIS_IS_THE_IDENTIFIER
    name: this is the name
    defaultMessageOnFailure: |
      "ERROR ERROR ERROR"
    schema:
      items:
          $ref: "#"
`
im getting the following panic:

> panic: runtime error: slice bounds out of range [1:0]
> 
> goroutine 53 [running]:
> github.com/Checkmarx/kics/pkg/resolver/file.findSectionYaml(0x518bc?, {0x40007ecdb0?, 0x24990?})
> 	/app/pkg/resolver/file/file.go:413 +0x248
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).resolveYamlPath(0x40010cb6e8, {0x40006a25b0, 0xc3, 0xd0}, 0x40010cb3f0?, 0x4000a80320, {0x40005602d0, 0x13}, 0x0?, 0x27363c0?, ...)
> 	/app/pkg/resolver/file/file.go:275 +0x6a0
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlWalk(0x40010cb6e8?, {0x40006a25b0, 0xc3, 0xd0}, 0x4?, 0x4000a80320, {0x40005602d0, 0x13}, 0x0?, 0x400101a3e8?, ...)
> 	/app/pkg/resolver/file/file.go:155 +0x108
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlWalk(0x40010cb6e8?, {0x40006a25b0, 0xc3, 0xd0}, 0x25c9c?, 0x40007a9f40, {0x40005602d0, 0x13}, 0x0?, 0x40010ca708?, ...)
> 	/app/pkg/resolver/file/file.go:164 +0x28c
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlWalk(0xc3?, {0x40006a25b0, 0xc3, 0xd0}, 0x9?, 0x40007a9e00, {0x40005602d0, 0x13}, 0x0?, 0x0?, ...)
> 	/app/pkg/resolver/file/file.go:164 +0x28c
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlWalk(0x40010cb6e8?, {0x40006a25b0, 0xc3, 0xd0}, 0x247774?, 0x40007a9900, {0x40005602d0, 0x13}, 0x0?, 0x40005ee000?, ...)
> 	/app/pkg/resolver/file/file.go:164 +0x28c
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlWalk(0x1c3d4?, {0x40006a25b0, 0xc3, 0xd0}, 0x248eb4?, 0x40007a9860, {0x40005602d0, 0x13}, 0x9?, 0x40005ee000?, ...)
> 	/app/pkg/resolver/file/file.go:164 +0x28c
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlWalk(0x199?, {0x40006a25b0, 0xc3, 0xd0}, 0x40007a9540?, 0x40007a95e0, {0x40005602d0, 0x13}, 0x40007a94a0?, 0x40005ee000?, ...)
> 	/app/pkg/resolver/file/file.go:164 +0x28c
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlWalk(0x3c71460?, {0x40006a25b0, 0xc3, 0xd0}, 0x0?, 0x40007a94a0, {0x40005602d0, 0x13}, 0x24990?, 0x400085d3f8?, ...)
> 	/app/pkg/resolver/file/file.go:164 +0x28c
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).yamlResolve(0x40010cb6e8, {0x40006a25b0, 0xc3, 0xd0}, {0x40005602d0, 0x13}, 0x400085d628?, 0x1d824?)
> 	/app/pkg/resolver/file/file.go:129 +0xf0
> github.com/Checkmarx/kics/pkg/resolver/file.(*Resolver).Resolve(0x400085d6e8, {0x40006a25b0, 0xc3, 0xd0}, {0x40005602d0, 0x13}, 0x400085d6c8?, 0x23ba94?)
> 	/app/pkg/resolver/file/file.go:50 +0x200
> github.com/Checkmarx/kics/pkg/parser/yaml.(*Parser).Resolve(0x400060fb48, {0x40006a25b0, 0xc3, 0xd0}, {0x40005602d0, 0x13})
> 	/app/pkg/parser/yaml/parser.go:25 +0x108
> github.com/Checkmarx/kics/pkg/parser.(*Parser).Parse(0x400103e990, {0x40005602d0, 0x13}, {0x40006a25b0, 0xc3, 0xd0})
> 	/app/pkg/parser/parser.go:124 +0xe4
> github.com/Checkmarx/kics/pkg/kics.(*Service).sink(0x4000594b60, {0x27657d0, 0x4000136000}, {0x40005602d0, 0x13}, {0x210aa06, 0x7}, {0x27487c0, 0x400060e098}, {0x4001188000, ...})
> 	/app/pkg/kics/sink.go:44 +0x18c
> github.com/Checkmarx/kics/pkg/kics.(*Service).PrepareSources.func1({0x27657d0, 0x4000136000}, {0x40005602d0, 0x13}, {0x27539c8?, 0x400060e098})
> 	/app/pkg/kics/service.go:72 +0xa8
> github.com/Checkmarx/kics/pkg/engine/provider.(*FileSystemSourceProvider).GetSources(0x4000a6f500, {0x27657d0, 0x4000136000}, 0x273e4d0?, 0x4000a6e000, 0x3fe974?)
> 	/app/pkg/engine/provider/filesystem.go:129 +0x140
> github.com/Checkmarx/kics/pkg/kics.(*Service).PrepareSources(0x4000594b60, {0x27657d0, 0x4000136000}, {0x210aa06, 0x7}, 0x7b4d4?, 0x4000bc6000?)
> 	/app/pkg/kics/service.go:68 +0x1c0
> created by github.com/Checkmarx/kics/pkg/scanner.PrepareAndScan
> 	/app/pkg/scanner/scanner.go:24 +0xb4


Discussion / hints:
Hello @liorj-orca,

Thank you very much for bringing this to our attention, we had a few issues in the past 2 weeks related to panics, they have all been tackled and no more panics should be happening in the Resolver stage as soon as version 1.7.4 comes out, I have personally tested the file you gave me on our master branch and the panic is handled correctly 😄 

Fix patch:
diff --git a/pkg/resolver/file/file.go b/pkg/resolver/file/file.go
index e2be17e6c9e..b405ad44524 100644
--- a/pkg/resolver/file/file.go
+++ b/pkg/resolver/file/file.go
@@ -222,7 +222,7 @@ func (r *Resolver) resolveYamlPath(
 	resolvedFilesCache map[string]ResolvedFile,
 	refBool bool) (yaml.Node, bool) {
 	value := v.Value
-	if resolveCount > constants.MaxResolvedFiles || (strings.HasPrefix(value, "#") && !refBool) {
+	if resolveCount > constants.MaxResolvedFiles || (strings.HasPrefix(value, "#") && !refBool) || (value == "#" && refBool) {
 		return *v, false
 	}
 	var splitPath []string
@@ -368,7 +368,7 @@ func (r *Resolver) resolvePath(
 	resolveCount int,
 	resolvedFilesCache map[string]ResolvedFile,
 	refBool bool) (any, bool) {
-	if resolveCount > constants.MaxResolvedFiles || (strings.HasPrefix(value, "#") && !refBool) {
+	if resolveCount > constants.MaxResolvedFiles || (strings.HasPrefix(value, "#") && !refBool) || (value == "#" && refBool) {
 		return value, false
 	}
 	var splitPath []string


Test patch:
diff --git a/e2e/fixtures/samples/panicYamlRef/file1.yaml b/e2e/fixtures/samples/panicYamlRef/file1.yaml
index 94b90bb12b4..4dc12d8e1ad 100644
--- a/e2e/fixtures/samples/panicYamlRef/file1.yaml
+++ b/e2e/fixtures/samples/panicYamlRef/file1.yaml
@@ -3,4 +3,7 @@
 
 
 data_file: 
-    $ref: "data.yaml"
\ No newline at end of file
+    $ref: "data.yaml"
+
+self_ref:
+    $ref: "#"
\ No newline at end of file