| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/go-chi/chi/v5" |
| 13 | ) |
| 14 | |
| 15 | func TestPreviewRelativeLifetimes(t *testing.T) { |
| 16 | for _, seconds := range []int{900, 123, 30} { |
| 17 | t.Run(time.Duration(seconds).String(), func(t *testing.T) { |
| 18 | r := newPreviewPassRig(t) |
| 19 | if _, e := r.s.db.Writer().Exec("UPDATE person_devices SET token_expires_at=? WHERE id=?", r.clock.Now().Add(time.Duration(seconds)*time.Second+999*time.Millisecond), r.credential.ID); e != nil { |
| 20 | t.Fatal(e) |
| 21 | } |
| 22 | p := r.mint(t) |
| 23 | raw, _ := json.Marshal(p) |
| 24 | var fields map[string]any |
| 25 | if e := json.Unmarshal(raw, &fields); e != nil { |
| 26 | t.Fatal(e) |
| 27 | } |
| 28 | if fields["expires_in"] != float64(min(60, seconds)) || fields["grant_expires_in"] != float64(seconds) { |
| 29 | t.Fatalf("relative deadlines: %s", raw) |
| 30 | } |
| 31 | }) |
| 32 | } |
| 33 | } |
| 34 | func TestPreviewJunkAndOtherTaskCookiesDoNotSelectRestrictedLane(t *testing.T) { |
| 35 | r := newPreviewPassRig(t) |
| 36 | app := &application{store: r.s, origin: r.s.personOrigin} |
| 37 | for _, cookie := range []string{ |
| 38 | "__Host-equipo-preview-tk_preview=junk", |
| 39 | "__Host-equipo-preview-tk_other=" + strings.Repeat("A", 43), |
| 40 | "__Secure-equipo-preview=junk", |
| 41 | } { |
| 42 | route := chi.NewRouter() |
| 43 | route.Get("/tasks/{id}/preview", app.previewGrantOrBrowser(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(204) })).ServeHTTP) |
| 44 | req := httptest.NewRequest("GET", "https://equipo.example/tasks/tk_preview/preview", nil) |
| 45 | req.Header.Set("Cookie", cookie) |
| 46 | w := httptest.NewRecorder() |
| 47 | route.ServeHTTP(w, req) |
| 48 | if w.Code != 204 { |
| 49 | t.Fatalf("planted cookie blocked browser: %q: %d", cookie, w.Code) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | func TestPreviewTerminalRefusalsAuditOnce(t *testing.T) { |
| 54 | for _, mode := range []string{"pass", "grant"} { |
| 55 | for _, reason := range []string{"revoked", "member", "epoch", "stale"} { |
| 56 | t.Run(mode+"/"+reason, func(t *testing.T) { |
| 57 | r := newPreviewPassRig(t) |
| 58 | p := r.mint(t) |
| 59 | token := p.Pass |
| 60 | if mode == "grant" { |
| 61 | g, e := r.s.exchangePreviewPass(r.ctx, p.Pass, "tk_preview", passCandidate, "192.0.2.1") |
| 62 | if e != nil { |
| 63 | t.Fatal(e) |
| 64 | } |
| 65 | token = g.Secret |
| 66 | } |
| 67 | query := "UPDATE person_devices SET revoked_at=CURRENT_TIMESTAMP" |
| 68 | switch reason { |
| 69 | case "member": |
| 70 | query = "UPDATE members SET active=0" |
| 71 | case "epoch": |
| 72 | query = "UPDATE members SET session_epoch=session_epoch+86400" |
| 73 | case "stale": |
| 74 | query = "UPDATE chat_steps SET attempt=2 WHERE id='preview_step'" |
| 75 | } |
| 76 | if _, e := r.s.db.Writer().Exec(query); e != nil { |
| 77 | t.Fatal(e) |
| 78 | } |
| 79 | for i := 0; i < 3; i++ { |
| 80 | var e error |
| 81 | if mode == "pass" { |
| 82 | _, e = r.s.exchangePreviewPass(r.ctx, token, "tk_preview", passCandidate, "192.0.2.1") |
| 83 | } else { |
| 84 | _, e = r.s.readPreviewGrant(r.ctx, token, "tk_preview", "192.0.2.1") |
| 85 | } |
| 86 | if e == nil { |
| 87 | t.Fatal("accepted dead token") |
| 88 | } |
| 89 | if i > 0 && !errors.Is(e, ErrPreviewAccess) { |
| 90 | t.Fatal("repeated identifiable refusal", e) |
| 91 | } |
| 92 | } |
| 93 | var audits int |
| 94 | if e := r.s.db.Reader().QueryRow("SELECT count(*) FROM audit_events WHERE kind='preview.pass.refused'").Scan(&audits); e != nil || audits != 1 { |
| 95 | t.Fatal("terminal refusal audit count", audits, e) |
| 96 | } |
| 97 | query = "SELECT count(*) FROM preview_passes WHERE consumed_at IS NULL" |
| 98 | if mode == "grant" { |
| 99 | query = "SELECT count(*) FROM preview_grants" |
| 100 | } |
| 101 | var active int |
| 102 | if e := r.s.db.Reader().QueryRow(query).Scan(&active); e != nil || active != 0 { |
| 103 | t.Fatal("dead token retained", active, e) |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | func TestPreviewPrunesWithoutAnotherMint(t *testing.T) { |
| 110 | for _, operation := range []string{"exchange", "read"} { |
| 111 | t.Run(operation, func(t *testing.T) { |
| 112 | r := newPreviewPassRig(t) |
| 113 | p := r.mint(t) |
| 114 | if _, e := r.s.exchangePreviewPass(r.ctx, p.Pass, "tk_preview", passCandidate, "192.0.2.1"); e != nil { |
| 115 | t.Fatal(e) |
| 116 | } |
| 117 | r.clock.Advance(16 * time.Minute) |
| 118 | if operation == "exchange" { |
| 119 | _, _ = r.s.exchangePreviewPass(r.ctx, strings.Repeat("A", 43), "tk_preview", passCandidate, "192.0.2.1") |
| 120 | } else { |
| 121 | _, _ = r.s.readPreviewGrant(r.ctx, strings.Repeat("A", 43), "tk_preview", "192.0.2.1") |
| 122 | } |
| 123 | for _, table := range []string{"preview_passes", "preview_grants"} { |
| 124 | var n int |
| 125 | if e := r.s.db.Reader().QueryRow("SELECT count(*) FROM " + table).Scan(&n); e != nil || n != 0 { |
| 126 | t.Fatal("expired rows retained", table, n, e) |
| 127 | } |
| 128 | } |
| 129 | }) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestPreviewTerminalAuditRollback(t *testing.T) { |
| 134 | for _, mode := range []string{"pass", "grant"} { |
| 135 | t.Run(mode, func(t *testing.T) { |
| 136 | r := newPreviewPassRig(t) |
| 137 | p := r.mint(t) |
| 138 | token := p.Pass |
| 139 | if mode == "grant" { |
| 140 | g, e := r.s.exchangePreviewPass(r.ctx, p.Pass, "tk_preview", passCandidate, "192.0.2.1") |
| 141 | if e != nil { |
| 142 | t.Fatal(e) |
| 143 | } |
| 144 | token = g.Secret |
| 145 | } |
| 146 | if _, e := r.s.db.Writer().Exec("UPDATE chat_steps SET attempt=2 WHERE id='preview_step'"); e != nil { |
| 147 | t.Fatal(e) |
| 148 | } |
| 149 | if _, e := r.s.db.Writer().Exec(`CREATE TRIGGER deny_preview_refusal BEFORE INSERT ON audit_events WHEN NEW.kind='preview.pass.refused' BEGIN SELECT RAISE(ABORT,'fixture'); END`); e != nil { |
| 150 | t.Fatal(e) |
| 151 | } |
| 152 | var e error |
| 153 | if mode == "pass" { |
| 154 | _, e = r.s.exchangePreviewPass(r.ctx, token, "tk_preview", passCandidate, "192.0.2.1") |
| 155 | } else { |
| 156 | _, e = r.s.readPreviewGrant(r.ctx, token, "tk_preview", "192.0.2.1") |
| 157 | } |
| 158 | if e == nil { |
| 159 | t.Fatal("accepted without audit") |
| 160 | } |
| 161 | query := "SELECT count(*) FROM preview_passes WHERE consumed_at IS NULL" |
| 162 | if mode == "grant" { |
| 163 | query = "SELECT count(*) FROM preview_grants" |
| 164 | } |
| 165 | var count int |
| 166 | if e := r.s.db.Reader().QueryRow(query).Scan(&count); e != nil || count != 1 { |
| 167 | t.Fatal("non-transactional terminal change", count, e) |
| 168 | } |
| 169 | }) |
| 170 | } |
| 171 | } |
| 172 | func TestPreviewDeviceTaskIndexes(t *testing.T) { |
| 173 | r := newPreviewPassRig(t) |
| 174 | for _, table := range []string{"preview_passes", "preview_grants"} { |
| 175 | var definition string |
| 176 | if e := r.s.db.Reader().QueryRow("SELECT sql FROM sqlite_master WHERE type='index' AND name=?", "idx_"+table+"_device_task").Scan(&definition); e != nil || !strings.Contains(definition, "(credential, task_id)") { |
| 177 | t.Fatal("missing composite index", table, definition, e) |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |