Files
Warp/server/internal/config/phase1_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

140 lines
4.1 KiB
Go

package config
import (
"strings"
"testing"
"time"
)
func TestRateLimitDefaults(t *testing.T) {
cfg := Default()
if !cfg.ClientAPI.RateLimit.Enabled {
t.Error("rate limiting must be on by default: login is worth brute-forcing")
}
for name, bucket := range map[string]RateLimitBucket{
"default": cfg.ClientAPI.RateLimit.Default,
"login": cfg.ClientAPI.RateLimit.Login,
"register": cfg.ClientAPI.RateLimit.Register,
} {
if bucket.Burst < 1 || bucket.Interval <= 0 {
t.Errorf("the %s bucket is unusable by default: %+v", name, bucket)
}
}
// Login and registration are deliberately tighter than the rest.
if cfg.ClientAPI.RateLimit.Login.Interval <= cfg.ClientAPI.RateLimit.Default.Interval {
t.Error("the login bucket should refill more slowly than the default one")
}
if cfg.ClientAPI.RateLimit.Register.Interval <= cfg.ClientAPI.RateLimit.Login.Interval {
t.Error("the registration bucket should refill more slowly than the login one")
}
if cfg.ClientAPI.AccessTokenLifetime != 0 {
t.Error("access tokens must not expire by default: clients without refresh support need that")
}
if cfg.ClientAPI.TrustXForwardedFor {
t.Error("X-Forwarded-For must not be trusted by default: it is spoofable")
}
}
func TestRateLimitCanBeDisabledFromTheEnvironment(t *testing.T) {
cfg := Default()
if err := ApplyEnv(cfg, func(key string) string {
if key == "WARP_RATE_LIMIT_ENABLED" {
return "false"
}
return ""
}); err != nil {
t.Fatalf("ApplyEnv: %v", err)
}
if cfg.ClientAPI.RateLimit.Enabled {
t.Error("WARP_RATE_LIMIT_ENABLED=false did not take effect")
}
}
func TestValidateRejectsUnusableRateLimits(t *testing.T) {
cfg := Default()
cfg.Global.ServerName = "warp.example"
cfg.ClientAPI.RateLimit.Login.Burst = 0
cfg.ClientAPI.RateLimit.Register.Interval = 0
cfg.ClientAPI.AccessTokenLifetime = -time.Second
err := cfg.Validate()
if err == nil {
t.Fatal("expected validation to fail")
}
for _, want := range []string{
"client_api.rate_limit.login.burst",
"client_api.rate_limit.register.interval",
"client_api.access_token_lifetime",
} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error should mention %s, got: %v", want, err)
}
}
}
// TestDisabledRateLimitsSkipValidation: a deployment that turned the limiter
// off should not have to keep its buckets valid.
func TestDisabledRateLimitsSkipValidation(t *testing.T) {
cfg := Default()
cfg.Global.ServerName = "warp.example"
cfg.ClientAPI.RateLimit = RateLimit{Enabled: false}
if err := cfg.Validate(); err != nil {
t.Errorf("Validate with rate limiting off = %v, want nil", err)
}
}
func TestRegistrationTokens(t *testing.T) {
cfg := Default()
if cfg.Registration.RequiresToken() {
t.Error("no tokens configured means no token stage")
}
if cfg.Registration.TokenValid("") || cfg.Registration.TokenValid("anything") {
t.Error("with no tokens configured, nothing may validate")
}
cfg.Registration.Tokens = []string{"first-token", "second-token"}
if !cfg.Registration.RequiresToken() {
t.Error("configured tokens must add the token stage")
}
for _, token := range []string{"first-token", "second-token"} {
if !cfg.Registration.TokenValid(token) {
t.Errorf("TokenValid(%q) = false, want true", token)
}
}
for _, token := range []string{"", "FIRST-TOKEN", "first-toke", "first-tokenn", "nope"} {
if cfg.Registration.TokenValid(token) {
t.Errorf("TokenValid(%q) = true, want false", token)
}
}
}
func TestValidateRejectsEmptyRegistrationToken(t *testing.T) {
cfg := Default()
cfg.Global.ServerName = "warp.example"
cfg.Registration.Tokens = []string{"fine", " "}
err := cfg.Validate()
if err == nil || !strings.Contains(err.Error(), "registration.tokens[1]") {
t.Fatalf("expected an error naming the empty token, got: %v", err)
}
}
func TestRegistrationTokensFromTheEnvironment(t *testing.T) {
cfg := Default()
if err := ApplyEnv(cfg, func(key string) string {
if key == "WARP_REGISTRATION_TOKENS" {
return "one, two"
}
return ""
}); err != nil {
t.Fatalf("ApplyEnv: %v", err)
}
if len(cfg.Registration.Tokens) != 2 || !cfg.Registration.TokenValid("two") {
t.Errorf("tokens = %v", cfg.Registration.Tokens)
}
}