Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
fix(rule-engine): eliminate eval() RCE in Sigma condition parser (C-1) (#116)
Beenu Arorafc455bc84d8a
diff --git a/services/api/app/services/rule_engine.py b/services/api/app/services/rule_engine.pyindex 5495d8cc3..c08a12c73 100644--- a/services/api/app/services/rule_engine.py+++ b/services/api/app/services/rule_engine.py@@ -146,19 +146,171 @@ def _eval_sigma_detection(detection: dict, flat_event: dict[str, Any]) -> bool:def _eval_condition(condition: str, selections: dict[str, bool]) -> bool:- """Evaluate a Sigma condition string."""+ """Evaluate a Sigma condition string using a safe boolean AST parser.++ SECURITY: Earlier revisions used `eval()` here, which allowed arbitrary+ Python expressions inside the Sigma `condition` field to execute in the+ API process. The condition is user-controlled (Sigma rule body) and+ therefore must be parsed, not evaluated.++ Supported grammar:+ - boolean literals (case-insensitive): true | false+ - selection name references (must be present in `selections` mapping)+ - unary operator: not <expr>+ - binary operators: <expr> and <expr> | <expr> or <expr>+ - parentheses: ( <expr> )+ - Sigma quantifiers (subset): "1 of selection*" / "all of selection*"++ Anything else short-circuits to the safe default of ANDing all selections.+ """expr = condition.strip()- # Replace selection names with their bool values- for name, val in selections.items():- expr = expr.replace(name, str(val))- # Simple evaluationtry:- expr = expr.replace("and", " and ").replace("or", " or ").replace("not", " not ")- # Safe eval with only booleans- return bool(eval(expr, {"__builtins__": {}}, {"True": True, "False": False})) # noqa: S307- except Exception:- # Default: AND all selections- return all(selections.values())+ return _SigmaConditionParser(expr, selections).parse()+ except Exception as exc: # noqa: BLE001 — keep behaviour identical to legacy fallback+ logger.debug("Sigma condition parse error (%r): %s", condition, exc)+ return all(selections.values()) if selections else False+++class _SigmaConditionParser:+ """Tiny recursive-descent parser for Sigma condition expressions.++ The grammar accepted is a strict subset of the Sigma spec, sufficient to+ cover every shipped detection rule in `detections/` while refusing+ anything that could lead to code execution.+ """++ _TOKEN_RE = re.compile(+ # Token alternatives: opening paren, closing paren, or a "word" that+ # may start with a digit (for the `1 of …` quantifier) or a letter /+ # underscore. The leading `\s*` is consumed by the caller via lstrip+ # so that trailing whitespace doesn't break the match loop.+ r"(?P<lparen>\()|(?P<rparen>\))|(?P<word>[A-Za-z0-9_][A-Za-z0-9_*]*)",+ )++ def __init__(self, expr: str, selections: dict[str, bool]) -> None:+ self._expr = expr+ self._selections = selections+ self._tokens = self._tokenize(expr)+ self._pos = 0++ def parse(self) -> bool:+ result = self._parse_or()+ if self._pos != len(self._tokens):+ raise ValueError(f"unexpected trailing token: {self._tokens[self._pos]!r}")+ return result++ # ── tokenizer ────────────────────────────────────────────────────────────+ def _tokenize(self, expr: str) -> list[str]:+ tokens: list[str] = []+ pos = 0+ n = len(expr)+ while pos < n:+ # Skip any inter-token whitespace.+ while pos < n and expr[pos].isspace():+ pos += 1+ if pos >= n:+ break+ m = self._TOKEN_RE.match(expr, pos)+ if not m:+ raise ValueError(f"invalid character at position {pos}: {expr[pos]!r}")+ if m.group("lparen"):+ tokens.append("(")+ elif m.group("rparen"):+ tokens.append(")")+ else:+ tokens.append(m.group("word"))+ pos = m.end()+ return tokens++ # ── grammar ──────────────────────────────────────────────────────────────+ def _peek(self) -> str | None:+ return self._tokens[self._pos] if self._pos < len(self._tokens) else None++ def _consume(self) -> str:+ tok = self._tokens[self._pos]+ self._pos += 1+ return tok++ def _match(self, *expected: str) -> bool:+ tok = self._peek()+ if tok is None:+ return False+ if tok.lower() in expected:+ self._consume()+ return True+ return False++ def _parse_or(self) -> bool:+ left = self._parse_and()+ while self._match("or"):+ right = self._parse_and()+ left = left or right+ return left++ def _parse_and(self) -> bool:+ left = self._parse_not()+ while self._match("and"):+ right = self._parse_not()+ left = left and right+ return left++ def _parse_not(self) -> bool:+ if self._match("not"):+ return not self._parse_not()+ return self._parse_atom()++ def _parse_atom(self) -> bool:+ tok = self._peek()+ if tok is None:+ raise ValueError("unexpected end of condition")++ if tok == "(":+ self._consume()+ value = self._parse_or()+ if not self._match(")"):+ raise ValueError("missing closing parenthesis")+ return value++ # Quantifier forms: "1 of <pattern>" / "all of <pattern>"+ lowered = tok.lower()+ if lowered in {"1", "all"} and (self._pos + 2) <= len(self._tokens):+ next_tok = self._tokens[self._pos + 1] if self._pos + 1 < len(self._tokens) else None+ if next_tok and next_tok.lower() == "of":+ self._consume() # quantifier+ self._consume() # 'of'+ pattern_tok = self._peek()+ if pattern_tok is None or pattern_tok in {"(", ")"}:+ raise ValueError("quantifier missing pattern")+ self._consume()+ return self._evaluate_quantifier(lowered, pattern_tok)++ # Bare identifier / literal.+ self._consume()+ if lowered == "true":+ return True+ if lowered == "false":+ return False+ if tok in self._selections:+ return bool(self._selections[tok])+ # Unknown selection name → treat as missing (False) rather than raising+ # so that legacy rules referring to absent selections still produce a+ # deterministic result.+ return False++ def _evaluate_quantifier(self, quantifier: str, pattern: str) -> bool:+ if pattern == "them":+ values = list(self._selections.values())+ elif "*" in pattern:+ regex = re.compile("^" + re.escape(pattern).replace(r"\*", ".*") + "$")+ values = [v for name, v in self._selections.items() if regex.match(name)]+ else:+ values = [self._selections.get(pattern, False)]++ if not values:+ return False+ if quantifier == "1":+ return any(values)+ return all(values)# ─── YARA Runner ──────────────────────────────────────────────────────────────diff --git a/services/api/tests/test_rule_engine_condition.py b/services/api/tests/test_rule_engine_condition.pynew file mode 100644index 000000000..f6b1b6a3a--- /dev/null+++ b/services/api/tests/test_rule_engine_condition.py@@ -0,0 +1,134 @@+"""Tests for the Sigma condition parser.++These tests pin the rule-engine condition evaluator to a safe boolean+subset and explicitly prove that the legacy `eval()` based RCE vector+(arbitrary Python in a Sigma `condition:` field) no longer executes.+"""++from __future__ import annotations++import os+import tempfile++import pytest+from app.services.rule_engine import _eval_condition, _SigmaConditionParser+++class TestSigmaConditionGrammar:+ """The accepted grammar mirrors the Sigma spec subset we support."""++ def test_simple_selection_true(self) -> None:+ assert _eval_condition("selection", {"selection": True}) is True++ def test_simple_selection_false(self) -> None:+ assert _eval_condition("selection", {"selection": False}) is False++ def test_and(self) -> None:+ assert _eval_condition("a and b", {"a": True, "b": True}) is True+ assert _eval_condition("a and b", {"a": True, "b": False}) is False++ def test_or(self) -> None:+ assert _eval_condition("a or b", {"a": False, "b": True}) is True+ assert _eval_condition("a or b", {"a": False, "b": False}) is False++ def test_not(self) -> None:+ assert _eval_condition("not a", {"a": False}) is True+ assert _eval_condition("not a", {"a": True}) is False++ def test_parentheses_and_precedence(self) -> None:+ sels = {"a": True, "b": False, "c": True}+ # `or` binds looser than `and`, so a and (b or c) ≠ (a and b) or c.+ assert _eval_condition("a and (b or c)", sels) is True+ assert _eval_condition("(a and b) or c", sels) is True+ assert _eval_condition("a and b or c", sels) is True # parsed as (a and b) or c++ def test_boolean_literals(self) -> None:+ assert _eval_condition("true", {}) is True+ assert _eval_condition("false", {}) is False+ assert _eval_condition("True and false", {}) is False++ def test_quantifier_one_of(self) -> None:+ sels = {"sel1": False, "sel2": True, "sel3": False}+ assert _eval_condition("1 of sel*", sels) is True+ sels_all_false = {"sel1": False, "sel2": False}+ assert _eval_condition("1 of sel*", sels_all_false) is False++ def test_quantifier_all_of(self) -> None:+ sels = {"sel1": True, "sel2": True}+ assert _eval_condition("all of sel*", sels) is True+ assert _eval_condition("all of sel*", {"sel1": True, "sel2": False}) is False++ def test_quantifier_them(self) -> None:+ assert _eval_condition("all of them", {"a": True, "b": True}) is True+ assert _eval_condition("1 of them", {"a": False, "b": True}) is True++ def test_unknown_selection_is_false(self) -> None:+ # Referring to a selection that does not exist resolves to False+ # rather than raising, matching the legacy fallback semantics.+ assert _eval_condition("missing", {"selection": True}) is False+ assert _eval_condition("missing or selection", {"selection": True}) is True+++class TestSigmaConditionRefusesCodeExecution:+ """The parser must not evaluate arbitrary Python expressions.++ These cases would have executed under the old `eval()` implementation+ and either raised, returned attacker-controlled values, or in the+ worst case (`__import__`) achieved remote code execution.+ """++ def test_attribute_access_does_not_execute(self) -> None:+ # `().__class__` was a classic eval sandbox escape vector. The+ # parser must reject the dot/parenthesis syntax outright and fall+ # back to the safe default (AND of selections, here empty → False).+ assert _eval_condition("().__class__.__mro__[1]", {}) is False++ def test_function_call_does_not_execute(self) -> None:+ # Side-effect proof: under the old eval() implementation, the+ # `open(... 'w')` call would create a real file on disk because+ # `__builtins__` was set to `{}` but `open` was still importable+ # through `().__class__.__base__.__subclasses__()` tricks. With+ # the AST parser we simply refuse to parse the expression.+ with tempfile.TemporaryDirectory() as tmp:+ sentinel = os.path.join(tmp, "pwned.txt")+ expr = f"__import__('os').system('touch {sentinel}')"+ result = _eval_condition(expr, {})+ assert result is False+ assert not os.path.exists(sentinel)++ def test_string_concatenation_does_not_execute(self) -> None:+ # `+` is not part of the supported grammar; legacy eval would have+ # happily concatenated strings and returned a truthy value.+ assert _eval_condition("'a' + 'b'", {}) is False++ def test_comparison_operators_rejected(self) -> None:+ # Comparison operators were silently accepted by eval; they are+ # not part of the Sigma condition grammar and must be refused.+ assert _eval_condition("1 == 1", {}) is False++ def test_walrus_does_not_assign(self) -> None:+ # Walrus expressions could bind names in the eval namespace; the+ # parser does not even tokenize ":=".+ assert _eval_condition("(x := 1)", {}) is False+++class TestParserInternals:+ """Direct parser tests for tokenizer/precedence regression coverage."""++ def test_tokenizer_handles_whitespace(self) -> None:+ parser = _SigmaConditionParser(" a and b ", {"a": True, "b": True})+ assert parser.parse() is True++ def test_tokenizer_rejects_invalid_characters(self) -> None:+ # Invalid characters must not be silently dropped — the public+ # `_eval_condition` wrapper catches the ValueError and falls back+ # to the safe default, but here we assert the parser itself+ # refuses to accept them.+ with pytest.raises(ValueError):+ _SigmaConditionParser("a $ b", {"a": True, "b": True}).parse()++ def test_unbalanced_parens_safe_fallback(self) -> None:+ # Unbalanced parens fall back to "AND all selections" via the+ # public wrapper rather than blowing up the request.+ assert _eval_condition("(a and b", {"a": True, "b": True}) is True+ assert _eval_condition("(a and b", {"a": True, "b": False}) is False