Allow only http(s) URLs from feeds and OPML, at storage and render (R2-S1, #33)

A feed's channel link, an entry's link and an OPML htmlUrl were stored
as given and rendered in href attributes (bulk writes skip model
validation), so a malicious feed could supply a javascript: or data:
link. CSP is a second layer, not the control.

apps.core.urlutils accepts only absolute http(s) URLs. Fetching stores
channel/entry links through it — relative links now resolve against the
feed's own URL instead of being stored as dead relative paths — OPML
import skips non-http(s) feed URLs and blanks unsafe htmlUrl, and the
add-feed form rejects non-http(s) (URLField also allows ftp). A
safe_http_url template filter guards the two places a feed-supplied URL
becomes a link (the article's "Original" link and the dashboard), so
values already in the database from before this fix are neutralized too.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5.5 committed 2026-11-01 11:57:23 -07:00
1 parent 85914f9107
commit 41f99e9405
13 files changed
+192 -7

No files matched your search

+11
View File
@@ -1,10 +1,21 @@
from django import forms
from apps.core.urlutils import http_url_or_empty
from .models import Subscription
class AddFeedForm(forms.Form):
feed_url = forms.URLField(label="Feed URL", max_length=1000, assume_scheme="https")
def clean_feed_url(self):
# URLField also accepts ftp:// and ftps://; feeds are only ever
# fetched over http(s).
url = http_url_or_empty(self.cleaned_data["feed_url"])
if not url:
raise forms.ValidationError("Enter a feed URL starting with http:// or https://.")
return url
tag_names = forms.CharField(
label="Tags",
max_length=500,
+5 -2
View File
@@ -17,6 +17,7 @@ from io import BytesIO
import defusedxml.ElementTree as DefusedET
from apps.articles.models import SubscriptionTag, Tag
from apps.core.urlutils import http_url_or_empty
from .models import Feed, Subscription
@@ -66,7 +67,9 @@ def parse_opml(file_obj) -> list[dict]:
def walk(container, ancestor_names):
for outline in container.findall("outline"):
xml_url = outline.get("xmlUrl")
xml_url = http_url_or_empty(outline.get("xmlUrl"))
if outline.get("xmlUrl") and not xml_url:
continue # a feed URL that isn't http(s) can never be fetched
if xml_url:
tag_names = list(ancestor_names)
category_attr = outline.get("category")
@@ -79,7 +82,7 @@ def parse_opml(file_obj) -> list[dict]:
{
"feed_url": xml_url,
"title": outline.get("title") or outline.get("text") or "",
"site_url": outline.get("htmlUrl") or "",
"site_url": http_url_or_empty(outline.get("htmlUrl")),
"tag_names": tag_names,
}
)
+3 -2
View File
@@ -23,6 +23,7 @@ from django.utils import timezone
from apps.articles.models import Article, PurgedArticle
from apps.core.models import SiteSettings
from apps.core.urlutils import resolve_http_url
from apps.feeds.models import Feed
from .ssrf_guard import SSRFBlocked, safe_get
@@ -222,7 +223,7 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
channel_title = parsed.feed.get("title")
if channel_title and not feed.title:
feed.title = channel_title[:500]
channel_link = parsed.feed.get("link")
channel_link = resolve_http_url(feed.feed_url, parsed.feed.get("link"))
if channel_link and not feed.site_url:
feed.site_url = channel_link[:1000]
@@ -249,7 +250,7 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
if guid in parsed_entries:
continue
title = (getattr(entry, "title", "") or "")[:1000]
link = (getattr(entry, "link", "") or "")[:2000]
link = resolve_http_url(feed.feed_url, getattr(entry, "link", ""))[:2000]
author = (getattr(entry, "author", "") or "")[:500]
summary = (getattr(entry, "summary", "") or "")[:5000]
content_html = sanitize_html(_entry_content_html(entry))
+24
View File
@@ -399,3 +399,27 @@ def test_old_entries_are_ingested_normally_when_retention_is_off(feed):
responses.add(responses.GET, FEED_URL, body=xml, status=200)
assert len(fetch_and_ingest(feed).new_article_ids) == 1
@pytest.mark.django_db
@responses.activate
def test_unsafe_urls_in_a_feed_are_not_stored(feed):
"""R2-S1: a feed's channel link and entry links are untrusted — only
absolute http(s) URLs may be persisted (they end up in href attributes)."""
xml = (
'<?xml version="1.0"?><rss version="2.0"><channel><title>T</title>'
"<link>javascript:alert(1)</link>"
"<item><title>Bad</title><guid>bad</guid><link>javascript:alert(2)</link></item>"
"<item><title>Relative</title><guid>rel</guid><link>/posts/1</link></item>"
"<item><title>Good</title><guid>good</guid><link>https://example.com/good</link></item>"
"</channel></rss>"
)
responses.add(responses.GET, FEED_URL, body=xml, status=200)
fetch_and_ingest(feed)
assert feed.site_url == ""
links = dict(Article.objects.filter(feed=feed).values_list("guid", "link"))
assert links["bad"] == ""
assert links["rel"] == "https://example.com/posts/1"
assert links["good"] == "https://example.com/good"
+18
View File
@@ -192,3 +192,21 @@ class TestExportOPML:
exported = export_opml(alice)
assert b"<opml" in exported
assert b"<outline" not in exported
class TestUnsafeOPMLUrls:
def test_non_http_feed_urls_are_skipped_and_unsafe_html_urls_blanked(self):
from io import BytesIO
from apps.feeds.opml import parse_opml
xml = b"""<?xml version="1.0"?><opml version="2.0"><head/><body>
<outline text="Bad" xmlUrl="javascript:alert(1)"/>
<outline text="FTP" xmlUrl="ftp://example.com/feed.xml"/>
<outline text="Good" xmlUrl="https://example.com/feed.xml" htmlUrl="javascript:alert(2)"/>
</body></opml>"""
entries = parse_opml(BytesIO(xml))
assert [e["feed_url"] for e in entries] == ["https://example.com/feed.xml"]
assert entries[0]["site_url"] == ""
+11
View File
@@ -117,3 +117,14 @@ def test_add_feed_view_rejects_duplicate_subscription(client, two_users):
assert response.status_code == 302
assert Subscription.objects.filter(user=alice, feed=feed).count() == 1
@pytest.mark.django_db
@pytest.mark.parametrize("url", ["ftp://example.com/feed.xml", "javascript:alert(1)"])
def test_add_feed_rejects_non_http_urls(client, two_users, url):
client.login(username="alice", password="pw-alice-123")
response = client.post(reverse("feeds:add_feed"), {"feed_url": url})
assert response.status_code == 200
assert not Feed.objects.filter(feed_url=url).exists()