Skip to content

Commit

Permalink
brakeman:fix - search for Gemfile's before start analysis
Browse files Browse the repository at this point in the history
If there was no `Gemfile` file in the current directory, Brakeman would
generate an error stating that the project to be analyzed was not a Ruby
on Rails project.

This commit fix this issue by looking for a directory path that contains
a `Gemfile` filename and them using this path as a work dir to execute
Brakeman.

Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup committed Dec 16, 2021
1 parent d7f1161 commit 0adf5d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
7 changes: 6 additions & 1 deletion internal/services/formatters/ruby/brakeman/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ZupIT/horusec/internal/enums/images"
"github.com/ZupIT/horusec/internal/helpers/messages"
"github.com/ZupIT/horusec/internal/services/formatters"
fileutils "github.com/ZupIT/horusec/internal/utils/file"
vulnhash "github.com/ZupIT/horusec/internal/utils/vuln_hash"
)

Expand Down Expand Up @@ -118,7 +119,11 @@ func (f *Formatter) newVulnerability(output *warning, projectSubPath string) *vu

func (f *Formatter) getDockerConfig(projectSubPath string) *docker.AnalysisData {
analysisData := &docker.AnalysisData{
CMD: f.AddWorkDirInCmd(CMD, projectSubPath, tools.Brakeman),
CMD: f.AddWorkDirInCmd(
CMD,
fileutils.GetSubPathByFilename(f.GetConfigProjectPath(), projectSubPath, "Gemfile"),
tools.Brakeman,
),
Language: languages.Ruby,
}

Expand Down
32 changes: 24 additions & 8 deletions internal/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ func GetPathFromFilename(filename, basePath string) string {
return filePath
}

func isSameExtensions(filename, path string) bool {
filenameExt := filepath.Ext(filename)
basePathExt := filepath.Ext(path)
return filenameExt == basePathExt
// GetSubPathByFilename works like GetSubPathByExtension but for filenames.
//
// The value returned will be the first path that contains a file with a given
// filename, otherwise will return an empty string.
func GetSubPathByFilename(projectPath, subPath, filename string) string {
pathToWalk := joinProjectPathWithSubPath(projectPath, subPath)
logger.LogDebugWithLevel(fmt.Sprintf("Seaching for files with %s name on %s", filename, pathToWalk))

if path := GetPathFromFilename(filename, pathToWalk); path != "" {
logger.LogDebugWithLevel(fmt.Sprintf("Found file %s on %s", filename, path))
return filepath.Dir(path)
}

return ""
}

// ReplacePathSeparator replace slashes from path to OS specific.
Expand Down Expand Up @@ -115,10 +125,6 @@ func GetSubPathByExtension(projectPath, subPath, ext string) (extensionPath stri
return ""
}

func buildPattern(ext string) string {
return "*" + ext
}

// relativeDirIfPathMatch return relative directory of path based on projectPath
// if path extension match ext.
func relativeDirIfPathMatch(projectPath, path, ext string) string {
Expand Down Expand Up @@ -316,3 +322,13 @@ func CreateAndWriteFile(input, filename string) error {
_, err = file.WriteString(input)
return err
}

func isSameExtensions(filename, path string) bool {
filenameExt := filepath.Ext(filename)
basePathExt := filepath.Ext(path)
return filenameExt == basePathExt
}

func buildPattern(ext string) string {
return "*" + ext
}

0 comments on commit 0adf5d5

Please sign in to comment.