|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// authMiddlewareHarness builds a contract harness, optionally toggling |
| 12 | +// AUTH_MODE=enabled. Because t.Setenv resets the value at test end, each |
| 13 | +// subtest can opt in or out of enforcement independently. |
| 14 | +func authMiddlewareHarness(t *testing.T, enabled bool) *contractHarness { |
| 15 | + t.Helper() |
| 16 | + h := newContractHarness(t) |
| 17 | + if enabled { |
| 18 | + t.Setenv("AUTH_MODE", "enabled") |
| 19 | + } |
| 20 | + return h |
| 21 | +} |
| 22 | + |
| 23 | +// mintAppPassword creates a real app-password record on the harness app and |
| 24 | +// returns the compact (XXX-XXX-formatted) plain-text key. The HTTP layer is |
| 25 | +// not used so the call works regardless of AUTH_MODE. |
| 26 | +func mintAppPassword(t *testing.T, h *contractHarness) string { |
| 27 | + t.Helper() |
| 28 | + h.app.mu.Lock() |
| 29 | + _, key := h.app.createAppPasswordLocked("middleware-test", time.Now().UTC()) |
| 30 | + h.app.mu.Unlock() |
| 31 | + if _, _, ok := normalizeAppPasswordInput(key); !ok { |
| 32 | + t.Fatalf("created app password has unexpected format: %q", key) |
| 33 | + } |
| 34 | + return formatAppPasswordCompact(key) |
| 35 | +} |
| 36 | + |
| 37 | +func rawRequest(method, path string) *http.Request { |
| 38 | + req := httptest.NewRequest(method, path, nil) |
| 39 | + req.Header.Set("X-CSRF-Protection", "1") |
| 40 | + return req |
| 41 | +} |
| 42 | + |
| 43 | +// --- AUTH_MODE=disabled (baseline — must keep existing behavior) --- |
| 44 | + |
| 45 | +func TestAuthMiddleware_Disabled_AllowsApiSavesWithoutAuth(t *testing.T) { |
| 46 | + h := authMiddlewareHarness(t, false) |
| 47 | + rr := h.do(rawRequest(http.MethodGet, "/api/saves")) |
| 48 | + assertStatus(t, rr, http.StatusOK) |
| 49 | +} |
| 50 | + |
| 51 | +// --- AUTH_MODE=enabled — enforcement --- |
| 52 | + |
| 53 | +func TestAuthMiddleware_Enabled_BlocksApiSavesWithoutAuth(t *testing.T) { |
| 54 | + h := authMiddlewareHarness(t, true) |
| 55 | + rr := h.do(rawRequest(http.MethodGet, "/api/saves")) |
| 56 | + assertStatus(t, rr, http.StatusUnauthorized) |
| 57 | + assertJSONContentType(t, rr) |
| 58 | +} |
| 59 | + |
| 60 | +func TestAuthMiddleware_Enabled_AllowsValidBearerAppPassword(t *testing.T) { |
| 61 | + h := authMiddlewareHarness(t, true) |
| 62 | + key := mintAppPassword(t, h) |
| 63 | + req := rawRequest(http.MethodGet, "/api/saves") |
| 64 | + req.Header.Set("Authorization", "Bearer "+key) |
| 65 | + rr := h.do(req) |
| 66 | + assertStatus(t, rr, http.StatusOK) |
| 67 | +} |
| 68 | + |
| 69 | +func TestAuthMiddleware_Enabled_AllowsValidXRSMAppPasswordHeader(t *testing.T) { |
| 70 | + h := authMiddlewareHarness(t, true) |
| 71 | + key := mintAppPassword(t, h) |
| 72 | + req := rawRequest(http.MethodGet, "/api/saves") |
| 73 | + req.Header.Set("X-RSM-App-Password", key) |
| 74 | + rr := h.do(req) |
| 75 | + assertStatus(t, rr, http.StatusOK) |
| 76 | +} |
| 77 | + |
| 78 | +func TestAuthMiddleware_Enabled_AllowsRemoteUserHeader(t *testing.T) { |
| 79 | + h := authMiddlewareHarness(t, true) |
| 80 | + req := rawRequest(http.MethodGet, "/api/saves") |
| 81 | + req.Header.Set("Remote-User", "alice") |
| 82 | + rr := h.do(req) |
| 83 | + assertStatus(t, rr, http.StatusOK) |
| 84 | +} |
| 85 | + |
| 86 | +func TestAuthMiddleware_Enabled_AllowsNonEmptySessionCookie(t *testing.T) { |
| 87 | + h := authMiddlewareHarness(t, true) |
| 88 | + req := rawRequest(http.MethodGet, "/api/saves") |
| 89 | + req.AddCookie(&http.Cookie{Name: "session", Value: "anything"}) |
| 90 | + rr := h.do(req) |
| 91 | + assertStatus(t, rr, http.StatusOK) |
| 92 | +} |
| 93 | + |
| 94 | +// --- AUTH_MODE=enabled — allowlist (bootstrap endpoints) --- |
| 95 | + |
| 96 | +func TestAuthMiddleware_Enabled_AllowsHealthzWithoutAuth(t *testing.T) { |
| 97 | + h := authMiddlewareHarness(t, true) |
| 98 | + rr := h.do(rawRequest(http.MethodGet, "/healthz")) |
| 99 | + assertStatus(t, rr, http.StatusOK) |
| 100 | +} |
| 101 | + |
| 102 | +func TestAuthMiddleware_Enabled_AllowsRuntimeConfigWithoutAuth(t *testing.T) { |
| 103 | + h := authMiddlewareHarness(t, true) |
| 104 | + rr := h.do(rawRequest(http.MethodGet, "/runtime-config")) |
| 105 | + assertStatus(t, rr, http.StatusOK) |
| 106 | +} |
| 107 | + |
| 108 | +func TestAuthMiddleware_Enabled_AllowsAuthLoginWithoutAuth(t *testing.T) { |
| 109 | + h := authMiddlewareHarness(t, true) |
| 110 | + req := httptest.NewRequest(http.MethodPost, "/auth/login", strings.NewReader(`{}`)) |
| 111 | + req.Header.Set("Content-Type", "application/json") |
| 112 | + req.Header.Set("X-CSRF-Protection", "1") |
| 113 | + rr := h.do(req) |
| 114 | + assertStatus(t, rr, http.StatusOK) |
| 115 | +} |
| 116 | + |
| 117 | +// --- AUTH_MODE=enabled — bad credentials still rejected --- |
| 118 | + |
| 119 | +func TestAuthMiddleware_Enabled_RejectsGarbageBearer(t *testing.T) { |
| 120 | + h := authMiddlewareHarness(t, true) |
| 121 | + req := rawRequest(http.MethodGet, "/api/saves") |
| 122 | + // Format-valid (six A-Z0-9 chars) but not bound to any real password. |
| 123 | + req.Header.Set("Authorization", "Bearer ZZZZZZ") |
| 124 | + rr := h.do(req) |
| 125 | + assertStatus(t, rr, http.StatusUnauthorized) |
| 126 | +} |
0 commit comments