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
@@ -154,6 +154,8 @@ class FederatedTestConfig(_TestConfig):
|
||||
FEDERATION_DOMAIN = "scrobble.test"
|
||||
FEDERATION_BASE_URL = "https://scrobble.test"
|
||||
FEDERATION_KEY_SECRET = "test-only-federation-key-secret-0123456789"
|
||||
# Lets tests federate with servers on 127.0.0.1 over http (allowed for .test only).
|
||||
FEDERATION_INSECURE_TESTING = "1"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
@@ -182,3 +184,61 @@ def sharing_user(fed_ctx, user):
|
||||
|
||||
sharing.update(user.id, {"enabled": True, "display_name": "Alice", "bio": "Listening."})
|
||||
return user
|
||||
|
||||
|
||||
class FakeRemote:
|
||||
"""A tiny HTTP server standing in for another fediverse server.
|
||||
|
||||
`routes[path] = (status, headers, body)`; `requests` records what it received.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
import threading
|
||||
|
||||
from werkzeug.serving import make_server
|
||||
from werkzeug.wrappers import Request, Response
|
||||
|
||||
self.routes: dict = {}
|
||||
self.requests: list = []
|
||||
|
||||
@Request.application
|
||||
def app(request):
|
||||
self.requests.append(
|
||||
{
|
||||
"method": request.method,
|
||||
"path": request.full_path.rstrip("?"),
|
||||
"headers": dict(request.headers),
|
||||
"body": request.get_data(),
|
||||
}
|
||||
)
|
||||
status, headers, body = self.routes.get(request.path, (404, {}, b"not found"))
|
||||
if callable(body):
|
||||
body = body(request)
|
||||
if isinstance(body, dict | list):
|
||||
import json as _json
|
||||
|
||||
body, headers = (
|
||||
_json.dumps(body).encode(),
|
||||
{"Content-Type": "application/activity+json", **headers},
|
||||
)
|
||||
return Response(body, status=status, headers=headers)
|
||||
|
||||
self.server = make_server("127.0.0.1", 0, app, threaded=True)
|
||||
self.base = f"http://127.0.0.1:{self.server.server_port}"
|
||||
# A short poll interval: shutdown() waits for the next poll
|
||||
threading.Thread(
|
||||
target=self.server.serve_forever, kwargs={"poll_interval": 0.02}, daemon=True
|
||||
).start()
|
||||
|
||||
def url(self, path: str) -> str:
|
||||
return self.base + path
|
||||
|
||||
def close(self):
|
||||
self.server.shutdown()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def remote():
|
||||
server = FakeRemote()
|
||||
yield server
|
||||
server.close()
|
||||
Reference in new issue
Block a user