fix(rule): raise SigmaError for non-string related fields and non-map detection - #528
Open
nkbeast wants to merge 1 commit into
Open
fix(rule): raise SigmaError for non-string related fields and non-map detection#528nkbeast wants to merge 1 commit into
nkbeast wants to merge 1 commit into
Conversation
… detection Type validation of the related meta field from 0a76bbd only covered non-map items (related: [foo]); non-string values inside the maps still crashed parsing with an unhandled AttributeError/TypeError instead of a proper SigmaRelatedError: * related: [{id: 123}] -> AttributeError: 'int' object has no attribute 'replace' (UUID() call, ValueError-only catch) * related: [{type: 123}] -> AttributeError on '.upper()', swallowed by the bare except but with an ugly message Similarly, a non-map detection section (detection: hello) crashed with a raw TypeError, bypassing the collect_errors machinery. The sibling SigmaFilter.from_dict already handles this case with an 'except TypeError' guard; SigmaRule.from_dict now mirrors it. All cases now produce SigmaRelatedError / SigmaDetectionError, collected or raised like every other rule error.
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.
Fixes malformed rules crashing with raw exceptions instead of proper validation errors.
What's broken
SigmaRule.from_yaml/from_dictis supposed to validate malformed rules and produceSigmaErrors (collected viacollect_errors=Trueor raised otherwise). Three malformed-rule shapes bypass that machinery and crash callers (e.g. sigma-cli batch checks) with raw exceptions:related: [{id: 123, type: derived}]— non-stringidinside an otherwise valid map.UUID(123)raisesAttributeError(onlyValueErroris caught), so the rule crashes instead of raisingSigmaRelatedError.related: [{id: ..., type: 123}]— non-stringtypehits.upper()under a bareexcept, producing aSigmaRelatedErroronly by accident of the catch-all.detection: hello(ordetection: [a, b],detection: 123) — a non-mapdetectionsection.detections["condition"]raisesTypeError, which is not caught inSigmaRule.from_dict, so parsing crashes even withcollect_errors=True.Fix
SigmaRelatedItem.from_dictvalidates thatidandtypeare strings before use, raisingSigmaRelatedError(mirrors theisinstancestyle of the earlier type-validation fix forrelated: [foo]).SigmaRule.from_dictgains anexcept TypeErrorbranch for the detection block, mirroring the existing guard inSigmaFilter.from_dict("Sigma filter must be a dictionary").All three shapes now yield a collected/raised
SigmaDetectionError/SigmaRelatedErrorlike every other rule error.Tests
Added 3 regression tests in
tests/test_rule.py:test_invalid_related_id_type—id: 123raisesSigmaRelatedErrortest_invalid_related_type_type—type: 123raisesSigmaRelatedErrortest_sigmarule_detection_not_map/test_sigmarule_detection_not_map_collect_errors— non-map detection raises / collectsSigmaDetectionErrorVerification
pytest tests/test_rule.py— 148 passedmypy— clean (66 files)black --check— clean