fix: don't send webmentions to this site or WEBMENTION_SKIP_HOSTS (v2.18.3)

Links to the site itself, and to self-hosted apps like Gitea, timed out
from inside the container and sat in the admin Log. They are now skipped
when queueing; already queued or failed ones are dropped at worker start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-12 13:40:27 -07:00
1 parent 52ecf0f713
commit b9e5321df2
10 files changed
+223 -5

No files matched your search

+2 -2
View File
@@ -468,5 +468,5 @@ def create_app(config=None):
# Package version (Semantic Versioning 2.0.0)
# See docs/standards/versioning-strategy.md for details
__version__ = "2.18.2"
__version_info__ = (2, 18, 2)
__version__ = "2.18.3"
__version_info__ = (2, 18, 3)
+4
View File
@@ -106,6 +106,10 @@ def load_config(app, config_override=None):
# Webmention configuration (v1.6.0)
app.config["WEBMENTION_ENABLED"] = os.getenv("WEBMENTION_ENABLED", "true").lower() == "true"
# Hosts never sent webmentions, besides this site itself (v2.18.3)
app.config["WEBMENTION_SKIP_HOSTS"] = [
h.strip().lower() for h in os.getenv("WEBMENTION_SKIP_HOSTS", "").split(",") if h.strip()
]
# WebSub (v2.10.0, ADR-071): built-in hub unless WEBSUB_HUB_URL names another
app.config["WEBSUB_ENABLED"] = os.getenv("WEBSUB_ENABLED", "true").lower() == "true"
+4 -1
View File
@@ -728,7 +728,10 @@ def _notify_status() -> dict:
@require_auth
def settings():
"""Site settings: notifications (v2.18.0). Template: templates/admin/settings.html"""
return render_template("admin/settings.html", notify=_notify_status(), user_me=g.me)
from starpunk.webmention_sender import skipped_hosts
return render_template("admin/settings.html", notify=_notify_status(), user_me=g.me,
skip_hosts=sorted(skipped_hosts(current_app.config)))
@bp.route("/log")
+27 -2
View File
@@ -22,6 +22,22 @@ from urllib.parse import urlparse
logger = logging.getLogger(__name__)
SKIPPED_NOTE = "Not sent: links to this site and WEBMENTION_SKIP_HOSTS are skipped"
def skipped_hosts(config) -> set[str]:
"""Hosts that never get webmentions: this site and WEBMENTION_SKIP_HOSTS (v2.18.3)"""
hosts = set(config.get("WEBMENTION_SKIP_HOSTS") or [])
site = urlparse(config.get("SITE_URL") or "").hostname
if site:
hosts.add(site.lower())
return hosts
def is_skipped(url: str, hosts: set[str]) -> bool:
"""True if url is on one of the skipped hosts (exact host, any port)"""
return (urlparse(url).hostname or "").lower() in hosts
def extract_links_from_html(html_content: str, exclude_code_blocks: bool = True) -> list[str]:
"""
@@ -189,17 +205,26 @@ def queue_webmentions_for_note(
Returns:
Number of webmentions queued
"""
# Links to this site or a skipped host are dropped before the cap (v2.18.3):
# this site only takes webmentions for its posts, and its own public
# address may not be reachable from inside the container
from flask import current_app, has_app_context
hosts = skipped_hosts(current_app.config) if has_app_context() else set()
def wanted(urls):
return [u for u in urls if not is_skipped(u, hosts)]
# Targets for the current content: reply target first, then links in
# document order, at most 10
links = extract_links_from_html(content_html) if content_html else []
current = _dedupe(([in_reply_to] if in_reply_to else []) + links)[:10]
current = wanted(_dedupe(([in_reply_to] if in_reply_to else []) + links))[:10]
if old_content_html is not None:
# Update (spec 3.1.4): also re-send to every target notified before,
# and to links the edit removed, so they can update or remove the
# mention. These are not capped.
removed = [u for u in extract_links_from_html(old_content_html) if u not in links]
target_urls = _dedupe(current + previous_targets(db_path, note_id) + removed)
target_urls = _dedupe(current + wanted(previous_targets(db_path, note_id) + removed))
else:
target_urls = current
+43
View File
@@ -205,6 +205,40 @@ class WebmentionWorker(threading.Thread):
except Exception as e:
logger.error(f"Failed to cleanup orphaned items: {e}")
self._drop_skipped_targets()
def _skipped_hosts(self) -> set:
from starpunk.webmention_sender import skipped_hosts
return skipped_hosts(self.app.config) if self.app is not None else set()
def _drop_skipped_targets(self):
"""
Stop sending to this site and WEBMENTION_SKIP_HOSTS (v2.18.3)
Queued or failed sends to those hosts are set to 'no_endpoint' (nothing
to send to), so they leave the retry queue and the admin Log.
"""
from starpunk.webmention_sender import SKIPPED_NOTE, is_skipped
hosts = self._skipped_hosts()
if not hosts:
return
try:
conn = sqlite3.connect(self.db_path)
rows = conn.execute(
"SELECT id, target_url FROM webmention_queue WHERE status IN ('pending', 'failed')"
).fetchall()
ids = [(SKIPPED_NOTE, i) for i, url in rows if is_skipped(url, hosts)]
if ids:
conn.executemany(
"UPDATE webmention_queue SET status = 'no_endpoint', last_error = ? WHERE id = ?",
ids)
conn.commit()
logger.info(f"Dropped {len(ids)} queued webmention(s) to skipped hosts")
conn.close()
except Exception as e:
logger.error(f"Failed to drop skipped webmention targets: {e}")
def _process_pending_validations(self) -> int:
"""
Process pending incoming webmentions
@@ -583,6 +617,15 @@ class WebmentionWorker(threading.Thread):
conn.commit()
conn.close()
from starpunk.webmention_sender import SKIPPED_NOTE, is_skipped
if is_skipped(target_url, self._skipped_hosts()):
conn = sqlite3.connect(self.db_path)
conn.execute("UPDATE webmention_queue SET status = 'no_endpoint', last_error = ? WHERE id = ?",
(SKIPPED_NOTE, queue_id))
conn.commit()
conn.close()
return
try:
# Discover endpoint if not cached
endpoint = cached_endpoint