fix(web): decode HTML entities in href so multi-parameter URLs aren't broken#174
Open
vinodborole wants to merge 1 commit into
Open
fix(web): decode HTML entities in href so multi-parameter URLs aren't broken#174vinodborole wants to merge 1 commit into
vinodborole wants to merge 1 commit into
Conversation
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.
Fixes #170.
_extract_linksscrapedhrefvalues out of raw HTML with a regex, so HTML entities in attribute values were never decoded. Because valid HTML requires&inside an href to be written as&, a link likewas surfaced verbatim as
...?q=foo&lang=en, andfetch_urlthen requested a URL with a literal&in the query string — which servers reject or misparse. This hits any correctly-encoded page (Google/GitHub search results, most CMS output).Fix: parse
hrefwith BeautifulSoup — already a dependency viamarkdownify— instead of a raw-text regex. The HTML parser decodes attribute entities per the spec.Notably, the tempting one-liner (
html.unescape(href)) is not correct here: it uses HTML's text rules and over-decodes query params that match no-semicolon legacy entities, turning?a=1©=2®=3into?a=1©=2®=3. Parsing the attribute avoids that —&copy=decodes to©=, not©. A regression test guards this.Also removes the now-unused
_HREF_RE, and as a side benefit the parser handleshrefs the regex missed (across newlines, unquoted, or after other attributes).Tests
All green (
PYTHONPATH=src pytest okf/tests/test_web_fetcher.py, 7 passed):©=/®=stay literal, not©/®),&).Known limitation: genuinely malformed HTML with a bare
©=(invalid — should be&copy=) remains ambiguous; matching a browser's full "ambiguous ampersand" tokenizer rule would need a stricter parser. Out of scope for the reported (spec-conformant) case.