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
Implements PLAN.md Phase 6: the media repository. Clients upload files, reserve an mxc:// URI and upload to it later, download and ask for thumbnails, all through the authenticated media endpoints of Matrix 1.11; other servers download this server's media through the federation media endpoints, and media uploaded elsewhere is fetched over federation on first use and kept. internal/mediaapi is the component. Content goes into a BlobStore named by its SHA-256, so the same file uploaded twice is stored once and the hash doubles as the ETag; the metadata that maps an mxc:// URI onto a blob is in media_repository and media_thumbnails. The local-filesystem store writes to a temporary file and renames it into place once hashed, so nothing ever refers to half a file. A storage driver can bring its own store through storage.BlobProvider, which is where PocketBase's filesystem abstraction (local or S3) plugs in with Phase 10. Reserved uploads belong to the user who made them, take one upload, lapse after a day, and are capped per user; a download of one waits for its content, woken in memory the moment it arrives, for as long as timeout_ms allows. Thumbnails come from a fixed set of configured sizes, so a thousand requested sizes cannot mean a thousand resizes. They are made on first request and kept, never enlarge, honour a JPEG's EXIF orientation, and point at the original when it already fits. The pixel count is checked from the header before anything is decoded, at most four resizes run at once, and concurrent requests for the same thumbnail share one. Remote media is fetched through the signed client: the multipart/mixed answer of Matrix 1.11, with a fallback to the legacy download endpoint for servers that answer M_UNRECOGNIZED, and Location redirects followed by a dialer that refuses loopback, private and link-local addresses. Concurrent requests share one fetch, detached from the request that started it. With federation off nothing is fetched, and what was cached before is still served. Downloads carry a sandboxing CSP, nosniff, inline Content-Disposition only for the types the spec lists as safe, and private immutable caching. The unauthenticated legacy download endpoints are off by default behind media.legacy_unauthenticated_downloads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
227 lines
7.4 KiB
Go
227 lines
7.4 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func writeTemp(t *testing.T, body string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "warp.yaml")
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLoadAppliesDefaults(t *testing.T) {
|
|
path := writeTemp(t, "version: 1\nglobal:\n server_name: warp.example\n")
|
|
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if got, want := cfg.Listen.Address, ":8008"; got != want {
|
|
t.Errorf("listen.address = %q, want %q", got, want)
|
|
}
|
|
if got, want := cfg.Database.Driver, "sqlite"; got != want {
|
|
t.Errorf("database.driver = %q, want %q", got, want)
|
|
}
|
|
if cfg.Federation.Enabled {
|
|
t.Error("federation must default to disabled")
|
|
}
|
|
if !cfg.Federation.AllowRuntimeToggle {
|
|
t.Error("federation.allow_runtime_toggle must default to true")
|
|
}
|
|
if got, want := cfg.ClientBaseURL(), "https://warp.example"; got != want {
|
|
t.Errorf("ClientBaseURL() = %q, want %q", got, want)
|
|
}
|
|
if cfg.Path() == "" {
|
|
t.Error("Path() must record the file the config came from")
|
|
}
|
|
}
|
|
|
|
func TestLoadRejectsUnknownFields(t *testing.T) {
|
|
path := writeTemp(t, "version: 1\nglobal:\n server_name: warp.example\n srever_name: typo\n")
|
|
|
|
_, err := Load(path)
|
|
if err == nil {
|
|
t.Fatal("expected an error for an unknown field")
|
|
}
|
|
if !strings.Contains(err.Error(), "srever_name") {
|
|
t.Errorf("error should name the offending field, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateCollectsEveryProblem(t *testing.T) {
|
|
cfg := Default()
|
|
cfg.Global.ServerName = ""
|
|
cfg.Database.Driver = "mysql"
|
|
cfg.Logging.Level = "chatty"
|
|
|
|
err := cfg.Validate()
|
|
if err == nil {
|
|
t.Fatal("expected validation to fail")
|
|
}
|
|
for _, want := range []string{"global.server_name", "database.driver", "logging.level"} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Errorf("error should mention %s, got: %v", want, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestApplyEnvOverrides(t *testing.T) {
|
|
cfg := Default()
|
|
cfg.Global.ServerName = "from-yaml.example"
|
|
cfg.Federation.Allowlist = []string{"a.example"}
|
|
|
|
env := map[string]string{
|
|
"WARP_SERVER_NAME": "from-env.example",
|
|
"WARP_FEDERATION_ENABLED": "true",
|
|
"WARP_FEDERATION_ALLOWLIST": "b.example, c.example",
|
|
"WARP_DB_MAX_OPEN_CONNS": "42",
|
|
"WARP_SHUTDOWN_TIMEOUT": "5s",
|
|
"WARP_WELL_KNOWN_CLIENT_BASE_URL": "https://matrix.example",
|
|
}
|
|
if err := ApplyEnv(cfg, func(k string) string { return env[k] }); err != nil {
|
|
t.Fatalf("ApplyEnv: %v", err)
|
|
}
|
|
|
|
if got, want := cfg.Global.ServerName, "from-env.example"; got != want {
|
|
t.Errorf("server_name = %q, want %q", got, want)
|
|
}
|
|
if !cfg.Federation.Enabled {
|
|
t.Error("federation.enabled should have been overridden to true")
|
|
}
|
|
if got, want := strings.Join(cfg.Federation.Allowlist, ","), "b.example,c.example"; got != want {
|
|
t.Errorf("allowlist = %q, want %q", got, want)
|
|
}
|
|
if got, want := cfg.Database.MaxOpenConns, 42; got != want {
|
|
t.Errorf("max_open_conns = %d, want %d", got, want)
|
|
}
|
|
if got, want := cfg.Listen.ShutdownTimeout, 5*time.Second; got != want {
|
|
t.Errorf("shutdown_timeout = %s, want %s", got, want)
|
|
}
|
|
if got, want := cfg.ClientBaseURL(), "https://matrix.example"; got != want {
|
|
t.Errorf("ClientBaseURL() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestApplyEnvRejectsMalformedValues(t *testing.T) {
|
|
cfg := Default()
|
|
err := ApplyEnv(cfg, func(k string) string {
|
|
if k == "WARP_FEDERATION_ENABLED" {
|
|
return "yes-please"
|
|
}
|
|
return ""
|
|
})
|
|
if err == nil || !strings.Contains(err.Error(), "WARP_FEDERATION_ENABLED") {
|
|
t.Fatalf("expected an error naming the variable, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestApplyEnvEmptyListSentinel(t *testing.T) {
|
|
cfg := Default()
|
|
cfg.ClientAPI.CORSAllowedOrigins = []string{"*"}
|
|
if err := ApplyEnv(cfg, func(k string) string {
|
|
if k == "WARP_CORS_ALLOWED_ORIGINS" {
|
|
return "-"
|
|
}
|
|
return ""
|
|
}); err != nil {
|
|
t.Fatalf("ApplyEnv: %v", err)
|
|
}
|
|
if len(cfg.ClientAPI.CORSAllowedOrigins) != 0 {
|
|
t.Errorf("expected the list to be cleared, got %v", cfg.ClientAPI.CORSAllowedOrigins)
|
|
}
|
|
}
|
|
|
|
// TestExampleConfigIsValid keeps warp.yaml.example honest: it must parse
|
|
// with unknown fields rejected and pass validation as shipped.
|
|
func TestExampleConfigIsValid(t *testing.T) {
|
|
path := filepath.Join("..", "..", "..", "warp.yaml.example")
|
|
if _, err := os.Stat(path); err != nil {
|
|
t.Skipf("example config not found: %v", err)
|
|
}
|
|
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("warp.yaml.example does not load: %v", err)
|
|
}
|
|
if cfg.Federation.Enabled {
|
|
t.Error("the shipped example must keep federation off")
|
|
}
|
|
if cfg.Registration.Enabled {
|
|
t.Error("the shipped example must keep registration off")
|
|
}
|
|
if cfg.Database.Driver != "sqlite" {
|
|
t.Errorf("example driver = %q, want sqlite", cfg.Database.Driver)
|
|
}
|
|
if cfg.Sync.Presence {
|
|
t.Error("the shipped example must keep presence off (PLAN.md open question 3)")
|
|
}
|
|
// A zero here would mean the example omitted the section and the
|
|
// validation below is passing on the defaults rather than on the file.
|
|
if cfg.Sync.TypingTimeout == 0 || cfg.Sync.LongPollTimeout == 0 {
|
|
t.Error("the shipped example must set the sync timeouts")
|
|
}
|
|
if cfg.ClientAPI.RateLimit.Sync.Burst == 0 {
|
|
t.Error("the shipped example must set the sync rate-limit bucket")
|
|
}
|
|
// The example documents the defaults, so it has to be them.
|
|
if !reflect.DeepEqual(cfg.Media, Default().Media) {
|
|
t.Errorf("example media section = %+v, want the defaults %+v", cfg.Media, Default().Media)
|
|
}
|
|
if cfg.ClientAPI.RateLimit.Media != Default().ClientAPI.RateLimit.Media {
|
|
t.Error("the shipped example must set the media rate-limit bucket to its default")
|
|
}
|
|
}
|
|
|
|
func TestMediaValidation(t *testing.T) {
|
|
for name, tweak := range map[string]func(*Media){
|
|
"empty base path": func(m *Media) { m.BasePath = "" },
|
|
"zero upload size": func(m *Media) { m.MaxUploadBytes = 0 },
|
|
"zero pixels": func(m *Media) { m.MaxThumbnailPixels = 0 },
|
|
"unknown method": func(m *Media) { m.ThumbnailSizes = []ThumbnailSize{{Width: 1, Height: 1, Method: "stretch"}} },
|
|
"zero-sized thumbnail": func(m *Media) { m.ThumbnailSizes = []ThumbnailSize{{Width: 0, Height: 10, Method: "crop"}} },
|
|
"huge thumbnail": func(m *Media) { m.ThumbnailSizes = []ThumbnailSize{{Width: 5000, Height: 10, Method: "scale"}} },
|
|
"no pending expiry": func(m *Media) { m.PendingUploadExpiry = 0 },
|
|
"no pending uploads": func(m *Media) { m.MaxPendingUploads = 0 },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
cfg := Default()
|
|
cfg.Global.ServerName = "warp.example"
|
|
tweak(&cfg.Media)
|
|
if err := cfg.Validate(); err == nil || !strings.Contains(err.Error(), "media.") {
|
|
t.Errorf("Validate = %v, want a media error", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
cfg := Default()
|
|
cfg.Global.ServerName = "warp.example"
|
|
cfg.Media.ThumbnailSizes = nil
|
|
if err := cfg.Validate(); err != nil {
|
|
t.Errorf("no thumbnail sizes at all is valid (thumbnails are then refused): %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMediaFromTheEnvironment(t *testing.T) {
|
|
cfg := Default()
|
|
env := map[string]string{
|
|
"WARP_MEDIA_BASE_PATH": "/srv/media",
|
|
"WARP_MEDIA_MAX_UPLOAD_BYTES": "1024",
|
|
"WARP_MEDIA_LEGACY_UNAUTHENTICATED_DOWNLOADS": "true",
|
|
}
|
|
if err := ApplyEnv(cfg, func(k string) string { return env[k] }); err != nil {
|
|
t.Fatalf("ApplyEnv: %v", err)
|
|
}
|
|
if cfg.Media.BasePath != "/srv/media" || cfg.Media.MaxUploadBytes != 1024 || !cfg.Media.LegacyUnauthenticatedDownloads {
|
|
t.Errorf("media = %+v", cfg.Media)
|
|
}
|
|
}
|