Skip to content

Commit 6abb623

Browse files
committed
fix(static_yara): keep rule cache, hash and skip count as one entry
_load_rules() set _rules_skipped_count and returned on both non-populating paths -- no rule files found, and compilation yielding nothing -- without replacing or clearing _compiled_rules / _rules_hash. The entry left behind still matched the earlier load's hash, so a later request for it hit the cache and paired those rules with the intervening load's count. Loading A (one valid rule, one rejected), then an empty or all-rejected B, then A again reported zero dropped rules for A, and node() went back to reporting a completed scan while one of A's own detectors had never run. Collapse the three globals into a frozen _RuleCacheEntry holding rules, hash and skip count, published only by replacing the entry wholesale, and clear that entry on every path that does not produce usable rules. A cache hit now takes its count from the entry, so the number cannot come from another load. _rules_skipped_count remains as the transaction-local channel _load_rules uses to publish the count to load_rules_with_skips, and is cleared at the start of the locked transaction so a load that raises cannot leave a previous total readable. _load_rules keeps its single-value signature, so existing monkeypatch.setattr(static_yara, "_load_rules", ...) doubles stay valid, and the reentrant-lock transaction is unchanged. Adds the A->B->A regression over both non-populating paths with asymmetric counts, cache-entry invalidation and immutability checks, and an end-to-end rescan test asserting the dropped rule is still surfaced. Signed-off-by: Souptik Chakraborty <[email protected]>
1 parent 6e07493 commit 6abb623

2 files changed

Lines changed: 223 additions & 38 deletions

File tree

‎src/skillspector/nodes/analyzers/static_yara.py‎

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,35 @@ def _enforce_rule_load_deadline() -> None:
174174
_check_rule_load_budget(budget)
175175

176176

177-
# Module-level cache keyed by a content hash of all rule directories.
178-
#
179-
# These three are one logical value: the compiled rules, the hash they were
180-
# compiled from, and how many files were dropped producing them. They must only
181-
# ever be written or read as a set, under ``_RULES_LOCK`` -- see
182-
# :func:`load_rules_with_skips` for why reading them separately is unsafe.
183-
_compiled_rules: yara.Rules | None = None
184-
_rules_hash: str | None = None
177+
@dataclass(frozen=True, slots=True)
178+
class _RuleCacheEntry:
179+
"""One compiled rule set, the hash it came from, and its own dropped-file count.
180+
181+
Frozen, and only ever published by replacing :data:`_rule_cache` wholesale,
182+
so the three halves cannot drift apart. They used to be three independent
183+
globals, and the non-populating paths of :func:`_load_rules` wrote the skip
184+
count while leaving the compiled rules and their hash in place. A later
185+
request for that stale hash then hit the cache and returned those rules
186+
paired with the intervening load's count -- zero, when the intervening load
187+
found no rule files at all -- so a rule set that had silently dropped a
188+
detector reported a complete scan, which is the false-clean result #554 is
189+
about.
190+
"""
191+
192+
rules: yara.Rules
193+
rules_hash: str
194+
skipped_count: int
195+
196+
197+
# Module-level cache keyed by a content hash of all rule directories. ``None``
198+
# means nothing usable is cached; there is deliberately no way to represent a
199+
# half-populated cache, so every non-populating load path simply clears it.
200+
_rule_cache: _RuleCacheEntry | None = None
201+
202+
# Not cache state: the skip count of whichever load most recently ran, published
203+
# under ``_RULES_LOCK`` so :func:`load_rules_with_skips` can read it inside the
204+
# same transaction that produced it. On a cache hit it is assigned *from the
205+
# cache entry*, so it always describes the rules actually returned.
185206
_rules_skipped_count: int = 0
186207

187208
# Reentrant so the load-and-read transaction in :func:`load_rules_with_skips`
@@ -465,13 +486,27 @@ def _load_rules(extra_dir: Path | None = None) -> yara.Rules | None:
465486
or a scan can report ``completed``/SAFE while some of its own detections
466487
never ran (#554).
467488
489+
A successful load publishes rules, hash and count together as one
490+
:class:`_RuleCacheEntry`, and every path that does not produce usable rules
491+
clears that entry outright. Both halves matter: without the first a cache
492+
hit could answer with another load's count, and without the second the
493+
stale rules would stay reachable under their old hash.
494+
468495
Callers should prefer :func:`load_rules_with_skips`, which returns both
469496
halves as one value; reading the count separately after this returns is
470497
racy across concurrent scans.
471498
"""
472-
global _compiled_rules, _rules_hash, _rules_skipped_count # noqa: PLW0603
499+
global _rule_cache, _rules_skipped_count # noqa: PLW0603
473500

474501
with _RULES_LOCK:
502+
# Cleared up front so that a load which raises part way through cannot
503+
# leave a previous load's total readable through
504+
# :func:`rules_skipped_count`. Every return path below assigns its own.
505+
# ``_rule_cache`` is deliberately *not* cleared here: an entry is
506+
# self-consistent, so on an exception it stays a valid answer for its
507+
# own hash rather than forcing a needless recompile.
508+
_rules_skipped_count = 0
509+
475510
dirs = [_BUILTIN_RULES_DIR]
476511
if extra_dir and extra_dir.is_dir():
477512
dirs.append(extra_dir)
@@ -481,15 +516,20 @@ def _load_rules(extra_dir: Path | None = None) -> yara.Rules | None:
481516
rule_files = _collect_rule_files(*dirs)
482517
if not rule_files:
483518
logger.info("%s: no YARA rule files found", ANALYZER_ID)
484-
_rules_skipped_count = 0
519+
# Non-populating: discard the entry instead of leaving the previous
520+
# rules cached under their old hash. Keeping them would let the next
521+
# request for that hash return them alongside this load's zero.
522+
_rule_cache = None
485523
return None
486524

487525
raw_cache = _read_rule_bytes_cache(rule_files)
488526
current_hash = _content_hash(rule_files, raw_cache)
489-
if _compiled_rules is not None and _rules_hash == current_hash:
490-
# Cache hit: _rules_skipped_count already describes this exact file
491-
# set, because it is only ever written together with _rules_hash.
492-
return _compiled_rules
527+
cached = _rule_cache
528+
if cached is not None and cached.rules_hash == current_hash:
529+
# The count is taken from the entry, so it describes these rules and
530+
# not whichever load happened to run in between.
531+
_rules_skipped_count = cached.skipped_count
532+
return cached.rules
493533

494534
namespace_files: dict[str, str] = {}
495535
sources, materialize_skipped = _build_namespace_map(
@@ -501,10 +541,15 @@ def _load_rules(extra_dir: Path | None = None) -> yara.Rules | None:
501541

502542
if compiled is None:
503543
logger.warning("%s: failed to compile any YARA rules", ANALYZER_ID)
544+
# Non-populating for the same reason as the no-rule-files path above.
545+
_rule_cache = None
504546
return None
505547

506-
_compiled_rules = compiled
507-
_rules_hash = current_hash
548+
_rule_cache = _RuleCacheEntry(
549+
rules=compiled,
550+
rules_hash=current_hash,
551+
skipped_count=skipped,
552+
)
508553
loaded = len(sources) - compile_skipped
509554
logger.info("%s: compiled %d YARA rule file(s) (%d skipped)", ANALYZER_ID, loaded, skipped)
510555
return compiled
@@ -530,11 +575,11 @@ def load_rules_with_skips(extra_dir: Path | None = None) -> tuple[yara.Rules | N
530575

531576

532577
def rules_skipped_count() -> int:
533-
"""Return how many rule files the most recent :func:`_load_rules` call dropped.
578+
"""Return how many rule files the rules from the most recent load dropped.
534579
535-
Zero both when nothing was skipped and when a cache hit meant no reload
536-
ran; a cache hit implies the same file set was already validated by the
537-
load that populated the cache, so nothing new could have been skipped.
580+
On a cache hit this is the cached entry's own count, not zero: the whole
581+
point is that the number travels with the rules it describes, so a rule set
582+
that dropped a detector keeps reporting it on every later cache hit.
538583
539584
Retained for callers that already hold :data:`_RULES_LOCK` or run
540585
single-threaded. Anything reading this straight after :func:`_load_rules`

‎tests/nodes/analyzers/test_static_yara.py‎

Lines changed: 158 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from __future__ import annotations
2323

2424
import base64
25+
import dataclasses
2526
import json
2627
import logging
2728
import threading
@@ -41,16 +42,14 @@
4142
def _clear_rule_cache():
4243
"""Reset the module-level compiled rules cache between tests.
4344
44-
The skip count is part of that cache: it is only meaningful alongside the
45-
hash it was produced from, so leaving it set would leak a previous test's
46-
dropped-rule total into the next one.
45+
The skip count is part of that cache entry: it is only meaningful alongside
46+
the hash it was produced from, so leaving it set would leak a previous
47+
test's dropped-rule total into the next one.
4748
"""
48-
static_yara._compiled_rules = None
49-
static_yara._rules_hash = None
49+
static_yara._rule_cache = None
5050
static_yara._rules_skipped_count = 0
5151
yield
52-
static_yara._compiled_rules = None
53-
static_yara._rules_hash = None
52+
static_yara._rule_cache = None
5453
static_yara._rules_skipped_count = 0
5554

5655

@@ -797,22 +796,22 @@ def test_rules_are_cached(self, tmp_path):
797796
tmp_path, "rule_cache", category="malware", severity="HIGH", strings={"a": "CACHETEST"}
798797
)
799798
_run("CACHETEST", "f.txt", str(tmp_path))
800-
first_rules = static_yara._compiled_rules
799+
first_rules = static_yara._rule_cache.rules
801800
_run("CACHETEST", "f.txt", str(tmp_path))
802-
assert static_yara._compiled_rules is first_rules
801+
assert static_yara._rule_cache.rules is first_rules
803802

804803
def test_cache_invalidated_on_new_rule(self, tmp_path):
805804
_write_rule(
806805
tmp_path, "rule_v1", category="malware", severity="HIGH", strings={"a": "V1MARKER"}
807806
)
808807
_run("V1MARKER", "f.txt", str(tmp_path))
809-
first_hash = static_yara._rules_hash
808+
first_hash = static_yara._rule_cache.rules_hash
810809

811810
_write_rule(
812811
tmp_path, "rule_v2", category="malware", severity="HIGH", strings={"a": "V2MARKER"}
813812
)
814813
_run("V2MARKER", "f.txt", str(tmp_path))
815-
assert static_yara._rules_hash != first_hash
814+
assert static_yara._rule_cache.rules_hash != first_hash
816815

817816

818817
# ── Internal helpers ──────────────────────────────────────────────────
@@ -971,8 +970,7 @@ def test_malformed_rule_is_reported_not_silently_dropped(self, tmp_path, monkeyp
971970
claim would be false, since the broken rule never ran against
972971
anything.
973972
"""
974-
static_yara._compiled_rules = None
975-
static_yara._rules_hash = None
973+
static_yara._rule_cache = None
976974
static_yara._rules_skipped_count = 0
977975
monkeypatch.setattr(static_yara, "_BUILTIN_RULES_DIR", tmp_path / "empty_builtin")
978976
(tmp_path / "empty_builtin").mkdir()
@@ -1393,14 +1391,19 @@ def _isolated_builtin(tmp_path: Path, monkeypatch) -> None:
13931391
monkeypatch.setattr(static_yara, "_BUILTIN_RULES_DIR", builtin)
13941392

13951393
@staticmethod
1396-
def _rule_dir(tmp_path: Path, name: str, *, broken: int) -> Path:
1397-
"""Build a rule dir with one valid rule and ``broken`` uncompilable ones."""
1394+
def _rule_dir(tmp_path: Path, name: str, *, broken: int, good: bool = True) -> Path:
1395+
"""Build a rule dir with ``broken`` uncompilable rules, optionally one valid one.
1396+
1397+
``good=False`` with ``broken=0`` yields an existing but empty directory,
1398+
which is how the "no rule files at all" load path is reached.
1399+
"""
13981400
rules_dir = tmp_path / name
13991401
rules_dir.mkdir(parents=True, exist_ok=True)
14001402
marker = f"MARKER_{name.upper()}"
1401-
(rules_dir / "good.yar").write_text(
1402-
f'rule good_{name} {{ strings: $a = "{marker}" condition: $a }}'
1403-
)
1403+
if good:
1404+
(rules_dir / "good.yar").write_text(
1405+
f'rule good_{name} {{ strings: $a = "{marker}" condition: $a }}'
1406+
)
14041407
for index in range(broken):
14051408
# Missing closing brace: a real YARA syntax error, not a decode failure.
14061409
(rules_dir / f"bad{index}.yar").write_text(
@@ -1433,6 +1436,143 @@ def test_skip_count_travels_with_the_rules_it_describes(self, tmp_path, monkeypa
14331436
# reading the global now would report a clean scan for A.
14341437
assert static_yara.rules_skipped_count() == 0
14351438

1439+
@pytest.mark.parametrize(
1440+
("label", "b_broken", "a_broken"),
1441+
[
1442+
# B finds no rule files at all. This path forced the count to zero,
1443+
# so A's cache hit reported zero dropped rules: a false-complete scan.
1444+
("no_rule_files", 0, 1),
1445+
# B compiles nothing because every one of its rules is rejected.
1446+
# Counts are deliberately asymmetric (A drops 2, B drops 1) so an
1447+
# inherited count is visible rather than coincidentally equal.
1448+
("all_rejected", 1, 2),
1449+
],
1450+
)
1451+
def test_cached_rules_never_report_a_later_loads_skip_count(
1452+
self, tmp_path, monkeypatch, label, b_broken, a_broken
1453+
):
1454+
"""load A -> load a non-populating B -> load A again must still report A's count.
1455+
1456+
``_load_rules`` used to set the skip count and return on both
1457+
non-populating paths without replacing *or* clearing the cached rules
1458+
and hash. The entry left behind still matched A's hash, so the third
1459+
load hit the cache and paired A's rules with B's count -- zero for the
1460+
empty/no-files case -- and a rule set that had dropped a detector went
1461+
back to reporting a complete scan.
1462+
1463+
Deterministic and single-threaded: this is a cache-integrity defect, not
1464+
a race, so it reproduces purely from the order of the three loads.
1465+
"""
1466+
self._isolated_builtin(tmp_path, monkeypatch)
1467+
dir_a = self._rule_dir(tmp_path, f"a_{label}", broken=a_broken)
1468+
dir_b = self._rule_dir(tmp_path, f"b_{label}", broken=b_broken, good=False)
1469+
1470+
rules_a, skipped_a = static_yara.load_rules_with_skips(dir_a)
1471+
assert rules_a is not None
1472+
assert skipped_a == a_broken
1473+
1474+
rules_b, skipped_b = static_yara.load_rules_with_skips(dir_b)
1475+
assert rules_b is None, "B must not produce usable rules in this scenario"
1476+
1477+
rules_a_again, skipped_a_again = static_yara.load_rules_with_skips(dir_a)
1478+
1479+
assert rules_a_again is not None, "A's rules must still be available"
1480+
assert skipped_a_again == a_broken, (
1481+
f"rule set A dropped {a_broken} rule(s) but the reload reported "
1482+
f"{skipped_a_again}, which is B's count ({skipped_b})"
1483+
)
1484+
1485+
@pytest.mark.parametrize(
1486+
("label", "broken", "good"),
1487+
[
1488+
("no_rule_files", 0, False),
1489+
("all_rejected", 1, False),
1490+
],
1491+
)
1492+
def test_non_populating_load_leaves_no_cache_entry(
1493+
self, tmp_path, monkeypatch, label, broken, good
1494+
):
1495+
"""A load that yields no usable rules must not leave a populated cache entry.
1496+
1497+
Covers the invalidation half directly, so a future change that starts
1498+
writing one part of the entry on these paths fails here rather than
1499+
only showing up as a wrong skip count three loads later.
1500+
"""
1501+
self._isolated_builtin(tmp_path, monkeypatch)
1502+
dir_a = self._rule_dir(tmp_path, f"seed_{label}", broken=1)
1503+
assert static_yara._load_rules(dir_a) is not None
1504+
assert static_yara._rule_cache is not None, "the seed load must populate the cache"
1505+
1506+
dir_b = self._rule_dir(tmp_path, f"empty_{label}", broken=broken, good=good)
1507+
1508+
assert static_yara._load_rules(dir_b) is None
1509+
assert static_yara._rule_cache is None, (
1510+
"the previous rules and hash are still cached after a load that "
1511+
"produced nothing, so a later request for that hash can be served "
1512+
"rules paired with this load's count"
1513+
)
1514+
1515+
def test_cache_entry_cannot_be_mutated_in_place(self, tmp_path, monkeypatch):
1516+
"""The three halves are one immutable value, not three fields to edit.
1517+
1518+
Reassigning ``_rule_cache`` wholesale is the only supported way to
1519+
publish a change, which is what makes a cache hit unable to mix one
1520+
load's rules with another's count.
1521+
"""
1522+
self._isolated_builtin(tmp_path, monkeypatch)
1523+
static_yara._load_rules(self._rule_dir(tmp_path, "frozen", broken=1))
1524+
entry = static_yara._rule_cache
1525+
assert entry is not None
1526+
assert entry.skipped_count == 1
1527+
1528+
with pytest.raises(dataclasses.FrozenInstanceError):
1529+
entry.skipped_count = 0
1530+
1531+
def test_rescan_after_a_non_populating_load_still_reports_the_dropped_rule(
1532+
self, tmp_path, monkeypatch
1533+
):
1534+
"""End to end: the A -> B -> A sequence must not resurrect a clean scan.
1535+
1536+
The load-level assertions above pin the count; this pins the user-visible
1537+
consequence the issue is actually about. Before the fix the second scan
1538+
of A reported ``completed`` with no rule-skip event at all, even though
1539+
one of A's own rules had never run.
1540+
"""
1541+
self._isolated_builtin(tmp_path, monkeypatch)
1542+
rules_dir = self._rule_dir(tmp_path, "scan", broken=1)
1543+
empty_dir = self._rule_dir(tmp_path, "empty_scan", broken=0, good=False)
1544+
state = {
1545+
"components": ["skill.md"],
1546+
"file_cache": {"skill.md": "contains MARKER_SCAN"},
1547+
"yara_rules_dir": str(rules_dir),
1548+
}
1549+
1550+
first = static_yara.node(state)
1551+
assert first["analyzer_status_events"][0]["status"] != "completed"
1552+
1553+
static_yara.node(
1554+
{
1555+
"components": ["skill.md"],
1556+
"file_cache": {"skill.md": "nothing to match"},
1557+
"yara_rules_dir": str(empty_dir),
1558+
}
1559+
)
1560+
1561+
second = static_yara.node(state)
1562+
1563+
assert any("good_scan" in finding.message for finding in second["findings"]), (
1564+
"the valid rule must still fire on the rescan"
1565+
)
1566+
assert second["analyzer_status_events"][0]["status"] != "completed", (
1567+
"a rescan served from cache must not claim a complete scan while one "
1568+
"of its own rules is still dropped"
1569+
)
1570+
assert any(
1571+
event.get("reason_code") is LedgerReason.READ_ERROR
1572+
and event.get("observed_artifacts") == 1
1573+
for event in second["inspection_ledger"]
1574+
), "the dropped rule must still be surfaced in the ledger on the rescan"
1575+
14361576
def test_load_and_read_is_serialized_against_other_scans(self, tmp_path, monkeypatch):
14371577
"""The load-and-read pair must be atomic, not merely adjacent.
14381578

0 commit comments

Comments
 (0)