Skip to content

Studio: read web pages in the encoding the page itself declares - #11598

Merged
Lyxot merged 10 commits into
unslothai:mainfrom
NilayYadav:studio-fetch-meta-charset
Sep 23, 2026
Merged

Lyxot merged 10 commits into
unslothai:mainfrom
NilayYadav:studio-fetch-meta-charset

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

When chat reads a page that states its encoding only inside the page, which is common on Japanese and Chinese sites like kakaku.com, Studio assumes UTF-8. The model gets garbled text and says it cannot read the page.

If the server does not name the encoding and the file has no marker, Studio now reads the encoding from the top of the page, the same way browsers do.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Review + Studio UI evidence

Pinned head: 635b4ca3bed142f832da18c93db2924c0246b5c4. Codex review has converged: the last two rounds were all declined, each with a WHATWG spec reference.

I tested the real Studio chat flow in Chromium at 1280×1400, on two isolated installs: merge base 2036ee0ba and PR head 635b4ca3b. On both sides the model calls web_search on three real public pages, and Studio fetches them over the network:

Page Server header BEFORE (merge base) AFTER (PR head)
kakaku.com text/html, <meta charset="shift_jis"> 4679 U+FFFD, no 価格.com 0 U+FFFD, パソコン / ノートパソコン …
Aozora Bunko, 夏目漱石『こころ』 text/html, XML prolog + meta Shift_JIS (binary content, 524288 bytes; not readable as text) こころ / 夏目漱石 / 上 先生と私 … (16037 chars)
jma.go.jp (control) text/html; charset=UTF-8 read, 1733 chars read, 1733 chars

Readable pages: 1 → 3. The control reads the same on both sides.

PR 11598 before/after

Review fixes pushed after the first commit:

  • Labels resolve through the WHATWG label table, so euc-kr decodes as cp949, big5 as big5hkscs, gb2312/gbk as gb18030, tis-620 as cp874 and iso-8859-9 as cp1254. Labels outside the table, such as utf-7, utf-32 and punycode, are ignored.
  • Only a real <meta charset> or an http-equiv=content-type declaration counts, so charset= inside a description or a refresh URL does not.
  • An unusable label does not stop the scan. The first <meta> wins over the XML prolog.
  • Meta sniffing applies only to HTML. XML types read only the prolog, other text types are never sniffed, and a body with no Content-Type is sniffed only when it looks like HTML or XML.

Tests: test_web_fetch_binary_guard.py passes 145 tests at the head. The new cases fail on the first commit, 15 of them on an earlier version of this test file. On Modal, the web-fetch suites pass (1318), and so do the other suites that reach _fetch_url_raw (82).

CI: the one red job, Repo tests (CPU, rest), fails test_formatter_fixed_point on sd_cpp_backend.py and test_sd_cpp_qwen21_edit.py. This PR doesn't touch either file. They came to main in #11587, after this branch's base, and main's own copies aren't formatted.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed main's web fetch in studio/backend/core/inference/tools.py ignores the page's own meta charset, so pages like kakaku.com arrive garbled. Thanks for the before/after on real pages, will get this reviewed.

@Lyxot Lyxot self-assigned this Sep 23, 2026
Collapse the BOM, meta, and UTF-8 fallback into one codec so the header-failure path cannot drift from the primary path, and classify HTML versus XML explicitly in the meta prescan. The BOM precedence test now uses a UTF-8 BOM so the contradicting meta is actually visible to the prescan, and an uppercase http-equiv declaration is covered.
The prescan matched <meta anywhere in the head, so markup quoted inside another tag's attribute was taken as the page's declaration, and deleting comments before scanning could splice an attribute value into a valid label. Walk comments and whole tags in one pass, consuming quoted attribute values, as the browser prescan does.
Walking every tag let the tag name and attribute patterns overlap, so a head with many unterminated tags backtracked for tens of seconds, and a tag cut off by the scan window let the walk resume inside its attribute value. Attributes now start only after whitespace or a slash, and an unterminated tag ends the scan as in the browser prescan.
@Lyxot

Lyxot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Pushed three follow-up commits on top of this branch:

  • d6024c7 refactor: the BOM, meta, and UTF-8 fallback is now one fallback_codec, so the path used when the header charset fails can't drift from the main one. The BOM test now uses a UTF-8 BOM (the UTF-16 body hid the <meta> from the prescan, so the test passed either way). Added cases for an uppercase <META HTTP-EQUIV> and a quoted charset='x'.
  • aeee35d fix: the prescan matched <meta anywhere, so markup quoted inside another tag's attribute (e.g. <script data-x='<meta charset="windows-1252">'>) could override a UTF-8 page. Deleting comments before scanning could also splice an attribute into a valid label. Comments and whole tags are now walked in one pass, consuming quoted values, like the browser prescan.
  • 637c90b fix: attributes must start after whitespace or /, and an unterminated tag ends the scan. This keeps the tag walk linear (crafted 2 KB heads took 10-24 s before, now <0.1 ms) and stops a tag cut off by the scan window from being read from inside its attribute.

Checked against pages fetched today with no header charset: kakaku.com, itmedia.co.jp (Shift_JIS), inven.co.jp (EUC-JP) and pconline.com.cn (GBK) go from thousands of U+FFFD on main to none. UTF-8 sites are unchanged.

@Lyxot

Lyxot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Codex review and CI for head 637c90b644, run on a staging copy of this PR with the same head: Lyxot#75

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Pages that declare their charset only in <meta> or an XML prolog now decode correctly (verified on kakaku.com, itmedia, inven, pconline); the header charset and a BOM still take precedence, and UTF-8 pages are unchanged.

@Lyxot
Lyxot merged commit 06c7663 into unslothai:main Sep 23, 2026
41 checks passed
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…othai#11598)

* Studio: read web pages in the encoding the page itself declares

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Studio: decode meta charset labels the way browsers do

* Studio: only sniff meta charset in HTML responses

* Studio: sniff headerless bodies only when they look like HTML

* Studio: read the XML prolog of headerless XML bodies

* Studio: match only a complete meta tag name

* refactor(studio): simplify meta charset fallback and tighten its tests

Collapse the BOM, meta, and UTF-8 fallback into one codec so the header-failure path cannot drift from the primary path, and classify HTML versus XML explicitly in the meta prescan. The BOM precedence test now uses a UTF-8 BOM so the contradicting meta is actually visible to the prescan, and an uppercase http-equiv declaration is covered.

* fix(studio): read meta charset only from real meta tags

The prescan matched <meta anywhere in the head, so markup quoted inside another tag's attribute was taken as the page's declaration, and deleting comments before scanning could splice an attribute value into a valid label. Walk comments and whole tags in one pass, consuming quoted attribute values, as the browser prescan does.

* fix(studio): end the meta prescan at an unterminated tag

Walking every tag let the tag name and attribute patterns overlap, so a head with many unterminated tags backtracked for tens of seconds, and a tag cut off by the scan window let the walk resume inside its attribute value. Attributes now start only after whitespace or a slash, and an unterminated tag ends the scan as in the browser prescan.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lyxot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants