|
22 | 22 | from __future__ import annotations |
23 | 23 |
|
24 | 24 | import base64 |
| 25 | +import dataclasses |
25 | 26 | import json |
26 | 27 | import logging |
27 | 28 | import threading |
|
41 | 42 | def _clear_rule_cache(): |
42 | 43 | """Reset the module-level compiled rules cache between tests. |
43 | 44 |
|
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. |
47 | 48 | """ |
48 | | - static_yara._compiled_rules = None |
49 | | - static_yara._rules_hash = None |
| 49 | + static_yara._rule_cache = None |
50 | 50 | static_yara._rules_skipped_count = 0 |
51 | 51 | yield |
52 | | - static_yara._compiled_rules = None |
53 | | - static_yara._rules_hash = None |
| 52 | + static_yara._rule_cache = None |
54 | 53 | static_yara._rules_skipped_count = 0 |
55 | 54 |
|
56 | 55 |
|
@@ -797,22 +796,22 @@ def test_rules_are_cached(self, tmp_path): |
797 | 796 | tmp_path, "rule_cache", category="malware", severity="HIGH", strings={"a": "CACHETEST"} |
798 | 797 | ) |
799 | 798 | _run("CACHETEST", "f.txt", str(tmp_path)) |
800 | | - first_rules = static_yara._compiled_rules |
| 799 | + first_rules = static_yara._rule_cache.rules |
801 | 800 | _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 |
803 | 802 |
|
804 | 803 | def test_cache_invalidated_on_new_rule(self, tmp_path): |
805 | 804 | _write_rule( |
806 | 805 | tmp_path, "rule_v1", category="malware", severity="HIGH", strings={"a": "V1MARKER"} |
807 | 806 | ) |
808 | 807 | _run("V1MARKER", "f.txt", str(tmp_path)) |
809 | | - first_hash = static_yara._rules_hash |
| 808 | + first_hash = static_yara._rule_cache.rules_hash |
810 | 809 |
|
811 | 810 | _write_rule( |
812 | 811 | tmp_path, "rule_v2", category="malware", severity="HIGH", strings={"a": "V2MARKER"} |
813 | 812 | ) |
814 | 813 | _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 |
816 | 815 |
|
817 | 816 |
|
818 | 817 | # ── Internal helpers ────────────────────────────────────────────────── |
@@ -971,8 +970,7 @@ def test_malformed_rule_is_reported_not_silently_dropped(self, tmp_path, monkeyp |
971 | 970 | claim would be false, since the broken rule never ran against |
972 | 971 | anything. |
973 | 972 | """ |
974 | | - static_yara._compiled_rules = None |
975 | | - static_yara._rules_hash = None |
| 973 | + static_yara._rule_cache = None |
976 | 974 | static_yara._rules_skipped_count = 0 |
977 | 975 | monkeypatch.setattr(static_yara, "_BUILTIN_RULES_DIR", tmp_path / "empty_builtin") |
978 | 976 | (tmp_path / "empty_builtin").mkdir() |
@@ -1393,14 +1391,19 @@ def _isolated_builtin(tmp_path: Path, monkeypatch) -> None: |
1393 | 1391 | monkeypatch.setattr(static_yara, "_BUILTIN_RULES_DIR", builtin) |
1394 | 1392 |
|
1395 | 1393 | @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 | + """ |
1398 | 1400 | rules_dir = tmp_path / name |
1399 | 1401 | rules_dir.mkdir(parents=True, exist_ok=True) |
1400 | 1402 | 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 | + ) |
1404 | 1407 | for index in range(broken): |
1405 | 1408 | # Missing closing brace: a real YARA syntax error, not a decode failure. |
1406 | 1409 | (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 |
1433 | 1436 | # reading the global now would report a clean scan for A. |
1434 | 1437 | assert static_yara.rules_skipped_count() == 0 |
1435 | 1438 |
|
| 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 | + |
1436 | 1576 | def test_load_and_read_is_serialized_against_other_scans(self, tmp_path, monkeypatch): |
1437 | 1577 | """The load-and-read pair must be atomic, not merely adjacent. |
1438 | 1578 |
|
|
0 commit comments