Add browser UI with in-browser signature verification

Every node now serves a search and listing-detail UI at GET /. It is
vanilla TypeScript, bundled unminified into web/dist (committed) and
embedded with go:embed, so `go build` is enough to run a node.

The browser verifies each listing itself (certificate, binding, validity,
signature, proof-of-work) with pure-JS ed25519, so a gateway can withhold
listings but not forge them. Unverifiable results are hidden and their
content is never shown. Listing text is only inserted via textContent and
the UI is served with a strict CSP.

Canonical JSON is reimplemented in TypeScript and tested against a golden
fixture signed by the Go code. /api/v1/status now reports difficulty.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 18:29:40 +01:00
co-authored by Claude Sonnet 5.5
parent 918e16ed37
commit 3fd40241bc
30 changed files with 6418 additions and 5 deletions
+32 -1
View File
@@ -22,6 +22,7 @@ import (
"git.stinnesbeck.com/nils/allmende/internal/listing"
"git.stinnesbeck.com/nils/allmende/internal/p2p"
"git.stinnesbeck.com/nils/allmende/internal/store"
"git.stinnesbeck.com/nils/allmende/web"
)
const (
@@ -57,7 +58,35 @@ func New(cfg Config) http.Handler {
mux.HandleFunc("POST /api/v1/listings", s.admin(s.create))
mux.HandleFunc("PUT /api/v1/listings/{id}", s.admin(s.update))
mux.HandleFunc("DELETE /api/v1/listings/{id}", s.admin(s.remove))
return mux
mux.Handle("GET /", s.ui())
return secure(mux)
}
// contentSecurityPolicy lets the UI load only its own script and styles and
// talk only to its own node. Listing text comes from strangers; the UI never
// inserts it as HTML, and this policy is the second line of defence.
const contentSecurityPolicy = "default-src 'none'; script-src 'self'; style-src 'self'; " +
"img-src 'self' data:; connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"
// ui serves the embedded browser UI.
func (s *server) ui() http.Handler {
files := http.FileServerFS(web.Files())
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h.Set("Content-Security-Policy", contentSecurityPolicy)
h.Set("Referrer-Policy", "no-referrer")
h.Set("Cross-Origin-Opener-Policy", "same-origin")
h.Set("Cache-Control", "no-cache") // revalidate, so upgrades reach users
files.ServeHTTP(w, r)
})
}
// secure adds headers every response should carry.
func secure(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Content-Type-Options", "nosniff")
next.ServeHTTP(w, r)
})
}
// --- reading --------------------------------------------------------------
@@ -76,6 +105,8 @@ func (s *server) status(w http.ResponseWriter, r *http.Request) {
"regions": s.Regions,
"peers": s.Node.PeerCount(),
"listings": n,
// Lets the browser report how much anti-spam work a listing carries.
"difficulty": s.Difficulty,
})
}
+66
View File
@@ -209,6 +209,72 @@ func TestSearchParameterValidation(t *testing.T) {
}
}
func TestServesTheWebUIWithStrictHeaders(t *testing.T) {
f := newFixture(t, token)
get := func(path string) (*http.Response, string) {
resp, err := http.Get(f.srv.URL + path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
return resp, string(b)
}
resp, body := get("/")
if resp.StatusCode != 200 || !strings.Contains(body, `<script type="module" src="app.js">`) {
t.Fatalf("index: %d %.80s", resp.StatusCode, body)
}
csp := resp.Header.Get("Content-Security-Policy")
for _, want := range []string{"default-src 'none'", "script-src 'self'", "connect-src 'self'", "frame-ancestors 'none'"} {
if !strings.Contains(csp, want) {
t.Fatalf("CSP %q lacks %q", csp, want)
}
}
if strings.Contains(csp, "unsafe-inline") || strings.Contains(csp, "unsafe-eval") {
t.Fatalf("CSP must not allow inline or eval: %q", csp)
}
resp, body = get("/app.js")
if resp.StatusCode != 200 || !strings.Contains(resp.Header.Get("Content-Type"), "javascript") {
t.Fatalf("app.js: %d %s", resp.StatusCode, resp.Header.Get("Content-Type"))
}
if !strings.Contains(body, "allmende-listing-v1") {
t.Fatal("the embedded bundle does not contain the verifier; rebuild it with `npm run build` in web/")
}
if resp, _ = get("/style.css"); resp.StatusCode != 200 {
t.Fatalf("style.css: %d", resp.StatusCode)
}
if resp, _ = get("/nope.js"); resp.StatusCode != 404 {
t.Fatalf("unknown file: %d, want 404", resp.StatusCode)
}
// The API is untouched and also refuses content sniffing.
resp, _ = get("/api/v1/status")
if resp.StatusCode != 200 || resp.Header.Get("X-Content-Type-Options") != "nosniff" {
t.Fatalf("status: %d nosniff=%q", resp.StatusCode, resp.Header.Get("X-Content-Type-Options"))
}
if req, _ := http.NewRequest("POST", f.srv.URL+"/", nil); req != nil {
if r, err := http.DefaultClient.Do(req); err == nil {
r.Body.Close()
if r.StatusCode != 405 {
t.Fatalf("POST /: %d, want 405", r.StatusCode)
}
}
}
}
func TestStatusReportsDifficultyForTheBrowserVerifier(t *testing.T) {
f := newFixture(t, token)
_, body := f.do(t, "GET", "/api/v1/status", "", false)
var st map[string]any
_ = json.Unmarshal(body, &st)
if st["difficulty"] != float64(difficulty) {
t.Fatalf("difficulty = %v, want %d", st["difficulty"], difficulty)
}
}
func TestStatus(t *testing.T) {
f := newFixture(t, token)
code, body := f.do(t, "GET", "/api/v1/status", "", false)