/proxy's regex-based HTML rewriting is fragile and applies site-specific hacks to every proxied page #5
Labels
No labels
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
paul_loth/krautspaceTV#5
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
backend/app.py's/proxyroute rewrites fetched HTML using several hand-rolled regexes applied unconditionally to every proxied page, regardless of hostname:SCRIPT_TAG_RE/HEAVY_SCRIPT_HOSTS— strips<script src="...">tags fromopencmp.net/cdntrf.com(fine, scoped by host).THEME_LINK_RE— force-rewrites any<link id="theme">tag'slight.min.csstodark.min.css. This is adbf.finalrewind.org-specific convention (their theme-switch mechanism), but nothing scopes it to that host — if some other proxied site happens to useid="theme"with alight.min.css-style href, this will silently point it at a stylesheet path that doesn't exist on that site, breaking its styling.INLINE_SCRIPT_RE— strips any inline<script>(nosrc) whose body contains the string"prefers-color-scheme", again on every proxied site, to stopfinalrewind.org's theme-detection script from re-asserting light mode. A legitimate, unrelated inline script on some other proxied site that happens to mention that CSS media feature would also get silently deleted.More generally, matching/removing tags with regex against arbitrary third-party HTML is inherently fragile (nested quotes, minified script bodies containing the literal string
</script>inside a JS string literal, attribute ordering, etc. can all produce wrong matches).Suggested fix
THEME_LINK_RE,INLINE_SCRIPT_RE) tourlparse(url).hostname == "dbf.finalrewind.org"(or a small per-host rewrite-rules table) instead of applying them globally.lxml.htmlorBeautifulSoup) for the tag stripping/rewriting, which handles malformed/edge-case markup correctly and makes host-scoped rules much easier to express and extend.Fixed in
a841b02(merged to main at103cdf2): THEME_LINK_RE/INLINE_SCRIPT_RE now only apply whenurlparse(url).hostname == "dbf.finalrewind.org". HEAVY_SCRIPT_HOSTS stripping, the tag injection, and COOKIE_BANNER_CSS remain global since those are legitimately host-agnostic.