diff --git a/.gitignore b/.gitignore index 130a362..d56a4c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ __pycache__/ *.py[cod] *.sqlite3 +*.sqlite3-journal +celerybeat-schedule .env .venv/ venv/ diff --git a/apps/feeds/opml.py b/apps/feeds/opml.py index 9745151..804231b 100644 --- a/apps/feeds/opml.py +++ b/apps/feeds/opml.py @@ -9,8 +9,10 @@ untrusted input, no XXE concern there. from __future__ import annotations import xml.etree.ElementTree as ET +import zipfile from datetime import datetime from datetime import timezone as dt_timezone +from io import BytesIO import defusedxml.ElementTree as DefusedET @@ -18,6 +20,32 @@ from apps.articles.models import SubscriptionTag, Tag from .models import Feed, Subscription +# FreshRSS's own "Export" produces a .zip (an .opml.xml plus a starred- +# articles .json we don't import) rather than a bare .opml file — accept +# that directly instead of making people unzip it themselves first. +MAX_ZIPPED_OPML_SIZE = 20 * 1024 * 1024 + + +def extract_opml_bytes(uploaded_bytes: bytes) -> bytes: + """If the upload is a zip (e.g. FreshRSS's export), returns the bytes + of the .opml/.opml.xml member inside it; otherwise returns the input + unchanged, assuming it's already a bare OPML file.""" + if not zipfile.is_zipfile(BytesIO(uploaded_bytes)): + return uploaded_bytes + + with zipfile.ZipFile(BytesIO(uploaded_bytes)) as archive: + candidates = [ + info + for info in archive.infolist() + if not info.is_dir() and info.filename.lower().endswith((".opml", ".opml.xml")) + ] + if not candidates: + raise ValueError("No .opml file found inside the uploaded zip.") + member = min(candidates, key=lambda info: info.file_size) + if member.file_size > MAX_ZIPPED_OPML_SIZE: + raise ValueError("The .opml file inside the zip is implausibly large.") + return archive.read(member) + def parse_opml(file_obj) -> list[dict]: """Returns a flat list of {feed_url, title, site_url, tag_names}. diff --git a/apps/feeds/test_opml.py b/apps/feeds/test_opml.py index 5afed4b..df248c9 100644 --- a/apps/feeds/test_opml.py +++ b/apps/feeds/test_opml.py @@ -1,3 +1,4 @@ +import zipfile from io import BytesIO import pytest @@ -6,7 +7,7 @@ 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, import_opml, parse_opml +from .opml import export_opml, extract_opml_bytes, import_opml, parse_opml User = get_user_model() @@ -54,6 +55,32 @@ 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)) diff --git a/apps/feeds/test_subscription_views.py b/apps/feeds/test_subscription_views.py index e71fda2..8b561cc 100644 --- a/apps/feeds/test_subscription_views.py +++ b/apps/feeds/test_subscription_views.py @@ -135,6 +135,27 @@ def test_import_opml_view_creates_subscriptions(client, alice): assert Subscription.objects.filter(user=alice).count() == 2 +@pytest.mark.django_db +def test_import_opml_view_accepts_a_freshrss_export_zip(client, alice): + import zipfile + from io import BytesIO + + client.login(username="alice", password="pw-alice-123") + + buf = BytesIO() + with zipfile.ZipFile(buf, "w") as archive: + archive.writestr("feeds_2026-10-30.opml.xml", SIMPLE_OPML) + archive.writestr("starred_2026-10-30.json", b'{"items": []}') + upload = SimpleUploadedFile( + "freshrss_export.zip", buf.getvalue(), content_type="application/zip" + ) + + response = client.post(reverse("feeds:import_opml"), {"opml_file": upload}) + + assert response.status_code == 302 + assert Subscription.objects.filter(user=alice).count() == 2 + + @pytest.mark.django_db def test_import_opml_view_rejects_malformed_file(client, alice): client.login(username="alice", password="pw-alice-123") diff --git a/apps/feeds/views.py b/apps/feeds/views.py index 2af8307..7fa77cd 100644 --- a/apps/feeds/views.py +++ b/apps/feeds/views.py @@ -11,7 +11,7 @@ from apps.articles.models import SubscriptionTag, Tag from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm from .models import Feed, Subscription -from .opml import export_opml, import_opml, parse_opml +from .opml import export_opml, extract_opml_bytes, import_opml, parse_opml from .services.fetcher import fetch_and_ingest from .tasks import import_opml_task @@ -148,8 +148,9 @@ def unsubscribe(request, subscription_id): def import_opml_view(request): form = OPMLImportForm(request.POST or None, request.FILES or None) if request.method == "POST" and form.is_valid(): - content = form.cleaned_data["opml_file"].read() + uploaded = form.cleaned_data["opml_file"].read() try: + content = extract_opml_bytes(uploaded) entry_count = len(parse_opml(BytesIO(content))) except Exception: messages.error(request, "That file doesn't look like a valid OPML export.") diff --git a/templates/feeds/import_opml.html b/templates/feeds/import_opml.html index 2758b05..8b9c967 100644 --- a/templates/feeds/import_opml.html +++ b/templates/feeds/import_opml.html @@ -3,7 +3,10 @@ {% block content %}
Upload an OPML file exported from FreshRSS or another feed reader.
++ Upload an OPML file, or a FreshRSS "Export" .zip — the .opml.xml inside + it is read automatically, you don't need to unzip it yourself. +