- Normalise mf2 author values: u-photo with alt text is a dict in mf2py and broke the database update for most real senders - Pass str(response.url) to urljoin during endpoint discovery; the TypeError marked every target as having no endpoint. Retry on discovery errors - Hold a cross-process flock so only one gunicorn process runs the worker - Keep author URL/photo only if http(s) (XSS via javascript: URLs) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
4.4 KiB
Python
129 lines
4.4 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
|
|
|
|
|
|
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()
|
|
|
|
page = httpx.Response(200, headers={"Link": '</wm>; rel="webmention"'},
|
|
request=httpx.Request("GET", "https://example.org/post"))
|
|
accepted = MagicMock(status_code=202, text="")
|
|
|
|
worker = WebmentionWorker(app, db_path=db_path, site_url=app.config["SITE_URL"],
|
|
poll_interval=0.05)
|
|
with patch("starpunk.webmention_ssrf.socket.gethostbyname", return_value="93.184.216.34"), \
|
|
patch("starpunk.webmention_worker.httpx.get", return_value=page), \
|
|
patch("starpunk.webmention_worker.httpx.post", return_value=accepted) as post:
|
|
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)
|
|
post.assert_called_once()
|
|
assert post.call_args.args[0] == "https://example.org/wm"
|
|
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
|