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>
This commit is contained in:
ksmithandClaude Sonnet 5.5 committed 2026-11-01 11:58:49 -07:00
1 parent 41f99e9405
commit 698940763f
6 files changed
+240 -40

No files matched your search

+74
View File
@@ -210,3 +210,77 @@ class TestUnsafeOPMLUrls:
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"]