Skip to content

Commit 4f4aaef

Browse files
committed
CM-48559 - Fix SAST pre-commit hook
1 parent 76b8bb3 commit 4f4aaef

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

cycode/cli/apps/scan/code_scanner.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from cycode.cli.apps.scan.scan_parameters import get_scan_parameters
1010
from cycode.cli.apps.scan.scan_result import (
1111
create_local_scan_result,
12+
enrich_scan_result_with_data_from_detection_rules,
1213
get_scan_result,
1314
get_sync_scan_result,
1415
print_local_scan_results,
@@ -77,37 +78,6 @@ def _should_use_sync_flow(command_scan_type: str, scan_type: str, sync_option: b
7778
return True
7879

7980

80-
def _enrich_scan_result_with_data_from_detection_rules(
81-
cycode_client: 'ScanClient', scan_result: ZippedFileScanResult
82-
) -> None:
83-
detection_rule_ids = set()
84-
for detections_per_file in scan_result.detections_per_file:
85-
for detection in detections_per_file.detections:
86-
detection_rule_ids.add(detection.detection_rule_id)
87-
88-
detection_rules = cycode_client.get_detection_rules(detection_rule_ids)
89-
detection_rules_by_id = {detection_rule.detection_rule_id: detection_rule for detection_rule in detection_rules}
90-
91-
for detections_per_file in scan_result.detections_per_file:
92-
for detection in detections_per_file.detections:
93-
detection_rule = detection_rules_by_id.get(detection.detection_rule_id)
94-
if not detection_rule:
95-
# we want to make sure that BE returned it. better to not map data instead of failed scan
96-
continue
97-
98-
if not detection.severity and detection_rule.classification_data:
99-
# it's fine to take the first one, because:
100-
# - for "secrets" and "iac" there is only one classification rule per-detection rule
101-
# - for "sca" and "sast" we get severity from detection service
102-
detection.severity = detection_rule.classification_data[0].severity
103-
104-
# detection_details never was typed properly. so not a problem for now
105-
detection.detection_details['custom_remediation_guidelines'] = detection_rule.custom_remediation_guidelines
106-
detection.detection_details['remediation_guidelines'] = detection_rule.remediation_guidelines
107-
detection.detection_details['description'] = detection_rule.description
108-
detection.detection_details['policy_display_name'] = detection_rule.display_name
109-
110-
11181
def _get_scan_documents_thread_func(
11282
ctx: typer.Context, is_git_diff: bool, is_commit_range: bool, scan_parameters: dict
11383
) -> Callable[[list[Document]], tuple[str, CliError, LocalScanResult]]:
@@ -140,7 +110,7 @@ def _scan_batch_thread_func(batch: list[Document]) -> tuple[str, CliError, Local
140110
should_use_sync_flow,
141111
)
142112

143-
_enrich_scan_result_with_data_from_detection_rules(cycode_client, scan_result)
113+
enrich_scan_result_with_data_from_detection_rules(cycode_client, scan_result)
144114

145115
local_scan_result = create_local_scan_result(
146116
scan_result, batch, command_scan_type, scan_type, severity_threshold

cycode/cli/apps/scan/commit_range_scanner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from cycode.cli.apps.scan.scan_parameters import get_scan_parameters
1414
from cycode.cli.apps.scan.scan_result import (
1515
create_local_scan_result,
16+
enrich_scan_result_with_data_from_detection_rules,
1617
init_default_scan_result,
1718
print_local_scan_results,
1819
)
@@ -120,6 +121,7 @@ def _scan_commit_range_documents(
120121
scan_parameters,
121122
timeout,
122123
)
124+
enrich_scan_result_with_data_from_detection_rules(cycode_client, scan_result)
123125

124126
progress_bar.update(ScanProgressBarSection.SCAN)
125127
progress_bar.set_section_length(ScanProgressBarSection.GENERATE_REPORT, 1)

cycode/cli/apps/scan/scan_result.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,34 @@ def print_local_scan_results(
179179
printer = ctx.obj.get('console_printer')
180180
printer.update_ctx(ctx)
181181
printer.print_scan_results(local_scan_results, errors)
182+
183+
184+
def enrich_scan_result_with_data_from_detection_rules(
185+
cycode_client: 'ScanClient', scan_result: ZippedFileScanResult
186+
) -> None:
187+
detection_rule_ids = set()
188+
for detections_per_file in scan_result.detections_per_file:
189+
for detection in detections_per_file.detections:
190+
detection_rule_ids.add(detection.detection_rule_id)
191+
192+
detection_rules = cycode_client.get_detection_rules(detection_rule_ids)
193+
detection_rules_by_id = {detection_rule.detection_rule_id: detection_rule for detection_rule in detection_rules}
194+
195+
for detections_per_file in scan_result.detections_per_file:
196+
for detection in detections_per_file.detections:
197+
detection_rule = detection_rules_by_id.get(detection.detection_rule_id)
198+
if not detection_rule:
199+
# we want to make sure that BE returned it. better to not map data instead of failed scan
200+
continue
201+
202+
if not detection.severity and detection_rule.classification_data:
203+
# it's fine to take the first one, because:
204+
# - for "secrets" and "iac" there is only one classification rule per-detection rule
205+
# - for "sca" and "sast" we get severity from detection service
206+
detection.severity = detection_rule.classification_data[0].severity
207+
208+
# detection_details never was typed properly. so not a problem for now
209+
detection.detection_details['custom_remediation_guidelines'] = detection_rule.custom_remediation_guidelines
210+
detection.detection_details['remediation_guidelines'] = detection_rule.remediation_guidelines
211+
detection.detection_details['description'] = detection_rule.description
212+
detection.detection_details['policy_display_name'] = detection_rule.display_name

0 commit comments

Comments
 (0)