Skip to content

Commit

Permalink
Merge pull request #6665 from liorj-orca/broken_symlink
Browse files Browse the repository at this point in the history
fix(engine): skip broken symlink/eloop
  • Loading branch information
gabriel-cx authored Aug 29, 2023
2 parents 954212d + 4092dc9 commit e3f8f15
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions e2e/fixtures/E2E_CLI_067_PAYLOAD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"document": []
}
22 changes: 22 additions & 0 deletions e2e/testcases/e2e-cli-067_ingore_broken_symlink_eloop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Package testcases provides end-to-end (E2E) testing functionality for the application.
package testcases

// E2E-CLI-067 - KICS scan but ignore broken symlinks and symlinks that create endless loops
// should perform the scan successfully and return exit code 0
func init() { //nolint
testSample := TestCase{
Name: "should perform a valid scan but ignore broken symlinks and symlinks that create endless loops [E2E-CLI-067]",
Args: args{
Args: []cmdArgs{
[]string{"scan", "-p", "\"/path/test/fixtures/link_test/broken_symlink\"", "\"/path/test/fixtures/link_test/eloop_link\"",
"--silent", "--payload-path", "/path/e2e/output/E2E_CLI_067_PAYLOAD.json"},
},
ExpectedPayload: []string{
"E2E_CLI_067_PAYLOAD.json",
},
},
WantStatus: []int{0},
}

Tests = append(Tests, testSample)
}
3 changes: 3 additions & 0 deletions pkg/engine/provider/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func GetSources(source []string) (ExtractedPath, error) {

getterDst, err := getPaths(&goGetter)
if err != nil {
if ignoreDamagedFiles(path) {
continue
}
log.Error().Msgf("%s", err)
return ExtractedPath{}, err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/engine/provider/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ func TestProvider_GetSources(t *testing.T) {
wantErr: true,
want: "",
},
{
name: "broken_sym_link",
args: args{
source: []string{
"test/fixtures/link_test/broken_symlink",
},
},
wantErr: false,
want: "",
},
}

for _, tt := range tests {
Expand Down
7 changes: 7 additions & 0 deletions pkg/engine/provider/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package provider
import (
"context"
"fmt"
ioFs "io/fs"
"os"
"path/filepath"
"strings"
"sync"
"syscall"

sentryReport "github.com/Checkmarx/kics/internal/sentry"
"github.com/Checkmarx/kics/pkg/model"
Expand Down Expand Up @@ -60,6 +62,11 @@ func (s *FileSystemSourceProvider) AddExcluded(excludePaths []string) error {
if os.IsNotExist(err) {
continue
}
if sysErr, ok := err.(*ioFs.PathError); ok {
log.Warn().Msgf("Failed getting file info for file '%s', Skipping due to: %s, Error number: %d",
excludePath, sysErr, sysErr.Err.(syscall.Errno))
continue
}
return errors.Wrap(err, "failed to open excluded file")
}
s.mu.Lock()
Expand Down
13 changes: 13 additions & 0 deletions pkg/engine/provider/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,19 @@ func TestFileSystemSourceProvider_AddExcluded(t *testing.T) {
want []string
wantErr bool
}{
{
name: "test_too_many_levels_of_symbolic_links",
fields: fields{
fs: fsystem,
},
args: args{
excludePaths: []string{
"test/fixtures/link_test/eloop_link",
},
},
want: []string{},
wantErr: false,
},
{
name: "test_add_excluded",
fields: fields{
Expand Down
4 changes: 3 additions & 1 deletion pkg/scan/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func (c *Client) prepareAndAnalyzePaths(ctx context.Context) (provider.Extracted
}

allPaths := combinePaths(terraformerExPaths, kuberneterExPaths, regularExPaths, queryExPaths, libExPaths)

if len(allPaths.Path) == 0 {
return provider.ExtractedPath{}, nil
}
log.Info().Msgf("Total files in the project: %d", getTotalFiles(allPaths.Path))

a := &analyzer.Analyzer{
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/link_test/broken_symlink
1 change: 1 addition & 0 deletions test/fixtures/link_test/eloop_link

0 comments on commit e3f8f15

Please sign in to comment.