Skip to content

🐛 fix(dom): ignore template and foreign base and meta tags - #2175

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/1mrxfkfa
Nov 15, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/1mrxfkfa

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Crawlers, link checkers and rewriters resolve relative links against the document's <base href> and follow its <meta http-equiv=refresh>. 🔗 The lookups behind Document.base_url(), Document.meta_refresh() and links() walked into <template> contents and accepted base and meta elements in the SVG and MathML namespaces. A browser acts on neither: template children sit in an inert document. An element a browser never acts on could hide the base it uses, or be reported as a redirect that never happens. After this change these APIs return the base and the redirect a browser follows.

The lookups skip template contents and accept only HTML-namespace elements. The refresh check in links() applies the same namespace test as Document.meta_refresh(), so both report the same redirect. The HTML parser never produces a foreign meta, so the namespace test matters only for trees rebuilt from pickled data.

Behavior changes

  • A <base href> inside a template, or a base element in SVG or MathML, no longer sets the base URL. The next real base element applies, or the fallback.
  • Link resolution in links(), rewrite_links(), resolve_links() and extract_links() uses that same base, so extract_links(..., external_only=True) no longer judges links against a <base> inside a template.
  • A <meta http-equiv=refresh> inside a template, or in SVG or MathML, no longer redirects. links(), rewrite_links() and resolve_links() report no link for it.
  • links() still reports <a href> and other URLs inside templates. Only the refresh redirect changes.

Fixes #1685

base_url(), meta_refresh(), links() and extract_links() read the
document's <base href> or refresh <meta> by walking every node. The
walk entered template contents, which belong to an inert document, and
it accepted base and meta elements in the SVG and MathML namespaces. An
element a browser never acts on could therefore hide the one it does,
or be reported as a redirect target, so the base URL used to resolve
links was wrong.

The walks now step over template contents, and the lookups accept only
HTML-namespace elements. The walks stay single passes in document order
with no extra allocation.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/1mrxfkfa (b0773fc) with main (aceeaf9)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 4e1f309 into tox-dev:main Nov 15, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

base_url() takes the <base href> from inside a template, not from the document

1 participant