Private
Public Access
A feed's channel link, an entry's link and an OPML htmlUrl were stored as given and rendered in href attributes (bulk writes skip model validation), so a malicious feed could supply a javascript: or data: link. CSP is a second layer, not the control. apps.core.urlutils accepts only absolute http(s) URLs. Fetching stores channel/entry links through it — relative links now resolve against the feed's own URL instead of being stored as dead relative paths — OPML import skips non-http(s) feed URLs and blanks unsafe htmlUrl, and the add-feed form rejects non-http(s) (URLField also allows ftp). A safe_http_url template filter guards the two places a feed-supplied URL becomes a link (the article's "Original" link and the dashboard), so values already in the database from before this fix are neutralized too. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
131 lines
4.4 KiB
Python
131 lines
4.4 KiB
Python
import pytest
|
|
import responses
|
|
from django.contrib.auth import get_user_model
|
|
from django.urls import reverse
|
|
|
|
from .models import Feed, Subscription
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
@pytest.fixture
|
|
def two_users(db):
|
|
alice = User.objects.create_user("alice", password="pw-alice-123")
|
|
bob = User.objects.create_user("bob", password="pw-bob-123")
|
|
return alice, bob
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_subscribing_to_same_feed_url_reuses_one_feed_row(two_users):
|
|
alice, bob = two_users
|
|
|
|
feed, created = Feed.objects.get_or_create(feed_url="https://example.com/feed.xml")
|
|
assert created
|
|
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
|
|
feed_again, created_again = Feed.objects.get_or_create(feed_url="https://example.com/feed.xml")
|
|
assert not created_again
|
|
assert feed_again.pk == feed.pk
|
|
|
|
Subscription.objects.create(user=bob, feed=feed_again)
|
|
|
|
assert Feed.objects.count() == 1
|
|
assert Subscription.objects.filter(feed=feed).count() == 2
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_a_user_cannot_subscribe_to_the_same_feed_twice(two_users):
|
|
alice, _ = two_users
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
|
|
with pytest.raises(Exception):
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
|
|
|
|
GOOD_FEED_XML = """<?xml version="1.0"?>
|
|
<rss version="2.0"><channel>
|
|
<title>Example Blog</title>
|
|
<item><title>First post</title><guid>guid-1</guid></item>
|
|
</channel></rss>
|
|
"""
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@responses.activate
|
|
def test_add_feed_view_subscribes_and_enqueues_the_initial_fetch(client, two_users):
|
|
"""The subscription is created synchronously; the first fetch is
|
|
dispatched via fetch_feed.delay() (T3) rather than run inline — in
|
|
tests CELERY_TASK_ALWAYS_EAGER still runs it immediately, so the
|
|
article it ingests is visible by the time the response returns."""
|
|
alice, _ = two_users
|
|
client.login(username="alice", password="pw-alice-123")
|
|
responses.add(responses.GET, "https://example.com/feed.xml", body=GOOD_FEED_XML, status=200)
|
|
|
|
response = client.post(reverse("feeds:add_feed"), {"feed_url": "https://example.com/feed.xml"})
|
|
|
|
assert response.status_code == 302
|
|
subscription = Subscription.objects.get(
|
|
user=alice, feed__feed_url="https://example.com/feed.xml"
|
|
)
|
|
assert subscription.feed.articles.filter(guid="guid-1").exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_add_feed_view_requires_login(client):
|
|
response = client.get(reverse("feeds:add_feed"))
|
|
assert response.status_code == 302
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@responses.activate
|
|
def test_add_feed_view_creates_tags_from_comma_separated_tag_names(client, two_users):
|
|
from apps.articles.models import SubscriptionTag, Tag
|
|
|
|
alice, _ = two_users
|
|
client.login(username="alice", password="pw-alice-123")
|
|
responses.add(responses.GET, "https://example.com/feed.xml", body=GOOD_FEED_XML, status=200)
|
|
|
|
response = client.post(
|
|
reverse("feeds:add_feed"),
|
|
{"feed_url": "https://example.com/feed.xml", "tag_names": "Tech, backend"},
|
|
)
|
|
|
|
assert response.status_code == 302
|
|
subscription = Subscription.objects.get(
|
|
user=alice, feed__feed_url="https://example.com/feed.xml"
|
|
)
|
|
tag_names = set(
|
|
Tag.objects.filter(
|
|
subscription_tags__subscription=subscription, subscription_tags__user=alice
|
|
).values_list("name", flat=True)
|
|
)
|
|
assert tag_names == {"Tech", "backend"}
|
|
assert SubscriptionTag.objects.filter(user=alice, subscription=subscription).count() == 2
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@responses.activate
|
|
def test_add_feed_view_rejects_duplicate_subscription(client, two_users):
|
|
alice, _ = two_users
|
|
client.login(username="alice", password="pw-alice-123")
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
|
|
response = client.post(reverse("feeds:add_feed"), {"feed_url": "https://example.com/feed.xml"})
|
|
|
|
assert response.status_code == 302
|
|
assert Subscription.objects.filter(user=alice, feed=feed).count() == 1
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@pytest.mark.parametrize("url", ["ftp://example.com/feed.xml", "javascript:alert(1)"])
|
|
def test_add_feed_rejects_non_http_urls(client, two_users, url):
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
response = client.post(reverse("feeds:add_feed"), {"feed_url": url})
|
|
|
|
assert response.status_code == 200
|
|
assert not Feed.objects.filter(feed_url=url).exists()
|