Files
Evection/apps/feeds/test_opml.py
T
ksmithandClaude Sonnet 5.5 698940763f Bound OPML uploads in size, depth, count and broker payload (R2-S5, #37)
An OPML upload was read fully into memory, parsed into a whole tree,
walked recursively, and (for large imports) embedded in a Celery
message. Nothing bounded raw size, ZIP entries, nesting depth, outline
count or feed count.

Uploads are capped at 5 MB (checked on the reported size before reading)
and ZIPs at 50 entries, with the decompressed member bounded by what it
actually yields rather than only its header. Parsing now streams with
iterparse and aborts the moment a limit is crossed — 24 levels deep,
20,000 outlines, 5,000 feeds — so a hostile file never builds a huge or
deep tree; a 50,000-level nesting bomb is rejected cleanly. Limit errors
are shown to the user. Large imports go to the worker by cache reference
(deleted once read, expires after an hour) instead of as the whole
document inside the broker message.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 11:58:49 -07:00

287 lines
10 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"] == ""
class TestOPMLLimits:
def _opml(self, body):
from io import BytesIO
return BytesIO(
f'<?xml version="1.0"?><opml version="2.0"><head/><body>{body}</body></opml>'.encode()
)
def test_deep_nesting_is_rejected_without_recursing(self):
from apps.feeds.opml import MAX_OPML_DEPTH, parse_opml
depth = MAX_OPML_DEPTH + 50
body = '<outline text="x">' * depth + "</outline>" * depth
with pytest.raises(ValueError, match="nested too deeply"):
parse_opml(self._opml(body))
def test_a_pathologically_deep_file_does_not_crash_the_parser(self):
from apps.feeds.opml import parse_opml
depth = 50_000
body = "<outline>" * depth + "</outline>" * depth
with pytest.raises(ValueError):
parse_opml(self._opml(body))
def test_too_many_outlines_is_rejected(self, monkeypatch):
from apps.feeds import opml
monkeypatch.setattr(opml, "MAX_OPML_OUTLINES", 5)
body = '<outline text="folder"/>' * 6
with pytest.raises(ValueError, match="too many entries"):
opml.parse_opml(self._opml(body))
def test_too_many_feeds_is_rejected(self, monkeypatch):
from apps.feeds import opml
monkeypatch.setattr(opml, "MAX_OPML_FEEDS", 2)
body = "".join(f'<outline xmlUrl="https://e.example.com/{i}.xml"/>' for i in range(3))
with pytest.raises(ValueError, match="too many feeds"):
opml.parse_opml(self._opml(body))
def test_a_zip_with_too_many_files_is_rejected(self, monkeypatch):
import zipfile
from io import BytesIO
from apps.feeds import opml
monkeypatch.setattr(opml, "MAX_ZIP_ENTRIES", 3)
buffer = BytesIO()
with zipfile.ZipFile(buffer, "w") as archive:
for i in range(5):
archive.writestr(f"f{i}.txt", "x")
archive.writestr("subs.opml", "<opml/>")
with pytest.raises(ValueError, match="too many files"):
opml.extract_opml_bytes(buffer.getvalue())
def test_a_feed_outlines_children_are_not_imported_as_separate_feeds(self):
from apps.feeds.opml import parse_opml
body = (
'<outline xmlUrl="https://a.example.com/f.xml">'
'<outline xmlUrl="https://child.example.com/f.xml"/></outline>'
'<outline xmlUrl="https://b.example.com/f.xml"/>'
)
urls = [e["feed_url"] for e in parse_opml(self._opml(body))]
assert urls == ["https://a.example.com/f.xml", "https://b.example.com/f.xml"]