Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cli/scancommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,14 @@ func EnrichCmd(c *components.Context) error {
func ScanCmd(c *components.Context) error {
if len(c.Arguments) == 0 && !c.IsFlagSet(flags.SpecFlag) {
return pluginsCommon.PrintHelpAndReturnError("providing either a <source pattern> argument or the 'spec' option is mandatory", c)
} else if len(c.Arguments) > 1 {
// If a non-existing flag was provided AFTER the provided source_pattern - it will be captured as another argument. Since 'scan' command
// Expects only a single argument, we use this check to verify all provided flags are valid.
// If a non exiting flag was provided BEFORE the source_pattern, the CLI will return an error before reaching this point.
errorMessage := fmt.Sprintf("Too many arguments provided (%d in total).\nSome flags may be incorrectly specified, causing them to be misinterpreted as arguments and ignored. Please verify that all flags are valid.", len(c.Arguments))
return pluginsCommon.PrintHelpAndReturnError(errorMessage, c)
}

serverDetails, err := createServerDetailsWithConfigOffer(c)
if err != nil {
return err
Expand Down
38 changes: 38 additions & 0 deletions scans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,44 @@ func TestXrayBinaryScanSimpleJsonWithProgress(t *testing.T) {
})
}

// This test verifies the correctness of a use case in 'scan' command, where a user provides the command's arguments before the command's flags, and there is an incorrect flag.
// Since the library that parses the command expects the flags to be provided before the arguments, it cannot recognize a wrongly provided flag when the order is reversed.
// This test checks the fix for this issue.
func TestXrayBinaryScanWithIncorrectFlagsAfterArgs(t *testing.T) {
testCases := []struct {
name string
flagsBeforeArgs bool
}{
{
name: "flags before args",
flagsBeforeArgs: true,
},
{
name: "args before flags",
flagsBeforeArgs: false,
},
}

callback := commonTests.MockProgressInitialization()
defer callback()
integration.InitScanTest(t, scangraph.GraphScanMinXrayVersion)
Copy link
Contributor

@attiasas attiasas May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

integration.InitScanTest should be the first line, skipping the test if needed

binariesPath := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "binaries", "*")

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
var args []string
if test.flagsBeforeArgs {
args = []string{"scan", "--watch=my-watch", binariesPath}
} else {
args = []string{"scan", binariesPath, "--watch=my-watch"}
Comment on lines +106 to +108
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args = []string{"scan", "--watch=my-watch", binariesPath}
} else {
args = []string{"scan", binariesPath, "--watch=my-watch"}
args = []string{"scan", "--not-defined-flag=value", binariesPath}
} else {
args = []string{"scan", binariesPath, "--watches=my-watch"}

change the flag name to make it more readable

}

err := securityTests.PlatformCli.Exec(args...)
assert.Error(t, err)
Comment on lines +111 to +112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err := securityTests.PlatformCli.Exec(args...)
assert.Error(t, err)
assert.Error(t, securityTests.PlatformCli.Exec(args...))

maybe you can also check if its the error you added (or error expected) and not random one

})
}
}

func testXrayBinaryScan(t *testing.T, format, policyName, watchName string, errorExpected bool) string {
binariesPath := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "binaries", "*")
args := []string{"scan", binariesPath, "--licenses", "--format=" + format}
Expand Down
Loading