Repository navigation
Trim whitespace around href values before resolving them - #222
Open
bytestream wants to merge 1 commit into
Open
bytestream wants to merge 1 commit into
bytestream wants to merge 1 commit into
Conversation
Browsers strip leading and trailing ASCII whitespace from href attribute values, but the crawler passed them to the URL resolver verbatim. An href such as "\nhttps://example.com" therefore wasn't recognised as an absolute URL and was resolved as a relative path against the page URL, yielding URLs like https://www.example.com/foo/_https://example.com (PHP's parse_url() replaces the control character with an underscore). Trim hrefs in GetLink/GetLinks, Http::crawl(), SimpleWebsitePaginator, the canonical link in Document, and HtmlDocument::getBaseHref(). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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
Browsers strip leading and trailing ASCII whitespace from
hrefattribute values (HTML spec), but the crawler passes them to the URL resolver verbatim. Markup like this exists in the wild:PHP's
parse_url()doesn't see a scheme because the string starts with a newline, and it rewrites the control character as_, soValidator::absoluteUrl()returnsnulland the resolver treats the href as a relative path:Two knock-on effects: the crawler requests a URL that doesn't exist (404), and because the mangled URL lives on the crawled host,
onSameHost()/onSameDomain()filters don't exclude what was actually an external link.Fix
Trim
hrefvalues before resolving them in every place the library reads one:GetLink/GetLinks(bothgetLinkUrl()andisSpecialNonHttpLink())Http::crawl()link discoverySimpleWebsitePaginator<link rel="canonical">inDocumentHtmlDocument::getBaseHref()(which also covers<base>handling inGetLink,Documentand the paginator)DomQueryalready trimmed attribute values, soDom::cssSelector('a')->link()was unaffected.Tests
Added regression tests for each of the above.
composer testpasses (1284 tests).composer csreports nothing.composer stanreports the same 133 pre-existing errors as onmain.🤖 Generated with Claude Code