mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 05:04:31 +00:00
The signature covers each target as id:state:misses:requested, joined with commas. A state that could hold those separators let a relay fold a second target into the first one's state without breaking it.
159 lines
8.1 KiB
Go
159 lines
8.1 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package polling
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A claim built and signed by the observer firmware, from its own test suite.
|
|
const firmwareClaim = `{"v": 2, "origin": "Bench", "origin_id": "755C4CB9256CA7CDC4ACFDC6CFEEDA849017E5B9F9514E99191BD67E0B0D4276", "timestamp": "2025-11-28T14:26:40.000000+00:00", "plugin": "meshcore.observer_fw/test", "policy_version": "p1", "community": true, "accepts_requests": true, "request_slots": 3, "max_slots": 6, "poll_url": "live.meshcore.ca", "nodes": [{"node_id": "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", "reply_hops": 0, "via": null, "key": "own", "snr": null, "state": "ok", "misses": 0, "last_attempt": null, "last_ok": null}, {"node_id": "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a", "reply_hops": 0, "via": null, "key": "password", "snr": null, "requested": true, "state": "login_failed", "misses": 3, "last_attempt": null, "last_ok": null}, {"node_id": "abababababababababababababababababababababababababababababababab", "reply_hops": 0, "via": null, "key": "shared", "snr": null, "state": "admin_stop", "misses": 12, "last_attempt": null, "last_ok": null}], "sig": "a120b6accce2ca3cfd0f24af0acbecb8aa7ffdd47e5cc0abf326fedae30d9971bb08eb9e741dc3331faeb9dc0f6603536511334e2aa06cfcb5dfe37a89710809"}`
|
|
|
|
func mutated(t *testing.T, edit func(m map[string]any)) []byte {
|
|
t.Helper()
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(firmwareClaim), &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
edit(m)
|
|
b, err := json.Marshal(m)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func node(m map[string]any, i int) map[string]any { return m["nodes"].([]any)[i].(map[string]any) }
|
|
|
|
func TestFirmwareSignedClaimVerifies(t *testing.T) {
|
|
c, err := ParseClaim([]byte(firmwareClaim))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !c.Signed || c.PollHost != "live.meshcore.ca" {
|
|
t.Fatalf("signed %v host %q", c.Signed, c.PollHost)
|
|
}
|
|
// Re-encoded, reordered and with the key in lower case it is the same claim.
|
|
c, err = ParseClaim(mutated(t, func(m map[string]any) {
|
|
m["origin_id"] = strings.ToLower(m["origin_id"].(string))
|
|
n := m["nodes"].([]any)
|
|
n[0], n[2] = n[2], n[0]
|
|
}))
|
|
if err != nil || !c.Signed {
|
|
t.Fatalf("re-encoded: %v %v", c.Signed, err)
|
|
}
|
|
// Fields outside the signature can change.
|
|
c, err = ParseClaim(mutated(t, func(m map[string]any) {
|
|
m["poll_interval"], m["policy_version"] = 999, "other"
|
|
node(m, 0)["snr"], node(m, 0)["key"] = 1.5, "shared"
|
|
}))
|
|
if err != nil || !c.Signed {
|
|
t.Fatalf("advisory fields: %v %v", c.Signed, err)
|
|
}
|
|
}
|
|
|
|
func TestTamperedSignedClaimIsRefused(t *testing.T) {
|
|
for name, edit := range map[string]func(m map[string]any){
|
|
"poll_url": func(m map[string]any) { m["poll_url"] = "dev.meshcore.ca" },
|
|
"timestamp": func(m map[string]any) { m["timestamp"] = "2025-11-28T14:26:41.000000+00:00" },
|
|
"community": func(m map[string]any) { m["community"] = false },
|
|
"accepts_requests": func(m map[string]any) { m["accepts_requests"] = false },
|
|
"request_slots": func(m map[string]any) { m["request_slots"] = 2 },
|
|
"max_slots": func(m map[string]any) { m["max_slots"] = 5 },
|
|
"target state": func(m map[string]any) { node(m, 0)["state"] = "admin_stop" },
|
|
"target misses": func(m map[string]any) { node(m, 1)["misses"] = 2 },
|
|
"target requested": func(m map[string]any) { node(m, 2)["requested"] = true },
|
|
"target removed": func(m map[string]any) { m["nodes"] = m["nodes"].([]any)[:2] },
|
|
"target listed twice": func(m map[string]any) { m["nodes"] = append(m["nodes"].([]any), node(m, 0)) },
|
|
"sig removed": func(m map[string]any) { delete(m, "sig") },
|
|
"poll_url removed": func(m map[string]any) { delete(m, "poll_url") },
|
|
"sig short": func(m map[string]any) { m["sig"] = "abcd" },
|
|
"sig not hex": func(m map[string]any) { m["sig"] = strings.Repeat("zz", 64) },
|
|
"slots missing": func(m map[string]any) { delete(m, "max_slots") },
|
|
"poll_url not a host": func(m map[string]any) { m["poll_url"] = "https://live.meshcore.ca" },
|
|
"state with separators": func(m map[string]any) { node(m, 0)["state"] = "ok:0:0," + strings.Repeat("ef", 32) + ":ok" },
|
|
"state empty": func(m map[string]any) { node(m, 0)["state"] = "" },
|
|
"state upper case": func(m map[string]any) { node(m, 0)["state"] = "OK" },
|
|
"sig null": func(m map[string]any) { m["sig"] = nil },
|
|
"both null": func(m map[string]any) { m["sig"], m["poll_url"] = nil, nil },
|
|
"sig null, no url": func(m map[string]any) { m["sig"] = nil; delete(m, "poll_url") },
|
|
"url null, no sig": func(m map[string]any) { m["poll_url"] = nil; delete(m, "sig") },
|
|
"sig a number": func(m map[string]any) { m["sig"] = 7 },
|
|
"sig empty": func(m map[string]any) { m["sig"] = "" },
|
|
} {
|
|
if _, err := ParseClaim(mutated(t, edit)); !errors.Is(err, ErrClaimSignature) {
|
|
t.Errorf("%s: %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUnsignedClaimIsStillAClaim(t *testing.T) {
|
|
c, err := ParseClaim(mutated(t, func(m map[string]any) { delete(m, "sig"); delete(m, "poll_url") }))
|
|
if err != nil || c.Signed || c.PollHost != "" {
|
|
t.Fatalf("%v signed %v host %q", err, c.Signed, c.PollHost)
|
|
}
|
|
}
|
|
|
|
func TestClaimPollHost(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"live.meshcore.ca": "live.meshcore.ca", "live.meshcore.ca:8443": "live.meshcore.ca",
|
|
"[::1]:8080": "::1", "[::1]": "::1", "192.168.1.5:80": "192.168.1.5",
|
|
"": "", "Live.meshcore.ca": "", "https://x.example": "", "a b": "", "x:y:z": "", "[bad": "",
|
|
strings.Repeat("a", 262): "",
|
|
} {
|
|
if got := claimPollHost(in); got != want {
|
|
t.Errorf("claimPollHost(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A claim signed here with a port in its poll_url verifies and gives the bare host.
|
|
func TestSignedClaimWithPort(t *testing.T) {
|
|
pub, priv, _ := ed25519.GenerateKey(nil)
|
|
id := hex.EncodeToString(pub)
|
|
ts := "2026-11-28T10:00:00.000000+00:00"
|
|
msg := "corescope-claim/v1|" + id + "|beacon.example.com:8443|" + ts + "|false|false|0|8|"
|
|
raw, _ := json.Marshal(map[string]any{"v": 2, "origin": "o", "origin_id": strings.ToUpper(id), "timestamp": ts, "plugin": "fw",
|
|
"community": false, "accepts_requests": false, "request_slots": 0, "max_slots": 8, "poll_url": "beacon.example.com:8443",
|
|
"nodes": []any{}, "sig": hex.EncodeToString(ed25519.Sign(priv, []byte(msg)))})
|
|
c, err := ParseClaim(raw)
|
|
if err != nil || !c.Signed || c.PollHost != "beacon.example.com" {
|
|
t.Fatalf("%v signed %v host %q", err, c.Signed, c.PollHost)
|
|
}
|
|
}
|
|
|
|
// Two targets must not be able to sign as one: a state that carries the separators would let a relay fold a second
|
|
// target into the first one's state and keep the signature valid.
|
|
func TestSignedClaimStateCannotCarryATarget(t *testing.T) {
|
|
_, priv := testKey(7)
|
|
id := hex.EncodeToString(priv.Public().(ed25519.PublicKey))
|
|
ts := "2026-11-28T10:00:00.000000+00:00"
|
|
a, b := strings.Repeat("0a", 32), strings.Repeat("0b", 32)
|
|
msg := "corescope-claim/v1|" + id + "|beacon.example.com|" + ts + "|false|false|0|8|" + a + ":ok:0:0," + b + ":ok:0:0"
|
|
sig := hex.EncodeToString(ed25519.Sign(priv, []byte(msg)))
|
|
claim := func(nodes []any) []byte {
|
|
raw, _ := json.Marshal(map[string]any{"v": 2, "origin": "o", "origin_id": id, "timestamp": ts, "plugin": "fw",
|
|
"community": false, "accepts_requests": false, "request_slots": 0, "max_slots": 8, "poll_url": "beacon.example.com",
|
|
"nodes": nodes, "sig": sig})
|
|
return raw
|
|
}
|
|
honest := []any{
|
|
map[string]any{"node_id": a, "key": "own", "state": "ok", "misses": 0},
|
|
map[string]any{"node_id": b, "key": "own", "state": "ok", "misses": 0},
|
|
}
|
|
if c, err := ParseClaim(claim(honest)); err != nil || !c.Signed || len(c.Nodes) != 2 {
|
|
t.Fatalf("honest claim: %v", err)
|
|
}
|
|
// The same signed bytes, with the second target folded into the first one's state and misses.
|
|
folded := []any{map[string]any{"node_id": a, "key": "own", "state": "ok:0:0," + b + ":ok", "misses": 0}}
|
|
if _, err := ParseClaim(claim(folded)); !errors.Is(err, ErrClaimSignature) {
|
|
t.Fatalf("folded claim was accepted: %v", err)
|
|
}
|
|
}
|