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
@@ -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}.
|
||||
|
||||
Reference in new issue
Block a user