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:
+32
-1
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user