Private
Public Access
Accept a FreshRSS "Export" .zip directly for OPML import
FreshRSS's Export feature produces a .zip (an .opml.xml plus a starred- articles .json this app doesn't import) rather than a bare .opml file — uploading that zip as-is failed with "That file doesn't look like a valid OPML export" since defusedxml can't parse a zip as XML. apps.feeds.opml.extract_opml_bytes detects a zip upload and pulls the .opml/.opml.xml member out of it before parsing; a bare .opml upload is returned unchanged, so existing behavior is untouched. Verified against the reporter's actual FreshRSS export (18 feeds across 6 categories) both directly through extract_opml_bytes/parse_opml and as a real upload through the running app. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
753ae9efc7
commit
f818b11041
6 files changed
+86
-4
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
*.sqlite3
|
*.sqlite3
|
||||||
|
*.sqlite3-journal
|
||||||
|
celerybeat-schedule
|
||||||
.env
|
.env
|
||||||
.venv/
|
.venv/
|
||||||
venv/
|
venv/
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ untrusted input, no XXE concern there.
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
import zipfile
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import timezone as dt_timezone
|
from datetime import timezone as dt_timezone
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
import defusedxml.ElementTree as DefusedET
|
import defusedxml.ElementTree as DefusedET
|
||||||
|
|
||||||
@@ -18,6 +20,32 @@ from apps.articles.models import SubscriptionTag, Tag
|
|||||||
|
|
||||||
from .models import Feed, Subscription
|
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]:
|
def parse_opml(file_obj) -> list[dict]:
|
||||||
"""Returns a flat list of {feed_url, title, site_url, tag_names}.
|
"""Returns a flat list of {feed_url, title, site_url, tag_names}.
|
||||||
|
|||||||
+28
-1
@@ -1,3 +1,4 @@
|
|||||||
|
import zipfile
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -6,7 +7,7 @@ from django.contrib.auth import get_user_model
|
|||||||
from apps.articles.models import SubscriptionTag, Tag
|
from apps.articles.models import SubscriptionTag, Tag
|
||||||
|
|
||||||
from .models import Feed, Subscription
|
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()
|
User = get_user_model()
|
||||||
|
|
||||||
@@ -54,6 +55,32 @@ def alice(db):
|
|||||||
return User.objects.create_user("alice", password="pw-alice-123")
|
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:
|
class TestParseOPML:
|
||||||
def test_parses_tagged_and_untagged_entries(self):
|
def test_parses_tagged_and_untagged_entries(self):
|
||||||
entries = parse_opml(BytesIO(SIMPLE_OPML))
|
entries = parse_opml(BytesIO(SIMPLE_OPML))
|
||||||
|
|||||||
@@ -135,6 +135,27 @@ def test_import_opml_view_creates_subscriptions(client, alice):
|
|||||||
assert Subscription.objects.filter(user=alice).count() == 2
|
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
|
@pytest.mark.django_db
|
||||||
def test_import_opml_view_rejects_malformed_file(client, alice):
|
def test_import_opml_view_rejects_malformed_file(client, alice):
|
||||||
client.login(username="alice", password="pw-alice-123")
|
client.login(username="alice", password="pw-alice-123")
|
||||||
|
|||||||
+3
-2
@@ -11,7 +11,7 @@ from apps.articles.models import SubscriptionTag, Tag
|
|||||||
|
|
||||||
from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm
|
from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm
|
||||||
from .models import Feed, Subscription
|
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 .services.fetcher import fetch_and_ingest
|
||||||
from .tasks import import_opml_task
|
from .tasks import import_opml_task
|
||||||
|
|
||||||
@@ -148,8 +148,9 @@ def unsubscribe(request, subscription_id):
|
|||||||
def import_opml_view(request):
|
def import_opml_view(request):
|
||||||
form = OPMLImportForm(request.POST or None, request.FILES or None)
|
form = OPMLImportForm(request.POST or None, request.FILES or None)
|
||||||
if request.method == "POST" and form.is_valid():
|
if request.method == "POST" and form.is_valid():
|
||||||
content = form.cleaned_data["opml_file"].read()
|
uploaded = form.cleaned_data["opml_file"].read()
|
||||||
try:
|
try:
|
||||||
|
content = extract_opml_bytes(uploaded)
|
||||||
entry_count = len(parse_opml(BytesIO(content)))
|
entry_count = len(parse_opml(BytesIO(content)))
|
||||||
except Exception:
|
except Exception:
|
||||||
messages.error(request, "That file doesn't look like a valid OPML export.")
|
messages.error(request, "That file doesn't look like a valid OPML export.")
|
||||||
|
|||||||
@@ -3,7 +3,10 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<p><a href="{% url 'feeds:subscription_list' %}">← Back to subscriptions</a></p>
|
<p><a href="{% url 'feeds:subscription_list' %}">← Back to subscriptions</a></p>
|
||||||
<h1>Import OPML</h1>
|
<h1>Import OPML</h1>
|
||||||
<p>Upload an OPML file exported from FreshRSS or another feed reader.</p>
|
<p>
|
||||||
|
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.
|
||||||
|
</p>
|
||||||
<form method="post" enctype="multipart/form-data">
|
<form method="post" enctype="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ form.as_p }}
|
{{ form.as_p }}
|
||||||
|
|||||||
Reference in new issue
Block a user