Skip to content

Commit

Permalink
fix(general): fix inline suppression collection inside lists (#5370)
Browse files Browse the repository at this point in the history
fix inline suppression collection inside lists
  • Loading branch information
gruebel committed Jul 23, 2023
1 parent fe5d976 commit ead2048
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
29 changes: 25 additions & 4 deletions checkov/yaml_doc/base_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ def _scan_yaml_array(
if isinstance(item, str):
item = self.set_lines_for_item(item)
if STARTLINE_MARK != item and ENDLINE_MARK != item:
skip_info: "_SkippedCheck" = {}
if skip_infos and skip_infos[0]:
# multiple items could be found, so we need to skip the correct one(s)
skip_info = ([skip for skip in skip_infos if item[STARTLINE_MARK] <= skip["line_number"] <= item[ENDLINE_MARK]] or [{}])[0]
skip_info = self._collect_inline_suppression_in_array(item=item, skip_infos=skip_infos)

self.update_result(
check,
Expand Down Expand Up @@ -347,3 +344,27 @@ def set_lines_for_item(self, item: str) -> dict[int | str, str | int] | str:
break

return item_dict

def _collect_inline_suppression_in_array(self, item: Any, skip_infos: list[_SkippedCheck]) -> _SkippedCheck:
if skip_infos and skip_infos[0]:
if isinstance(item, dict):
# multiple items could be found, so we need to skip the correct one(s)
skip_info = [
skip for skip in skip_infos if item[STARTLINE_MARK] <= skip["line_number"] <= item[ENDLINE_MARK]
]
if skip_info:
return skip_info[0]
elif isinstance(item, list):
# depending on the check a list of uncomplaint items can be found and need to be correctly matched
for sub_item in item:
if isinstance(sub_item, dict):
# only one of the list items need to be matched
skip_info = [
skip
for skip in skip_infos
if sub_item[STARTLINE_MARK] <= skip["line_number"] <= sub_item[ENDLINE_MARK]
]
if skip_info:
return skip_info[0]

return {} # nothing found
9 changes: 9 additions & 0 deletions tests/gitlab_ci/resources/rules/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ planOnlySubset:
- if: $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_PIPELINE_SOURCE != "schedule"
when: manual
allow_failure: true

jobSkip:
script: echo "This job creates double pipelines!"
rules:
- changes:
- $DOCKERFILES_DIR/*
# checkov:skip=CKV_GITLABCI_2: Ignore
- if: $CI_PIPELINE_SOURCE == "push"
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
2 changes: 1 addition & 1 deletion tests/gitlab_ci/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_runner(self):
self.assertEqual(len(report.failed_checks), 5)
self.assertEqual(report.parsing_errors, [])
self.assertEqual(len(report.passed_checks), 9)
self.assertEqual(report.skipped_checks, [])
self.assertEqual(len(report.skipped_checks), 1)
report.print_console()

def test_runner_honors_enforcement_rules(self):
Expand Down

0 comments on commit ead2048

Please sign in to comment.