Skip to content

Commit 862eca9

Browse files
author
CometAPI
committed
fix: validate rendered Markdown link targets
1 parent 9599ba7 commit 862eca9

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

scripts/_checks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from typing import cast
1919
from urllib.parse import quote, unquote
2020

21+
from markdown_it import MarkdownIt
22+
2123
if sys.version_info >= (3, 11):
2224
import tomllib
2325
else:
@@ -194,6 +196,18 @@ def _actions_path_has_canonical_url(
194196
return False
195197

196198

199+
def _rendered_markdown_link_targets(text: str) -> list[str]:
200+
targets: list[str] = []
201+
for token in MarkdownIt("commonmark", {"html": True}).parse(text):
202+
for child in token.children or []:
203+
if child.type != "link_open":
204+
continue
205+
target = child.attrGet("href")
206+
if isinstance(target, str):
207+
targets.append(target)
208+
return targets
209+
210+
197211
_WHEEL_DIGEST = re.compile(
198212
r"\bwheel\s+sha256\b[^0-9a-f]{0,96}(?P<digest>[0-9a-f]{64})(?![0-9a-f])",
199213
re.IGNORECASE | re.DOTALL,
@@ -762,6 +776,12 @@ def _canonical_actions_run_violations(
762776
)
763777
for variant in variants
764778
]
779+
malformed = malformed or any(
780+
_ACTIONS_PATH.search(target) is not None
781+
and _CANONICAL_ACTIONS_URL.fullmatch(target) is None
782+
for variant in variants
783+
for target in _rendered_markdown_link_targets(variant)
784+
)
765785
malformed = malformed or any(
766786
count > direct_paths[identity]
767787
for paths in variant_paths[1:]

tests/test_release_documents.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ def test_release_evidence_rejects_obsolete_workflow_reference_marker(
708708
"prefix<https://github.com/cometapi-dev/cometapi-python/actions/runs/30515861246>",
709709
"[evil](mailto:foo](https://github.com/cometapi-dev/cometapi-python/"
710710
"actions/runs/30515861246))",
711+
"[evil](mailto:foo([x](https://github.com/cometapi-dev/cometapi-python/"
712+
"actions/runs/30515861246)))",
711713
"http://github.com/cometapi-dev/cometapi-python/actions/runs/30511373822",
712714
"https://evil.example/?next=https%3A%2F%2Fgithub.com%2Fcometapi-dev%2F"
713715
"cometapi-python%2Factions%2Fruns%2F30511373822",

0 commit comments

Comments
 (0)