feat: build wahrwelt A/D toolkit

This commit is contained in:
TakuyaYagam1 committed 2026-10-14 11:30:41 +03:00
1 parent 303438d206
commit 653924d8c6
564 files changed
+36310 -49998

No files matched your search

+53 -193
View File
@@ -1,117 +1,50 @@
"""Keep the offline rule pack and the human review guide navigable."""
"""Связность каталога правил и локальной документации."""
import json
from pathlib import Path
import re
import shlex
import shutil
import subprocess
import tempfile
import unittest
from urllib.parse import unquote, urlsplit
ROOT = Path(__file__).resolve().parents[1]
CATALOG = ROOT / "docs" / "vulnerabilities"
FIELDS = ("**Поиск:**", "**Уязвимый вариант:**", "**Исправление:**", "**Проверка:**")
class CatalogTests(unittest.TestCase):
def test_explicit_detection_contracts_match_the_linked_section(self):
rules = json.loads((ROOT / "rules" / "defense.json").read_text(encoding="utf-8"))["rules"]
for rule in rules:
if not rule.get("detection"):
continue
with self.subTest(rule=rule["id"]):
path, anchor = rule["doc"].split("#", 1)
text = (ROOT / path).read_text(encoding="utf-8")
section = text.split(f'<a id="{anchor}"></a>', 1)[1].split("\n<a id=", 1)[0]
self.assertIn(rule["detection"], section)
@classmethod
def setUpClass(cls):
cls.rules = json.loads((ROOT / "rules" / "defense.json").read_text(
encoding="utf-8"))["rules"]
def test_readme_and_index_counts_match_real_rules_and_sections(self):
rule_ids = {rule["id"] for rule in json.loads(
(ROOT / "rules" / "defense.json").read_text(encoding="utf-8"))["rules"]}
sections = {}
for path in CATALOG.glob("*.md"):
if path.name != "README.md" and not path.stem.endswith("-RU"):
sections[path.resolve()] = re.findall(r"^## ([A-Z][A-Z0-9-]+)\. ",
path.read_text(encoding="utf-8"), re.M)
total_sections = sum(len(ids) for ids in sections.values())
for path in (ROOT / "README.md", CATALOG / "README.md"):
with self.subTest(path=path):
text = path.read_text(encoding="utf-8")
summary = re.search(r"\*\*(\d+) sections, (\d+) scanner rules", text)
self.assertIsNotNone(summary)
self.assertEqual(tuple(map(int, summary.groups())), (total_sections, len(rule_ids)))
rows = re.findall(r"^\| \[[^\]]+\]\(([^)]+\.md)\) \| (\d+) \|$", text, re.M)
table = {(path.parent / target).resolve(): int(count) for target, count in rows}
self.assertEqual(set(table), set(sections))
for target, count in table.items():
self.assertEqual(count, len(sections[target]), target)
index = (CATALOG / "README.md").read_text(encoding="utf-8")
coverage = re.search(r"(\d+) automatic signals and (\d+) manual-only sections", index)
self.assertIsNotNone(coverage)
manual_count = sum(rid not in rule_ids for ids in sections.values() for rid in ids)
self.assertEqual(tuple(map(int, coverage.groups())), (len(rule_ids), manual_count))
rules = json.loads((ROOT / "rules/defense.json").read_text(encoding="utf-8"))["rules"]
records = sum(len(json.loads((ROOT / "tests/fixtures" / name).read_text(encoding="utf-8"))["cases"])
for name in ("defense_variants.json", "defense_expansion.json", "defense_boundaries.json"))
development = (ROOT / "docs/development.md").read_text(encoding="utf-8")
inventory = re.search(r"Current inventory: (\d+) rules, (\d+) rule-language pairs and (\d+) independent fixture records", development)
self.assertIsNotNone(inventory)
self.assertEqual(tuple(map(int, inventory.groups())),
(len(rules), sum(len(rule["languages"]) for rule in rules), records))
def test_rule_links_reach_matching_sections(self):
rules = json.loads((ROOT / "rules" / "defense.json").read_text(encoding="utf-8"))["rules"]
for rule in rules:
def test_rule_links_reach_their_catalog_sections(self):
for rule in self.rules:
with self.subTest(rule=rule["id"]):
path, anchor = rule["doc"].split("#", 1)
self.assertTrue(path.startswith("docs/vulnerabilities/"))
text = (ROOT / path).read_text(encoding="utf-8")
self.assertIn(f'<a id="{anchor}"></a>', text)
self.assertIn(f"## {rule['id']}. ", text)
self.assertIn(f"rule {rule['id']}.", text)
source = (ROOT / path).read_text(encoding="utf-8")
self.assertIn(f'<a id="{anchor}"></a>', source)
self.assertIn(f"## {rule['id']}. ", source)
def test_every_section_has_search_fix_and_verification(self):
ids = set()
def test_each_catalog_section_has_russian_review_fields(self):
seen = set()
for path in CATALOG.glob("*.md"):
if path.name == "README.md" or path.stem.endswith("-RU"):
if path.name == "README.md":
continue
text = path.read_text(encoding="utf-8")
source = path.read_text(encoding="utf-8")
sections = re.findall(
r'^## ([A-Z][A-Z0-9-]+)\. ([\s\S]*?)(?=^<a id=|\Z)', text, re.M)
r"^## ([A-Z][A-Z0-9-]+)\. ([\s\S]*?)(?=^<a id=|\Z)", source, re.M)
self.assertTrue(sections, path)
for rid, body in sections:
with self.subTest(rule=rid):
self.assertNotIn(rid, ids)
ids.add(rid)
for field in ("**Search:**", "**Vulnerable example:**",
"**Fix:**", "**Verification:**"):
for rule_id, body in sections:
with self.subTest(rule=rule_id):
self.assertNotIn(rule_id, seen)
seen.add(rule_id)
for field in FIELDS:
self.assertIn(field, body)
if "no separate scanner rule" in body:
self.assertIn("rg -n ", body)
self.assertRegex(body, r"~~~\n\n\S")
self.assertGreater(len(ids), 100)
@unittest.skipUnless(shutil.which("rg"), "manual search validation requires ripgrep")
def test_manual_searches_are_valid_read_only_rg_commands(self):
with tempfile.TemporaryDirectory() as tmp:
# Only run documented rg argv, on an inert empty fixture.
Path(tmp, "empty.txt").write_text("")
for path in CATALOG.glob("*.md"):
for command in re.findall(r"^rg -n .+$", path.read_text(encoding="utf-8"), re.M):
argv = shlex.split(command)
self.assertEqual(argv[0], "rg")
self.assertIn("-e", argv)
self.assertEqual(argv[-1], ".")
self.assertNotIn("--pre", argv)
proc = subprocess.run(argv, cwd=tmp, capture_output=True, timeout=2)
self.assertIn(proc.returncode, (0, 1), (path, command, proc.stderr))
def test_local_markdown_links_exist(self):
paths = [*ROOT.glob("*.md")]
for directory in ("docs", "lab", "integration", "deployment", ".github"):
paths.extend((ROOT / directory).rglob("*.md"))
for path in paths:
source = self._without_fences(path.read_text(encoding="utf-8"))
def test_local_markdown_links_and_anchors_resolve(self):
for path in self.markdown_paths():
source = self.without_fences(path.read_text(encoding="utf-8"))
targets = re.findall(r"\]\(([^)]+)\)", source)
targets.extend(re.findall(r"^\s*\[[^\]]+\]:\s*(\S+)", source, re.M))
for raw_target in targets:
@@ -119,117 +52,44 @@ class CatalogTests(unittest.TestCase):
parsed = urlsplit(target)
if parsed.scheme or parsed.netloc:
continue
file_part = unquote(parsed.path)
anchor = unquote(parsed.fragment)
file_part, anchor = unquote(parsed.path), unquote(parsed.fragment)
dest = ((ROOT / file_part.lstrip("/")) if file_part.startswith("/")
else (path.parent / file_part) if file_part else path).resolve()
self.assertTrue(dest.is_relative_to(ROOT), (path, target))
self.assertTrue(dest.is_file(), (path, target))
if anchor and dest.suffix.lower() == ".md":
self.assertIn(anchor, self._anchors(dest.read_text(encoding="utf-8")),
(path, target))
with self.subTest(source=path.relative_to(ROOT), link=target):
self.assertTrue(dest.is_relative_to(ROOT))
self.assertTrue(dest.is_file())
if anchor and dest.suffix.lower() == ".md":
self.assertIn(anchor, self.anchors(dest.read_text(encoding="utf-8")))
def test_retired_operator_entry_points_are_removed(self):
for relative in (
"deployment/doctor.py", "integration/stack_smoke.py", "lab/health_service.py",
"scripts/defense_ops.py", "scripts/common.py",
):
self.assertFalse((ROOT / relative).exists(), relative)
@staticmethod
def _without_fences(text):
return re.sub(r"(?ms)^(`{3,}|~{3,})[^\n]*\n.*?^\1\s*$", "", text)
def markdown_paths():
return [*ROOT.glob("*.md"), * (ROOT / "docs").rglob("*.md")]
@staticmethod
def without_fences(source):
return re.sub(r"(?ms)^(`{3,}|~{3,})[^\n]*\n.*?^\1\s*$", "", source)
@classmethod
def _anchors(cls, text):
text = cls._without_fences(text)
anchors = set(re.findall(r'<(?:a|h[1-6])\s+[^>]*id=[\"\']([^\"\']+)', text))
seen = {}
for heading in re.findall(r"^#{1,6}\s+(.+?)(?:\s+#+)?$", text, re.M):
heading = re.sub(r"\[([^\]]+)\]\([^)]*\)", r"\1", heading)
def anchors(cls, source):
source = cls.without_fences(source)
anchors = set(re.findall(r'<(?:a|h[1-6])\s+[^>]*id=[\"\']([^\"\']+)', source))
occurrences = {}
for heading in re.findall(r"^#{1,6}\s+(.+?)(?:\s+#+)?$", source, re.M):
heading = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", heading)
heading = re.sub(r"<[^>]*>", "", heading).lower()
slug = re.sub(r"[^\w\- ]", "", heading).replace(" ", "-")
occurrence = seen.get(slug, 0)
seen[slug] = occurrence + 1
anchors.add(slug if not occurrence else f"{slug}-{occurrence}")
count = occurrences.get(slug, 0)
occurrences[slug] = count + 1
anchors.add(slug if not count else f"{slug}-{count}")
return anchors
@staticmethod
def _markdown_paths():
paths = [*ROOT.glob("*.md")]
for directory in ("docs", "lab", "integration", "deployment", ".github"):
paths.extend((ROOT / directory).rglob("*.md"))
return paths
def test_primary_markdown_prose_is_english(self):
for path in self._markdown_paths():
if path.stem.endswith("-RU"):
continue
prose = self._without_fences(path.read_text(encoding="utf-8"))
# The language switch is the only Russian text in an English page.
prose = re.sub(r"^English \| \[Русский\]\([^\n]+\)\s*$", "", prose, flags=re.M)
self.assertNotRegex(prose, r"[\u0400-\u04ff]", str(path.relative_to(ROOT)))
def test_every_markdown_page_has_a_russian_pair_and_language_switch(self):
paths = self._markdown_paths()
for path in paths:
with self.subTest(path=path.relative_to(ROOT)):
if path.stem.endswith("-RU"):
source = path.with_name(path.stem[:-3] + ".md")
self.assertTrue(source.is_file(), "orphan Russian translation")
continue
translated = path.with_name(path.stem + "-RU.md")
self.assertTrue(translated.is_file(), "missing Russian translation")
en = path.read_text(encoding="utf-8")
ru = translated.read_text(encoding="utf-8")
self.assertTrue(f"English | [Русский]({translated.name})" in en, "missing English-to-Russian switch")
self.assertTrue(f"[English]({path.name}) | Русский" in ru, "missing Russian-to-English switch")
self.assertRegex(self._without_fences(ru), r"[А-Яа-яЁё]")
def test_russian_pairs_preserve_executable_examples_and_navigation(self):
fences = re.compile(r"(?ms)^(`{3,}|~{3,})[^\n]*\n.*?^\1[ \t]*$")
for path in self._markdown_paths():
if path.stem.endswith("-RU"):
continue
with self.subTest(path=path.relative_to(ROOT)):
translated = path.with_name(path.stem + "-RU.md")
en = path.read_text(encoding="utf-8")
ru = translated.read_text(encoding="utf-8")
self.assertEqual([m[0] for m in fences.finditer(en)],
[m[0] for m in fences.finditer(ru)],
"commands and code examples must not change in translation")
self.assertTrue(self._anchors(en) <= self._anchors(ru),
"preserve original section anchors in the translation")
for target in re.findall(r"\]\(([^)]+)\)", self._without_fences(ru)):
parsed = urlsplit(target)
if parsed.scheme or parsed.netloc or not parsed.path.endswith(".md"):
continue
dest = (translated.parent / unquote(parsed.path)).resolve()
self.assertTrue(dest == path.resolve() or dest.stem.endswith("-RU"),
"Russian navigation must stay in Russian except the English switch")
def test_russian_catalog_keeps_all_sections_and_review_fields(self):
for path in CATALOG.glob("*-RU.md"):
if path.name == "README-RU.md":
continue
with self.subTest(path=path.name):
en = path.with_name(path.stem[:-3] + ".md").read_text(encoding="utf-8")
ru = path.read_text(encoding="utf-8")
pattern = r"^## ([A-Z][A-Z0-9-]+)\. "
self.assertEqual(re.findall(pattern, en, re.M), re.findall(pattern, ru, re.M))
sections = re.findall(r"^## ([A-Z][A-Z0-9-]+)\. ([\s\S]*?)(?=^## |\Z)", ru, re.M)
for rid, body in sections:
for field in ("**Поиск:**", "**Уязвимый вариант:**", "**Исправление:**", "**Проверка:**"):
self.assertIn(field, body, rid)
def test_russian_catalog_indexes_keep_canonical_counts(self):
for path in (ROOT / "README.md", CATALOG / "README.md"):
with self.subTest(path=path.relative_to(ROOT)):
en = path.read_text(encoding="utf-8")
ru = path.with_name("README-RU.md").read_text(encoding="utf-8")
canonical = re.search(r"\*\*(\d+) sections, (\d+) scanner rules", en)
translated = re.search(r"\*\*(\d+) раздел[а-я]+, (\d+) правил[а-я ]*сканера", ru)
self.assertIsNotNone(translated)
self.assertEqual(canonical.groups(), translated.groups())
row = r"^\| \[[^\]]+\]\(([^)]+\.md)\) \| (\d+) \|$"
english_rows = re.findall(row, en, re.M)
russian_rows = [(target.replace("-RU.md", ".md"), count)
for target, count in re.findall(row, ru, re.M)]
self.assertEqual(english_rows, russian_rows)
if __name__ == "__main__":
unittest.main()