Private
Public Access
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>
213 lines
7.5 KiB
Python
213 lines
7.5 KiB
Python
import zipfile
|
|
from io import BytesIO
|
|
|
|
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
from .models import Feed, Subscription
|
|
from .opml import export_opml, extract_opml_bytes, import_opml, parse_opml
|
|
|
|
User = get_user_model()
|
|
|
|
SIMPLE_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<opml version="2.0">
|
|
<head><title>Subscriptions</title></head>
|
|
<body>
|
|
<outline text="Tech">
|
|
<outline type="rss" text="Example Blog" title="Example Blog"
|
|
xmlUrl="https://example.com/feed.xml" htmlUrl="https://example.com"/>
|
|
</outline>
|
|
<outline type="rss" text="Untagged Feed" title="Untagged Feed"
|
|
xmlUrl="https://untagged.example.com/feed.xml"/>
|
|
</body>
|
|
</opml>
|
|
"""
|
|
|
|
NESTED_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<opml version="2.0">
|
|
<body>
|
|
<outline text="Parent">
|
|
<outline text="Child">
|
|
<outline type="rss" text="Deeply nested feed"
|
|
xmlUrl="https://nested.example.com/feed.xml"/>
|
|
</outline>
|
|
</outline>
|
|
</body>
|
|
</opml>
|
|
"""
|
|
|
|
CATEGORY_ATTR_OPML = b"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<opml version="2.0">
|
|
<body>
|
|
<outline type="rss" text="Multi-tag feed" category="Tech,Backend"
|
|
xmlUrl="https://multi.example.com/feed.xml"/>
|
|
</body>
|
|
</opml>
|
|
"""
|
|
|
|
MALFORMED_XML = b"<opml><body><outline"
|
|
|
|
|
|
@pytest.fixture
|
|
def alice(db):
|
|
return User.objects.create_user("alice", password="pw-alice-123")
|
|
|
|
|
|
def _zip_of(filename: str, content: bytes) -> bytes:
|
|
buf = BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as archive:
|
|
archive.writestr(filename, content)
|
|
return buf.getvalue()
|
|
|
|
|
|
class TestExtractOPMLBytes:
|
|
def test_bare_opml_is_returned_unchanged(self):
|
|
assert extract_opml_bytes(SIMPLE_OPML) == SIMPLE_OPML
|
|
|
|
def test_extracts_opml_from_a_freshrss_style_export_zip(self):
|
|
# FreshRSS's own "Export" produces exactly this: an .opml.xml plus
|
|
# a starred-articles .json we don't need for feed import.
|
|
zipped = _zip_of("feeds_2026-10-30.opml.xml", SIMPLE_OPML)
|
|
with zipfile.ZipFile(BytesIO(zipped), "a") as archive:
|
|
archive.writestr("starred_2026-10-30.json", b'{"items": []}')
|
|
|
|
assert extract_opml_bytes(zipped) == SIMPLE_OPML
|
|
|
|
def test_zip_with_no_opml_member_raises(self):
|
|
zipped = _zip_of("starred_2026-10-30.json", b'{"items": []}')
|
|
with pytest.raises(ValueError):
|
|
extract_opml_bytes(zipped)
|
|
|
|
|
|
class TestParseOPML:
|
|
def test_parses_tagged_and_untagged_entries(self):
|
|
entries = parse_opml(BytesIO(SIMPLE_OPML))
|
|
|
|
assert len(entries) == 2
|
|
tagged = next(e for e in entries if e["feed_url"] == "https://example.com/feed.xml")
|
|
assert tagged["tag_names"] == ["Tech"]
|
|
assert tagged["title"] == "Example Blog"
|
|
|
|
untagged = next(
|
|
e for e in entries if e["feed_url"] == "https://untagged.example.com/feed.xml"
|
|
)
|
|
assert untagged["tag_names"] == []
|
|
|
|
def test_nesting_produces_a_tag_per_level(self):
|
|
entries = parse_opml(BytesIO(NESTED_OPML))
|
|
|
|
assert len(entries) == 1
|
|
# Unlike the old single-folder scheme, each enclosing level becomes
|
|
# its own tag rather than collapsing to the nearest one.
|
|
assert entries[0]["tag_names"] == ["Parent", "Child"]
|
|
|
|
def test_category_attribute_produces_multiple_tags(self):
|
|
entries = parse_opml(BytesIO(CATEGORY_ATTR_OPML))
|
|
|
|
assert len(entries) == 1
|
|
assert entries[0]["tag_names"] == ["Tech", "Backend"]
|
|
|
|
def test_malformed_xml_raises(self):
|
|
with pytest.raises(Exception):
|
|
parse_opml(BytesIO(MALFORMED_XML))
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestImportOPML:
|
|
def test_creates_feeds_tags_and_subscriptions(self, alice):
|
|
result = import_opml(alice, BytesIO(SIMPLE_OPML))
|
|
|
|
assert result == {"total": 2, "created": 2, "already_subscribed": 0}
|
|
assert Tag.objects.filter(user=alice, name="Tech").exists()
|
|
|
|
tagged_sub = Subscription.objects.get(
|
|
user=alice, feed__feed_url="https://example.com/feed.xml"
|
|
)
|
|
assert SubscriptionTag.objects.filter(
|
|
user=alice, subscription=tagged_sub, tag__name="Tech"
|
|
).exists()
|
|
|
|
untagged_sub = Subscription.objects.get(
|
|
user=alice, feed__feed_url="https://untagged.example.com/feed.xml"
|
|
)
|
|
assert not SubscriptionTag.objects.filter(user=alice, subscription=untagged_sub).exists()
|
|
|
|
def test_reimporting_the_same_file_does_not_duplicate(self, alice):
|
|
import_opml(alice, BytesIO(SIMPLE_OPML))
|
|
result = import_opml(alice, BytesIO(SIMPLE_OPML))
|
|
|
|
assert result == {"total": 2, "created": 0, "already_subscribed": 2}
|
|
assert Subscription.objects.filter(user=alice).count() == 2
|
|
assert Tag.objects.filter(user=alice, name="Tech").count() == 1
|
|
|
|
def test_reuses_existing_feed_across_users(self, alice):
|
|
bob = User.objects.create_user("bob", password="pw-bob-123")
|
|
import_opml(alice, BytesIO(SIMPLE_OPML))
|
|
import_opml(bob, BytesIO(SIMPLE_OPML))
|
|
|
|
assert Feed.objects.filter(feed_url="https://example.com/feed.xml").count() == 1
|
|
assert (
|
|
Subscription.objects.filter(feed__feed_url="https://example.com/feed.xml").count() == 2
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestExportOPML:
|
|
def test_export_round_trips_through_import(self, alice):
|
|
import_opml(alice, BytesIO(SIMPLE_OPML))
|
|
|
|
exported = export_opml(alice)
|
|
assert b'category="Tech"' in exported
|
|
|
|
bob = User.objects.create_user("bob", password="pw-bob-123")
|
|
result = import_opml(bob, BytesIO(exported))
|
|
|
|
assert result["created"] == 2
|
|
tagged_sub = Subscription.objects.get(
|
|
user=bob, feed__feed_url="https://example.com/feed.xml"
|
|
)
|
|
assert SubscriptionTag.objects.filter(
|
|
user=bob, subscription=tagged_sub, tag__name="Tech"
|
|
).exists()
|
|
untagged_sub = Subscription.objects.get(
|
|
user=bob, feed__feed_url="https://untagged.example.com/feed.xml"
|
|
)
|
|
assert not SubscriptionTag.objects.filter(user=bob, subscription=untagged_sub).exists()
|
|
|
|
def test_export_represents_multiple_tags_as_comma_separated_category(self, alice):
|
|
feed = Feed.objects.create(feed_url="https://multi.example.com/feed.xml")
|
|
subscription = Subscription.objects.create(user=alice, feed=feed)
|
|
for name in ["Tech", "Backend"]:
|
|
tag, _ = Tag.objects.get_or_create(user=alice, name=name)
|
|
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=tag)
|
|
|
|
exported = export_opml(alice)
|
|
|
|
# Alphabetical (export_opml orders by tag name), not insertion order.
|
|
assert b'category="Backend,Tech"' in exported
|
|
|
|
def test_export_with_no_subscriptions(self, alice):
|
|
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"] == ""
|