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

+50 -3
View File
@@ -173,9 +173,9 @@ def test_import_opml_dispatches_background_task_above_threshold(client, alice, m
dispatched = {}
def fake_delay(user_id, content):
def fake_delay(user_id, upload_key):
dispatched["user_id"] = user_id
dispatched["content"] = content
dispatched["upload_key"] = upload_key
from . import views as feeds_views
@@ -187,6 +187,11 @@ def test_import_opml_dispatches_background_task_above_threshold(client, alice, m
assert response.status_code == 302
assert dispatched["user_id"] == alice.id
# The task gets a reference to the document, not the document itself.
from django.core.cache import cache
assert dispatched["upload_key"].startswith("opml-import:")
assert cache.get(dispatched["upload_key"]) == SIMPLE_OPML
# The background task was dispatched instead of a synchronous import.
assert not Subscription.objects.filter(user=alice).exists()
@@ -204,9 +209,51 @@ def test_export_opml_view_returns_downloadable_file(client, alice, alices_subscr
@pytest.mark.django_db
def test_import_opml_task_runs_import(alice):
from django.core.cache import cache
from .tasks import import_opml_task
result = import_opml_task(alice.id, SIMPLE_OPML.decode("utf-8"))
cache.set("opml-import:test", SIMPLE_OPML, timeout=60)
result = import_opml_task(alice.id, "opml-import:test")
assert result["created"] == 2
assert Subscription.objects.filter(user=alice).count() == 2
assert cache.get("opml-import:test") is None, "the stored upload is removed once read"
@pytest.mark.django_db
def test_import_opml_task_does_nothing_if_the_upload_expired(alice):
from .tasks import import_opml_task
assert import_opml_task(alice.id, "opml-import:gone") is None
assert not Subscription.objects.filter(user=alice).exists()
@pytest.mark.django_db
def test_import_opml_rejects_an_oversized_upload(client, alice, monkeypatch):
from . import forms
monkeypatch.setattr(forms, "MAX_UPLOAD_BYTES", 100)
client.login(username="alice", password="pw-alice-123")
upload = SimpleUploadedFile("big.opml", SIMPLE_OPML, content_type="text/x-opml")
response = client.post(reverse("feeds:import_opml"), {"opml_file": upload})
assert response.status_code == 200
assert "too large" in response.content.decode()
assert not Subscription.objects.filter(user=alice).exists()
@pytest.mark.django_db
def test_import_opml_reports_a_limit_error_instead_of_a_generic_one(client, alice, monkeypatch):
from . import opml
monkeypatch.setattr(opml, "MAX_OPML_FEEDS", 1)
client.login(username="alice", password="pw-alice-123")
upload = SimpleUploadedFile("subs.opml", SIMPLE_OPML, content_type="text/x-opml")
response = client.post(reverse("feeds:import_opml"), {"opml_file": upload}, follow=True)
assert "too many feeds" in response.content.decode()
assert not Subscription.objects.filter(user=alice).exists()