Skip to content

fix(rule): raise SigmaError for non-string related fields and non-map detection - #528

Open
nkbeast wants to merge 1 commit into
SigmaHQ:mainfrom
nkbeast:fix/related-id-and-detection-type-errors
Open

fix(rule): raise SigmaError for non-string related fields and non-map detection#528
nkbeast wants to merge 1 commit into
SigmaHQ:mainfrom
nkbeast:fix/related-id-and-detection-type-errors

Conversation

@nkbeast

@nkbeast nkbeast commented Aug 11, 2026

Copy link
Copy Markdown

Fixes malformed rules crashing with raw exceptions instead of proper validation errors.

What's broken

SigmaRule.from_yaml/from_dict is supposed to validate malformed rules and produce SigmaErrors (collected via collect_errors=True or raised otherwise). Three malformed-rule shapes bypass that machinery and crash callers (e.g. sigma-cli batch checks) with raw exceptions:

  1. related: [{id: 123, type: derived}] — non-string id inside an otherwise valid map.
    UUID(123) raises AttributeError (only ValueError is caught), so the rule crashes instead of raising SigmaRelatedError.

  2. related: [{id: ..., type: 123}] — non-string type hits .upper() under a bare except, producing a SigmaRelatedError only by accident of the catch-all.

  3. detection: hello (or detection: [a, b], detection: 123) — a non-map detection section.
    detections["condition"] raises TypeError, which is not caught in SigmaRule.from_dict, so parsing crashes even with collect_errors=True.

Fix

  • SigmaRelatedItem.from_dict validates that id and type are strings before use, raising SigmaRelatedError (mirrors the isinstance style of the earlier type-validation fix for related: [foo]).
  • SigmaRule.from_dict gains an except TypeError branch for the detection block, mirroring the existing guard in SigmaFilter.from_dict ("Sigma filter must be a dictionary").

All three shapes now yield a collected/raised SigmaDetectionError / SigmaRelatedError like every other rule error.

Tests

Added 3 regression tests in tests/test_rule.py:

  • test_invalid_related_id_typeid: 123 raises SigmaRelatedError
  • test_invalid_related_type_typetype: 123 raises SigmaRelatedError
  • test_sigmarule_detection_not_map / test_sigmarule_detection_not_map_collect_errors — non-map detection raises / collects SigmaDetectionError

Verification

  • pytest tests/test_rule.py — 148 passed
  • Full suite — 1525 passed, 1 skipped (2 pre-existing plugin-install failures need network/pip)
  • mypy — clean (66 files)
  • black --check — clean

… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant