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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user