fix: WebSub hardening from the adversarial review (v2.10.1)
- Per-item error handling in the worker; hard time limits on requests - Notes with photos announced after attach; feed cache/ETag follow content_version - Keep failing subscriptions until lease end; retry DNS and render errors; hub.mode=denied for gone topics - Spec 5.1: decode unreserved chars, echo requested topic, ignore lease and secret on unsubscribe, case-insensitive Content-Type, refuse empty secret - Limits count pending requests per callback address; callback validation; request_id guards the verification race (migration 017) - Render once per topic per pass; likes only push /likes; Vary: Accept Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
409b8b31b4
commit
b2043286fa
17 files changed
+825
-151
No files matched your search
@@ -150,13 +150,17 @@ def create_note_submit():
|
||||
# Parse tags (v1.3.0 Phase 3)
|
||||
tags = parse_tag_input(tags_input)
|
||||
|
||||
# With photos, WebSub hears about the note once they're attached (v2.10.1)
|
||||
has_uploads = any(f.filename for f in request.files.getlist('media_files'))
|
||||
|
||||
try:
|
||||
# Create note first (per Q4)
|
||||
note = create_note(
|
||||
content,
|
||||
published=published,
|
||||
custom_slug=custom_slug if custom_slug else None,
|
||||
tags=tags if tags else None
|
||||
tags=tags if tags else None,
|
||||
has_media=has_uploads,
|
||||
)
|
||||
|
||||
# Handle media uploads (v1.2.0 Phase 3)
|
||||
@@ -195,6 +199,11 @@ def create_note_submit():
|
||||
if errors:
|
||||
flash(f"Note created, but some images failed: {'; '.join(errors)}", "warning")
|
||||
|
||||
if not media_ids:
|
||||
# No photo made it; announce the note as it is (v2.10.1)
|
||||
from starpunk.notes import _websub_changed
|
||||
_websub_changed(note)
|
||||
|
||||
# Crosspost after media is attached so images go out too (v2.1.0)
|
||||
if "syndication_form" in request.form:
|
||||
from starpunk.syndication import select_targets
|
||||
|
||||
@@ -141,7 +141,12 @@ def _generate_feed_with_cache(format_name: str, non_streaming_generator):
|
||||
|
||||
# Caching enabled - use FeedCache
|
||||
feed_cache = get_cache()
|
||||
notes_checksum = feed_cache.generate_notes_checksum(notes)
|
||||
# The content version covers changes the note list doesn't show, such as
|
||||
# photos attached or captions edited (v2.10.1)
|
||||
from starpunk.database import get_db
|
||||
from starpunk.websub import get_content_version
|
||||
notes_checksum = (feed_cache.generate_notes_checksum(notes)
|
||||
+ ":" + get_content_version(get_db(current_app)))
|
||||
|
||||
# Check If-None-Match header for conditional requests
|
||||
if_none_match = request.headers.get('If-None-Match')
|
||||
@@ -508,15 +513,15 @@ def feed():
|
||||
)
|
||||
|
||||
# Route to appropriate generator
|
||||
if format_name == 'rss':
|
||||
return feed_rss()
|
||||
elif format_name == 'atom':
|
||||
return feed_atom()
|
||||
if format_name == 'atom':
|
||||
response = feed_atom()
|
||||
elif format_name == 'json':
|
||||
return feed_json()
|
||||
response = feed_json()
|
||||
else:
|
||||
# Shouldn't reach here, but be defensive
|
||||
return feed_rss()
|
||||
response = feed_rss()
|
||||
# The format (and its rel=self) depends on Accept (v2.10.1)
|
||||
response.headers.add('Vary', 'Accept')
|
||||
return response
|
||||
|
||||
|
||||
@bp.route("/feed.rss")
|
||||
|
||||
@@ -15,7 +15,10 @@ from starpunk import websub
|
||||
bp = Blueprint("websub", __name__)
|
||||
|
||||
# Subscription requests per client IP per minute
|
||||
# (per gunicorn process; needs TRUSTED_PROXY=true behind a reverse proxy,
|
||||
# or every client shares the proxy's address)
|
||||
_limiter = RateLimiter(limit=30, window_seconds=60)
|
||||
_requests = 0
|
||||
|
||||
|
||||
def _text(message: str, status: int):
|
||||
@@ -28,10 +31,15 @@ def hub():
|
||||
return _text("WebSub is not enabled on this site", 404)
|
||||
|
||||
from starpunk.routes.webmention import get_client_ip
|
||||
global _requests
|
||||
_requests += 1
|
||||
if _requests % 500 == 0:
|
||||
_limiter.cleanup() # forget idle clients so memory doesn't grow
|
||||
if not _limiter.is_allowed(get_client_ip()):
|
||||
return _text("Too many requests", 429)
|
||||
|
||||
if "application/x-www-form-urlencoded" not in request.headers.get("Content-Type", ""):
|
||||
# Media types are case-insensitive (v2.10.1)
|
||||
if request.mimetype != "application/x-www-form-urlencoded":
|
||||
return _text("Send an application/x-www-form-urlencoded body", 400)
|
||||
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user