From 41f99e94053911b23b81d829fe49cefa31acadb1 Mon Sep 17 00:00:00 2001 From: Keith Smith Date: Sun, 1 Nov 2026 11:57:23 -0700 Subject: [PATCH] Allow only http(s) URLs from feeds and OPML, at storage and render (R2-S1, #33) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- apps/core/templatetags/__init__.py | 0 apps/core/templatetags/safe_urls.py | 12 +++++++ apps/core/test_urlutils.py | 44 ++++++++++++++++++++++++++ apps/core/urlutils.py | 30 ++++++++++++++++++ apps/feeds/forms.py | 11 +++++++ apps/feeds/opml.py | 7 ++-- apps/feeds/services/fetcher.py | 5 +-- apps/feeds/test_fetcher.py | 24 ++++++++++++++ apps/feeds/test_opml.py | 18 +++++++++++ apps/feeds/tests.py | 11 +++++++ apps/reader/tests.py | 30 ++++++++++++++++++ templates/core/dashboard.html | 3 +- templates/reader/_article_content.html | 4 +-- 13 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 apps/core/templatetags/__init__.py create mode 100644 apps/core/templatetags/safe_urls.py create mode 100644 apps/core/test_urlutils.py create mode 100644 apps/core/urlutils.py diff --git a/apps/core/templatetags/__init__.py b/apps/core/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/core/templatetags/safe_urls.py b/apps/core/templatetags/safe_urls.py new file mode 100644 index 0000000..4fd13fa --- /dev/null +++ b/apps/core/templatetags/safe_urls.py @@ -0,0 +1,12 @@ +from django import template + +from apps.core.urlutils import http_url_or_empty + +register = template.Library() + + +@register.filter +def safe_http_url(value): + """ "" unless value is an absolute http(s) URL — use before putting any + feed-supplied URL in an href (see apps.core.urlutils).""" + return http_url_or_empty(value) diff --git a/apps/core/test_urlutils.py b/apps/core/test_urlutils.py new file mode 100644 index 0000000..09d5ec5 --- /dev/null +++ b/apps/core/test_urlutils.py @@ -0,0 +1,44 @@ +import pytest +from django.template import Context, Template + +from .urlutils import http_url_or_empty, resolve_http_url + + +@pytest.mark.parametrize( + "value", + [ + "javascript:alert(1)", + "JaVaScRiPt:alert(1)", + "java\tscript:alert(1)", + " javascript:alert(1)", + "data:text/html,", + "vbscript:msgbox(1)", + "file:///etc/passwd", + "ftp://example.com/feed.xml", + "//example.com/no-scheme", + "/relative/path", + "http://", + "", + None, + ], +) +def test_unsafe_or_non_absolute_urls_are_rejected(value): + assert http_url_or_empty(value) == "" + + +@pytest.mark.parametrize("value", ["http://example.com/a", "https://example.com/a?b=1#c"]) +def test_http_and_https_urls_pass_through(value): + assert http_url_or_empty(value) == value + + +def test_relative_links_resolve_against_the_feed_url_and_are_still_checked(): + base = "https://blog.example.com/feed.xml" + assert resolve_http_url(base, "/2026/post") == "https://blog.example.com/2026/post" + assert resolve_http_url(base, "javascript:alert(1)") == "" + assert resolve_http_url(base, "https://other.example.com/x") == "https://other.example.com/x" + + +def test_template_filter_blanks_unsafe_urls(): + template = Template("{% load safe_urls %}[{{ url|safe_http_url }}]") + assert template.render(Context({"url": "javascript:alert(1)"})) == "[]" + assert template.render(Context({"url": "https://example.com/"})) == "[https://example.com/]" diff --git a/apps/core/urlutils.py b/apps/core/urlutils.py new file mode 100644 index 0000000..46d5106 --- /dev/null +++ b/apps/core/urlutils.py @@ -0,0 +1,30 @@ +from urllib.parse import urljoin, urlsplit + +ALLOWED_SCHEMES = {"http", "https"} + + +def http_url_or_empty(value: str | None) -> str: + """The URL if it's an absolute http(s) URL, else "". Feed content is + untrusted: a link of "javascript:..." or "data:..." is harmless inside + an escaped attribute value but becomes script the moment someone + clicks it, so every URL we store from a feed or OPML file, and every + one we render as a link, goes through this. (urlsplit already ignores + the tab/newline tricks browsers also ignore inside a scheme.)""" + if not value: + return "" + value = value.strip() + try: + parts = urlsplit(value) + except ValueError: + return "" + if parts.scheme.lower() not in ALLOWED_SCHEMES or not parts.netloc: + return "" + return value + + +def resolve_http_url(base: str, value: str | None) -> str: + """Like http_url_or_empty, but first resolves a relative link (feeds + often give "/2026/post") against the feed's own URL.""" + if not value: + return "" + return http_url_or_empty(urljoin(base, value.strip())) diff --git a/apps/feeds/forms.py b/apps/feeds/forms.py index ff9af68..e85cc05 100644 --- a/apps/feeds/forms.py +++ b/apps/feeds/forms.py @@ -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, diff --git a/apps/feeds/opml.py b/apps/feeds/opml.py index 804231b..caa7713 100644 --- a/apps/feeds/opml.py +++ b/apps/feeds/opml.py @@ -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, } ) diff --git a/apps/feeds/services/fetcher.py b/apps/feeds/services/fetcher.py index 9247fb1..dd416a0 100644 --- a/apps/feeds/services/fetcher.py +++ b/apps/feeds/services/fetcher.py @@ -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)) diff --git a/apps/feeds/test_fetcher.py b/apps/feeds/test_fetcher.py index ba59ce0..1e0615c 100644 --- a/apps/feeds/test_fetcher.py +++ b/apps/feeds/test_fetcher.py @@ -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 = ( + 'T' + "javascript:alert(1)" + "Badbadjavascript:alert(2)" + "Relativerel/posts/1" + "Goodgoodhttps://example.com/good" + "" + ) + 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" diff --git a/apps/feeds/test_opml.py b/apps/feeds/test_opml.py index df248c9..64da460 100644 --- a/apps/feeds/test_opml.py +++ b/apps/feeds/test_opml.py @@ -192,3 +192,21 @@ class TestExportOPML: exported = export_opml(alice) assert b" + + + + """ + + entries = parse_opml(BytesIO(xml)) + + assert [e["feed_url"] for e in entries] == ["https://example.com/feed.xml"] + assert entries[0]["site_url"] == "" diff --git a/apps/feeds/tests.py b/apps/feeds/tests.py index 73639e9..02d3d36 100644 --- a/apps/feeds/tests.py +++ b/apps/feeds/tests.py @@ -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() diff --git a/apps/reader/tests.py b/apps/reader/tests.py index b1a36b4..9ef424d 100644 --- a/apps/reader/tests.py +++ b/apps/reader/tests.py @@ -554,3 +554,33 @@ def test_authenticated_pages_load_the_tab_badge_script_with_its_endpoint(client, assert f'data-url="{reverse("reader:new_since")}"' in content client.logout() assert "tab-badge" not in client.get(reverse("accounts:login")).content.decode() + + +@pytest.mark.django_db +def test_unsafe_stored_links_are_not_rendered_as_hrefs(client, alice, django_user_model): + """R2-S1 render boundary: even if a bad URL is already in the database + (stored before validation existed), it must not become a clickable + javascript: link.""" + feed = Feed.objects.create( + feed_url="https://example.com/feed.xml", site_url="javascript:alert(1)" + ) + Subscription.objects.create(user=alice, feed=feed) + article = Article.objects.create( + feed=feed, guid="g1", title="Hello", link="javascript:alert(2)" + ) + + client.login(username="alice", password="pw-alice-123") + detail = client.get(reverse("reader:article_detail", args=[article.pk])).content.decode() + assert "javascript:" not in detail + assert "Original" not in detail + + article.link = "https://example.com/real" + article.save() + detail = client.get(reverse("reader:article_detail", args=[article.pk])).content.decode() + assert 'href="https://example.com/real"' in detail + + django_user_model.objects.create_user("admin", password="pw-admin-123", is_staff=True) + client.login(username="admin", password="pw-admin-123") + Feed.objects.filter(pk=feed.pk).update(fetch_error_count=2, last_fetch_status="error") + dashboard = client.get(reverse("core:dashboard")).content.decode() + assert "javascript:" not in dashboard diff --git a/templates/core/dashboard.html b/templates/core/dashboard.html index bc4c8b6..a910f39 100644 --- a/templates/core/dashboard.html +++ b/templates/core/dashboard.html @@ -1,4 +1,5 @@ {% extends "base.html" %} +{% load safe_urls %} {% block title %}Dashboard — Evection{% endblock %} {% block content %}

Dashboard

@@ -27,7 +28,7 @@ {% for feed in erroring_feeds %} - {{ feed.title|default:feed.feed_url }} + {{ feed.title|default:feed.feed_url }} {{ feed.subscriber_count }} {{ feed.get_last_fetch_status_display }} {{ feed.fetch_error_count }} diff --git a/templates/reader/_article_content.html b/templates/reader/_article_content.html index 239b0dc..840ea31 100644 --- a/templates/reader/_article_content.html +++ b/templates/reader/_article_content.html @@ -1,4 +1,4 @@ -
+{% load safe_urls %}

{{ article.title|default:"(untitled)" }}