Private
Public Access
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:
1 parent
85914f9107
commit
41f99e9405
13 files changed
+192
-7
No files matched your search
Whitespace-only changes.
@@ -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)
|
||||||
@@ -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,<script>alert(1)</script>",
|
||||||
|
"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/]"
|
||||||
@@ -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()))
|
||||||
@@ -1,10 +1,21 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
|
|
||||||
|
from apps.core.urlutils import http_url_or_empty
|
||||||
|
|
||||||
from .models import Subscription
|
from .models import Subscription
|
||||||
|
|
||||||
|
|
||||||
class AddFeedForm(forms.Form):
|
class AddFeedForm(forms.Form):
|
||||||
feed_url = forms.URLField(label="Feed URL", max_length=1000, assume_scheme="https")
|
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(
|
tag_names = forms.CharField(
|
||||||
label="Tags",
|
label="Tags",
|
||||||
max_length=500,
|
max_length=500,
|
||||||
|
|||||||
+5
-2
@@ -17,6 +17,7 @@ from io import BytesIO
|
|||||||
import defusedxml.ElementTree as DefusedET
|
import defusedxml.ElementTree as DefusedET
|
||||||
|
|
||||||
from apps.articles.models import SubscriptionTag, Tag
|
from apps.articles.models import SubscriptionTag, Tag
|
||||||
|
from apps.core.urlutils import http_url_or_empty
|
||||||
|
|
||||||
from .models import Feed, Subscription
|
from .models import Feed, Subscription
|
||||||
|
|
||||||
@@ -66,7 +67,9 @@ def parse_opml(file_obj) -> list[dict]:
|
|||||||
|
|
||||||
def walk(container, ancestor_names):
|
def walk(container, ancestor_names):
|
||||||
for outline in container.findall("outline"):
|
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:
|
if xml_url:
|
||||||
tag_names = list(ancestor_names)
|
tag_names = list(ancestor_names)
|
||||||
category_attr = outline.get("category")
|
category_attr = outline.get("category")
|
||||||
@@ -79,7 +82,7 @@ def parse_opml(file_obj) -> list[dict]:
|
|||||||
{
|
{
|
||||||
"feed_url": xml_url,
|
"feed_url": xml_url,
|
||||||
"title": outline.get("title") or outline.get("text") or "",
|
"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,
|
"tag_names": tag_names,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from django.utils import timezone
|
|||||||
|
|
||||||
from apps.articles.models import Article, PurgedArticle
|
from apps.articles.models import Article, PurgedArticle
|
||||||
from apps.core.models import SiteSettings
|
from apps.core.models import SiteSettings
|
||||||
|
from apps.core.urlutils import resolve_http_url
|
||||||
from apps.feeds.models import Feed
|
from apps.feeds.models import Feed
|
||||||
|
|
||||||
from .ssrf_guard import SSRFBlocked, safe_get
|
from .ssrf_guard import SSRFBlocked, safe_get
|
||||||
@@ -222,7 +223,7 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
|||||||
channel_title = parsed.feed.get("title")
|
channel_title = parsed.feed.get("title")
|
||||||
if channel_title and not feed.title:
|
if channel_title and not feed.title:
|
||||||
feed.title = channel_title[:500]
|
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:
|
if channel_link and not feed.site_url:
|
||||||
feed.site_url = channel_link[:1000]
|
feed.site_url = channel_link[:1000]
|
||||||
|
|
||||||
@@ -249,7 +250,7 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
|||||||
if guid in parsed_entries:
|
if guid in parsed_entries:
|
||||||
continue
|
continue
|
||||||
title = (getattr(entry, "title", "") or "")[:1000]
|
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]
|
author = (getattr(entry, "author", "") or "")[:500]
|
||||||
summary = (getattr(entry, "summary", "") or "")[:5000]
|
summary = (getattr(entry, "summary", "") or "")[:5000]
|
||||||
content_html = sanitize_html(_entry_content_html(entry))
|
content_html = sanitize_html(_entry_content_html(entry))
|
||||||
|
|||||||
@@ -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)
|
responses.add(responses.GET, FEED_URL, body=xml, status=200)
|
||||||
|
|
||||||
assert len(fetch_and_ingest(feed).new_article_ids) == 1
|
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"
|
||||||
@@ -192,3 +192,21 @@ class TestExportOPML:
|
|||||||
exported = export_opml(alice)
|
exported = export_opml(alice)
|
||||||
assert b"<opml" in exported
|
assert b"<opml" in exported
|
||||||
assert b"<outline" not 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"] == ""
|
||||||
@@ -117,3 +117,14 @@ def test_add_feed_view_rejects_duplicate_subscription(client, two_users):
|
|||||||
|
|
||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
assert Subscription.objects.filter(user=alice, feed=feed).count() == 1
|
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()
|
||||||
@@ -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
|
assert f'data-url="{reverse("reader:new_since")}"' in content
|
||||||
client.logout()
|
client.logout()
|
||||||
assert "tab-badge" not in client.get(reverse("accounts:login")).content.decode()
|
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
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
{% load safe_urls %}
|
||||||
{% block title %}Dashboard — Evection{% endblock %}
|
{% block title %}Dashboard — Evection{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Dashboard</h1>
|
<h1>Dashboard</h1>
|
||||||
@@ -27,7 +28,7 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{% for feed in erroring_feeds %}
|
{% for feed in erroring_feeds %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><a href="{{ feed.site_url|default:feed.feed_url }}">{{ feed.title|default:feed.feed_url }}</a></td>
|
<td><a href="{{ feed.site_url|default:feed.feed_url|safe_http_url }}">{{ feed.title|default:feed.feed_url }}</a></td>
|
||||||
<td>{{ feed.subscriber_count }}</td>
|
<td>{{ feed.subscriber_count }}</td>
|
||||||
<td>{{ feed.get_last_fetch_status_display }}</td>
|
<td>{{ feed.get_last_fetch_status_display }}</td>
|
||||||
<td>{{ feed.fetch_error_count }}</td>
|
<td>{{ feed.fetch_error_count }}</td>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<div class="article-content-header">
|
{% load safe_urls %}<div class="article-content-header">
|
||||||
<h2 class="article-content-title">{{ article.title|default:"(untitled)" }}</h2>
|
<h2 class="article-content-title">{{ article.title|default:"(untitled)" }}</h2>
|
||||||
<p class="article-meta">
|
<p class="article-meta">
|
||||||
{% if article.feed.favicon_url %}<img src="{{ article.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
|
{% if article.feed.favicon_url %}<img src="{{ article.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
{# fetched_at stands in when the feed gave no publish date #}
|
{# fetched_at stands in when the feed gave no publish date #}
|
||||||
· <time datetime="{{ article.published_at|default:article.fetched_at|date:"c" }}" data-local-format="long">{{ article.published_at|default:article.fetched_at|date:"M j, Y H:i" }}</time>
|
· <time datetime="{{ article.published_at|default:article.fetched_at|date:"c" }}" data-local-format="long">{{ article.published_at|default:article.fetched_at|date:"M j, Y H:i" }}</time>
|
||||||
{% if article.author %} · {{ article.author }}{% endif %}
|
{% if article.author %} · {{ article.author }}{% endif %}
|
||||||
{% if article.link %} · <a href="{{ article.link }}" target="_blank" rel="noopener noreferrer">Original ↗</a>{% endif %}
|
{% with safe_link=article.link|safe_http_url %}{% if safe_link %} · <a href="{{ safe_link }}" target="_blank" rel="noopener noreferrer">Original ↗</a>{% endif %}{% endwith %}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
Reference in new issue
Block a user