From f7c802ed32c7028ad093eba4c9a8d9126cab6dac Mon Sep 17 00:00:00 2001 From: Nirmal Khedkar Date: Thu, 22 Oct 2026 12:03:16 -0700 Subject: [PATCH 1/2] test: cover reference transport reliability Signed-off-by: Nirmal Khedkar --- transport/httpjson/transport_test.go | 293 +++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 transport/httpjson/transport_test.go diff --git a/transport/httpjson/transport_test.go b/transport/httpjson/transport_test.go new file mode 100644 index 0000000..d5beb44 --- /dev/null +++ b/transport/httpjson/transport_test.go @@ -0,0 +1,293 @@ +package httpjson + +import ( + "bytes" + "context" + "crypto/ed25519" + "errors" + "io" + "net/http" + "net/http/httptest" + "reflect" + "strings" + "sync" + "testing" + "time" + + "github.com/NTARI-RAND/sohocloud-protocol/employment" + "github.com/NTARI-RAND/sohocloud-protocol/fees" + "github.com/NTARI-RAND/sohocloud-protocol/identity" + "github.com/NTARI-RAND/sohocloud-protocol/listing" + "github.com/NTARI-RAND/sohocloud-protocol/liveness" +) + +// Each request has its own capture; a mutex lets the same handler serve the +// concurrent-client test without adding a race in the test double itself. +type testCoordinator struct { + mu sync.Mutex + received []any + jobs []employment.Assignment + fee fees.FeeDeclaration + err error +} + +func (c *testCoordinator) record(v any) error { + c.mu.Lock() + defer c.mu.Unlock() + c.received = append(c.received, v) + return c.err +} +func (c *testCoordinator) SubmitListing(_ context.Context, v listing.CapabilityListing) error { + return c.record(v) +} +func (c *testCoordinator) Heartbeat(_ context.Context, v liveness.Heartbeat) error { + return c.record(v) +} +func (c *testCoordinator) Decline(_ context.Context, v employment.Decline) error { return c.record(v) } +func (c *testCoordinator) ReportJob(_ context.Context, v employment.JobReport) error { + return c.record(v) +} +func (c *testCoordinator) PollJobs(_ context.Context, id identity.NodeID) ([]employment.Assignment, error) { + return c.jobs, c.record(id) +} +func (c *testCoordinator) Fees(context.Context) (fees.FeeDeclaration, error) { + return c.fee, c.record("fees") +} + +type roundTripFunc func(*http.Request) (*http.Response, error) + +func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } + +// Uses real client serialization, routing and handler decoding without sockets. +func inProcessClient(h http.Handler) *Client { + return &Client{BaseURL: "http://coordinator.invalid", HTTP: &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + return w.Result(), nil + })}} +} + +// Catches wrong endpoint/message wiring, lost fields, timestamp precision loss, +// unsigned/altered responses and incorrect node_id escaping in the real pair. +func TestSignedLifecycle(t *testing.T) { + priv := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{7}, 32)) + pub := priv.Public().(ed25519.PublicKey) + now := time.Unix(1700000000, 123456789).UTC() + node := identity.NodeID("node +/?&=日本語") + l := listing.CapabilityListing{NodeID: node, Class: listing.ClassServer, + Printers: []listing.PrinterCapability{{Kind: listing.Printer3D, Model: "printer"}}, + GPUs: []listing.GPUCapability{{API: listing.GPUCUDA, Model: "gpu", VRAMMB: 8192}}, + Capacity: listing.Capacity{VCPUs: 8, MemMB: 16384, DiskMB: 1024, StorageCommitMB: 2048, PrintQPS: 2}, + OptIn: listing.WorkloadOptIn{Compute: true, Print: true, Storage: true}, IssuedAt: now, Seq: 1<<63 + 3} + l.Sign(priv) + h := liveness.Heartbeat{NodeID: node, SentAt: now, Seq: 1<<63 + 4} + h.Sign(priv) + d := employment.Decline{NodeID: node, JobID: "declined", Reason: employment.DeclineLocalPolicy, DeclinedAt: now} + d.Sign(priv) + r := employment.JobReport{NodeID: node, JobID: "finished", ExitCode: 17, FailureCause: "out of memory", TmpfsExhausted: true, StartedAt: now, FinishedAt: now.Add(time.Second)} + r.Sign(priv) + a := employment.Assignment{NodeID: node, JobID: "offered", Spec: employment.JobSpec{Workload: "compute", Image: "image:tag", Args: []string{"a b", "日本語"}, PrinterKind: "threed", GPUAPI: "cuda", GPUMinVRAMMB: 4096}, Fee: fees.Terms{ContributorShareBps: 6500, PlatformFeeBps: 3500}, OfferedAt: now} + a.Sign(priv) + fee := fees.FeeDeclaration{CoordinatorID: "coord", Terms: a.Fee, EffectiveAt: now, Seq: 1<<63 + 5} + fee.Sign(priv) + c := &testCoordinator{jobs: []employment.Assignment{a}, fee: fee} + srv := httptest.NewServer(Handler(c)) + defer srv.Close() + client := &Client{BaseURL: srv.URL, HTTP: srv.Client()} + ctx := context.Background() + for _, call := range []func() error{func() error { return client.SubmitListing(ctx, l) }, func() error { return client.Heartbeat(ctx, h) }, func() error { return client.Decline(ctx, d) }, func() error { return client.ReportJob(ctx, r) }} { + if err := call(); err != nil { + t.Fatal(err) + } + } + jobs, err := client.PollJobs(ctx, node) + if err != nil { + t.Fatal(err) + } + gotFee, err := client.Fees(ctx) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(jobs, []employment.Assignment{a}) || !jobs[0].Verify(pub) { + t.Fatalf("assignment changed in transport: %+v", jobs) + } + if !reflect.DeepEqual(gotFee, fee) || !gotFee.Verify(pub) { + t.Fatalf("fee changed in transport: %+v", gotFee) + } + if want := []any{l, h, d, r, node, "fees"}; !reflect.DeepEqual(c.received, want) { + t.Fatalf("coordinator received %#v, want %#v", c.received, want) + } + for _, v := range c.received[:4] { + if !v.(interface{ Verify(ed25519.PublicKey) bool }).Verify(pub) { + t.Fatalf("signature lost for %T", v) + } + } +} + +func TestHandlerRejectsBadRequestsBeforeDispatch(t *testing.T) { + for _, path := range []string{"/v0/listing", "/v0/heartbeat", "/v0/decline", "/v0/report", "/v0/jobs", "/v0/fees"} { + t.Run(path, func(t *testing.T) { + c := &testCoordinator{} + h := Handler(c) + method := http.MethodGet + if path == "/v0/jobs" || path == "/v0/fees" { + method = http.MethodPost + } + w := httptest.NewRecorder() + h.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader("{}"))) + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("status %d, want 405", w.Code) + } + if len(c.received) != 0 { + t.Fatal("bad method dispatched") + } + }) + } + for _, path := range []string{"/v0/listing", "/v0/heartbeat", "/v0/decline", "/v0/report"} { + for _, body := range []string{"", "{", "[]", `{"Signature":"not base64!"}`, `{"NodeID":42}`} { + t.Run(path+"/"+body, func(t *testing.T) { + c := &testCoordinator{} + w := httptest.NewRecorder() + Handler(c).ServeHTTP(w, httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))) + if w.Code != http.StatusBadRequest || len(c.received) != 0 { + t.Fatalf("status %d, dispatches %d; want 400 and none", w.Code, len(c.received)) + } + }) + } + } + c := &testCoordinator{} + w := httptest.NewRecorder() + Handler(c).ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/v0/jobs", nil)) + if w.Code != 400 || len(c.received) != 0 { + t.Fatalf("missing node_id: status %d, dispatches %d", w.Code, len(c.received)) + } +} + +func TestCoordinatorErrorsReachClient(t *testing.T) { + c := &testCoordinator{err: errors.New("coordinator unavailable")} + client := inProcessClient(Handler(c)) + for name, call := range clientCalls(client, context.Background()) { + t.Run(name, func(t *testing.T) { + err := call() + if err == nil || !strings.Contains(err.Error(), "status 500") || !strings.Contains(err.Error(), "coordinator unavailable") { + t.Fatalf("lost coordinator error: %v", err) + } + }) + } +} + +func clientCalls(c *Client, ctx context.Context) map[string]func() error { + return map[string]func() error{ + "listing": func() error { return c.SubmitListing(ctx, listing.CapabilityListing{}) }, + "heartbeat": func() error { return c.Heartbeat(ctx, liveness.Heartbeat{}) }, + "decline": func() error { return c.Decline(ctx, employment.Decline{}) }, + "report": func() error { return c.ReportJob(ctx, employment.JobReport{}) }, + "jobs": func() error { _, err := c.PollJobs(ctx, "node"); return err }, + "fees": func() error { _, err := c.Fees(ctx); return err }, + } +} + +type trackedBody struct { + io.Reader + closed bool + read int +} + +func (b *trackedBody) Read(p []byte) (int, error) { + n, err := b.Reader.Read(p) + b.read += n + return n, err +} +func (b *trackedBody) Close() error { b.closed = true; return nil } + +func TestClientClosesResponseBodies(t *testing.T) { + for _, tc := range []struct { + name string + status int + body string + wantErr bool + }{ + {"success", 200, `[]`, false}, {"bad-json", 200, `{`, true}, + {"failure", 503, strings.Repeat("x", 8192), true}, + } { + t.Run(tc.name, func(t *testing.T) { + for _, operation := range []string{"jobs", "listing"} { + b := &trackedBody{Reader: strings.NewReader(tc.body)} + c := &Client{BaseURL: "http://coordinator.invalid", HTTP: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) { + return &http.Response{StatusCode: tc.status, Header: make(http.Header), Body: b}, nil + })}} + err := clientCalls(c, context.Background())[operation]() + wantErr := tc.wantErr + if operation == "listing" && tc.status == 200 { + wantErr = false + } + if (err != nil) != wantErr { + t.Fatalf("%s: err %v, want error %v", operation, err, wantErr) + } + if !b.closed { + t.Fatalf("%s: body not closed", operation) + } + if tc.status == 503 && (b.read > 4096 || !strings.Contains(err.Error(), "status 503")) { + t.Fatalf("unbounded/missing HTTP error: read %d, err %v", b.read, err) + } + } + }) + } +} + +func TestClientCancellation(t *testing.T) { + srv := httptest.NewServer(Handler(&testCoordinator{})) + defer srv.Close() + c := &Client{BaseURL: srv.URL, HTTP: srv.Client()} + ctx, cancel := context.WithCancel(context.Background()) + cancel() + for name, call := range clientCalls(c, ctx) { + t.Run(name, func(t *testing.T) { + if err := call(); !errors.Is(err, context.Canceled) { + t.Fatalf("want cancellation, got %v", err) + } + }) + } + // Cancel after the transport has started too, without sleeps or live sockets. + entered := make(chan struct{}) + ctx, cancel = context.WithCancel(context.Background()) + defer cancel() + c.HTTP = &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { + close(entered) + <-r.Context().Done() + return nil, r.Context().Err() + })} + done := make(chan error, 1) + go func() { _, err := c.Fees(ctx); done <- err }() + <-entered + cancel() + select { + case err := <-done: + if !errors.Is(err, context.Canceled) { + t.Fatal(err) + } + case <-time.After(5 * time.Second): + t.Fatal("request did not cancel") + } +} + +func TestConcurrentClientCalls(t *testing.T) { + c := &testCoordinator{} + client := inProcessClient(Handler(c)) + var wg sync.WaitGroup + for i := 0; i < 16; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for name, call := range clientCalls(client, context.Background()) { + if err := call(); err != nil { + t.Errorf("%s: %v", name, err) + } + } + }() + } + wg.Wait() + if len(c.received) != 96 { + t.Fatalf("lost requests: got %d, want 96", len(c.received)) + } +} From f3e09c5cc7f8599765c4018da62a6288a4db2883 Mon Sep 17 00:00:00 2001 From: Nirmal Khedkar Date: Thu, 22 Oct 2026 12:07:18 -0700 Subject: [PATCH 2/2] fix: reject invalid signed timestamps and fuzz inputs Signed-off-by: Nirmal Khedkar --- CONFORMANCE.md | 1 + canon/canon.go | 4 +- canon/verify_fuzz_test.go | 55 +++++++++++ employment/employment.go | 9 +- fees/fees.go | 3 +- identity/keylife.go | 6 +- lease/lease.go | 12 ++- listing/listing.go | 3 +- liveness/liveness.go | 3 +- operator/verify_fuzz_test.go | 68 ++++++++++++++ transport/httpjson/decode_fuzz_test.go | 110 ++++++++++++++++++++++ verification_fuzz_test.go | 125 +++++++++++++++++++++++++ 12 files changed, 385 insertions(+), 14 deletions(-) create mode 100644 canon/verify_fuzz_test.go create mode 100644 operator/verify_fuzz_test.go create mode 100644 transport/httpjson/decode_fuzz_test.go create mode 100644 verification_fuzz_test.go diff --git a/CONFORMANCE.md b/CONFORMANCE.md index 4284e72..28636fe 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -26,6 +26,7 @@ This module is the **coordination protocol** of a JFA **substrate**: the depende | **Fee legibility** | Fees exist on the wire only as a coordinator-signed `FeeDeclaration` — authored, legible, contestable | `fees/`; golden vectors | | **Legibility is an output, not a comment** | `SPEC.md` is normative and a build deliverable: a foreign implementer builds from it without reading this source | `testdata/vectors.json` (ground truth) + `testdata/reproduce_spec.py` (independent reproducer) + `vectors/` golden test — drift between code and committed vectors fails the build | | **Canonical, deterministic encoding** | `canon/` length-prefixed, domain-separated signing encoder | golden vectors regenerated-and-compared byte-for-byte on every test run | +| **Representable timestamps** (SPEC §3) | Every timestamp-bearing message verifier checks `canon.ValidTime` before checking the signature; a signature over incomplete canonical bytes cannot authorize an invalid timestamp | `TestVerifyRejectsUnrepresentableTime`, `TestVerifyRepresentableTimeBoundaries`, and `FuzzMessageVerification` | ## JFA registry bindings diff --git a/canon/canon.go b/canon/canon.go index ce3951f..10ed503 100644 --- a/canon/canon.go +++ b/canon/canon.go @@ -139,8 +139,8 @@ func (buf *Buffer) Err() error { // Sum returns a copy of the accumulated canonical bytes. The caller signs or // verifies over exactly these bytes. When Err() is non-nil the returned bytes // are incomplete by construction (the offending field was skipped, not -// wrapped), so any signature over them will fail to verify — callers should -// check Err() rather than relying on that. +// wrapped). They can still be signed: verifiers must explicitly reject invalid +// timestamps rather than trusting a signature over incomplete bytes. func (buf *Buffer) Sum() []byte { out := make([]byte, len(buf.b)) copy(out, buf.b) diff --git a/canon/verify_fuzz_test.go b/canon/verify_fuzz_test.go new file mode 100644 index 0000000..3fdf831 --- /dev/null +++ b/canon/verify_fuzz_test.go @@ -0,0 +1,55 @@ +package canon + +import ( + "crypto/ed25519" + "encoding/hex" + "encoding/json" + "os" + "testing" +) + +func FuzzVerifySig(f *testing.F) { + // Seed with the normative signatures, not just random invalid byte strings. + data, err := os.ReadFile("../testdata/vectors.json") + if err != nil { + f.Fatal(err) + } + var fixture struct { + Messages []struct { + PublicKey string `json:"public_key_hex"` + Payload string `json:"canonical_bytes_hex"` + Signature string `json:"signature_hex"` + } + } + if err := json.Unmarshal(data, &fixture); err != nil { + f.Fatal(err) + } + for _, m := range fixture.Messages { + pub, err := hex.DecodeString(m.PublicKey) + if err != nil { + f.Fatal(err) + } + payload, err := hex.DecodeString(m.Payload) + if err != nil { + f.Fatal(err) + } + sig, err := hex.DecodeString(m.Signature) + if err != nil { + f.Fatal(err) + } + f.Add(pub, payload, sig) + } + f.Add([]byte{}, []byte{}, []byte{}) + f.Fuzz(func(t *testing.T, pub, payload, sig []byte) { + got := VerifySig(pub, payload, sig) + if len(pub) != 32 || len(sig) != 64 { + if got { + t.Fatal("accepted malformed key/signature") + } + return + } + if want := ed25519.Verify(pub, payload, sig); got != want { + t.Fatalf("signature result %v, want %v", got, want) + } + }) +} diff --git a/employment/employment.go b/employment/employment.go index c90e5a7..7649d0a 100644 --- a/employment/employment.go +++ b/employment/employment.go @@ -111,7 +111,8 @@ func (a *Assignment) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid coordinator signature over the // assignment. func (a Assignment) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, a.CanonicalBytes(), a.Signature) + return canon.ValidTime(a.OfferedAt) && + canon.VerifySig(pub, a.CanonicalBytes(), a.Signature) } // CanonicalBytes returns the deterministic signing payload for the decline, @@ -132,7 +133,8 @@ func (d *Decline) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid node signature over the decline. func (d Decline) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, d.CanonicalBytes(), d.Signature) + return canon.ValidTime(d.DeclinedAt) && + canon.VerifySig(pub, d.CanonicalBytes(), d.Signature) } // CanonicalBytes returns the deterministic signing payload for the job report, @@ -156,5 +158,6 @@ func (r *JobReport) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid node signature over the report. func (r JobReport) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, r.CanonicalBytes(), r.Signature) + return canon.ValidTime(r.StartedAt) && canon.ValidTime(r.FinishedAt) && + canon.VerifySig(pub, r.CanonicalBytes(), r.Signature) } diff --git a/fees/fees.go b/fees/fees.go index f8a0bf4..1decf3b 100644 --- a/fees/fees.go +++ b/fees/fees.go @@ -55,5 +55,6 @@ func (f *FeeDeclaration) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid coordinator signature over the // declaration. func (f FeeDeclaration) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, f.CanonicalBytes(), f.Signature) + return canon.ValidTime(f.EffectiveAt) && + canon.VerifySig(pub, f.CanonicalBytes(), f.Signature) } diff --git a/identity/keylife.go b/identity/keylife.go index 7f8b6e8..c86c22a 100644 --- a/identity/keylife.go +++ b/identity/keylife.go @@ -68,7 +68,8 @@ func (k *KeyRotation) Sign(priv ed25519.PrivateKey) { // malformed key to ed25519.Verify and panic. Validating the successor here is // the same discipline the operator rotation path already applies. func (k KeyRotation) Verify(currentPub ed25519.PublicKey) bool { - return k.Algo == "ed25519" && + return canon.ValidTime(k.RotatedAt) && + k.Algo == "ed25519" && len(k.NewPublicKey) == ed25519.PublicKeySize && canon.VerifySig(currentPub, k.CanonicalBytes(), k.Signature) } @@ -102,7 +103,8 @@ func (k *KeyRevocation) Sign(priv ed25519.PrivateKey) { // is rejected. A revocation is only ever honored against the key it actually // kills. func (k KeyRevocation) Verify(trustedPub ed25519.PublicKey) bool { - return len(k.RevokedPublicKey) == ed25519.PublicKeySize && + return canon.ValidTime(k.RevokedAt) && + len(k.RevokedPublicKey) == ed25519.PublicKeySize && bytes.Equal(k.RevokedPublicKey, trustedPub) && canon.VerifySig(k.RevokedPublicKey, k.CanonicalBytes(), k.Signature) } diff --git a/lease/lease.go b/lease/lease.go index d7ed802..89d10f0 100644 --- a/lease/lease.go +++ b/lease/lease.go @@ -175,7 +175,8 @@ func (l *StorageLease) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid coordinator signature. func (l StorageLease) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, l.CanonicalBytes(), l.Signature) + return canon.ValidTime(l.IssuedAt) && canon.ValidTime(l.ExpiresAt) && + canon.VerifySig(pub, l.CanonicalBytes(), l.Signature) } // CanonicalBytes for the decline (SPEC.md §4.8). @@ -195,7 +196,8 @@ func (d *LeaseDecline) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid node signature. func (d LeaseDecline) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, d.CanonicalBytes(), d.Signature) + return canon.ValidTime(d.DeclinedAt) && + canon.VerifySig(pub, d.CanonicalBytes(), d.Signature) } // CanonicalBytes for the release (SPEC.md §4.9). @@ -214,7 +216,8 @@ func (r *LeaseRelease) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid node signature. func (r LeaseRelease) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, r.CanonicalBytes(), r.Signature) + return canon.ValidTime(r.ReleasedAt) && + canon.VerifySig(pub, r.CanonicalBytes(), r.Signature) } // CanonicalBytes for the proof response (SPEC.md §4.10). @@ -237,5 +240,6 @@ func (p *ProofResponse) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid node signature. func (p ProofResponse) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, p.CanonicalBytes(), p.Signature) + return canon.ValidTime(p.RespondedAt) && + canon.VerifySig(pub, p.CanonicalBytes(), p.Signature) } diff --git a/listing/listing.go b/listing/listing.go index 24fb491..86bad79 100644 --- a/listing/listing.go +++ b/listing/listing.go @@ -139,5 +139,6 @@ func (l *CapabilityListing) Sign(priv ed25519.PrivateKey) { // pub is the node's public key, resolved out-of-band by the coordinator; the // protocol does not distribute keys. func (l CapabilityListing) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, l.CanonicalBytes(), l.Signature) + return canon.ValidTime(l.IssuedAt) && + canon.VerifySig(pub, l.CanonicalBytes(), l.Signature) } diff --git a/liveness/liveness.go b/liveness/liveness.go index 59d9c43..ac27213 100644 --- a/liveness/liveness.go +++ b/liveness/liveness.go @@ -38,5 +38,6 @@ func (h *Heartbeat) Sign(priv ed25519.PrivateKey) { // Verify reports whether Signature is a valid node signature over the // heartbeat. pub is the node's public key, resolved out-of-band. func (h Heartbeat) Verify(pub ed25519.PublicKey) bool { - return canon.VerifySig(pub, h.CanonicalBytes(), h.Signature) + return canon.ValidTime(h.SentAt) && + canon.VerifySig(pub, h.CanonicalBytes(), h.Signature) } diff --git a/operator/verify_fuzz_test.go b/operator/verify_fuzz_test.go new file mode 100644 index 0000000..60a436c --- /dev/null +++ b/operator/verify_fuzz_test.go @@ -0,0 +1,68 @@ +package operator + +import ( + "bytes" + "crypto/ed25519" + "encoding/json" + "testing" +) + +func FuzzOperatorVerification(f *testing.F) { + privs, keys := sevenKeys() + tx := sampleTransmission() + tx.Sign(privs[2], privs[5], 2, 5) + next := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{7}, 32)).Public().(ed25519.PublicKey) + rotation := OperatorRotation{OperatorID: "cloudy", KeyIndex: 3, NewPublicKey: next, Algo: AlgoEd25519, Nonce: bytes.Repeat([]byte{1}, 16), Seq: 1} + rotation.Sign(privs[2], privs[5], 2, 5) + response := ConformanceResponse{OperatorID: "cloudy", Challenge: []byte("challenge"), Algo: AlgoEd25519} + response.Sign(privs[2], privs[5], 2, 5) + for i, m := range []any{tx, rotation, response} { + b, err := json.Marshal(m) + if err != nil { + f.Fatal(err) + } + f.Add(uint8(i), string(b), keys[2].PublicKey) + } + f.Add(uint8(0), `{"Idx0":-1,"Idx1":7,"Algo":"unknown"}`, []byte{}) + f.Fuzz(func(t *testing.T, kind uint8, payload string, key []byte) { + // A registry supplied by an external coordinator may contain malformed + // key material; verification must fail safely rather than panic. + _, km := sevenKeys() + km[2] = KeyRecord{PublicKey: key, Algo: AlgoEd25519} + switch kind % 3 { + case 0: + var m OperatorTransmission + if json.Unmarshal([]byte(payload), &m) != nil { + return + } + if m.Verify(km) == nil { + m.OperatorID += "changed" + if m.Verify(km) == nil { + t.Fatal("transmission identity not signed") + } + } + case 1: + var m OperatorRotation + if json.Unmarshal([]byte(payload), &m) != nil { + return + } + if m.Verify(km) == nil { + m.OperatorID += "changed" + if m.Verify(km) == nil { + t.Fatal("rotation identity not signed") + } + } + case 2: + var m ConformanceResponse + if json.Unmarshal([]byte(payload), &m) != nil { + return + } + if m.Verify(km) == nil { + m.Challenge = append(m.Challenge, 1) + if m.Verify(km) == nil { + t.Fatal("challenge not signed") + } + } + } + }) +} diff --git a/transport/httpjson/decode_fuzz_test.go b/transport/httpjson/decode_fuzz_test.go new file mode 100644 index 0000000..1cfa190 --- /dev/null +++ b/transport/httpjson/decode_fuzz_test.go @@ -0,0 +1,110 @@ +package httpjson + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/NTARI-RAND/sohocloud-protocol/employment" + "github.com/NTARI-RAND/sohocloud-protocol/fees" + "github.com/NTARI-RAND/sohocloud-protocol/listing" + "github.com/NTARI-RAND/sohocloud-protocol/liveness" +) + +func FuzzHandlerJSON(f *testing.F) { + now := time.Unix(1700000000, 123).UTC() + seeds := []any{listing.CapabilityListing{NodeID: "node", IssuedAt: now, Seq: 1<<63 + 1}, liveness.Heartbeat{NodeID: "node", SentAt: now, Seq: 1<<63 + 2}, employment.Decline{NodeID: "node", JobID: "job", DeclinedAt: now}, employment.JobReport{NodeID: "node", JobID: "job", StartedAt: now, FinishedAt: now}} + for i, v := range seeds { + b, err := json.Marshal(v) + if err != nil { + f.Fatal(err) + } + f.Add(uint8(i), string(b)) + } + for _, s := range []string{"", "{", "[]", "null", `{"SentAt":"2500-01-01T00:00:00Z"}`, `{"Seq":18446744073709551615}`, `{"Seq":18446744073709551616}`, `{"Signature":"!"}`} { + f.Add(uint8(1), s) + } + f.Fuzz(func(t *testing.T, kind uint8, body string) { + paths := []string{"/v0/listing", "/v0/heartbeat", "/v0/decline", "/v0/report"} + c := &testCoordinator{} + w := httptest.NewRecorder() + Handler(c).ServeHTTP(w, httptest.NewRequest(http.MethodPost, paths[int(kind)%len(paths)], strings.NewReader(body))) + switch w.Code { + case 204: + if len(c.received) != 1 { + t.Fatal("decoded request was lost") + } + case 400: + if len(c.received) != 0 { + t.Fatal("bad JSON reached coordinator") + } + default: + t.Fatalf("unexpected status %d", w.Code) + } + // The reference handler is not a semantic validator/authenticator. Decode + // success (including null/unknown fields) does not authorize a message. + }) +} + +func FuzzClientJSON(f *testing.F) { + now := time.Unix(1700000000, 123).UTC() + for i, v := range []any{[]employment.Assignment{{NodeID: "node", JobID: "job", OfferedAt: now}}, fees.FeeDeclaration{CoordinatorID: "coord", EffectiveAt: now, Seq: 1<<63 + 1}} { + b, err := json.Marshal(v) + if err != nil { + f.Fatal(err) + } + f.Add(i == 0, true, string(b)) + } + for _, s := range []string{"", "{", "[]", "null", `{"Seq":18446744073709551616}`, `{"EffectiveAt":"bad-time"}`, strings.Repeat("x", 8192)} { + f.Add(false, true, s) + f.Add(true, false, s) + } + f.Fuzz(func(t *testing.T, jobs, success bool, payload string) { + code := 503 + if success { + code = 200 + } + body := &trackedBody{Reader: strings.NewReader(payload)} + c := &Client{BaseURL: "http://coordinator.invalid", HTTP: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) { + return &http.Response{StatusCode: code, Header: make(http.Header), Body: body}, nil + })}} + var err error + if jobs { + _, err = c.PollJobs(context.Background(), "node") + } else { + _, err = c.Fees(context.Background()) + } + if !body.closed { + t.Fatal("response body leaked") + } + if !success && (err == nil || body.read > 4096) { + t.Fatalf("HTTP failure lost or read unbounded: read %d, err %v", body.read, err) + } + + }) +} + +func TestClientRejectsMalformedResponses(t *testing.T) { + for _, operation := range []string{"jobs", "fees"} { + bad := []string{"", "{", `{"Signature":"!"}`, `{"EffectiveAt":"not a timestamp"}`, `{"Seq":18446744073709551616}`} + if operation == "jobs" { + bad = []string{"", "{", `{}`, `[{"Signature":"!"}]`, `[{"OfferedAt":"not a timestamp"}]`} + } else { + bad = append(bad, `[]`) + } + for _, payload := range bad { + t.Run(operation+"/"+payload, func(t *testing.T) { + c := &Client{BaseURL: "http://coordinator.invalid", HTTP: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) { + return &http.Response{StatusCode: 200, Header: make(http.Header), Body: &trackedBody{Reader: strings.NewReader(payload)}}, nil + })}} + if err := clientCalls(c, context.Background())[operation](); err == nil { + t.Fatal("malformed response accepted") + } + }) + } + } +} diff --git a/verification_fuzz_test.go b/verification_fuzz_test.go new file mode 100644 index 0000000..ac87acb --- /dev/null +++ b/verification_fuzz_test.go @@ -0,0 +1,125 @@ +package sohocloud_test + +import ( + "bytes" + "crypto/ed25519" + "reflect" + "testing" + "time" + + "github.com/NTARI-RAND/sohocloud-protocol/employment" + "github.com/NTARI-RAND/sohocloud-protocol/fees" + "github.com/NTARI-RAND/sohocloud-protocol/identity" + "github.com/NTARI-RAND/sohocloud-protocol/lease" + "github.com/NTARI-RAND/sohocloud-protocol/listing" + "github.com/NTARI-RAND/sohocloud-protocol/liveness" +) + +type nodeMessage interface { + CanonicalBytes() []byte + Sign(ed25519.PrivateKey) + Verify(ed25519.PublicKey) bool +} + +func nodeMessages(pub ed25519.PublicKey) []nodeMessage { + return []nodeMessage{ + &listing.CapabilityListing{NodeID: "node", Seq: 1}, + &liveness.Heartbeat{NodeID: "node", Seq: 1}, + &employment.Assignment{NodeID: "node", JobID: "job"}, + &employment.Decline{NodeID: "node", JobID: "job"}, + &employment.JobReport{NodeID: "node", JobID: "job"}, + &fees.FeeDeclaration{CoordinatorID: "coord", Seq: 1}, + &lease.StorageLease{NodeID: "node", LeaseID: "lease", Seq: 1}, + &lease.LeaseDecline{NodeID: "node", LeaseID: "lease"}, + &lease.LeaseRelease{NodeID: "node", LeaseID: "lease"}, + &lease.ProofResponse{NodeID: "node", LeaseID: "lease"}, + &identity.KeyRotation{NodeID: "node", Algo: "ed25519", NewPublicKey: pub, Seq: 1}, + &identity.KeyRevocation{NodeID: "node", RevokedPublicKey: pub, Seq: 1}, + } +} + +func setMessageTimes(m nodeMessage, stamp time.Time) { + value := reflect.ValueOf(m).Elem() + for i := 0; i < value.NumField(); i++ { + if value.Field(i).Type() == reflect.TypeOf(time.Time{}) { + value.Field(i).Set(reflect.ValueOf(stamp)) + } + } +} + +// An attacker can sign the encoder's incomplete output. SPEC §3 still +// requires rejection: valid cryptography cannot make an invalid time valid. +func TestVerifyRejectsUnrepresentableTime(t *testing.T) { + priv := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{9}, 32)) + pub := priv.Public().(ed25519.PublicKey) + good := time.Unix(1700000000, 0).UTC() + for _, m := range nodeMessages(pub) { + value := reflect.ValueOf(m).Elem() + for field := 0; field < value.NumField(); field++ { + if value.Field(field).Type() != reflect.TypeOf(time.Time{}) { + continue + } + for _, bad := range []time.Time{time.Time{}, time.Unix(0, -1<<63).Add(-time.Nanosecond), time.Unix(0, 1<<63-1).Add(time.Nanosecond), time.Date(2500, 1, 1, 0, 0, 0, 0, time.UTC)} { + t.Run(value.Type().Name()+"/"+value.Type().Field(field).Name+"/"+bad.Format(time.RFC3339Nano), func(t *testing.T) { + setMessageTimes(m, good) + value.Field(field).Set(reflect.ValueOf(bad)) + value.FieldByName("Signature").SetBytes(ed25519.Sign(priv, m.CanonicalBytes())) + if m.Verify(pub) { + t.Fatal("accepted signed message with unrepresentable timestamp") + } + }) + } + } + } +} + +// Also guard both ends of the valid interval: the fix must reject out-of-range +// values without rejecting legitimate int64 nanoseconds or nanosecond precision. +func TestVerifyRepresentableTimeBoundaries(t *testing.T) { + priv := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{9}, 32)) + pub := priv.Public().(ed25519.PublicKey) + for _, m := range nodeMessages(pub) { + for _, stamp := range []time.Time{time.Unix(0, -1<<63), time.Unix(0, 1<<63-1), time.Unix(1700000000, 123456789)} { + setMessageTimes(m, stamp) + m.Sign(priv) + if !m.Verify(pub) { + t.Fatalf("%T rejected valid time %v", m, stamp) + } + } + } +} + +func FuzzMessageVerification(f *testing.F) { + f.Add(uint8(0), int64(1700000000), int32(123456789), []byte{}, []byte{}) + f.Add(uint8(11), int64(16725225600), int32(0), []byte{0}, []byte{0}) // year 2500 + f.Add(uint8(6), int64(-9223372037), int32(145224192), []byte{}, []byte{}) // minimum + f.Fuzz(func(t *testing.T, kind uint8, seconds int64, nanos int32, key, sig []byte) { + priv := ed25519.NewKeyFromSeed(bytes.Repeat([]byte{9}, 32)) + pub := priv.Public().(ed25519.PublicKey) + messages := nodeMessages(pub) + m := messages[int(kind)%len(messages)] + stamp := time.Unix(seconds, int64(nanos)).UTC() + setMessageTimes(m, stamp) + // Derive validity independently of canon.ValidTime, including normalized + // fractional seconds at both interval endpoints. + sec, ns := stamp.Unix(), stamp.Nanosecond() + valid := (sec > -9223372037 || sec == -9223372037 && ns >= 145224192) && (sec < 9223372036 || sec == 9223372036 && ns <= 854775807) + signature := ed25519.Sign(priv, m.CanonicalBytes()) + value := reflect.ValueOf(m).Elem() + value.FieldByName("Signature").SetBytes(signature) + if got := m.Verify(pub); got != valid { + t.Fatalf("%T time %v: verified %v, want %v", m, stamp, got, valid) + } + // Arbitrary untrusted key/signature lengths must never panic. Matching keys + // may pass, so do not equate random input with invalid cryptography. + value.FieldByName("Signature").SetBytes(sig) + _ = m.Verify(ed25519.PublicKey(key)) + // The last byte is a signed field in every family (never the domain prefix). + payload := m.CanonicalBytes() + payload[len(payload)-1] ^= 1 + value.FieldByName("Signature").SetBytes(ed25519.Sign(priv, payload)) + if m.Verify(pub) { + t.Fatalf("%T accepted signature over altered canonical bytes", m) + } + }) +}