Files
Warp/server/internal/federation/keys_test.go
T
tajniak81andClaude Opus 5.5 6aaf554336
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Phase 5: federation
Implements PLAN.md Phase 5: the Server-Server API. Warp joins rooms on
other servers and lets their users join its rooms, invites across servers
with the invitee's server countersigning, rejects remote invites (locally
if the inviter is unreachable), and exchanges events, typing, receipts and
to-device messages with other homeservers — all behind the runtime switch,
which now starts and stops real outbound traffic.

The protocol lives in internal/federation with no knowledge of rooms:
X-Matrix signing (fuzzed parser), the spec's discovery algorithm keeping
the dial address and the TLS name apart, a signed client that refuses
every request while the switch is off or the policy denies the
destination, and a key ring that caches other servers' keys for at most a
week, with optional notaries.

The roomserver takes events from elsewhere through the spec's intake:
signatures and hashes (a hash mismatch keeps the redacted form), the rules
against the event's own auth events, against the state before it — with
/get_missing_events for small gaps and /state_ids for wide ones — and
against the current state, where a failure is a soft failure. Events held
without their state are outliers: a column on roomserver_events, because
the current state of a room joined over federation is almost all
outliers, and excluded from every timeline query. Forks are resolved at
the room's head. Membership across servers is make_join/send_join,
make_leave/send_leave and countersigned invites, in both directions;
m.room.server_acl is enforced on everything another server sends.
History before a remote join is backfilled when /messages pages past it,
stored at negative stream positions.

internal/federationapi is the running component: the inbound endpoints,
and an outbound queue that persists what it owes each destination, sends
it in transactions of 50 PDUs and 100 EDUs, backs off from ten seconds to
six hours, survives restarts, and drops events persisted while federation
is off rather than delivering a backlog in one burst. Its Module is what
the switch starts and stops.

Also: a TLS listener for port 8448, trusted key servers, key validity and
an outbound CA bundle in the config; per-destination health and backoff
reset in the admin API; remote profiles, aliases and directories in the
Client-Server API; a Complement federation profile. See
docs/federation.md, including its known gaps (no partial-state joins, no
key rotation).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 17:14:20 +02:00

260 lines
8.5 KiB
Go

package federation
import (
"context"
"encoding/json"
"errors"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/dariuszjaniak/warp/server/internal/matrix/signing"
"github.com/dariuszjaniak/warp/server/internal/storage"
_ "github.com/dariuszjaniak/warp/server/internal/storage/drivers"
)
// testStore opens a migrated SQLite database for the key cache.
func testStore(t *testing.T) storage.FederationStore {
t.Helper()
db, err := storage.Open(t.Context(), storage.Config{
Driver: storage.DriverSQLite,
ConnectionString: "file:" + strings.ReplaceAll(t.TempDir(), `\`, "/") + "/warp.db",
}, nil)
if err != nil {
t.Fatalf("open: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
if err := db.Migrate(t.Context()); err != nil {
t.Fatalf("migrate: %v", err)
}
return db.Federation()
}
// remote is a fake homeserver: a TLS server answering for a server name,
// with its own key.
type remote struct {
name string
key *signing.KeyPair
srv *httptest.Server
mux *http.ServeMux
keyHits atomic.Int32
// validFor is the validity its key response claims.
validFor time.Duration
}
func newRemote(t *testing.T, name string) *remote {
t.Helper()
key, err := signing.Generate("")
if err != nil {
t.Fatal(err)
}
r := &remote{name: name, key: key, mux: http.NewServeMux(), validFor: time.Hour}
r.mux.HandleFunc("GET /_matrix/key/v2/server", func(w http.ResponseWriter, _ *http.Request) {
r.keyHits.Add(1)
doc, _ := json.Marshal(serverKeyResponse{
ServerName: r.name,
ValidUntilTS: time.Now().Add(r.validFor).UnixMilli(),
VerifyKeys: map[string]verifyKey{r.key.KeyID(): {Key: r.key.PublicBase64()}},
OldVerifyKeys: map[string]oldVerifyKey{},
})
signed, _ := signing.SignJSON(r.name, r.key, doc)
_, _ = w.Write(signed)
})
r.srv = httptest.NewTLSServer(r.mux)
t.Cleanup(r.srv.Close)
return r
}
// clientFor builds a client that resolves every remote in the list to its
// test server. The Host is "example.com", the name the test certificate is
// issued for, and the address is the listener: the dial-target path is
// exercised on every request.
func clientFor(t *testing.T, serverName string, key *signing.KeyPair, remotes ...*remote) *Client {
t.Helper()
byName := map[string]*remote{}
for _, r := range remotes {
byName[r.name] = r
}
var tlsConfig = remotes[0].srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone()
return NewClient(ClientOptions{
ServerName: serverName,
Key: key,
TLSConfig: tlsConfig,
Enabled: func() bool { return true },
Resolve: func(_ context.Context, name string) (Resolved, error) {
r, ok := byName[name]
if !ok {
return Resolved{}, errors.New("no such test server")
}
_, port, _ := net.SplitHostPort(r.srv.Listener.Addr().String())
return Resolved{Addresses: []string{r.srv.Listener.Addr().String()}, Host: "example.com:" + port}, nil
},
})
}
func newKeyRing(t *testing.T, name string, remotes ...*remote) (*KeyRing, *signing.KeyPair) {
t.Helper()
key, err := signing.Generate("")
if err != nil {
t.Fatal(err)
}
return NewKeyRing(KeyRingOptions{
ServerName: name,
Key: key,
Store: testStore(t),
Client: clientFor(t, name, key, remotes...),
}), key
}
func TestKeyRingFetchesAndCaches(t *testing.T) {
r := newRemote(t, "remote.example")
ring, _ := newKeyRing(t, "warp.example", r)
pub, err := ring.PublicKey(t.Context(), "remote.example", r.key.KeyID(), time.Now().UnixMilli())
if err != nil {
t.Fatalf("PublicKey: %v", err)
}
if !pub.Equal(r.key.Public) {
t.Error("fetched the wrong key")
}
for range 3 {
if _, err := ring.PublicKey(t.Context(), "remote.example", r.key.KeyID(), time.Now().UnixMilli()); err != nil {
t.Fatal(err)
}
}
if n := r.keyHits.Load(); n != 1 {
t.Errorf("%d key fetches, want 1", n)
}
// A key needed for a time past its validity is fetched again, in case
// the server has extended it.
if _, err := ring.PublicKey(t.Context(), "remote.example", r.key.KeyID(),
time.Now().Add(2*time.Hour).UnixMilli()); err == nil {
t.Error("a key was accepted for a time past its validity")
}
if n := r.keyHits.Load(); n != 2 {
t.Errorf("%d key fetches after asking past validity, want 2", n)
}
// Validity is capped at a week, whatever the server claims.
r.validFor = 365 * 24 * time.Hour
ring2, _ := newKeyRing(t, "warp.example", r)
if _, err := ring2.PublicKey(t.Context(), "remote.example", r.key.KeyID(),
time.Now().Add(30*24*time.Hour).UnixMilli()); err == nil {
t.Error("a key was believed for longer than a week")
}
}
func TestKeyRingRefusesAForgedResponse(t *testing.T) {
r := newRemote(t, "remote.example")
impostor, _ := signing.Generate("")
r.mux = http.NewServeMux()
r.mux.HandleFunc("GET /_matrix/key/v2/server", func(w http.ResponseWriter, _ *http.Request) {
// Lists the real key but is signed with another one.
doc, _ := json.Marshal(serverKeyResponse{
ServerName: r.name,
ValidUntilTS: time.Now().Add(time.Hour).UnixMilli(),
VerifyKeys: map[string]verifyKey{r.key.KeyID(): {Key: impostor.PublicBase64()}},
})
signed, _ := signing.SignJSON(r.name, r.key, doc)
_, _ = w.Write(signed)
})
r.srv.Config.Handler = r.mux
ring, _ := newKeyRing(t, "warp.example", r)
if _, err := ring.PublicKey(t.Context(), "remote.example", r.key.KeyID(), 0); !errors.Is(err, ErrUnknownKey) {
t.Errorf("PublicKey with a forged response = %v, want ErrUnknownKey", err)
}
}
func TestOwnKeyResponseVerifies(t *testing.T) {
ring, key := newKeyRing(t, "warp.example", newRemote(t, "unused.example"))
doc, err := ring.OwnKeyResponse()
if err != nil {
t.Fatal(err)
}
if err := signing.VerifyJSON("warp.example", key.KeyID(), key.Public, doc); err != nil {
t.Errorf("our own key response does not verify: %v", err)
}
var parsed serverKeyResponse
if err := json.Unmarshal(doc, &parsed); err != nil {
t.Fatal(err)
}
if parsed.VerifyKeys[key.KeyID()].Key != key.PublicBase64() {
t.Errorf("verify_keys = %+v", parsed.VerifyKeys)
}
}
func TestSignedRequestIsAuthenticatedEndToEnd(t *testing.T) {
// The remote here is the *sender*: a key server for remote.example,
// and a client signing as remote.example to warp.example.
sender := newRemote(t, "remote.example")
receiver := newRemote(t, "warp.example")
ring := NewKeyRing(KeyRingOptions{
ServerName: "warp.example",
Key: receiver.key,
Store: testStore(t),
Client: clientFor(t, "warp.example", receiver.key, sender),
})
var gotOrigin atomic.Value
receiver.mux.HandleFunc("PUT /_matrix/federation/v1/send/{txn}", func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
origin, err := ring.AuthenticateRequest(r.Context(), r, body)
if err != nil {
http.Error(w, `{"errcode":"M_UNAUTHORIZED","error":"`+err.Error()+`"}`, http.StatusUnauthorized)
return
}
gotOrigin.Store(origin)
_, _ = w.Write([]byte(`{"pdus":{}}`))
})
client := clientFor(t, "remote.example", sender.key, receiver)
if _, err := client.SendTransaction(t.Context(), "warp.example", "txn/1 with spaces", Transaction{
Origin: "remote.example", OriginServerTS: 1, PDUs: []json.RawMessage{},
}); err != nil {
t.Fatalf("SendTransaction: %v", err)
}
if got, _ := gotOrigin.Load().(string); got != "remote.example" {
t.Errorf("authenticated origin = %q", got)
}
// A request signed by a key the origin does not publish is refused.
stranger, _ := signing.Generate("")
forged := clientFor(t, "remote.example", stranger, receiver)
_, err := forged.SendTransaction(t.Context(), "warp.example", "2", Transaction{Origin: "remote.example"})
var httpErr *HTTPError
if !errors.As(err, &httpErr) || httpErr.Status != http.StatusUnauthorized {
t.Errorf("forged request = %v, want 401", err)
}
}
func TestClientRefusesWhileDisabledOrDenied(t *testing.T) {
r := newRemote(t, "remote.example")
key, _ := signing.Generate("")
enabled := false
client := clientFor(t, "warp.example", key, r)
client.enabled = func() bool { return enabled }
if _, err := client.Version(t.Context(), "remote.example"); !errors.Is(err, ErrDisabled) {
t.Errorf("Version while disabled = %v, want ErrDisabled", err)
}
enabled = true
client.policy = NewPolicy(nil, []string{"remote.example"})
if _, err := client.Version(t.Context(), "remote.example"); !errors.Is(err, ErrNotAllowed) {
t.Errorf("Version to a denied server = %v, want ErrNotAllowed", err)
}
if _, err := client.Version(t.Context(), "warp.example"); !errors.Is(err, ErrSelf) {
t.Errorf("Version to ourselves = %v, want ErrSelf", err)
}
if r.keyHits.Load() != 0 {
t.Error("a refused request reached the network")
}
}