fix(kubernetes): report unparsable YAML files as parsing errors - #7630
Open
Zuhef wants to merge 1 commit into
Open
fix(kubernetes): report unparsable YAML files as parsing errors#7630Zuhef wants to merge 1 commit into
Zuhef wants to merge 1 commit into
Conversation
A Kubernetes manifest that fails to load, for example because it contains a value the YAML safe loader cannot construct, was dropped with only a debug log. The file did not appear in the report, so a scan that examined nothing was indistinguishable from a clean scan. Collect the YAML errors while parsing and add them to the report via add_parsing_errors, the same way the ARM, Bicep, CloudFormation and Terraform runners already do, so the files show up as parsing errors and CKV_PARSE_ERROR_FAIL can act on them.
This was referenced Jul 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Description
A Kubernetes manifest that cannot be loaded is currently dropped from the scan without appearing anywhere in the report, so a scan that examined nothing looks exactly like a clean scan.
Reproducer from the issue — a
Servicewith a single=as a label value. It is valid YAML, butSafeLoaderhas no constructor for thetag:yaml.org,2002:valuetag that=resolves to, so loading raises aConstructorError:Before this change (the same file without the label fails
CKV_K8S_21):After this change:
Root cause
checkov/kubernetes/parser/parser.pycatchesYAMLError, logs it atdebuglevel and returnsNone.kubernetes_utils.get_files_definitionsthen discards thatNonewithout recording anything, and the Kubernetes runner — unlike the ARM, Bicep, CloudFormation and Terraform runners — never callsreport.add_parsing_errors(). The result is thatparsing_errorsstays0and the skipped file is invisible in every output format.Fix
Collect the YAML error while parsing and report it, following the existing convention in the other frameworks:
parse()takes an optionalout_parsing_errorsmapping and records the error message for the file._parse_filereturns the errors it collected instead of writing to a shared mapping, because parsing runs throughparallel_runnerand may happen in a separate process.get_files_definitionsmerges them for the caller.get_folder_definitions/get_files_definitions/create_definitionsaccept an optionalout_parsing_errorsargument, so existing callers are unaffected, andcreate_definitionslogs an aggregated warning like the ARM and Bicep utils do.Runner.runpasses a mapping down and callsreport.add_parsing_errors(...), matchingcloudformation/runner.py.Because the file is now part of the report, the existing
CKV_PARSE_ERROR_FAILflag makes such a scan exit non-zero, which is what the issue asks for. The default exit code is deliberately left unchanged to avoid breaking existing pipelines.This also covers Helm and Kustomize, since those runners scan their rendered output through the Kubernetes runner.
Fixes #7453
Testing
The 9 failures and 4 errors are identical to those on an unmodified checkout (verified by stashing the change and re-running): they are Windows-specific path-separator assertions, a UTF-8 BOM test, and a pre-existing collection error in
test_yaml_policies.py. The 2 additional passes are the new test under both graph connectors.tests/kubernetes/runner/test_runner.py::test_unparsable_file_is_reported_as_parsing_error— asserts the file is listed inreport.parsing_errors, thatsummary["parsing_errors"] == 1, that no checks are reported for it, and thatget_exit_codereturns0normally but1withCKV_PARSE_ERROR_FAILset. It fails without the fix (Lists differ: [] != ['.../service.yaml']).python -m pytest tests/helm tests/kustomize -q→3 failed, 26 passed, 19 skipped; the 3 failures aresignal.SIGALRMnot existing on Windows and are unrelated.python -m flake8 checkov/kubernetes/ tests/kubernetes/runner/test_runner.py→ clean.python -m mypy --config-file mypy.ini checkov/kubernetes/→ 11 errors, the same 11 as on an unmodified checkout.Checklist: