aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/test_receiver.py
blob: 7e8f531a9b626670b8de75349ede9ba29723bfe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import logging
import uuid
from types import ModuleType
from unittest.mock import Mock, patch

import httpx
import pytest
from flask import Flask

from webmentions_ssg import DATABASE as db
from webmentions_ssg.models import ReceivedWebmention
from webmentions_ssg.url_security import AddressResolutionError

SOURCE_URL = "https://source.example/article"
TARGET_URL = "https://dennisfink.me/blog/example/"


@pytest.fixture
def receiver(app: Flask) -> ModuleType:
    from webmentions_ssg.tasks import receiver

    return receiver


def create_webmention(
    app: Flask,
    *,
    source: str = SOURCE_URL,
    target: str = TARGET_URL,
    status: str = "received",
    failure_reason: str | None = None,
) -> uuid.UUID:
    identifier = uuid.uuid7()

    with app.app_context():
        webmention = ReceivedWebmention(
            uuid=identifier,
            source=source,
            target=target,
            status=status,
            failure_reason=failure_reason,
        )

        db.session.add(webmention)
        db.session.commit()

    return identifier


def get_webmention_state(app: Flask, identifier: uuid.UUID) -> tuple[str, str | None]:
    with app.app_context():
        webmention = db.session.get(ReceivedWebmention, identifier)

        assert webmention is not None

        return (webmention.status, webmention.failure_reason)


def set_stream_response(httpx_client: Mock, response: httpx.Response) -> Mock:
    client = httpx_client.return_value.__enter__.return_value
    client.stream.return_value.__enter__.return_value = response
    return client


@pytest.mark.parametrize(
    ("body", "expected"),
    [
        pytest.param(f'<a href="{TARGET_URL}">Reply</a>', True, id="a-href"),
        pytest.param(f'<area href="{TARGET_URL}" alt="Target">', True, id="area-href"),
        pytest.param(
            f'<link href="{TARGET_URL}" rel="alternate">', True, id="link-href"
        ),
        pytest.param(f'<img src="{TARGET_URL}" alt="">', True, id="img-src"),
        pytest.param(f'<audio src="{TARGET_URL}"></audio>', True, id="audio-src"),
        pytest.param(f'<video src="{TARGET_URL}"></video>', True, id="video-src"),
        pytest.param(
            f'<audio><source src="{TARGET_URL}"></audio>', True, id="audio-source-src"
        ),
        pytest.param(
            f'<video><source src="{TARGET_URL}"></video>', True, id="video-source-src"
        ),
        pytest.param(f'<iframe src="{TARGET_URL}"></iframe>', True, id="iframe-src"),
        pytest.param(f'<embed src="{TARGET_URL}">', True, id="embed-src"),
        pytest.param(f'<script src="{TARGET_URL}"></script>', True, id="script-src"),
        pytest.param(
            f'<video><track src="{TARGET_URL}"></video>', True, id="track-src"
        ),
        pytest.param(
            f'<input type="image" src="{TARGET_URL}" alt="">',
            True,
            id="image-input-src",
        ),
        pytest.param(
            f'<input type="IMAGE" src="{TARGET_URL}" alt="">',
            True,
            id="image-input-case-insensitive",
        ),
        pytest.param(
            f'<blockquote cite="{TARGET_URL}">Quotation</blockquote>',
            True,
            id="blockquote-cite",
        ),
        pytest.param(f'<q cite="{TARGET_URL}">Quotation</q>', True, id="q-cite"),
        pytest.param(f'<ins cite="{TARGET_URL}">Addition</ins>', True, id="ins-cite"),
        pytest.param(f'<del cite="{TARGET_URL}">Removal</del>', True, id="del-cite"),
        pytest.param(
            '<base href="https://dennisfink.me/blog/"><a href="example/">Reply</a>',
            True,
            id="base-url",
        ),
        pytest.param(f'<img cite="{TARGET_URL}" alt="">', False, id="img-cite-invalid"),
        pytest.param(
            f'<blockquote src="{TARGET_URL}">Quote</blockquote>',
            False,
            id="blockquote-src-invalid",
        ),
        pytest.param(f'<a src="{TARGET_URL}">Reply</a>', False, id="a-src-invalid"),
        pytest.param(f'<div href="{TARGET_URL}"></div>', False, id="div-href-invalid"),
        pytest.param(f'<link src="{TARGET_URL}">', False, id="link-src-invalid"),
        pytest.param(
            f'<input type="text" src="{TARGET_URL}">',
            False,
            id="text-input-src-invalid",
        ),
        pytest.param(
            f'<picture><source src="{TARGET_URL}"></picture>',
            False,
            id="picture-source-src-invalid",
        ),
        pytest.param(f'<base href="{TARGET_URL}">', False, id="base-is-not-mention"),
        pytest.param(
            f'<a href="{TARGET_URL}more">Different page</a>', False, id="longer-url"
        ),
        pytest.param(
            f'<img cite="{TARGET_URL}" src="https://example.com/image.jpg" alt="">',
            False,
            id="invalid-cite-does-not-override-valid-src",
        ),
        pytest.param(f"<p>{TARGET_URL}</p>", False, id="text-content"),
        pytest.param(
            '<a href="https://example.com/">Other site</a>', False, id="missing-target"
        ),
    ],
)
def test_html_mentions_target(receiver: ModuleType, body: str, expected: bool) -> None:
    assert (
        receiver.html_mentions_target(body.encode(), SOURCE_URL, TARGET_URL) is expected
    )


def test_html_mentions_relative_target(receiver: ModuleType) -> None:
    assert receiver.html_mentions_target(
        b'<a href="../target/">Reply</a>',
        "https://source.example/posts/article/",
        "https://source.example/posts/target/",
    )


@pytest.mark.parametrize(
    ("body", "target_url", "expected"),
    [
        (TARGET_URL, TARGET_URL, True),
        (f"This post replies to {TARGET_URL}", TARGET_URL, True),
        (
            f"https://example.com/first {TARGET_URL} https://example.com/last",
            TARGET_URL,
            True,
        ),
        (f"{TARGET_URL}more", TARGET_URL, False),
        ("https://dennisfink.me/blog/other/", TARGET_URL, False),
        ("/blog/example/", TARGET_URL, False),
        ("There are no links here.", TARGET_URL, False),
    ],
)
def test_text_mentions_target(
    receiver: ModuleType, body: str, target_url: str, expected: bool
) -> None:
    assert receiver.text_mentions_target(body, target_url) is expected


@patch("webmentions_ssg.tasks.receiver.httpx.Client")
def test_fetch_source_returns_response_and_body(
    httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
    response = httpx.Response(
        200,
        headers={"Content-Type": "text/html"},
        content=b"<p>Document</p>",
        request=httpx.Request("GET", SOURCE_URL),
    )
    client = set_stream_response(httpx_client, response)

    with app.app_context():
        fetched_response, body = receiver.fetch_source(SOURCE_URL)

    assert fetched_response is response
    assert body == b"<p>Document</p>"
    client.stream.assert_called_once_with("GET", SOURCE_URL)

    options = httpx_client.call_args.kwargs

    assert options["headers"] == {
        "Accept": "text/html, application/xhtml+xml;q=0.9, text/plain;q=0.8",
        "User-Agent": f"{receiver.APP_NAME}/{receiver.VERSION} ReceivedWebmention",
    }
    assert options["follow_redirects"] is True
    assert options["max_redirects"] == app.config.get(
        "WEBMENTIONS_SSG_MAX_REDIRECTS", 20
    )
    assert options["trust_env"] is False
    assert options["event_hooks"] == {"request": [receiver.ensure_public_request]}


@patch("webmentions_ssg.tasks.receiver.httpx.Client")
@pytest.mark.parametrize(
    ("status_code", "exception_name"),
    [
        (400, "VerificationError"),
        (404, "VerificationError"),
        (410, "SourceGoneError"),
        (408, "TemporaryFetchError"),
        (425, "TemporaryFetchError"),
        (429, "TemporaryFetchError"),
        (500, "TemporaryFetchError"),
        (503, "TemporaryFetchError"),
    ],
)
def test_fetch_source_maps_http_status_to_exception(
    httpx_client: Mock,
    app: Flask,
    receiver: ModuleType,
    status_code: int,
    exception_name: str,
) -> None:
    set_stream_response(
        httpx_client,
        httpx.Response(status_code, request=httpx.Request("GET", SOURCE_URL)),
    )

    exception_type = getattr(receiver, exception_name)

    with app.app_context(), pytest.raises(exception_type, match=f"HTTP {status_code}"):
        receiver.fetch_source(SOURCE_URL)


@patch("webmentions_ssg.tasks.receiver.httpx.Client")
def test_fetch_source_propagates_network_error(
    httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
    client = httpx_client.return_value.__enter__.return_value
    client.stream.side_effect = httpx.ConnectError(
        "Connection refused", request=httpx.Request("GET", SOURCE_URL)
    )

    with (
        app.app_context(),
        pytest.raises(httpx.ConnectError, match="Connection refused"),
    ):
        receiver.fetch_source(SOURCE_URL)


@patch("webmentions_ssg.tasks.receiver.httpx.Client")
def test_fetch_source_rejects_declared_oversized_body(
    httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
    app.config["WEBMENTIONS_SSG_MAX_SOURCE_BYTES"] = 10

    set_stream_response(
        httpx_client,
        httpx.Response(
            200,
            headers={"Content-Type": "text/html", "Content-Length": "11"},
            content=b"x" * 11,
            request=httpx.Request("GET", SOURCE_URL),
        ),
    )

    with (
        app.app_context(),
        pytest.raises(receiver.VerificationError, match="Source document is too large"),
    ):
        receiver.fetch_source(SOURCE_URL)


@patch("webmentions_ssg.tasks.receiver.httpx.Client")
def test_fetch_source_rejects_streamed_oversized_body(
    httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
    app.config["WEBMENTIONS_SSG_MAX_SOURCE_BYTES"] = 10

    class BodyStream(httpx.SyncByteStream):
        def __iter__(self):
            yield b"x" * 6
            yield b"x" * 6

    set_stream_response(
        httpx_client,
        httpx.Response(
            200,
            headers={"Content-Type": "text/html"},
            stream=BodyStream(),
            request=httpx.Request("GET", SOURCE_URL),
        ),
    )

    with (
        app.app_context(),
        pytest.raises(receiver.VerificationError, match="Source document is too large"),
    ):
        receiver.fetch_source(SOURCE_URL)


@patch("webmentions_ssg.tasks.receiver.fetch_source")
@pytest.mark.parametrize(
    ("content_type", "body", "expected"),
    [
        pytest.param(
            "text/html; charset=utf-8",
            f'<a href="{TARGET_URL}">Reply</a>'.encode(),
            True,
            id="html",
        ),
        pytest.param(
            "TEXT/HTML; CHARSET=UTF-8",
            f'<a href="{TARGET_URL}">Reply</a>'.encode(),
            True,
            id="case-insensitive-html",
        ),
        pytest.param(
            "application/xhtml+xml",
            b'<a href="https://example.com/">Other</a>',
            False,
            id="xhtml-without-target",
        ),
        pytest.param(
            "text/plain; charset=utf-8",
            f"Reply to {TARGET_URL}".encode(),
            True,
            id="plain-text-utf-8",
        ),
        pytest.param(
            "text/plain; charset=iso-8859-1",
            (f"Grüße. Reply to {TARGET_URL}").encode("iso-8859-1"),
            True,
            id="plain-text-declared-encoding",
        ),
        pytest.param(
            "text/plain; charset=utf-8",
            b"\xff Reply to " + TARGET_URL.encode(),
            True,
            id="plain-text-invalid-byte",
        ),
        pytest.param(
            "text/plain", b"No target here.", False, id="plain-text-without-target"
        ),
    ],
)
def test_source_mentions_target_by_media_type(
    fetch_source: Mock,
    receiver: ModuleType,
    content_type: str,
    body: bytes,
    expected: bool,
) -> None:
    response = httpx.Response(
        200,
        headers={"Content-Type": content_type},
        content=body,
        request=httpx.Request("GET", SOURCE_URL),
    )
    fetch_source.return_value = response, body

    assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) is expected
    fetch_source.assert_called_once_with(SOURCE_URL)


@patch("webmentions_ssg.tasks.receiver.fetch_source")
@pytest.mark.parametrize(
    ("content_type", "expected_media_type"),
    [
        ("application/json", "application/json"),
        ("application/pdf", "application/pdf"),
        ("", "missing"),
    ],
)
def test_source_mentions_target_rejects_unsupported_media_type(
    fetch_source: Mock,
    receiver: ModuleType,
    content_type: str,
    expected_media_type: str,
) -> None:
    headers = {}

    if content_type:
        headers["Content-Type"] = content_type

    response = httpx.Response(
        200,
        headers=headers,
        content=b"Document",
        request=httpx.Request("GET", SOURCE_URL),
    )
    fetch_source.return_value = response, b"Document"

    with pytest.raises(
        receiver.VerificationError,
        match=f"Unsupported source content type: {expected_media_type}",
    ):
        receiver.source_mentions_target(SOURCE_URL, TARGET_URL)


@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
def test_verify_webmention_marks_row_verifying_before_check(
    source_mentions_target: Mock, app: Flask, receiver: ModuleType
) -> None:
    identifier = create_webmention(
        app, status="failed", failure_reason="Earlier failure"
    )

    def verify_source(source_url: str, target_url: str) -> bool:
        webmention = db.session.get(ReceivedWebmention, identifier)

        assert webmention is not None
        assert webmention.status == "verifying"
        assert webmention.failure_reason is None
        assert source_url == webmention.source
        assert target_url == webmention.target

        return True

    source_mentions_target.side_effect = verify_source

    receiver.verify_webmention.call_local(identifier)

    assert get_webmention_state(app, identifier) == ("verified", None)


@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
@pytest.mark.parametrize(
    ("outcome", "expected_status", "expected_reason"),
    [
        ("verified", "verified", None),
        ("missing", "deleted", "Source does not mention target"),
        ("gone", "deleted", "Source returned HTTP 410"),
        ("permanent-failure", "failed", "Source returned HTTP 404"),
    ],
)
def test_verify_webmention_persists_final_state(
    source_mentions_target: Mock,
    app: Flask,
    receiver: ModuleType,
    outcome: str,
    expected_status: str,
    expected_reason: str | None,
) -> None:
    identifier = create_webmention(app)

    match outcome:
        case "verified":
            source_mentions_target.return_value = True
        case "missing":
            source_mentions_target.return_value = False
        case "gone":
            source_mentions_target.side_effect = receiver.SourceGoneError(
                "Source returned HTTP 410"
            )
        case "permanent-failure":
            source_mentions_target.side_effect = receiver.VerificationError(
                "Source returned HTTP 404"
            )
        case _:
            raise AssertionError(f"Unexpected outcome: {outcome}")

    receiver.verify_webmention.call_local(identifier)

    assert get_webmention_state(app, identifier) == (expected_status, expected_reason)


@patch("webmentions_ssg.tasks.receiver.source_mentions_target")
@pytest.mark.parametrize("failure", ["temporary-http", "network"])
def test_verify_webmention_persists_retryable_failure_and_reraises(
    source_mentions_target: Mock, app: Flask, receiver: ModuleType, failure: str
) -> None:
    identifier = create_webmention(app)

    match failure:
        case "temporary-http":
            exception = receiver.TemporaryFetchError("Source returned HTTP 503")
        case "network":
            exception = httpx.ConnectError(
                "Connection refused", request=httpx.Request("GET", SOURCE_URL)
            )
        case _:
            raise AssertionError(f"Unexpected failure: {failure}")

    source_mentions_target.side_effect = exception

    with pytest.raises(type(exception), match=str(exception)):
        receiver.verify_webmention.call_local(identifier)

    assert get_webmention_state(app, identifier) == ("failed", str(exception))


def test_verify_webmention_ignores_unknown_identifier(
    receiver: ModuleType, caplog: pytest.LogCaptureFixture
) -> None:
    identifier = uuid.uuid7()

    with caplog.at_level(logging.WARNING):
        receiver.verify_webmention.call_local(identifier)

    assert f"Cannot verify unknown ReceivedWebmention {identifier}" in caplog.text


@patch("webmentions_ssg.tasks.receiver.is_public_url", return_value=True)
def test_ensure_public_request_accepts_public_url(
    is_public_url: Mock, receiver: ModuleType
) -> None:
    receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))

    is_public_url.assert_called_once_with(SOURCE_URL)


@patch("webmentions_ssg.tasks.receiver.is_public_url", return_value=False)
def test_ensure_public_request_rejects_non_public_address(
    is_public_url: Mock, receiver: ModuleType
) -> None:
    with pytest.raises(
        receiver.VerificationError, match="Source resolves to a non-public address"
    ):
        receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))

    is_public_url.assert_called_once_with(SOURCE_URL)


@patch(
    "webmentions_ssg.tasks.receiver.is_public_url",
    side_effect=AddressResolutionError("Could not resolve hostname"),
)
def test_ensure_public_request_maps_dns_failure_to_temporary_error(
    is_public_url: Mock, receiver: ModuleType
) -> None:
    with pytest.raises(
        receiver.TemporaryFetchError, match="Source hostname could not be resolved"
    ):
        receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))

    is_public_url.assert_called_once_with(SOURCE_URL)


@patch(
    "webmentions_ssg.tasks.receiver.is_public_url",
    side_effect=ValueError("No hostname was specified"),
)
def test_ensure_public_request_rejects_url_without_hostname(
    is_public_url: Mock, receiver: ModuleType
) -> None:
    with pytest.raises(receiver.VerificationError, match="Source URL has no hostname"):
        receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL))

    is_public_url.assert_called_once_with(SOURCE_URL)


@patch("webmentions_ssg.tasks.receiver.BeautifulSoup")
def test_html_mentions_target_ignores_non_string_attribute(
    beautiful_soup: Mock, receiver: ModuleType
) -> None:
    document = beautiful_soup.return_value
    document.select_one.return_value = None

    element = Mock()
    element.get.return_value = ["not", "a", "string"]
    document.select.return_value = [element]

    assert not receiver.html_mentions_target(b"<html></html>", SOURCE_URL, TARGET_URL)


@patch("webmentions_ssg.tasks.receiver.httpx.Client")
def test_fetch_source_ignores_invalid_content_length(
    httpx_client: Mock, app: Flask, receiver: ModuleType
) -> None:
    response = httpx.Response(
        200,
        headers={"Content-Type": "text/html", "Content-Length": "invalid"},
        content=b"<p>Document</p>",
        request=httpx.Request("GET", SOURCE_URL),
    )
    set_stream_response(httpx_client, response)

    with app.app_context():
        fetched_response, body = receiver.fetch_source(SOURCE_URL)

    assert fetched_response is response
    assert body == b"<p>Document</p>"


@patch("webmentions_ssg.tasks.receiver.fetch_source")
def test_source_mentions_target_falls_back_for_unknown_encoding(
    fetch_source: Mock, receiver: ModuleType
) -> None:
    body = f"Reply to {TARGET_URL}".encode()

    response = httpx.Response(
        200,
        headers={"Content-Type": "text/plain"},
        content=body,
        request=httpx.Request("GET", SOURCE_URL),
    )
    response.encoding = "unknown-encoding"
    fetch_source.return_value = response, body

    assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL)
    fetch_source.assert_called_once_with(SOURCE_URL)