diff options
| -rw-r--r-- | tests/tasks/test_receiver.py | 32 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/receiver.py | 27 |
2 files changed, 57 insertions, 2 deletions
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py index 1943b65..6858be9 100644 --- a/tests/tasks/test_receiver.py +++ b/tests/tasks/test_receiver.py @@ -168,7 +168,33 @@ def test_html_mentions_relative_target(receiver: ModuleType) -> None: TARGET_URL, True, ), + pytest.param( + f"See {TARGET_URL}, thanks", TARGET_URL, True, id="trailing-comma" + ), + pytest.param(f"({TARGET_URL})", TARGET_URL, True, id="parenthesized-url"), + pytest.param( + f"See ({TARGET_URL}), thanks!", TARGET_URL, True, id="mixed-punctuation" + ), + pytest.param(f"See {TARGET_URL}. Next", TARGET_URL, True, id="trailing-period"), + pytest.param( + "https://example.com/wiki/Foo_(bar)", + "https://example.com/wiki/Foo_(bar)", + True, + id="balanced-parentheses-in-url", + ), + pytest.param( + "https://example.com/wiki/Foo_(bar)", + "https://example.com/wiki/Foo_(bar", + False, + id="balanced-parentheses-not-trimmed", + ), (f"{TARGET_URL}more", TARGET_URL, False), + pytest.param( + f"{TARGET_URL},another-page", + TARGET_URL, + False, + id="comma-within-longer-url", + ), ("https://dennisfink.me/blog/other/", TARGET_URL, False), ("/blog/example/", TARGET_URL, False), ("There are no links here.", TARGET_URL, False), @@ -339,6 +365,12 @@ def test_fetch_source_rejects_streamed_oversized_body( id="plain-text-utf-8", ), pytest.param( + "text/plain; charset=utf-8", + f"See ({TARGET_URL}), thanks".encode(), + True, + id="plain-text-with-surrounding-punctuation", + ), + pytest.param( "text/plain; charset=iso-8859-1", (f"Grüße. Reply to {TARGET_URL}").encode("iso-8859-1"), True, 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: |
