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>
31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
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()))
|