knowledge-catalog icon indicating copy to clipboard operation
knowledge-catalog copied to clipboard

Spec-conformant HTML pages with properly-encoded multi-parameter URLs hand the agent a broken URL

Open Shyam-723 opened this issue 5 months ago • 2 comments

Bug

_extract_links in web/fetcher.py reads href attribute values directly from raw HTML without decoding HTML entities. Because valid HTML requires & inside attribute values to be written as &, a page containing something as:

<a href="https://example.com/search?q=foo&amp;lang=en">Search</a>

causes the fetcher to surface https://example.com/search?q=foo&amp;lang=en as a link. When the web agent later calls fetch_url with that string, the request is sent with a literal &amp; in the query string, which many servers either reject or silently misparse, so the agent ends up fetching the wrong page or getting a 400.

Repro Any real-world page whose query-string links are correctly HTML-encoded (which is the spec-required form) triggers this. For example, Google Search results, GitHub search pages, and most CMS-generated pages encode & as & in href attributes.

Shyam-723 avatar Jul 02 '26 15:07 Shyam-723

Confirming this from another implementation, and it points at a clean fix: the root cause is regex-scraping href out of raw HTML, which leaves the entity encoding intact. A real HTML parser decodes attribute values for you, so the bug just disappears.

For comparison, okf-kit extracts links with selectolax (a DOM parser) and the same input comes out already decoded:

  <a href="...?q=foo&amp;lang=en&amp;page=2">
  → https://example.com/search?q=foo&lang=en&page=2      (no literal &amp;)

So two options:

  1. Minimal — HTML-unescape the match before using it: html.unescape(href) decodes &amp;, &#38;, etc. Cheap, keeps the existing regex.
  2. Sturdier — pull href from an HTML parser (selectolax / lxml / BeautifulSoup) instead of regex. Decoding is automatic, and you also stop tripping on adjacent edge cases: href split across lines, single vs. double quotes, or &amp; inside other attributes.

Worth flagging the mirror image, since it bites on output too: a URL you scraped should be stored decoded (&), and any HTML you later emit re-encodes it. Round-tripping through raw strings is where these regressions creep back in — a parse-in / serialize-out discipline avoids both directions.

vinodborole avatar Jul 07 '26 11:07 vinodborole

Confirmed from another implementation (I maintain okf-kit, an independent OKF producer/consumer) — the root cause is regex-scraping href out of raw HTML, which leaves the entity encoding intact. Opened #174 with a fix.

It parses href with BeautifulSoup (already a dependency via markdownify) instead of the regex, so attribute entities decode per the HTML spec. One subtlety worth flagging: the tempting one-liner html.unescape(href) is itself wrong for URLs — it over-decodes query params that match no-semicolon legacy entities, turning ?a=1&copy=2&reg=3 into ?a=1©=2®=3. Parsing the attribute avoids that (&amp;copy= → &copy=, not ©), and there's a regression test for it.

vinodborole avatar Jul 07 '26 12:07 vinodborole