Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: fall back when a page declares a charset we do not know (#10221)
Nilaya12162ab5b91
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex a4bac415011..7b509013039 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -12104,12 +12104,25 @@ def _fetch_url_raw()declared = resp.headers.get_content_charset()- declared_codec = codecs.lookup(declared).name if declared else None+ declared_codec = None+ try:+ if declared:+ declared_codec = codecs.lookup(declared).name+ except (LookupError, ValueError):+ # ValueError, not only LookupError: a NUL inside the label.+ declared = Nonebom_codec = next((codec for bom, codec in _UNICODE_BOM_CODECS if raw_bytes.startswith(bom)),None,)- raw_html = raw_bytes.decode(declared or bom_codec or "utf-8", errors = "replace")+ try:+ raw_html = raw_bytes.decode(declared or bom_codec or "utf-8", errors = "replace")+ except (LookupError, ValueError):+ # Survives lookup, fails the decode: base64/hex/zlib are not text codecs,+ # "undefined" always raises, idna rejects replace. The fallback cannot raise.+ declared = None+ declared_codec = None+ raw_html = raw_bytes.decode(bom_codec or "utf-8", errors = "replace")# Catch mislabeled or unlabeled binary, including valid UTF-8 controls.if _looks_binary(raw_html):diff --git a/studio/backend/tests/test_web_fetch_binary_guard.py b/studio/backend/tests/test_web_fetch_binary_guard.pyindex 10db9539138..05a82529cec 100644--- a/studio/backend/tests/test_web_fetch_binary_guard.py+++ b/studio/backend/tests/test_web_fetch_binary_guard.py@@ -340,6 +340,48 @@ def test_declared_latin1_cp1252_punctuation_kept(monkeypatch, charset):assert "binary content" not in out[email protected]("charset", ["unicode", "utf8mb4", "x-user-defined"])+def test_unknown_charset_falls_back_instead_of_failing_the_fetch(monkeypatch, charset):+ body = b"<html><body><p>MARKERWORD in a page a browser renders fine.</p></body></html>"+ out = _fetch_with(monkeypatch, body, f"text/html; charset={charset}")+ assert "MARKERWORD" in out+ assert "unknown encoding" not in out+++def test_unknown_charset_still_reaches_the_bom_codec(monkeypatch):+ body = codecs.BOM_UTF16_LE + "MARKERWORD and more text".encode("utf-16-le")+ out = _fetch_with(monkeypatch, body, "text/plain; charset=utf16le")+ assert "MARKERWORD" in out+++ "charset",+ ["base64", "hex", "zlib", "quopri", "rot13", "uu", "bz2", "undefined", "idna"],+)+def test_unusable_codec_charset_falls_back(monkeypatch, charset):+ # These pass codecs.lookup and fail at decode: guarding the lookup alone lost the page.+ body = b"<html><body><p>MARKERWORD in a page a browser renders fine.</p></body></html>"+ out = _fetch_with(monkeypatch, body, f"text/html; charset={charset}")+ assert "MARKERWORD" in out+ assert "not a text encoding" not in out+++def test_malformed_charset_falls_back(monkeypatch):+ # A NUL in the label raises ValueError out of codecs.lookup, not LookupError.+ body = b"<html><body><p>MARKERWORD in a page a browser renders fine.</p></body></html>"+ out = _fetch_with(monkeypatch, body, 'text/html; charset="utf-8\x00"')+ assert "MARKERWORD" in out+ assert "Failed to fetch URL" not in out++[email protected]("charset", ["utf-16", "utf-16-le", "utf-32"])+def test_declared_wide_codec_still_honoured(monkeypatch, charset):+ # The guard discards a label whose decode raises; a codec decoding ITS OWN page must not.+ body = "MARKERWORD and more text".encode(charset)+ out = _fetch_with(monkeypatch, body, f"text/plain; charset={charset}")+ assert "MARKERWORD" in out++def test_high_byte_binary_not_rescued_as_cp1252(monkeypatch):# cp1252 maps these bytes to printable characters, but they lack ASCII structure.body = bytes(range(0xA0, 0x100)) * 40