Skip to content

Commit 48f3b8e

Browse files
cliffhallclaude
andcommitted
fix(fetch): scan the first line in linear time and escape the heading
Copilot round 2 on modelcontextprotocol#5398: the link-destination and tag regexes rescanned an unterminated line quadratically, stripped tag-like text inside code spans, and the title was emitted unescaped, so '<T>' became a tag and '__init__' emphasis. Read the visible text in one pass that keeps code spans, and backslash-escape Markdown and HTML specials in the heading. Issue: modelcontextprotocol#2472 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 41f28a9 commit 48f3b8e

2 files changed

Lines changed: 69 additions & 6 deletions

File tree

‎src/mcp_server_fetch/title.py‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
# escapes, Sphinx's "¶" permalink and other punctuation are ignored, but word
2525
# boundaries are kept, so "Rust" does not match "Trust".
2626
_WORD = re.compile(r"[^\W_]+")
27-
# What a reader does not see on a Markdown line: a link's destination and any
28-
# HTML tag (an autolink's URL included). A title found only there is not shown.
29-
_LINK_DESTINATION = re.compile(r"\]\([^)]*\)")
30-
_HTML_TAG = re.compile(r"<[^>]*>")
27+
# Characters that Markdown or inline HTML would interpret in the emitted
28+
# heading ("<T>" read as a tag, "__init__" as emphasis, "&lt;" as an entity),
29+
# each escaped with a backslash so the heading shows the title as written.
30+
_MARKDOWN_SPECIAL = re.compile(r"([\\`*_\[\]<>#&|~])")
3131

3232

3333
def _normalized(text: str) -> str:
@@ -40,7 +40,36 @@ def _normalized(text: str) -> str:
4040

4141

4242
def _visible(line: str) -> str:
43-
return _HTML_TAG.sub(" ", _LINK_DESTINATION.sub("]", line))
43+
"""The text a reader sees on one Markdown line.
44+
45+
A link's destination (`](...)`) and an HTML tag (an autolink's URL
46+
included) are dropped; a code span's contents are kept, tag-like text
47+
and all. One pass, so a long line with an unterminated `](`, `<` or
48+
backtick costs linear time: each closer is searched for forward from
49+
where it is needed, and once a search for one fails it is never repeated.
50+
"""
51+
out: list[str] = []
52+
missing: set[str] = set()
53+
i, end = 0, len(line)
54+
while i < end:
55+
for opener, closer, keep in (
56+
("`", "`", True),
57+
("<", ">", False),
58+
("](", ")", False),
59+
):
60+
if closer in missing or not line.startswith(opener, i):
61+
continue
62+
j = line.find(closer, i + len(opener))
63+
if j == -1:
64+
missing.add(closer)
65+
continue
66+
out.append(line[i + len(opener) : j] if keep else " ")
67+
i = j + 1
68+
break
69+
else:
70+
out.append(line[i])
71+
i += 1
72+
return "".join(out)
4473

4574

4675
def _collapsed(text: str) -> str:
@@ -105,4 +134,5 @@ def with_page_title(html: str, content: str) -> str:
105134
title = page_title(html)
106135
if title is None or _opens_with_title(content, title):
107136
return content
108-
return f"# {title}\n\n{content.lstrip()}"
137+
heading = _MARKDOWN_SPECIAL.sub(r"\\\1", title)
138+
return f"# {heading}\n\n{content.lstrip()}"

‎tests/test_page_title.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,38 @@ def test_partial_words_do_not_match(title: str, content: str) -> None:
175175
assert with_page_title(html, content) == f"# {title}\n\n{content}"
176176

177177

178+
def test_code_span_contents_count_as_visible() -> None:
179+
# A heading's inline HTML example is text, not a tag, inside a code span.
180+
content = "# The `<input>` element\n\nBody."
181+
assert (
182+
with_page_title("<title>The &lt;input&gt; element</title>", content) == content
183+
)
184+
185+
186+
@pytest.mark.parametrize("opener", ["](", "<", "`"])
187+
def test_unterminated_markup_on_a_long_first_line(opener: str) -> None:
188+
# Linear in the line's length: a quadratic scan of 200,000 openers would
189+
# not finish in the test's time. The title is not on the line, so it is added.
190+
content = opener * 200_000
191+
result = with_page_title("<title>Title Here</title>", content)
192+
assert result == f"# Title Here\n\n{content}"
193+
194+
195+
@pytest.mark.parametrize(
196+
("title_html", "heading"),
197+
[
198+
("Using &lt;T&gt;", "# Using \\<T\\>"),
199+
("__init__ and *args", "# \\_\\_init\\_\\_ and \\*args"),
200+
(
201+
"[Draft] R&amp;D #3 `x` | a~b \\ c",
202+
"# \\[Draft\\] R\\&D \\#3 \\`x\\` \\| a\\~b \\\\ c",
203+
),
204+
],
205+
)
206+
def test_heading_escapes_markdown_and_html(title_html: str, heading: str) -> None:
207+
result = with_page_title(f"<title>{title_html}</title>", "Body.")
208+
assert result == f"{heading}\n\nBody."
209+
210+
178211
def test_title_with_no_word_characters_is_not_matched() -> None:
179212
assert with_page_title("<title>—</title>", "Body.") == "# —\n\nBody."

0 commit comments

Comments
 (0)