Private
Public Access
fetch_and_ingest fetched a feed_url supplied straight from the "add feed" form or an uploaded OPML file, with requests following redirects on its own and no check that the target (or a redirect's target) was actually a public, fetchable feed rather than an internal service, localhost, or a cloud metadata endpoint — reachable from wherever this app runs, on any authenticated user's behalf. apps.feeds.services.ssrf_guard.safe_get() replaces the direct requests.get() call: it resolves and validates the hostname (rejecting anything that isn't a globally routable address) before the initial request and before following each redirect hop manually, caps the number of hops, and bounds the response body size by streaming it instead of buffering it unbounded. OPML-imported feeds go through the same check the first time poll_due_feeds fetches them, since this is the one chokepoint every feed URL passes through regardless of which path created the Feed row. Known residual gap, documented in ssrf_guard.py: this validates then connects rather than pinning the TCP connection to the exact address it validated, so a narrow DNS-rebinding race isn't closed. Fixing that needs a custom transport and felt like more machinery than this self-hosted app's threat model currently justifies. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
22 lines
873 B
Python
22 lines
873 B
Python
import socket
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _fake_public_dns(monkeypatch):
|
|
"""apps.feeds.services.ssrf_guard resolves a feed's real hostname via
|
|
socket.getaddrinfo before every fetch. Tests use made-up hostnames
|
|
like example.com/a.example.com purely as fixture data — they must
|
|
not depend on real DNS/network access to pass that check, and must
|
|
resolve deterministically regardless of this sandbox's network
|
|
access. Every lookup here resolves to a fixed, definitely-public IP;
|
|
tests that specifically exercise the "resolves to a non-public
|
|
address" rejection path patch socket.getaddrinfo themselves instead.
|
|
"""
|
|
|
|
def _fake_getaddrinfo(host, *args, **kwargs):
|
|
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0))]
|
|
|
|
monkeypatch.setattr(socket, "getaddrinfo", _fake_getaddrinfo)
|