fix(linkify): prevent .tld suffix from matching inside href URLs (#817) - #895
Open
Mukller wants to merge 1 commit into
Open
fix(linkify): prevent .tld suffix from matching inside href URLs (#817)#895Mukller wants to merge 1 commit into
Mukller wants to merge 1 commit into
Conversation
…itmproxy#817) The linkify regex has a leading optional group that may match zero characters (the `\.*` branch for relative references). When it matches zero characters, the negative lookbehind `(?<![/=?#&\.])` checks the char immediately before the first real character of the pattern — which is the `.` of a dotted suffix. In an href attribute like `https://example.com`, the `.` before `com` is preceded by `e` (a word character). `e` is not in `[/=?#&.]`, so the lookbehind passes and `.com` is matched as a relative reference, corrupting the URL. Adding `\w` to the lookbehind set prevents any dot that is immediately preceded by a word character from being the start of a relative-reference match. Standalone relative references like `.Foo` in prose are preceded by whitespace or punctuation and are unaffected.
Mukller
commented
Jul 29, 2026
Mukller
left a comment
Author
There was a problem hiding this comment.
Code Review
Correctness of the Fix
Root cause: linkify's regex contains \.* (zero or more dots) as one branch of
the first optional group. When this branch matches zero characters, the lookbehind
(?<![/=?#&\.]) is evaluated at the position just before the first . of the dotted
suffix. In example.com, that position is after e — a word character not in the
lookbehind set — so the check passes and .com is matched erroneously.
Fix: adding \w to the lookbehind prevents any .-led match when the immediately
preceding character is a word character. This is correct because:
| scenario | char before . |
lookbehind ([/=?#&.\w]) |
outcome |
|---|---|---|---|
https://example.com |
e (word) |
blocked ✓ | .com no longer matched |
see .Foo |
(space) |
passes | .Foo still matched |
(invoke .bar) |
( |
passes | .bar still matched |
foo.bar.baz |
o, r |
blocked | already caught by \b\w+ branch |
No Regressions
- Relative references (
.Foo,..Foo) in prose always follow whitespace or
punctuation. Adding\wto the lookbehind does not affect them. - Normal dotted identifiers (
foo.bar) are matched by the\b\w+branch, not
\.*, so the lookbehind position differs and they are unaffected. - Other URL heuristics (
(?![/#])lookahead) remain unchanged.
Change Size
One character added to a character class in one regex. Risk of unintended breakage
is minimal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #817.
When a docstring contains a Markdown/reST hyperlink whose URL ends in a TLD that
matches a project identifier (e.g.
https://example.comwhencomis a functionname), pdoc produces corrupted HTML like:
Root Cause
The
linkifyregex inrender_helpers.pyincludes a leading optional group:When
\.*matches zero characters, the lookbehind is evaluated at the positionimmediately before the first
.of the dotted suffix. Inhref="https://example.com",that position is after the
einexample.eis a word character — not in[/=?#&.]— so the lookbehind passes,.comis matched as a relative reference,and is replaced with
<a href="#com">.com</a>, corrupting the surroundinghref.Fix
Add
\wto the negative lookbehind:A
.immediately preceded by a word character is always part of a larger token(a URL segment, a dotted name already caught by the
\b\w+branch, etc.) and cannever be the first character of a standalone relative reference. Existing relative
references like
.Fooappear after whitespace or punctuation, not after\w, sothey are unaffected.
Verification