From 4c1aae2374902df144ef0b73ddc39653e69dcfab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=80=D1=83=D0=B1=D0=B8=D0=BD=20=D0=A4=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D0=BF=D0=BF?= <289370046+zarubinphil@users.noreply.github.com> Date: Fri, 21 Aug 2026 08:50:57 +0300 Subject: [PATCH] stage9: close avtor-codex wave six guards --- scripts/instruction_guard.py | 16 ++++-- scripts/pd_guard.py | 98 ++++++++++++++++++++++++++---------- scripts/pii_gate.py | 9 ++-- scripts/themis_bot.py | 19 ++++++- 4 files changed, 106 insertions(+), 36 deletions(-) diff --git a/scripts/instruction_guard.py b/scripts/instruction_guard.py index 8d3c3f5..31c92a9 100644 --- a/scripts/instruction_guard.py +++ b/scripts/instruction_guard.py @@ -30,6 +30,7 @@ import argparse import re import sys +import unicodedata _HOMOGLYPH_SOURCE = { "A": "А", "B": "В", "C": "С", "E": "Е", "H": "Н", "K": "К", "M": "М", @@ -104,7 +105,8 @@ _STOCK_PHRASES = ( # прилагательных пропускал «Забудь предыдущие инструкции» и «Игнорируй # прошлые инструкции» — минимальные варианты собственных стоковых фраз. re.compile(r"\b(?:игнориру(?:й|йте|ировать)|забудь|забудьте|отмени|отмените|" - r"проигнориру(?:й|йте)|не\s+учитыва(?:й|йте))\s+" + r"проигнориру(?:й|йте)|не\s+учитыва(?:й|йте))" + r"(?:\s+|[,—-]\s*)+(?:пожалуйста\s*,?\s+)?" r"(?:[а-яё]+\s+){0,3}" r"(?:инструкц\w*|указан\w*|правил\w*|сообщени\w*|протокол\w*|" r"предписан\w*|ограничен\w*)", re.I), @@ -112,13 +114,13 @@ _STOCK_PHRASES = ( r"свои)\s+(?:инструкц\w*|правил\w*)", re.I), re.compile(r"\bс\s+этого\s+момента\s+ты\s+(?:—|-|являешься|действуешь)", re.I), re.compile(r"\bтвоя\s+новая\s+роль\b", re.I), - re.compile(r"\bignore\s+(?:all\s+)?(?:previous|prior|above)\s+instructions?\b", re.I), - re.compile(r"\bdisregard\s+(?:all\s+)?(?:previous|prior|above)\s+instructions?\b", re.I), + re.compile(r"\bignore\s+(?:all\s+)?(?:the\s+)?(?:previous|prior|above)\s+instructions?\b", re.I), + re.compile(r"\bdisregard\s+(?:all\s+)?(?:the\s+)?(?:previous|prior|above)\s+instructions?\b", re.I), re.compile(r"\bnew\s+instructions?\s*[:—-]", re.I), re.compile(r"\breveal\s+your\s+(?:prompt|system\s+prompt|instructions)\b", re.I), re.compile(r"\byou\s+are\s+now\b", re.I), re.compile(r"\byour\s+new\s+task\s+is\b", re.I), - re.compile(r"^\s*system\s*:", re.I | re.M), + re.compile(r"^\s*system\s*(?:prompt\s*)?:", re.I | re.M), re.compile(r"^\s*(?:новые\s+инструкции|важно\s+для\s+ии)\s*[:—-]", re.I | re.M), re.compile(r"\bignoriruy\s+(?:vse\s+)?(?:predydushchie|proshlye)\s+instruktsii\b", re.I), re.compile(r"\bignore\s+previous\s+instructions?\b", re.I), @@ -134,6 +136,12 @@ def normalize_text(text: str) -> str: text = repaired except UnicodeError: pass + text = re.sub(r"\b(?:alt|title|aria-label)\s*=\s*(['\"])(.*?)\1", + lambda m: " " + m.group(2) + " ", text, flags=re.I | re.S) + text = text.replace("", " ") + text = re.sub(r"<[^>]+>", " ", text) + text = re.sub(r"(?m)^[ \t]+", "", text) + text = "".join(ch for ch in text if unicodedata.category(ch) != "Cf") text = text.replace("\u00ad", "") lat = "".join(re.escape(ch) for ch in _HOMOGLYPH_SOURCE) text = re.sub( diff --git a/scripts/pd_guard.py b/scripts/pd_guard.py index d8da62d..8ce5391 100644 --- a/scripts/pd_guard.py +++ b/scripts/pd_guard.py @@ -61,12 +61,23 @@ _CYR_TO_LAT_CONFUSABLES = str.maketrans({ "Р": "P", "С": "C", "Т": "T", "У": "Y", "Х": "X", "а": "a", "е": "e", "о": "o", "р": "p", "с": "c", "у": "y", "х": "x", }) +_LAT_TO_CYR_CONFUSABLES = str.maketrans({ + "A": "А", "B": "В", "E": "Е", "K": "К", "M": "М", "H": "Н", "O": "О", + "P": "Р", "C": "С", "T": "Т", "Y": "У", "X": "Х", + "a": "а", "e": "е", "o": "о", "p": "р", "c": "с", "y": "у", "x": "х", +}) def normalize_public_scan(text: str) -> str: """Для публичных каналов: NFKC, без невидимых знаков, mixed-script homograph.""" text = unicodedata.normalize("NFKC", text) text = "".join(ch for ch in text if unicodedata.category(ch) != "Cf") + lat = "".join(re.escape(chr(ch)) for ch in _LAT_TO_CYR_CONFUSABLES) + text = re.sub( + rf"(?<=[А-Яа-яЁё])[{lat}]+|[{lat}]+(?=[А-Яа-яЁё])", + lambda m: m.group(0).translate(_LAT_TO_CYR_CONFUSABLES), + text, + ) cyr = "".join(re.escape(chr(ch)) for ch in _CYR_TO_LAT_CONFUSABLES) text = re.sub( rf"(?<=[A-Za-z])[{cyr}]+|[{cyr}]+(?=[A-Za-z])", @@ -274,40 +285,75 @@ def scan_pii(text: str, where: str) -> list[str]: def git(*args: str) -> str: - r = subprocess.run(["git", *args], capture_output=True, cwd=ROOT) + r = subprocess.run(["git", "-c", "core.quotepath=false", *args], + capture_output=True, cwd=ROOT) return r.stdout.decode("utf-8", "replace") if r.returncode == 0 else "" def git_bytes(*args: str) -> bytes: """Сырой блоб без декодирования: .docx это zip, декодировать его в utf-8 значит потерять содержимое до проверки.""" - r = subprocess.run(["git", *args], capture_output=True, cwd=ROOT) + r = subprocess.run(["git", "-c", "core.quotepath=false", *args], + capture_output=True, cwd=ROOT) return r.stdout if r.returncode == 0 else b"" +def git_z(*args: str) -> list[str]: + r = subprocess.run(["git", "-c", "core.quotepath=false", *args], + capture_output=True, cwd=ROOT) + if r.returncode != 0: + return [] + return [p.decode("utf-8", "replace") for p in r.stdout.split(b"\0") if p] + + +def git_blob(spec: str) -> bytes | None: + r = subprocess.run(["git", "-c", "core.quotepath=false", "cat-file", "blob", spec], + capture_output=True, cwd=ROOT) + return r.stdout if r.returncode == 0 else None + + # Office/ODT-форматы — это zip с XML внутри, а не двоичная непрозрачность. Судебные # документы именно .docx: фамилия доверителя лежит в word/document.xml, и без # распаковки сторож объявляет такой коммит чистым (ложная уверенность хуже молчания). OFFICE_ZIP_EXT = (".docx", ".xlsx", ".pptx", ".odt") +_ZIP_TEXT_EXT = ( + ".xml", ".rels", ".txt", ".md", ".html", ".htm", ".xhtml", ".csv", ".json", + ".opf", ".ncx", +) -def _office_xml_text(blob: bytes) -> str: - """Видимый текст из office-zip: снимаем zip и XML-разметку, чтобы фамилия - внутри стала обычной строкой для scan_text/scan_pii. Не zip — пусто.""" +def _office_xml_text(blob: bytes, depth: int = 0) -> str: + """Видимый текст из zip-контейнера: решаем по содержимому, не по расширению.""" + if depth > 2 or len(blob) > 20 * 1024 * 1024: + return "" try: zf = zipfile.ZipFile(io.BytesIO(blob)) except (zipfile.BadZipFile, OSError): return "" parts = [] for name in zf.namelist(): - if not name.endswith(".xml"): + low = name.lower() + if name.endswith("/") or "__macosx/" in low: continue try: - raw = zf.read(name).decode("utf-8", "replace") + info = zf.getinfo(name) + if info.file_size > 5 * 1024 * 1024: + continue + data = zf.read(name) except (OSError, KeyError, RuntimeError): continue + if data.startswith(b"PK\x03\x04"): + nested = _office_xml_text(data, depth + 1) + if nested: + parts.append(nested) + continue + if not low.endswith(_ZIP_TEXT_EXT) and b"\0" in data[:512]: + continue + raw = data.decode("utf-8", "replace") raw = re.sub(r"", "\n", raw) raw = re.sub(r"", "\t", raw) + raw = re.sub(r"]*>", "\n", raw, + flags=re.I) parts.append(html.unescape(re.sub(r"<[^>]+>", "", raw))) return "\n".join(parts) @@ -324,9 +370,10 @@ def _pdf_text(blob: bytes) -> str: def _visible_blob_text(path: str, blob: bytes) -> str: + zipped = _office_xml_text(blob) + if zipped: + return zipped low = path.lower() - if low.endswith(OFFICE_ZIP_EXT): - return _office_xml_text(blob) if low.endswith(".pdf"): return _pdf_text(blob) return blob.decode("utf-8", "replace") @@ -343,7 +390,7 @@ def _scan_file_text(path: str, blob: bytes, pat: re.Pattern | None) -> list[str] def staged_files() -> list[str]: - return [f for f in git("diff", "--cached", "--name-only", "--diff-filter=ACMRT").split("\n") if f] + return git_z("diff", "--cached", "--name-only", "-z", "--diff-filter=ACMRT") def _is_test_fixture_code(path: str) -> bool: @@ -361,17 +408,12 @@ def check_staged(pat: re.Pattern | None) -> list[str]: problems = [] for f in staged_files(): problems += scan_text(f, pat, "путь файла") - blob = git_bytes("show", f":{f}") - if not blob: + blob = git_blob(f":{f}") + if blob is None: + problems.append(f"{f}:0 — файл есть в индексе, но blob не прочитан. " + "Fail-closed: проверка не может считать его чистым") continue hits = _scan_file_text(f, blob, pat) - if hits and f.lower().endswith(OFFICE_ZIP_EXT): - # Отклонённый бинарный судебный документ снимается с индекса: - # заблокированный коммит иначе оставляет .docx в staged, и каждый - # следующий коммит наследует ту же блокировку. Снятие с индекса - # (файл на диске цел) даёт работать с остальным. - subprocess.run(["git", "reset", "-q", "--", f], cwd=ROOT, - capture_output=True) problems += hits return problems @@ -439,8 +481,8 @@ def _push_files(local_sha: str, remote_sha: str) -> list[str]: if remote_sha and not _ZERO_SHA_RE.match(remote_sha): base = _commit_for_object(remote_sha) if base: - return [f for f in git("diff", "--name-only", f"{base}..{commit}").splitlines() if f] - return [f for f in git("ls-tree", "-r", "--name-only", commit).splitlines() if f] + return git_z("diff", "--name-only", "-z", f"{base}..{commit}") + return git_z("ls-tree", "-r", "-z", "--name-only", commit) def check_push_content(pat: re.Pattern | None, local_sha: str, remote_sha: str) -> list[str]: @@ -449,20 +491,22 @@ def check_push_content(pat: re.Pattern | None, local_sha: str, remote_sha: str) if not commit: return [] problems = [] + meta_pat = name_pattern(client_names(), cyrillic=True) or pat + problems += scan_text(git("cat-file", "-p", commit), meta_pat, "метаданные коммита") for f in _push_files(local_sha, remote_sha): problems += scan_text(f, pat, "путь файла") - blob = git_bytes("show", f"{commit}:{f}") - if blob: + blob = git_blob(f"{commit}:{f}") + if blob is None: + problems.append(f"{f}:0 — blob уходящего коммита не прочитан. " + "Fail-closed: pre-push не считает его чистым") + else: problems += _scan_file_text(f, blob, pat) return problems def check_tree(pat: re.Pattern | None) -> list[str]: problems = [] - for f in [x for x in git("ls-files").split("\n") if x]: - if f == "knowledge/lessons-log.md": - # Исторический журнал уроков уже в дереве; новые правки держат staged/push. - continue + for f in git_z("ls-files", "-z"): path = os.path.join(ROOT, f) if not os.path.isfile(path): continue diff --git a/scripts/pii_gate.py b/scripts/pii_gate.py index 6b488a2..de5e33d 100644 --- a/scripts/pii_gate.py +++ b/scripts/pii_gate.py @@ -74,7 +74,7 @@ FIO_CAPS_RE = re.compile( r"[А-ЯЁ]*(?:ОВИЧ|ЕВИЧ|ЬЕВИЧ|ОВН|ЕВН|ЬЕВН|ИНИЧН)[А-ЯЁ]{0,2}\b" ) # Фамилия + инициалы: «Иванов И.И.» — вторая частая форма в судебных документах. -FIO_INITIALS_RE = re.compile(r"\b[А-ЯЁ][а-яё]+\s+[А-ЯЁ]\.\s?[А-ЯЁ]\.") +FIO_INITIALS_RE = re.compile(r"\b[А-ЯЁ][А-ЯЁа-яё]+\s+[А-ЯЁ]\.\s?[А-ЯЁ]\.") # Имя + отчество без фамилии: обиход расписок и переписки («Марии Петровне # переданы документы»). Отчество — якорь, чтобы не ловить две обычные заглавные # лексемы подряд. @@ -263,8 +263,11 @@ ROLE_FULLNAME_RE = re.compile( # Ловим по славянским суффиксам транслитерации — обычное английское слово на # «-ova/-enko/-skiy» практически не встречается, поэтому ложная тревога мала. TRANSLIT_FIO_RE = re.compile( - r"\b[A-Z][a-z]+(?:ova|eva|ina|enko|skiy|sky|skaya|ov|ev|in|uk|yuk)\b" - r"(?:\s+[A-Z][a-z]+){0,2}") + r"(?