Safe outbound HTTP and remote actors (phase 2, step 2)
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 1m22s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 1m22s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
- net.py: the only client federation uses, on httpcore (BSD-3-Clause) with a guarded network backend: https only, every resolved address must be public (private, loopback, link-local, CGNAT, multicast and IPv4-mapped forms refused), and the connection goes to the checked address; 3 redirects max (GET only), 1 MB cap, 10s/20s timeouts - outbound.py: signed GETs (instance actor, so authorized-fetch servers answer) and signed POSTs (as the user), trying the configured signature schemes in order and the next only on a 401 - remote.py + federation_remote_actors: fetch, validate (id matches URL and host, actor type, inbox on its host, key owned by the actor), cache for 24h, refresh on demand, 410 marks an actor gone; resolve a key id to its actor, including standalone key documents - Config: FEDERATION_SIGNATURE_SCHEMES, FEDERATION_BLOCKED_DOMAINS, FEDERATION_DELIVERY_CONCURRENCY, and FEDERATION_INSECURE_TESTING (http and private addresses; only allowed for .test domains) - Tests against a real local HTTP server, including verifying that our fetches carry a valid signature Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
1 parent
a6d3fb6c93
commit
52b9f0b27b
10 files changed
+823
-3
No files matched your search
@@ -0,0 +1,72 @@
|
||||
"""Signed requests to other servers, through the SSRF-safe client in net.py."""
|
||||
|
||||
import json
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from scrobbler.federation import keys
|
||||
from scrobbler.federation.net import Client, FetchError, Response
|
||||
from scrobbler.federation.protocol import signatures
|
||||
from scrobbler.federation.protocol.media import ACTIVITY_JSON, LD_JSON
|
||||
from scrobbler.federation.protocol.signatures import SignedRequest
|
||||
|
||||
|
||||
def client() -> Client:
|
||||
"""One pooled client per app (created on first use)."""
|
||||
existing = current_app.extensions.get("federation_client")
|
||||
if existing is None:
|
||||
from scrobbler import __version__
|
||||
|
||||
config = current_app.extensions["federation"]
|
||||
existing = Client(
|
||||
user_agent=f"Scrobbler/{__version__} (+{config.base_url})",
|
||||
insecure_testing=config.insecure_testing,
|
||||
)
|
||||
current_app.extensions["federation_client"] = existing
|
||||
return existing
|
||||
|
||||
|
||||
def instance_key_id() -> str:
|
||||
return f"{current_app.extensions['federation'].base_url}/actor#main-key"
|
||||
|
||||
|
||||
def user_key_id(username: str) -> str:
|
||||
return f"{current_app.extensions['federation'].base_url}/users/{username}#main-key"
|
||||
|
||||
|
||||
def _signed_headers(method, url, headers, body, key_id, private_key, scheme) -> dict[str, str]:
|
||||
request = SignedRequest(method, url, headers, body)
|
||||
return {**headers, **signatures.sign(request, scheme, key_id, private_key)}
|
||||
|
||||
|
||||
def signed_get_json(url: str) -> dict:
|
||||
"""GET an ActivityPub document, signed as the instance actor, so servers using
|
||||
'authorized fetch' answer too."""
|
||||
private = keys.private_key(keys.key_for(None))
|
||||
headers = {"Accept": f"{ACTIVITY_JSON}, {LD_JSON}"}
|
||||
schemes = current_app.extensions["federation"].signature_schemes
|
||||
response = None
|
||||
for scheme in schemes:
|
||||
signed = _signed_headers("GET", url, headers, b"", instance_key_id(), private, scheme)
|
||||
response = client().request("GET", url, signed)
|
||||
if response.status != 401:
|
||||
break
|
||||
if response.status != 200:
|
||||
raise FetchError("http", f"{url} answered {response.status}", response.status)
|
||||
return response.json()
|
||||
|
||||
|
||||
def signed_post(url: str, activity: dict, *, user_id: int, username: str) -> tuple[Response, str]:
|
||||
"""POST an activity to an inbox, signed as the user. Tries the configured signature
|
||||
schemes in order, moving to the next only when the server answers 401. Returns the
|
||||
response and the scheme used."""
|
||||
body = json.dumps(activity).encode()
|
||||
private = keys.private_key(keys.key_for(user_id))
|
||||
headers = {"Content-Type": LD_JSON, "Accept": ACTIVITY_JSON}
|
||||
response, used = None, ""
|
||||
for used in current_app.extensions["federation"].signature_schemes:
|
||||
signed = _signed_headers("POST", url, headers, body, user_key_id(username), private, used)
|
||||
response = client().request("POST", url, signed, body)
|
||||
if response.status != 401:
|
||||
break
|
||||
return response, used
|
||||
Reference in new issue
Block a user