Serve every page under a hash-based Content-Security-Policy, and update KaTeX to 0.16.47
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
fd9f665ef8
commit
fdd4dcf841
16 files changed
+207
-23
No files matched your search
+16
-4
@@ -14,6 +14,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"embed"
|
||||
"encoding/hex"
|
||||
@@ -260,12 +261,23 @@ func (n noDirs) Open(name string) (fs.File, error) {
|
||||
}
|
||||
|
||||
func render(w http.ResponseWriter, name string, data any) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := tmpl.ExecuteTemplate(w, name, data); err != nil {
|
||||
// The status is already written by then, so there is nothing honest to
|
||||
// send; the page simply ends short.
|
||||
renderStatus(w, http.StatusOK, name, data)
|
||||
}
|
||||
|
||||
// renderStatus is render with a status other than 200.
|
||||
func renderStatus(w http.ResponseWriter, code int, name string, data any) {
|
||||
// Rendered whole before anything is sent: the policy names the page's
|
||||
// inline scripts by their hashes, and a template that fails halfway is
|
||||
// an error page rather than half a page.
|
||||
var page bytes.Buffer
|
||||
if err := tmpl.ExecuteTemplate(&page, name, data); err != nil {
|
||||
http.Error(w, "template error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
securePage(w.Header(), &page)
|
||||
w.WriteHeader(code)
|
||||
_, _ = page.WriteTo(w)
|
||||
}
|
||||
|
||||
// humanLatency writes a mean latency the way a person reads one: 5 ms, 8.8 s,
|
||||
|
||||
Reference in new issue
Block a user