Skip to content

Commit 39f70a5

Browse files
CM-63882 - Added scanType validation (#452)
Co-authored-by: omerr-cycode <omer.roth@cycode.com>
1 parent 3d15222 commit 39f70a5

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

cycode/cli/apps/scan/scan_command.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,32 @@
2828
_SECRET_RICH_HELP_PANEL = 'Secret options'
2929

3030

31+
def _single_value_callback(ctx: typer.Context, param: typer.CallbackParam, value: list) -> list:
32+
if len(value) > 1:
33+
values_str = ', '.join(str(v) for v in value)
34+
param_hint = '/'.join(sorted(param.opts, key=len))
35+
err = typer.BadParameter(
36+
f'Only one value can be specified per command. Got: {values_str}. Run a separate command for each value.',
37+
ctx=ctx,
38+
param_hint=param_hint,
39+
)
40+
err.exit_code = 1
41+
raise err
42+
return value
43+
44+
3145
def scan_command(
3246
ctx: typer.Context,
3347
scan_type: Annotated[
34-
ScanTypeOption,
48+
list[ScanTypeOption],
3549
typer.Option(
3650
'--scan-type',
3751
'-t',
3852
help='Specify the type of scan you wish to execute.',
3953
case_sensitive=False,
54+
callback=_single_value_callback,
4055
),
41-
] = ScanTypeOption.SECRET,
56+
] = (ScanTypeOption.SECRET,),
4257
soft_fail: Annotated[
4358
bool, typer.Option('--soft-fail', help='Run the scan without failing; always return a non-error status code.')
4459
] = False,
@@ -137,6 +152,9 @@ def scan_command(
137152
param_hint='--export-file',
138153
)
139154

155+
# _single_value_callback validated exactly one value was provided; unwrap from list
156+
scan_type = scan_type[0]
157+
140158
ctx.obj['show_secret'] = show_secret
141159
ctx.obj['soft_fail'] = soft_fail
142160
ctx.obj['stop_on_error'] = stop_on_error

tests/cli/commands/scan/test_scan_command.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import re
2+
13
import click
24
import pytest
35
import typer
6+
from typer.testing import CliRunner
47

8+
from cycode.cli.app import app
59
from cycode.cli.apps.scan.scan_command import scan_command_result_callback
610
from cycode.cli.consts import ISSUE_DETECTED_STATUS_CODE, NO_ISSUES_STATUS_CODE, SCAN_ERROR_STATUS_CODE
711

812

13+
def _strip_ansi(text: str) -> str:
14+
return re.sub(r'\x1b\[[0-9;]*[mGKHF]', '', text)
15+
16+
917
def _make_ctx(**obj_overrides: object) -> click.Context:
1018
obj = {
1119
'soft_fail': False,
@@ -25,6 +33,21 @@ def _invoke_result_callback(ctx: click.Context) -> int:
2533
return exc_info.value.exit_code
2634

2735

36+
class TestScanCommand:
37+
def test_multiple_scan_types_rejected(self) -> None:
38+
result = CliRunner().invoke(app, ['scan', '-t', 'iac', '-t', 'sast', 'path', '.'])
39+
assert result.exit_code == 1
40+
output = _strip_ansi(result.output)
41+
assert '-t/--scan-type' in output
42+
assert 'iac' in output
43+
assert 'sast' in output
44+
45+
def test_single_scan_type_accepted(self) -> None:
46+
result = CliRunner().invoke(app, ['scan', '-t', 'iac', '--help'])
47+
assert result.exit_code == 0
48+
assert 'Error' not in result.output
49+
50+
2851
class TestScanCommandResultCallback:
2952
def test_no_issues_no_errors_exits_zero(self) -> None:
3053
assert _invoke_result_callback(_make_ctx()) == NO_ISSUES_STATUS_CODE

0 commit comments

Comments
 (0)