Files
StarPunk/tests/test_webmention_worker.py
T
PhilandClaude Opus 5.5 0640730416 feat: Pin webmention connections; re-send when responses change (v2.7.2)
- Connect to the address that passed the SSRF check (Host header and SNI
  carry the hostname, so TLS still verifies it), closing the DNS rebinding
  gap (spec 4.5). The webmention POST uses the same path.
- Treat a change in the responses a note shows as an update and re-send its
  webmentions (spec 3.1.4), from validation and from admin moderation.
  Unchanged re-validations send nothing, so there is no ping-pong.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 09:01:38 -07:00

134 lines
4.5 KiB
Python

"""
Webmention worker regression tests (v2.6.8)
The worker thread used Flask's current_app (via the SSRF checks) without an
app context, so every send and validation failed. It was also stopped
whenever any app context ended.
"""
import sqlite3
import httpx
import time
from unittest.mock import MagicMock, patch
from starpunk.webmention_worker import WebmentionWorker
PUBLIC = [(2, 1, 6, '', ('93.184.216.34', 0))]
def _wait_for(predicate, timeout=5):
deadline = time.time() + timeout
while time.time() < deadline:
if predicate():
return True
time.sleep(0.05)
return False
def test_worker_sends_queued_webmention(app):
db_path = app.config["DATABASE_PATH"]
conn = sqlite3.connect(db_path)
conn.execute(
"INSERT INTO webmention_queue (source_url, target_url, status) "
"VALUES (?, ?, 'pending')",
("http://localhost:5000/note/1", "https://example.org/post"),
)
conn.commit()
conn.close()
posts = []
def target_site(request):
if request.method == "POST":
posts.append((request.headers["host"], request.url.path, request.content))
return httpx.Response(202)
return httpx.Response(200, headers={"Link": '</wm>; rel="webmention"'})
worker = WebmentionWorker(app, db_path=db_path, site_url=app.config["SITE_URL"],
poll_interval=0.05)
with patch("starpunk.webmention_ssrf.socket.getaddrinfo", return_value=PUBLIC), \
patch("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(target_site)):
worker.start()
try:
def status():
c = sqlite3.connect(db_path)
row = c.execute("SELECT status, last_error FROM webmention_queue").fetchone()
c.close()
return row
assert _wait_for(lambda: status()[0] != "pending"), status()
assert status() == ("sent", None)
assert [(h, p) for h, p, _ in posts] == [("example.org", "/wm")]
assert b"source=" in posts[0][2] and b"target=" in posts[0][2]
finally:
worker.stop()
worker.join(timeout=2)
def test_worker_survives_app_context_teardown(app):
worker = WebmentionWorker(app, db_path=app.config["DATABASE_PATH"],
site_url=app.config["SITE_URL"], poll_interval=0.05)
worker.start()
try:
with app.app_context():
pass
app.test_client().get("/")
time.sleep(0.2)
assert worker.is_alive()
finally:
worker.stop()
worker.join(timeout=2)
def test_only_one_worker_processes_queues(app):
"""Each gunicorn process starts a worker; only one may hold the queue lock"""
kwargs = dict(db_path=app.config["DATABASE_PATH"], site_url=app.config["SITE_URL"])
first = WebmentionWorker(app, **kwargs)
second = WebmentionWorker(app, **kwargs)
try:
assert first._acquire_lock()
assert not second._acquire_lock()
first._release_lock()
assert second._acquire_lock()
finally:
first._release_lock()
second._release_lock()
def test_author_photo_with_alt_text_is_a_string():
"""mf2py returns u-photo with alt as a dict; the database needs a URL string"""
import mf2py
from starpunk.webmention_processor import extract_author
html = """
<div class="h-entry">
<span class="p-author h-card">
<a class="u-url p-name" href="https://a.example/">A Person</a>
<img class="u-photo" src="https://a.example/me.jpg" alt=""></span>
<a href="https://site.example/note/1">target</a>
</div>"""
author = extract_author(mf2py.parse(doc=html, url="https://src.example/"),
"https://src.example/")
assert author == {
"author_name": "A Person",
"author_url": "https://a.example/",
"author_photo": "https://a.example/me.jpg",
}
def test_author_urls_must_be_http():
"""A javascript: author URL would be rendered as a clickable link (XSS)"""
import mf2py
from starpunk.webmention_processor import extract_author
html = """
<div class="h-entry">
<span class="p-author h-card">
<a class="u-url p-name" href="javascript:alert(1)">Evil</a>
<img class="u-photo" src="data:image/svg+xml,x" alt=""></span>
</div>"""
author = extract_author(mf2py.parse(doc=html, url="https://src.example/"),
"https://src.example/")
assert author["author_url"] == "https://src.example/"
assert author["author_photo"] is None