Render maths in the chat, without letting the model write markup
CI / test (push) Successful in 1m20s

Replies are plain text, so a model asked about an integral answers with
\int_0^1 x^2 dx and the reader gets the source.

KaTeX is vendored into the binary rather than loaded from a CDN. This gateway
makes no outbound requests of its own and is expected to run where it could
not make one; a chat page that silently loses its maths on an air-gapped
cluster would be worse than one that never had it. 600 KB of JS, CSS and WOFF2
fonts, which the stripped binary absorbs without changing its rounded size.

The ordering is the safety argument, and there is a test for it rather than a
comment alone. The reply is put in place with textContent exactly as before,
and only then walked for maths: renderMathInElement replaces the spans it
recognises, so nothing the model wrote is ever parsed as HTML and the markup
that appears is KaTeX's. trust stays false — KaTeX's default, and what keeps
\href and \includegraphics out of a reply — and throwOnError is off so one
malformed expression does not take the rest with it.

Rendered once the reply is complete. Typesetting each chunk would try to set
half an expression on every token and flicker through the whole answer.

The static handler now serves a small embedded asset tree rather than one
file, so it moved to its own embed: a file server over the tree that also
carries the templates would serve those too. Directory listings are refused —
nothing links to one, so the only requests are from somebody looking around.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5 committed 2026-09-16 17:03:46 +02:00
1 parent 3b67b1fe42
commit cbf276a41b
29 files changed
+240 -21

No files matched your search

+17 -6
View File
@@ -226,15 +226,26 @@ func (emptySource) Models() []adminapi.ModelView { return nil }
func (emptySource) Replicas(string) []adminapi.ReplicaView { return nil }
func (emptySource) Explain(string) adminapi.ExplainView { return adminapi.ExplainView{} }
func TestStaticIsNotAFileServer(t *testing.T) {
// A file server over the embedded templates would also serve the templates,
// which is harmless today and the kind of thing that stops being harmless.
func TestStaticServesAssetsAndNothingElse(t *testing.T) {
// The assets live in their own embed. A file server over the tree that also
// carries the templates would serve those as well, and a directory listing
// would publish the shape of the tree to nobody's benefit.
h := Static()
if code, _ := get(t, h, "http://x/_static/base.css", nil); code != http.StatusOK {
t.Errorf("the stylesheet is not served: %d", code)
for _, path := range []string{
"/_static/base.css",
"/_static/katex/katex.min.css",
"/_static/katex/katex.min.js",
"/_static/katex/auto-render.min.js",
"/_static/katex/fonts/KaTeX_Main-Regular.woff2",
} {
if code, _ := get(t, h, "http://x"+path, nil); code != http.StatusOK {
t.Errorf("%s is not served: %d", path, code)
}
}
for _, path := range []string{
"/_static/dashboard.html", "/_static/", "/_static/../web.go",
"/_static/dashboard.html", "/_static/chat.html",
"/_static/", "/_static/katex/", "/_static/katex/fonts/",
"/_static/../web.go",
} {
if code, _ := get(t, h, "http://x"+path, nil); code == http.StatusOK {
t.Errorf("%s was served", path)