From c607a14f8b4ee5a628eab07535dc9f1eaf1ac929 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Thu, 27 Aug 2026 12:16:05 +0300 Subject: [PATCH] fix(markdown): decode a percent-encoded link before checking that it exists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Каждый markdown-рендерер процентно кодирует ссылку на файл с пробелами или не-ASCII в имени. Проверка сравнивала закодированную форму с файловой системой и объявляла отсутствующим файл, который лежит на месте: MARKDOWN_LINK_TARGET_MISSING: GitLab%2017.10.0%20%E2%86%92%20Docker%20Migration.md Ложное срабатывание блокировало `validate` на репозитории Infra/Docker. Ссылка декодируется перед резолвом; тест закрепляет и пробел, и то, что настоящая битая ссылка по-прежнему находится. Co-Authored-By: Claude Opus 5 --- src/stewardship/markdown.py | 7 ++++++- tests/test_adoption_gates.py | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/stewardship/markdown.py b/src/stewardship/markdown.py index fb3ccf1..e90b20f 100644 --- a/src/stewardship/markdown.py +++ b/src/stewardship/markdown.py @@ -10,6 +10,8 @@ from dataclasses import dataclass from pathlib import Path from typing import Any +from urllib.parse import unquote + from .io import ResourceError from .types import Finding @@ -183,7 +185,10 @@ def _check_local_links(repo_root: Path, rel_path: str, text: str) -> list[Findin findings: list[Finding] = [] base_dir = (repo_root / rel_path).parent for match in _LINK_RE.finditer(mask_code(text)): - target = match.group(1) + # A link to a name with spaces or non-ASCII is percent-encoded by every + # Markdown renderer that produces one. Comparing the encoded form against + # the filesystem reports a file that is plainly there as missing. + target = unquote(match.group(1)) if target.startswith(("http://", "https://", "mailto:", "#")): continue target_path_part = target.split("#", 1)[0] diff --git a/tests/test_adoption_gates.py b/tests/test_adoption_gates.py index 6b5a148..b8189d7 100644 --- a/tests/test_adoption_gates.py +++ b/tests/test_adoption_gates.py @@ -316,6 +316,15 @@ def test_a_real_broken_link_is_still_reported(tmp_path: Path) -> None: assert codes == ["MARKDOWN_LINK_TARGET_MISSING"] +def test_percent_encoded_link_resolves_to_the_real_file(tmp_path) -> None: + """Renderers percent-encode spaces and non-ASCII; the file is still there.""" + from stewardship.markdown import check_markdown_file + + (tmp_path / "GitLab 17.10.0 Migration.md").write_text("# M" + chr(10), encoding="utf-8") + link = "See [m](GitLab%2017.10.0%20Migration.md)." + (tmp_path / "CLAUDE.md").write_text("# Doc" + chr(10) * 2 + link + chr(10), encoding="utf-8") + assert check_markdown_file(tmp_path, "CLAUDE.md") == [] + # --- path confinement: "." is a whole path, never a segment ------------------ def test_repository_root_is_a_legal_content_root_path() -> None: