Files
Warp/server/internal/clientapi/devices_test.go
T
tajniak81andClaude Opus 5 6004bb01de
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 1: accounts and sessions
Implements PLAN.md Phase 1. A client can now register, log in, manage its
devices and set its profile; an operator can create accounts and flip the
runtime switches. There are still no rooms and no real /sync.

userapi: accounts, argon2id passwords in PHC format, devices, access and
refresh tokens stored as SHA-256 hashes, account data and the admin flag.
Its storage interface is storage.UserStore, implemented once in sqlstore
with `?` placeholders; the postgres driver supplies a rebind function and
unique-violation recognition rather than keeping a second copy of thirty
statements. Timestamps in these tables are int64 milliseconds, the unit the
Client-Server API uses on the wire.

Client-Server API: register (user-interactive auth with m.login.dummy and
optional registration tokens), register/available, token validity, login
with m.login.password, logout, logout/all, refresh, account/whoami,
capabilities, devices/* with password-gated deletion, profile/*, global and
per-room account data.

GET /sync is a deliberate placeholder: it answers a valid token with an
empty but well-formed response and refuses a revoked one. Phase 1 has no
rooms, so that is the whole truth, and it is what makes session revocation
observable. Phase 4 replaces it with the syncapi.

Auth middleware resolves a bearer token (header or the legacy query
parameter) to a device and user, answering an expired token with the soft
logout the spec asks for. Rate limiting is a token bucket per endpoint class
keyed on the client address, returning M_LIMIT_EXCEEDED with retry_after_ms.

adminapi v1 under /_warp/admin/v1: admin-only auth via a normal Matrix
token, GET/PUT /federation and /registration driving internal/settings and
reporting a config-pinned switch as a 409, GET/POST /users, GET /stats.

warp create-account [--admin] creates the first account straight in the
database, so a new install with registration off has a way in.

Complement is wired up for the federation=off profile: an image meeting its
contract, an entrypoint that configures Warp entirely from WARP_*
variables, and a runner that builds, clones and executes the target set
(TestRegistration, TestLogin, TestDeviceManagement). It has not been run
yet -- there is no container runtime on the development machine -- so the
three tests' assertions were read from the Complement sources and pinned as
httptest tests instead, and CI gained a job that runs the real thing.

Also fixes a .gitignore bug from Phase 0: the unanchored `warp` pattern,
meant for the built binary, also matched the directory server/cmd/warp and
silently excluded the entire command package from the repository. The
binary patterns are now anchored to the repository root, and cmd/warp is
committed for the first time.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-15 18:18:37 +02:00

251 lines
8.9 KiB
Go

package clientapi_test
import (
"net/http"
"testing"
)
const devicesPath = "/_matrix/client/v3/devices"
func TestGetDevices(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
ts.login(acc, "login_device")
ts.login(acc, "login_device_2")
rec := ts.get(devicesPath, acc.AccessToken)
expectStatus(t, rec, http.StatusOK)
devices, _ := jsonBody(t, rec)["devices"].([]any)
seen := map[string]bool{}
for _, device := range devices {
id, _ := device.(map[string]any)["device_id"].(string)
seen[id] = true
}
for _, want := range []string{acc.DeviceID, "login_device", "login_device_2"} {
if !seen[want] {
t.Errorf("/devices did not return %s (got %v)", want, seen)
}
}
}
// TestGetDevicesIsPerUser is the check that matters most here: one user must
// never see another's sessions.
func TestGetDevicesIsPerUser(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "superuser")
bob := ts.register("bob", "bobspassword")
rec := ts.get(devicesPath, bob.AccessToken)
expectStatus(t, rec, http.StatusOK)
devices, _ := jsonBody(t, rec)["devices"].([]any)
if len(devices) != 1 {
t.Fatalf("bob sees %d devices, want 1", len(devices))
}
if devices[0].(map[string]any)["device_id"] == alice.DeviceID {
t.Fatal("bob can see alice's device")
}
}
func TestGetDevice(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
rec := ts.do(request{method: http.MethodPost, path: loginPath, body: map[string]any{
"type": "m.login.password",
"identifier": map[string]any{"type": "m.id.user", "user": acc.UserID},
"password": "superuser",
"device_id": "login_device",
"initial_device_display_name": "device display",
}})
expectStatus(t, rec, http.StatusOK)
rec = ts.get(devicesPath+"/login_device", acc.AccessToken)
expectStatus(t, rec, http.StatusOK)
body := jsonBody(t, rec)
if body["device_id"] != "login_device" {
t.Errorf("device_id = %v", body["device_id"])
}
if body["display_name"] != "device display" {
t.Errorf("display_name = %v, want %q", body["display_name"], "device display")
}
}
func TestGetUnknownDeviceIs404(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
expectError(t, ts.get(devicesPath+"/unknown_device", acc.AccessToken),
http.StatusNotFound, "M_NOT_FOUND")
}
func TestPutDevice(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
rec := ts.do(request{method: http.MethodPut, path: devicesPath + "/" + acc.DeviceID,
token: acc.AccessToken, body: map[string]any{"display_name": "new device display"}})
expectStatus(t, rec, http.StatusOK)
rec = ts.get(devicesPath+"/"+acc.DeviceID, acc.AccessToken)
if got := jsonBody(t, rec)["display_name"]; got != "new device display" {
t.Errorf("display_name = %v, want the updated one", got)
}
}
func TestPutUnknownDeviceIs404(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
rec := ts.do(request{method: http.MethodPut, path: devicesPath + "/unknown_device",
token: acc.AccessToken, body: map[string]any{"display_name": "x"}})
expectError(t, rec, http.StatusNotFound, "M_NOT_FOUND")
}
// TestDeleteDeviceRequiresInteractiveAuth is the reason this endpoint is
// interactive: a stolen access token must not be enough to log out the real
// owner, which is exactly what they are trying to do when they reach for it.
func TestDeleteDeviceRequiresInteractiveAuth(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
victim := ts.login(acc, "")
path := devicesPath + "/" + victim.DeviceID
// No body at all: the client does not know yet that it has to
// authenticate, so this is the normal first request.
rec := ts.do(request{method: http.MethodDelete, path: path, token: acc.AccessToken})
expectStatus(t, rec, http.StatusUnauthorized)
body := jsonBody(t, rec)
for _, key := range []string{"session", "flows", "params"} {
if _, present := body[key]; !present {
t.Errorf("the challenge is missing %s: %s", key, rec.Body.String())
}
}
flows, _ := body["flows"].([]any)
stages, _ := flows[0].(map[string]any)["stages"].([]any)
if len(stages) != 1 || stages[0] != "m.login.password" {
t.Fatalf("stages = %v, want [m.login.password]", stages)
}
session := body["session"]
// The wrong password fails the stage and says so, keeping the session.
rec = ts.do(request{method: http.MethodDelete, path: path, token: acc.AccessToken,
body: map[string]any{"auth": map[string]any{
"type": "m.login.password",
"identifier": map[string]any{"type": "m.id.user", "user": acc.UserID},
"password": "super-wrong-password",
"session": session,
}}})
expectStatus(t, rec, http.StatusUnauthorized)
body = jsonBody(t, rec)
if body["errcode"] != "M_FORBIDDEN" {
t.Errorf("errcode = %v, want M_FORBIDDEN: %s", body["errcode"], rec.Body.String())
}
for _, key := range []string{"session", "flows", "params", "error"} {
if _, present := body[key]; !present {
t.Errorf("a failed stage is missing %s: %s", key, rec.Body.String())
}
}
// The device survived the failed attempt.
expectStatus(t, ts.get(path, acc.AccessToken), http.StatusOK)
// The right password completes it.
rec = ts.do(request{method: http.MethodDelete, path: path, token: acc.AccessToken,
body: map[string]any{"auth": map[string]any{
"type": "m.login.password",
"identifier": map[string]any{"type": "m.id.user", "user": acc.UserID},
"password": "superuser",
"session": session,
}}})
expectStatus(t, rec, http.StatusOK)
expectError(t, ts.get(path, acc.AccessToken), http.StatusNotFound, "M_NOT_FOUND")
expectError(t, ts.get("/_matrix/client/v3/sync", victim.AccessToken),
http.StatusUnauthorized, "M_UNKNOWN_TOKEN")
}
// TestDeleteDeviceRequiresTheOwnersPassword is the flaw the stage exists to
// prevent: knowing *somebody's* password must not pass the
// re-authentication on another account's session.
func TestDeleteDeviceRequiresTheOwnersPassword(t *testing.T) {
ts := newTestServer(t, nil)
alice := ts.register("alice", "superuser")
bob := ts.register("bob", "bobspassword")
victim := ts.login(alice, "")
path := devicesPath + "/" + victim.DeviceID
rec := ts.do(request{method: http.MethodDelete, path: path, token: alice.AccessToken})
session := jsonBody(t, rec)["session"]
rec = ts.do(request{method: http.MethodDelete, path: path, token: alice.AccessToken,
body: map[string]any{"auth": map[string]any{
"type": "m.login.password",
"identifier": map[string]any{"type": "m.id.user", "user": bob.UserID},
"password": "bobspassword",
"session": session,
}}})
expectError(t, rec, http.StatusForbidden, "M_FORBIDDEN")
expectStatus(t, ts.get(path, alice.AccessToken), http.StatusOK)
// Alice's own password still works.
rec = ts.do(request{method: http.MethodDelete, path: path, token: alice.AccessToken,
body: map[string]any{"auth": map[string]any{
"type": "m.login.password",
"identifier": map[string]any{"type": "m.id.user", "user": alice.UserID},
"password": "superuser",
"session": session,
}}})
expectStatus(t, rec, http.StatusOK)
expectError(t, ts.get(path, alice.AccessToken), http.StatusNotFound, "M_NOT_FOUND")
}
func TestDeleteDevices(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
first := ts.login(acc, "DEVICE_ONE")
second := ts.login(acc, "DEVICE_TWO")
const path = "/_matrix/client/v3/delete_devices"
rec := ts.do(request{method: http.MethodPost, path: path, token: acc.AccessToken,
body: map[string]any{"devices": []string{"DEVICE_ONE", "DEVICE_TWO"}}})
expectStatus(t, rec, http.StatusUnauthorized)
session := jsonBody(t, rec)["session"]
rec = ts.do(request{method: http.MethodPost, path: path, token: acc.AccessToken,
body: map[string]any{
"devices": []string{"DEVICE_ONE", "DEVICE_TWO"},
"auth": map[string]any{
"type": "m.login.password",
"identifier": map[string]any{"type": "m.id.user", "user": acc.UserID},
"password": "superuser",
"session": session,
},
}})
expectStatus(t, rec, http.StatusOK)
for _, token := range []string{first.AccessToken, second.AccessToken} {
expectError(t, ts.get("/_matrix/client/v3/sync", token),
http.StatusUnauthorized, "M_UNKNOWN_TOKEN")
}
// The session doing the deleting is untouched.
expectStatus(t, ts.get("/_matrix/client/v3/sync", acc.AccessToken), http.StatusOK)
}
// TestDeviceLastSeenIsRecorded covers the bookkeeping the device list shows.
func TestDeviceLastSeenIsRecorded(t *testing.T) {
ts := newTestServer(t, nil)
acc := ts.register("alice", "superuser")
rec := ts.get(devicesPath+"/"+acc.DeviceID, acc.AccessToken)
expectStatus(t, rec, http.StatusOK)
body := jsonBody(t, rec)
if ts, _ := body["last_seen_ts"].(float64); ts <= 0 {
t.Errorf("last_seen_ts = %v, want the time of the request that created it", body["last_seen_ts"])
}
}