mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
scope: do not create scope if path is invalid
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
9ec5da24dd
commit
e1ce040ead
@ -2,6 +2,7 @@ package scope
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/anchore/stereoscope"
|
"github.com/anchore/stereoscope"
|
||||||
|
|
||||||
@ -32,7 +33,11 @@ func NewScope(userInput string, o Option) (Scope, func(), error) {
|
|||||||
|
|
||||||
switch protocol.Type {
|
switch protocol.Type {
|
||||||
case directoryProtocol:
|
case directoryProtocol:
|
||||||
// populate the scope object for dir
|
err := isValidPath(protocol.Value)
|
||||||
|
if err != nil {
|
||||||
|
return Scope{}, func() {}, fmt.Errorf("unable to process path, must exist and be a directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
s, err := NewScopeFromDir(protocol.Value, o)
|
s, err := NewScopeFromDir(protocol.Value, o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Scope{}, func() {}, fmt.Errorf("could not populate scope from path (%s): %w", protocol.Value, err)
|
return Scope{}, func() {}, fmt.Errorf("could not populate scope from path (%s): %w", protocol.Value, err)
|
||||||
@ -115,3 +120,18 @@ func (s Scope) Source() interface{} {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isValidPath ensures that the user-provided input will correspond to a path
|
||||||
|
// that exists and is a directory
|
||||||
|
func isValidPath(userInput string) error {
|
||||||
|
fileMeta, err := os.Stat(userInput)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fileMeta.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("path is not a directory: %s", userInput)
|
||||||
|
}
|
||||||
|
|||||||
@ -213,3 +213,40 @@ func TestFilesByGlob(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsValidPath(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
input string
|
||||||
|
isError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "path is valid",
|
||||||
|
input: "test-fixtures",
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "file is invalid",
|
||||||
|
input: "test-fixtures/.vimrc",
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "path does not exist",
|
||||||
|
input: "foo/bar/baz",
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range testCases {
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
err := isValidPath(test.input)
|
||||||
|
if err != nil && !test.isError {
|
||||||
|
t.Errorf("did not expect and error, got: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil && test.isError {
|
||||||
|
t.Errorf("expected an error but didn't get one")
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user