Private
Public Access
Allow only http(s) URLs from feeds and OPML, at storage and render (R2-S1, #33)
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>
This commit is contained in:
1 parent
85914f9107
commit
41f99e9405
13 files changed
+192
-7
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,12 @@
|
||||
from django import template
|
||||
|
||||
from apps.core.urlutils import http_url_or_empty
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.filter
|
||||
def safe_http_url(value):
|
||||
""" "" unless value is an absolute http(s) URL — use before putting any
|
||||
feed-supplied URL in an href (see apps.core.urlutils)."""
|
||||
return http_url_or_empty(value)
|
||||
@@ -0,0 +1,44 @@
|
||||
import pytest
|
||||
from django.template import Context, Template
|
||||
|
||||
from .urlutils import http_url_or_empty, resolve_http_url
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
[
|
||||
"javascript:alert(1)",
|
||||
"JaVaScRiPt:alert(1)",
|
||||
"java\tscript:alert(1)",
|
||||
" javascript:alert(1)",
|
||||
"data:text/html,<script>alert(1)</script>",
|
||||
"vbscript:msgbox(1)",
|
||||
"file:///etc/passwd",
|
||||
"ftp://example.com/feed.xml",
|
||||
"//example.com/no-scheme",
|
||||
"/relative/path",
|
||||
"http://",
|
||||
"",
|
||||
None,
|
||||
],
|
||||
)
|
||||
def test_unsafe_or_non_absolute_urls_are_rejected(value):
|
||||
assert http_url_or_empty(value) == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["http://example.com/a", "https://example.com/a?b=1#c"])
|
||||
def test_http_and_https_urls_pass_through(value):
|
||||
assert http_url_or_empty(value) == value
|
||||
|
||||
|
||||
def test_relative_links_resolve_against_the_feed_url_and_are_still_checked():
|
||||
base = "https://blog.example.com/feed.xml"
|
||||
assert resolve_http_url(base, "/2026/post") == "https://blog.example.com/2026/post"
|
||||
assert resolve_http_url(base, "javascript:alert(1)") == ""
|
||||
assert resolve_http_url(base, "https://other.example.com/x") == "https://other.example.com/x"
|
||||
|
||||
|
||||
def test_template_filter_blanks_unsafe_urls():
|
||||
template = Template("{% load safe_urls %}[{{ url|safe_http_url }}]")
|
||||
assert template.render(Context({"url": "javascript:alert(1)"})) == "[]"
|
||||
assert template.render(Context({"url": "https://example.com/"})) == "[https://example.com/]"
|
||||
@@ -0,0 +1,30 @@
|
||||
from urllib.parse import urljoin, urlsplit
|
||||
|
||||
ALLOWED_SCHEMES = {"http", "https"}
|
||||
|
||||
|
||||
def http_url_or_empty(value: str | None) -> str:
|
||||
"""The URL if it's an absolute http(s) URL, else "". Feed content is
|
||||
untrusted: a link of "javascript:..." or "data:..." is harmless inside
|
||||
an escaped attribute value but becomes script the moment someone
|
||||
clicks it, so every URL we store from a feed or OPML file, and every
|
||||
one we render as a link, goes through this. (urlsplit already ignores
|
||||
the tab/newline tricks browsers also ignore inside a scheme.)"""
|
||||
if not value:
|
||||
return ""
|
||||
value = value.strip()
|
||||
try:
|
||||
parts = urlsplit(value)
|
||||
except ValueError:
|
||||
return ""
|
||||
if parts.scheme.lower() not in ALLOWED_SCHEMES or not parts.netloc:
|
||||
return ""
|
||||
return value
|
||||
|
||||
|
||||
def resolve_http_url(base: str, value: str | None) -> str:
|
||||
"""Like http_url_or_empty, but first resolves a relative link (feeds
|
||||
often give "/2026/post") against the feed's own URL."""
|
||||
if not value:
|
||||
return ""
|
||||
return http_url_or_empty(urljoin(base, value.strip()))
|
||||
Reference in new issue
Block a user