From 14d4304c3013c7344452000aaae4f9ec8e5195c8 Mon Sep 17 00:00:00 2001 From: LirShindalman <49649760+lirshindalman@users.noreply.github.com> Date: Mon, 19 Aug 2024 10:11:50 +0300 Subject: [PATCH] fix(general): add try except to loads file (#6668) * . * . * . * . --- checkov/ansible/graph_builder/local_graph.py | 9 ++++++--- checkov/common/parsers/yaml/loader.py | 10 ++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/checkov/ansible/graph_builder/local_graph.py b/checkov/ansible/graph_builder/local_graph.py index db6c8a64554..733109cd598 100644 --- a/checkov/ansible/graph_builder/local_graph.py +++ b/checkov/ansible/graph_builder/local_graph.py @@ -142,8 +142,11 @@ def get_files_definitions(root_folder: str | Path) -> dict[str | Path, dict[str, file_paths = get_scannable_file_paths(root_folder=root_folder) for file_path in file_paths: - result = parse_file(f=file_path) - if result is not None: - definitions[file_path] = result[0] + try: + result = parse_file(f=file_path) + if result is not None: + definitions[file_path] = result[0] + except Exception as err: + logging.warning(f'fail to pars file {file_path}, {err}') return definitions diff --git a/checkov/common/parsers/yaml/loader.py b/checkov/common/parsers/yaml/loader.py index ef8e4fcb728..a4f9eb5d142 100644 --- a/checkov/common/parsers/yaml/loader.py +++ b/checkov/common/parsers/yaml/loader.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging from collections.abc import Hashable from pathlib import Path from typing import Any, TYPE_CHECKING @@ -17,13 +18,14 @@ def loads(content: str) -> list[dict[str, Any]]: """ Load the given YAML string """ - - template = list(yaml.load_all(content, Loader=SafeLineLoader)) - + try: + template = list(yaml.load_all(content, Loader=SafeLineLoader)) + except Exception as e: + logging.warning(f'Fail to load yaml content, {e}') + template = [None] # Convert an empty file to an empty dict if template is None: template = {} - return template