Skip to content

fix(types): transparent slicing for zero stop and friendlier YAML errors - #529

Open
nkbeast wants to merge 1 commit into
SigmaHQ:mainfrom
nkbeast:fix/sigmastring-slice-and-yaml-errors
Open

fix(types): transparent slicing for zero stop and friendlier YAML errors#529
nkbeast wants to merge 1 commit into
SigmaHQ:mainfrom
nkbeast:fix/sigmastring-slice-and-yaml-errors

Conversation

@nkbeast

@nkbeast nkbeast commented Aug 11, 2026

Copy link
Copy Markdown

Three small, independent fixes around rule parsing and SigmaString semantics.

1. SigmaString slicing with a zero stop index (sigma/types.py)

SigmaString.__getitem__ used end = idx.stop or inf, mapping the falsy integer 0 to inf. As a result s[:0], s[0:0], s[1:0] and s[1:1] returned non-empty slices instead of the empty SigmaString, unlike plain str:

>>> SigmaString("abc")[1:0]  == "abc"[1:0]  # was: SigmaString(['bc']) != SigmaString('')
False

Fix: end = idx.stop if idx.stop is not None else inf.

2. Empty/comment-only YAML documents crash with raw AttributeError (sigma/rule/base.py)

SigmaRule.from_yaml("") or a comment-only document parses to None and from_dict(None) crashes with AttributeError: 'NoneType' object has no attribute 'get'. Ruleset directories routinely contain leftover empty .yml files, so batch tooling blows up with a raw exception. Empty documents now go through the normal validation path and produce collected/raised SigmaErrors like SigmaTitleError.

3. Duplicate-key error prints the literal {k} (sigma/rule/base.py)

raise yaml.error.YAMLError("Duplicate key '{k}'") is missing the f prefix, so the diagnostic literally says Duplicate key '{k}' instead of naming the offending key. Now reports e.g. Duplicate key 'selection'.

Tests

  • test_string_index_slice_zero_stop (tests/test_types.py) — [:0], [0:0], [1:0], [1:1], [-1:0] all return SigmaString("")
  • test_sigmarule_fromyaml_empty / test_sigmarule_fromyaml_empty_collect_errors (tests/test_rule.py)
  • test_sigmarule_fromyaml_duplicate_key_reports_key — asserts the key name appears in the message

Verification

  • pytest tests/test_types.py tests/test_rule.py — 296 passed
  • mypy — clean (66 files)
  • black --check — clean

* SigmaString slicing treated a zero stop index as "no end" because
  `idx.stop or inf` maps the falsy int 0 to inf. s[:0], s[0:0] and
  s[1:0] therefore returned non-empty strings instead of the empty
  SigmaString, breaking the documented transparent compatibility
  with str slicing. Use `idx.stop if idx.stop is not None else inf`.

* SigmaRule.from_yaml crashed with a raw AttributeError on empty or
  comment-only YAML documents (yaml.load returns None). Treat the
  empty document as an empty mapping so the usual collected/raised
  rule validation errors are produced instead.

* The duplicate YAML key error message printed the literal text
  '{k}' instead of the offending key due to a missing f-string.
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