| 1 | package dashboard |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func TestProviderRequiresBoundTokenAndExistingIdentity(t *testing.T) { |
| 13 | db, e := sql.Open("sqlite", t.TempDir()+"/provider.db") |
| 14 | if e != nil { |
| 15 | t.Fatal(e) |
| 16 | } |
| 17 | defer db.Close() |
| 18 | db.SetMaxOpenConns(1) |
| 19 | b, _ := Migrations.ReadFile("migrations/0001_dashboard.sql") |
| 20 | if _, e = db.Exec(string(b)); e != nil { |
| 21 | t.Fatal(e) |
| 22 | } |
| 23 | k, c := fixture(t) |
| 24 | c.Issued = time.Now().Unix() |
| 25 | c.Expires = c.Issued + 120 |
| 26 | resolveCalls, readCalls := 0, 0 |
| 27 | cfg := ProviderConfig{DB: db, Issuer: c.Issuer, Origin: c.Audience, Directory: c.Directory, Team: c.Team, Kind: "documents", Keys: map[string][]byte{c.KeyID: k.SignPub()}, Resolve: func(_ context.Context, sub string) (int64, error) { |
| 28 | resolveCalls++ |
| 29 | if sub != c.Subject { |
| 30 | return 0, ErrRefused |
| 31 | } |
| 32 | return 1, nil |
| 33 | }, Read: func(_ context.Context, id int64, sub string, _ time.Time) (Response, error) { |
| 34 | readCalls++ |
| 35 | return Response{Items: []Item{{ID: "one", Title: "Private canary", URL: c.Audience + "/d/one", Updated: time.Now()}}}, nil |
| 36 | }} |
| 37 | h, e := NewProvider(cfg) |
| 38 | if e != nil { |
| 39 | t.Fatal(e) |
| 40 | } |
| 41 | request := func(token string) *httptest.ResponseRecorder { |
| 42 | r := httptest.NewRequest("GET", c.Audience+Path, nil) |
| 43 | if token != "" { |
| 44 | r.Header.Set("Authorization", "Bearer "+token) |
| 45 | } |
| 46 | w := httptest.NewRecorder() |
| 47 | h.ServeHTTP(w, r) |
| 48 | return w |
| 49 | } |
| 50 | if w := request(""); w.Code != 401 { |
| 51 | t.Fatal(w.Code) |
| 52 | } |
| 53 | bad := c |
| 54 | bad.Team = "other" |
| 55 | token, _ := Sign(k, bad) |
| 56 | if w := request(token); w.Code != 401 { |
| 57 | t.Fatal(w.Code) |
| 58 | } |
| 59 | if resolveCalls != 0 || readCalls != 0 { |
| 60 | t.Fatal("unverified request reached identity/data") |
| 61 | } |
| 62 | token, _ = Sign(k, c) |
| 63 | w := request(token) |
| 64 | if w.Code != 200 || !strings.Contains(w.Body.String(), "Private canary") || w.Header().Get("Cache-Control") != "private, no-store" { |
| 65 | t.Fatalf("response %d", w.Code) |
| 66 | } |
| 67 | if w = request(token); w.Code != 401 { |
| 68 | t.Fatal("replay", w.Code) |
| 69 | } |
| 70 | other := c |
| 71 | other.ID, _ = ID() |
| 72 | other.Subject = "pr_" + strings.Repeat("b", 26) |
| 73 | token, _ = Sign(k, other) |
| 74 | if w = request(token); w.Code != 403 || strings.Contains(w.Body.String(), "canary") { |
| 75 | t.Fatal("unknown identity", w.Code) |
| 76 | } |
| 77 | if readCalls != 1 { |
| 78 | t.Fatal("read reached more than authorized request", readCalls) |
| 79 | } |
| 80 | } |
| 81 | func TestMalformedWireIsNotEmptySuccess(t *testing.T) { |
| 82 | r := Response{Version: 1, Team: "pilot", Kind: "documents"} |
| 83 | if r.Valid("https://docs.example.com", "pilot", "documents") { |
| 84 | t.Fatal("nil items accepted") |
| 85 | } |
| 86 | r.Items = []Item{} |
| 87 | if !r.Valid("https://docs.example.com", "pilot", "documents") { |
| 88 | t.Fatal("explicit empty refused") |
| 89 | } |
| 90 | r.Kind = "events" |
| 91 | if r.Valid("https://docs.example.com", "pilot", "events") { |
| 92 | t.Fatal("missing timezone accepted") |
| 93 | } |
| 94 | r.Timezone = "Europe/Dublin" |
| 95 | if !r.Valid("https://docs.example.com", "pilot", "events") { |
| 96 | t.Fatal("valid empty agenda refused") |
| 97 | } |
| 98 | for _, link := range []string{"https://evil.example.com/d/1", "https://docs.example.com@evil.example.com/d/1", "https://docs.example.com/\\evil"} { |
| 99 | if SafeLink(link, "https://docs.example.com") { |
| 100 | t.Fatal("unsafe link") |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |