Private
Public Access
Add SSRF protection to feed fetching: validate scheme/address, bound redirects and response size (S1)
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>
This commit is contained in:
1 parent
fd8eeb605e
commit
9439f9cc59
4 files changed
+286
-1
No files matched your search
@@ -23,6 +23,8 @@ from django.utils import timezone
|
||||
from apps.articles.models import Article
|
||||
from apps.feeds.models import Feed
|
||||
|
||||
from .ssrf_guard import SSRFBlocked, safe_get
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
FETCH_TIMEOUT_SECONDS = 15
|
||||
@@ -139,7 +141,10 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
||||
headers["If-Modified-Since"] = feed.last_modified
|
||||
|
||||
try:
|
||||
response = requests.get(feed.feed_url, headers=headers, timeout=FETCH_TIMEOUT_SECONDS)
|
||||
response = safe_get(feed.feed_url, headers=headers, timeout=FETCH_TIMEOUT_SECONDS)
|
||||
except SSRFBlocked as exc:
|
||||
feed.last_fetched_at = timezone.now()
|
||||
return _record_error(feed, f"Refused to fetch: {exc}")
|
||||
except requests.RequestException as exc:
|
||||
feed.last_fetched_at = timezone.now()
|
||||
return _record_error(feed, f"Request failed: {exc}")
|
||||
|
||||
Reference in new issue
Block a user