| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestRosterStopFinishedRedirects(t *testing.T) { |
| 13 | r := newToolRig(t) |
| 14 | if err := r.runner.Send(r.ctx, "finish-before-stop", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Finished."}); err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | r.wait("finished", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) |
| 18 | status, location := r.post(r.inst.Cookie(), "/conversation/turns/"+r.turn+"/stop", url.Values{}) |
| 19 | if status != http.StatusSeeOther || location != "/conversation" { |
| 20 | t.Fatalf("finished Stop: %d %s", status, location) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestRosterActionCSRF(t *testing.T) { |
| 25 | r := newToolRig(t) |
| 26 | if res := r.call("csrf-question", wire.ToolAskPerson, map[string]any{"question": "Which page?", "options": []string{}}); res.Code != "asked" { |
| 27 | t.Fatal(res) |
| 28 | } |
| 29 | snap, err := r.client.Snapshot(r.ctx) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | var card wire.Card |
| 34 | for _, c := range snap.Cards { |
| 35 | if c.Kind == wire.CardQuestion { |
| 36 | card = c |
| 37 | } |
| 38 | } |
| 39 | if _, err := r.client.Answer(r.ctx, card.ID, "This one", card.Revision); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | paths := []string{"/conversation/cards/" + card.ID + "/visibility", "/conversation/cards/" + card.ID + "/reply", "/conversation/turns/" + r.turn + "/stop"} |
| 43 | for _, path := range paths { |
| 44 | req, _ := http.NewRequest("POST", r.inst.URL+path, strings.NewReader(url.Values{"aside": {"true"}, "revision": {"2"}, "request_id": {"csrf-reply"}, "body": {"Unsafe reply"}}.Encode())) |
| 45 | req.AddCookie(r.inst.Cookie()) |
| 46 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 47 | req.Header.Set("Origin", "https://foreign.example") |
| 48 | req.Header.Set("Sec-Fetch-Site", "cross-site") |
| 49 | res, err := http.DefaultClient.Do(req) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | io.Copy(io.Discard, res.Body) |
| 54 | res.Body.Close() |
| 55 | if res.StatusCode != http.StatusForbidden { |
| 56 | t.Fatalf("cross-site %s: %d", path, res.StatusCode) |
| 57 | } |
| 58 | } |
| 59 | var n int |
| 60 | for _, q := range []string{"SELECT count(*) FROM chat_card_visibility", "SELECT count(*) FROM chat_card_threads", "SELECT count(*) FROM chat_messages WHERE text='Unsafe reply'", "SELECT count(*) FROM runner_commands WHERE kind='cancel'"} { |
| 61 | if err := r.inst.DB.Writer().QueryRow(q).Scan(&n); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if n != 0 { |
| 65 | t.Fatalf("CSRF changed state: %s", q) |
| 66 | } |
| 67 | } |
| 68 | var state string |
| 69 | if err := r.inst.DB.Writer().QueryRow("SELECT state FROM chat_turns WHERE id=?", r.turn).Scan(&state); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if state != wire.TurnRunning { |
| 73 | t.Fatalf("cross-site Stop changed turn: %s", state) |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |