Files
ksmithandClaude Sonnet 5.5 698940763f 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>
2026-11-01 11:58:49 -07:00

197 lines
7.8 KiB
Python

import logging
import uuid
from io import BytesIO
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.views.decorators.http import require_POST
from apps.articles.forms import AddTagForm
from apps.articles.models import SubscriptionTag, Tag
from .forms import AddFeedForm, EditSubscriptionForm, OPMLImportForm
from .models import Feed, Subscription
from .opml import MAX_UPLOAD_BYTES, export_opml, extract_opml_bytes, import_opml, parse_opml
from .tasks import enqueue_fetch, import_opml_task
logger = logging.getLogger(__name__)
OPML_ASYNC_THRESHOLD = 50
OPML_UPLOAD_TTL_SECONDS = 60 * 60
@login_required
def add_feed(request):
form = AddFeedForm(request.POST or None)
if request.method == "POST" and form.is_valid():
feed_url = form.cleaned_data["feed_url"]
feed, _created = Feed.objects.get_or_create(feed_url=feed_url)
subscription, created = Subscription.objects.get_or_create(user=request.user, feed=feed)
if not created:
messages.info(request, "You're already subscribed to that feed.")
return redirect("reader:stream")
tag_names = [
name.strip() for name in form.cleaned_data["tag_names"].split(",") if name.strip()
]
for tag_name in tag_names:
tag, _ = Tag.objects.get_or_create(user=request.user, name=tag_name)
SubscriptionTag.objects.get_or_create(
user=request.user, subscription=subscription, tag=tag
)
# Enqueue the initial fetch instead of running it inline (T3) — a
# slow or large feed would otherwise tie up a Gunicorn worker for
# the duration of the request; fetch_feed handles the save/error
# bookkeeping the same way the periodic poll's dispatch does.
try:
enqueue_fetch(feed.pk)
except Exception:
# The subscription itself is saved; with the broker down the
# periodic poller picks the (never-fetched) feed up once it's
# back, so this isn't worth failing the request over.
logger.exception("Could not queue the initial fetch for feed %s", feed.pk)
messages.success(
request,
f"Subscribed to {feed_url}. Fetching its articles now — "
"refresh in a few seconds if you don't see them yet.",
)
return redirect("reader:stream")
return render(request, "feeds/add_feed.html", {"form": form})
@login_required
def subscription_list(request):
subscriptions = list(
Subscription.objects.filter(user=request.user)
.select_related("feed")
.order_by("order", "id")
)
tags_by_subscription = {}
for row in SubscriptionTag.objects.filter(user=request.user).select_related("tag"):
tags_by_subscription.setdefault(row.subscription_id, []).append(row.tag)
for subscription in subscriptions:
subscription.tags = tags_by_subscription.get(subscription.pk, [])
return render(request, "feeds/subscriptions.html", {"subscriptions": subscriptions})
@login_required
def edit_subscription(request, subscription_id):
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
form = EditSubscriptionForm(request.POST or None, instance=subscription, user=request.user)
if request.method == "POST" and form.is_valid():
form.save()
messages.success(request, f"Updated '{subscription.display_title}'.")
return redirect("feeds:subscription_list")
return render(
request, "feeds/edit_subscription.html", {"form": form, "subscription": subscription}
)
@login_required
def subscription_tags_partial(request, subscription_id):
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
tags = Tag.objects.filter(
subscription_tags__subscription=subscription, subscription_tags__user=request.user
)
# Starred/Saved for later categorize articles, not feeds — don't
# suggest them here (see Tag.is_system).
all_user_tags = Tag.objects.filter(user=request.user, is_system=False).exclude(
pk__in=[t.pk for t in tags]
)
return render(
request,
"feeds/_subscription_tags.html",
{"subscription": subscription, "tags": tags, "all_user_tags": all_user_tags},
)
@login_required
@require_POST
def add_tag_to_subscription(request, subscription_id):
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
form = AddTagForm(request.POST)
if form.is_valid():
tag_name = form.cleaned_data["tag_name"].strip()
if tag_name:
tag, _ = Tag.objects.get_or_create(user=request.user, name=tag_name)
SubscriptionTag.objects.get_or_create(
user=request.user, subscription=subscription, tag=tag
)
return subscription_tags_partial(request, subscription_id)
@login_required
@require_POST
def remove_tag_from_subscription(request, subscription_id, tag_id):
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
SubscriptionTag.objects.filter(
user=request.user, subscription=subscription, tag_id=tag_id
).delete()
return subscription_tags_partial(request, subscription_id)
@login_required
@require_POST
def unsubscribe(request, subscription_id):
subscription = get_object_or_404(Subscription, user=request.user, pk=subscription_id)
title = subscription.display_title
subscription.delete()
messages.success(request, f"Unsubscribed from '{title}'.")
return redirect("feeds:subscription_list")
@login_required
def import_opml_view(request):
form = OPMLImportForm(request.POST or None, request.FILES or None)
if request.method == "POST" and form.is_valid():
uploaded = form.cleaned_data["opml_file"].read(MAX_UPLOAD_BYTES + 1)
try:
content = extract_opml_bytes(uploaded)
entry_count = len(parse_opml(BytesIO(content)))
except ValueError as exc:
# Our own limit/format errors carry a user-presentable message.
messages.error(request, str(exc))
return redirect("feeds:import_opml")
except Exception:
messages.error(request, "That file doesn't look like a valid OPML export.")
return redirect("feeds:import_opml")
if entry_count > OPML_ASYNC_THRESHOLD:
# Hand the worker a reference, not the document: a multi-MB
# file doesn't belong inside a broker message.
upload_key = f"opml-import:{uuid.uuid4().hex}"
try:
cache.set(upload_key, content, timeout=OPML_UPLOAD_TTL_SECONDS)
import_opml_task.delay(request.user.id, upload_key)
except Exception:
logger.exception("Could not queue the OPML import")
messages.error(request, "Couldn't queue that import right now — try again shortly.")
return redirect("feeds:import_opml")
messages.success(request, f"Importing {entry_count} feeds in the background.")
else:
result = import_opml(request.user, BytesIO(content))
messages.success(
request,
f"Imported {result['created']} feed(s) "
f"({result['already_subscribed']} already subscribed).",
)
return redirect("feeds:subscription_list")
return render(request, "feeds/import_opml.html", {"form": form})
@login_required
def export_opml_view(request):
content = export_opml(request.user)
response = HttpResponse(content, content_type="text/x-opml")
response["Content-Disposition"] = 'attachment; filename="evection-subscriptions.opml"'
return response