fix(serverless): report unparsable serverless files as parsing errors - #7632
Open
Zuhef wants to merge 1 commit into
Open
fix(serverless): report unparsable serverless files as parsing errors#7632Zuhef wants to merge 1 commit into
Zuhef wants to merge 1 commit into
Conversation
A serverless.yml that fails to parse was dropped from the scan without appearing in the report, so parsing_errors stayed 0 and CKV_PARSE_ERROR_FAIL had nothing to act on. Collect the parse errors and add them to the report via add_parsing_errors, the same way the ARM, Bicep, CloudFormation and Terraform runners already do.
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.
Description
A
serverless.ymlthat fails to parse is dropped from the scan without appearing anywhere in the report, so a scan that examined nothing looks the same as a clean scan.checkov/serverless/parsers/parser.pycatchesCfnParseError, logs a warning and returnsNone.serverless/utils.py::get_files_definitionsthen discards thatNonewithout recording anything, andserverless/runner.pynever callsreport.add_parsing_errors()— unlike the ARM, Bicep, CloudFormation and Terraform runners.Reproducer — a
serverless.ymlwhose provider tag value is a single=(valid YAML, but the loader has no constructor for thetag:yaml.org,2002:valueit resolves to):Before:
After:
The warning was already logged, so this is not about visibility in a terminal — it is about the file being absent from the machine-readable report, which is what CI consumes, and about
CKV_PARSE_ERROR_FAILhaving nothing to act on.Note serverless files are pre-filtered by
SLS_FILE_MASKbefore parsing, so a parse failure here always means "a serverless file that could not be read", never "a file that belongs to another framework".Fix
Follows the existing convention, with an optional
out_parsing_errorsargument so no caller signature breaks:parsers/parser.py::parserecords the error message for the file onCfnParseError._parallel_parsereturns the errors it collected rather than writing to a shared mapping, because parsing goes throughparallel_runnerand may run in a separate process;get_files_definitionsmerges them for the caller.runner.pypasses a mapping down and callsreport.add_parsing_errors(...), matchingcloudformation/runner.py.This is the same gap I fixed for the Kubernetes runner in #7630; serverless was the remaining one in that family. The two PRs are independent but share a rationale, so they may be easiest to review together.
Testing
Baseline on an unmodified checkout is
5 failed, 47 passedwith the identical five failures — Windows-only path assertions intest_runner.py(test_record_relative_path_*) plustest_AdminPolicyDocument::test_summary. None are in the code touched here.Added
test_unparsable_file_is_reported_as_parsing_error, which 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:Also verified a valid
serverless.ymlis unaffected — still3 passed, 0 parsing_errors.flake8 checkov/serverless tests/serverless/runner/test_runner.py— clean.mypy --config-file mypy.ini checkov/serverless— 6 errors before and after; comparing the two lists with line numbers stripped shows they are identical, so no new type errors.