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
@@ -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/]"
|
||||
Reference in new issue
Block a user