Skip to content

Commit

Permalink
Merge pull request #6501 from Checkmarx/bug/kics/865
Browse files Browse the repository at this point in the history
fix(Tracker): Add Resolved File Lines' counter
  • Loading branch information
asofsilva authored Jul 19, 2023
2 parents 8e2b2c5 + 8f07e8e commit 11f1989
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 8 additions & 0 deletions pkg/kics/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (s *Service) sink(ctx context.Context, filename, scanID string, rc io.Reade
return nil
}

linesResolved := 0
for _, ref := range documents.ResolvedFiles {
if ref.Path != filename {
linesResolved += len(*ref.LinesContent)
}
}
s.Tracker.TrackFileFoundCountLines(linesResolved)

fileCommands := s.Parser.CommentsCommands(filename, *content)

for _, document := range documents.Docs {
Expand Down
21 changes: 14 additions & 7 deletions pkg/resolver/file/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (r *Resolver) Resolve(fileContent []byte, path string, resolveCount int, re
// resolve the paths
obj, _ = r.walk(fileContent, obj, obj, path, resolveCount, resolvedFilesCache, false)

b, err := r.marshler(obj)
b, err := json.MarshalIndent(obj, "", "")
if err == nil {
return b
}
Expand Down Expand Up @@ -236,9 +237,7 @@ func (r *Resolver) resolveYamlPath(
value = checkServerlessFileReference(value)

path := filepath.Join(filepath.Dir(filePath), value)
splitPath = strings.Split(path, "#") // splitting by removing the section to look for in the file
// index 0 contains the path of the file while the other indexes contain the sections (e.g. path = "./definitions.json#User/schema")
onlyFilePath := splitPath[0]
onlyFilePath := getPathFromString(path)
_, err := os.Stat(path)
if err != nil {
return *v, false
Expand All @@ -261,7 +260,7 @@ func (r *Resolver) resolveYamlPath(
}
}

r.ResolvedFiles[value] = model.ResolvedFile{
r.ResolvedFiles[getPathFromString(value)] = model.ResolvedFile{
Content: resolvedFilesCache[filename].fileContent,
Path: path,
LinesContent: utils.SplitLines(string(resolvedFilesCache[filename].fileContent)),
Expand All @@ -273,7 +272,7 @@ func (r *Resolver) resolveYamlPath(
if strings.Contains(strings.ToLower(value), "!ref") { // Cloudformation !Ref check
return *obj, false
}
if len(splitPath) == 1 {
if !strings.Contains(path, "#") {
return *obj, true
}
}
Expand Down Expand Up @@ -353,6 +352,14 @@ func (r *Resolver) resolveFile(
return nil, false
}

func getPathFromString(path string) string {
lastIndex := strings.LastIndex(path, "#")
if lastIndex == -1 {
return path
}
return path[:lastIndex]
}

// isPath returns true if the value is a valid path
func (r *Resolver) resolvePath(
originalFileContent []byte,
Expand Down Expand Up @@ -391,7 +398,7 @@ func (r *Resolver) resolvePath(
}
}

r.ResolvedFiles[value] = model.ResolvedFile{
r.ResolvedFiles[getPathFromString(value)] = model.ResolvedFile{
Content: resolvedFilesCache[onlyFilePath].fileContent,
Path: path,
LinesContent: utils.SplitLines(string(resolvedFilesCache[onlyFilePath].fileContent)),
Expand Down

0 comments on commit 11f1989

Please sign in to comment.