Files
strata/serve/test_web_math.py
Niko1221andDavid a965c962cf Web chat: draw math with the bundled KaTeX, opt-in and offline (#2972, #2974)
Chat settings > Render math (off by default). When it is on, \( \), \[ \], $x$
and $$ $$ in an answer are drawn as formulas by KaTeX 0.19.0, which is vendored
in serve/web/vendor/katex (script, style, 20 woff2 fonts, MIT licence and a
PROVENANCE.json with the npm integrity and the sha256 of every file, so the page
needs no network). Nothing of KaTeX loads until an answer holds a formula.

The shared core (math.js, from #2972) keeps the text of every message exactly as
the model wrote it: formulas are swapped for placeholders before the Markdown
runs and drawn over them afterwards, so history, copying, the Markdown export
and the requests never see the drawing. Code, link targets, prices ($5 and $10)
and a formula still being streamed stay text. A formula that fails, is over
16 KiB or times out stays as its source text.

Safety: KaTeX runs with trust off (no \href, \url, \includegraphics, \htmlClass:
they are drawn as their own name), maxExpand 1000, maxSize 20 and a fresh macro
table per formula; it builds DOM nodes itself, so no HTML string from a model
reaches the page. A formula already drawn is placed again at once while an
answer streams, so it does not flicker back to source.

serve/test_web_math.js runs the real vendored KaTeX against hostile formulas;
serve/test_web_math.py checks the vendored files against PROVENANCE.json and
that nothing outside this server is addressed. serve/web/vendor is -text in
.gitattributes so the hashes hold on a Windows checkout.

Ported by hand from PRs #2972 (core) and #2974 (KaTeX) by CC-David-CC.

Co-authored-by: David <336302908+CC-David-CC@users.noreply.github.com>
2026-10-26 19:52:56 +01:00

108 lines
5.2 KiB
Python

"""Chat math on the served page (serve/web/math.js, math-adapter.js, vendor/katex): opt-in, offline, no scripts from the model."""
from __future__ import annotations
import hashlib
import json
import re
import shutil
import subprocess
import unittest
import urllib.error
import urllib.request
from pathlib import Path
from serve.frontend import ChatTemplate
from serve.server import ByteTokenizer, MockEngine, Service, serve
WEB = Path(__file__).parent / "web"
KATEX = WEB / "vendor" / "katex"
@unittest.skipUnless(shutil.which("node"), "node is not installed")
class MathInNode(unittest.TestCase):
def test_delimiters_escaping_drawing_and_hostile_formulas(self):
r = subprocess.run([shutil.which("node"), str(Path(__file__).with_name("test_web_math.js"))],
capture_output=True, text=True, timeout=120)
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
class Vendored(unittest.TestCase):
def test_the_files_are_the_ones_in_provenance(self):
prov = json.loads((KATEX / "PROVENANCE.json").read_text(encoding="utf-8"))
self.assertEqual((prov["package"], prov["version"], prov["license"]), ("katex", "0.19.0", "MIT"))
self.assertTrue(prov["integrity"].startswith("sha512-"))
on_disk = {p.relative_to(KATEX).as_posix() for p in KATEX.rglob("*") if p.is_file() and p.name != "PROVENANCE.json"}
self.assertEqual(on_disk, set(prov["sha256"]), "a file was added or removed without PROVENANCE.json")
for name, digest in prov["sha256"].items():
with self.subTest(name=name):
self.assertEqual(hashlib.sha256((KATEX / name).read_bytes()).hexdigest(), digest)
self.assertTrue((KATEX / "LICENSE").read_text(encoding="utf-8").startswith("The MIT License"))
def test_it_works_offline(self):
css = (KATEX / "dist" / "katex.min.css").read_text(encoding="utf-8")
fonts = set(re.findall(r"url\(fonts/([\w-]+\.woff2)\)", css))
self.assertEqual(len(fonts), 20)
for font in fonts:
self.assertTrue((KATEX / "dist" / "fonts" / font).is_file(), font)
# no address outside this server in anything that loads on the page
for f in (WEB / "math.js", WEB / "math-adapter.js", KATEX / "dist" / "katex.min.css"):
self.assertNotRegex(f.read_text(encoding="utf-8"), r"https?://(?!www\.w3\.org)", f.name)
self.assertNotIn("@import", css)
def test_the_page_loads_nothing_until_math_is_on(self):
html = (WEB / "index.html").read_text(encoding="utf-8")
js = (WEB / "app.js").read_text(encoding="utf-8")
self.assertIn('src="web/math.js"', html)
self.assertIn('src="web/math-adapter.js"', html)
self.assertNotIn("katex", html.lower().replace("math-adapter", ""), "KaTeX is not named in the page: the adapter loads it")
self.assertIn("math: false", js, "off by default")
self.assertIn('id="s-math"', html)
self.assertIn('aria-checked="false"', re.search(r'<button[^>]*id="s-math"[^>]*>', html).group(0))
adapter = re.sub(r"^\s*//.*$", "", (WEB / "math-adapter.js").read_text(encoding="utf-8"), flags=re.M) # the code, not its comments
self.assertIn("trust: false", adapter)
self.assertNotIn("innerHTML", adapter)
self.assertNotIn("renderToString", adapter, "KaTeX builds DOM nodes; no HTML string goes into the page")
class Served(unittest.TestCase):
@classmethod
def setUpClass(cls):
tok = ByteTokenizer()
cls.svc = Service(MockEngine(tok, "ok"), tok, ChatTemplate(Path(__file__).parent / "chat_template.jinja"))
cls.httpd = serve(cls.svc, port=0)
cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}"
@classmethod
def tearDownClass(cls):
cls.httpd.shutdown()
cls.httpd.server_close()
def get(self, path):
try:
with urllib.request.urlopen(self.base + path, timeout=10) as r:
return r.status, r.headers.get("Content-Type", ""), r.read()
except urllib.error.HTTPError as e:
return e.code, e.headers.get("Content-Type", ""), e.read()
def test_scripts_style_and_fonts_are_served(self):
for path, want in (("/web/math.js", "javascript"), ("/web/math-adapter.js", "javascript"),
("/web/vendor/katex/dist/katex.min.js", "javascript"),
("/web/vendor/katex/dist/katex.min.css", "text/css"),
("/web/vendor/katex/dist/fonts/KaTeX_Main-Regular.woff2", "font/woff2")):
with self.subTest(path=path):
code, ctype, body = self.get(path)
self.assertEqual(code, 200)
self.assertIn(want, ctype)
self.assertTrue(body)
def test_nothing_else_below_vendor(self):
for path in ("/web/vendor/katex/PROVENANCE.json", "/web/vendor/katex/LICENSE", "/web/vendor/katex/dist/fonts/x.ttf",
"/web/vendor/katex/../../app.js", "/web/vendor/", "/web/vendor/katex/dist/fonts",
"/web/vendor/katex/dist/missing.js"):
with self.subTest(path=path):
self.assertEqual(self.get(path)[0], 404)
if __name__ == "__main__":
unittest.main()