aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg
diff options
context:
space:
mode:
authorDennis Fink2026-10-30 19:34:42 +0100
committerDennis Fink2026-10-30 19:34:42 +0100
commit616ab6a8daaae6334b832fee12d9e8e335f0aba6 (patch)
tree946a1438559548c1074dd4b61b9bb64b5e0eedf8 /webmentions_ssg
parentdcbbfdb38d4e18e377d4e326d82f35341840c6cc (diff)
downloadwebmentions-ssg-616ab6a8daaae6334b832fee12d9e8e335f0aba6.tar.gz
webmentions-ssg-616ab6a8daaae6334b832fee12d9e8e335f0aba6.zip
fix(receiver): handle punctuation around plain-text URLs
Ignore surrounding prose punctuation when matching target IRIs in text/plain sources so valid Webmentions are not rejected when URLs appear in normal sentences. Preserve balanced URL parentheses and reject longer URLs that only share the target as a prefix.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/tasks/receiver.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py
index 2bb7222..d195cf5 100644
--- a/webmentions_ssg/tasks/receiver.py
+++ b/webmentions_ssg/tasks/receiver.py
@@ -32,6 +32,7 @@ class TemporaryFetchError(Exception):
IRI_PATTERN = rfc3987.get_compiled_pattern("IRI")
+TRAILING_IRI_PUNCTUATION = ".,;:!?"
HTML_URL_ATTRIBUTES = {
@@ -90,13 +91,35 @@ def html_mentions_target(body: bytes, source_url: str, target_url: str) -> bool:
def text_mentions_target(body: str, target_url: str) -> bool:
"""
- Check whether plain text contains the exact target IRI.
+ Check whether plain text contains the target IRI, ignoring trailing prose
+ punctuation but not extra URL path or query characters.
:param body: Plain-text document body.
:param target_url: Exact IRI to look for.
:return: Whether the target IRI occurs in the document.
"""
- return any(match.group() == target_url for match in IRI_PATTERN.finditer(body))
+ for match in IRI_PATTERN.finditer(body):
+ candidate = match.group()
+
+ # A URL in prose can be followed by punctuation that the IRI pattern
+ # consumes as part of its match. Only remove trailing punctuation,
+ # leaving longer URLs (e.g. ``target/more``) unmatched.
+ if not candidate.startswith(target_url):
+ continue
+
+ while candidate != target_url:
+ if candidate[-1] in TRAILING_IRI_PUNCTUATION:
+ candidate = candidate[:-1]
+ elif candidate[-1] == ")" and candidate.count(")") > candidate.count("("):
+ # Keep balanced parentheses that belong to the URL itself.
+ candidate = candidate[:-1]
+ else:
+ break
+
+ if candidate == target_url:
+ return True
+
+ return False
def ensure_public_request(request: httpx.Request) -> None: