| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "encoding/hex" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "net/http" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "the-valley/web/export" |
| 18 | "the-valley/web/export/exporttest" |
| 19 | ) |
| 20 | |
| 21 | // A private export, signed as the publisher signs one, goes through the |
| 22 | // real receiver, and the real valley-web, with readers signing in at an |
| 23 | // origin, serves it. app is anyone's, site alice's alone and docs bob's |
| 24 | // alone. Alice and bob each redeem a read link the stored authorization |
| 25 | // binds. Each is served the knots, the source and the archives of what |
| 26 | // anyone and their principal may read, and every other project answers |
| 27 | // 404, as both private projects do to anyone. Withdrawing alice's grant |
| 28 | // takes site from her next request, while her session and bob's grant |
| 29 | // hold. |
| 30 | func TestNamedReadersSignInOverAPrivateExport(t *testing.T) { |
| 31 | e := newEdge(t, exporttest.New(t).Private(t)) |
| 32 | grants := map[string][]string{"app": {export.Anyone}, "site": {"alice"}, "docs": {"bob"}} |
| 33 | e.grant(2, grants) |
| 34 | e.upload() |
| 35 | browser := filepath.Join(filepath.Dir(e.store), "browser") |
| 36 | if err := os.Mkdir(browser, 0o700); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | web := e.valleyWeb("--origin", readerOrigin) |
| 40 | waitFor(t, "valley-web makes its browser state", func(ctx context.Context) bool { |
| 41 | data, err := os.ReadFile(filepath.Join(browser, "state")) |
| 42 | return err == nil && string(data) == "the-valley/web-reader-state/v1\n" |
| 43 | }) |
| 44 | alice, bob := readLink(1, "alice"), readLink(2, "bob") |
| 45 | e.grant(3, grants, alice, bob) |
| 46 | tokens := map[string]string{"anyone": "", "alice": redeem(t, web, alice), "bob": redeem(t, web, bob)} |
| 47 | |
| 48 | appTip, siteTip := e.f.Inputs.SourceTips["app"], e.f.Inputs.SourceTips["site"] |
| 49 | pages := map[string][]string{ |
| 50 | "app": {"/tools/app/knots/oc-a000002", "/tools/app/source/tree/" + appTip + "/", "/tools/app/source/archive/" + appTip + ".tar.gz"}, |
| 51 | "site": {"/tools/site/knots/oc-b000001", "/tools/site/source/tree/" + siteTip + "/", "/tools/site/source/archive/" + siteTip + ".tar.gz"}, |
| 52 | "docs": {"/pages/docs/knots/oc-b000001", "/pages/docs/source/tree/" + siteTip + "/", "/pages/docs/source/archive/" + siteTip + ".tar.gz"}, |
| 53 | } |
| 54 | knots := map[string]string{"site": `href="/tools/site/knots/oc-b000001"`, "docs": `href="/pages/docs/knots/oc-b000001"`} |
| 55 | served := func(grants map[string][]string) { |
| 56 | t.Helper() |
| 57 | for reader, token := range tokens { |
| 58 | for project, targets := range pages { |
| 59 | want := http.StatusNotFound |
| 60 | for _, r := range grants[project] { |
| 61 | if r == export.Anyone || r == reader { |
| 62 | want = http.StatusOK |
| 63 | } |
| 64 | } |
| 65 | for _, target := range targets { |
| 66 | status, _, renewed := read(t, web, target, token) |
| 67 | if status != want || renewed != (token != "") { |
| 68 | t.Errorf("GET %s as %s answered %d, renewed %v; want %d", target, reader, status, renewed, want) |
| 69 | } |
| 70 | } |
| 71 | _, page, _ := read(t, web, "/-/knots", token) |
| 72 | if listed := knots[project] != "" && strings.Contains(page, knots[project]); knots[project] != "" && listed != (want == http.StatusOK) { |
| 73 | t.Errorf("the knots page as %s lists %s's knot: %v", reader, project, listed) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | served(grants) |
| 79 | withdrawn := map[string][]string{"app": {export.Anyone}, "docs": {"bob"}} |
| 80 | e.grant(4, withdrawn, alice, bob) |
| 81 | served(withdrawn) |
| 82 | } |
| 83 | |
| 84 | // readerOrigin is the origin the test's valley-web takes reader posts from. |
| 85 | const readerOrigin = "https://example.org" |
| 86 | |
| 87 | // readerLink is a read link: its credential, and the binding an |
| 88 | // authorization gives it. |
| 89 | type readerLink struct { |
| 90 | credential string |
| 91 | binding export.Binding |
| 92 | } |
| 93 | |
| 94 | func readLink(n int, principal string) readerLink { |
| 95 | secret := bytes.Repeat([]byte{byte(n), 0xa5}, 16) |
| 96 | sum := sha256.Sum256(secret) |
| 97 | id := fmt.Sprintf("lk-%016x", n) |
| 98 | return readerLink{id + "." + base64.RawURLEncoding.EncodeToString(secret), |
| 99 | export.Binding{ID: id, Principal: principal, Verifier: hex.EncodeToString(sum[:])}} |
| 100 | } |
| 101 | |
| 102 | // grant posts an authorization with sequence seq that lets the readers |
| 103 | // given read each project of the edge's export, under its identity, and |
| 104 | // binds the links given, and fails the test unless it is stored. |
| 105 | func (e *edge) grant(seq int64, readers map[string][]string, links ...readerLink) { |
| 106 | e.t.Helper() |
| 107 | a := &export.Authorization{Instance: exporttest.Instance, Destination: exporttest.Destination, Sequence: seq, Checked: exporttest.Collected} |
| 108 | for _, p := range e.f.Export.Projects { |
| 109 | if rs := readers[p.Name]; len(rs) > 0 { |
| 110 | a.Projects = append(a.Projects, export.AuthorizedProject{Name: p.Name, Identity: p.Identity, Readers: rs}) |
| 111 | } |
| 112 | } |
| 113 | for _, l := range links { |
| 114 | a.Bindings = append(a.Bindings, l.binding) |
| 115 | } |
| 116 | if status, reply, _ := e.request(http.MethodPost, export.AuthorizationPath, exporttest.SignAuthorization(e.t, a), nil); status != http.StatusOK { |
| 117 | e.t.Fatalf("authorization %d was answered %d: %s", seq, status, reply) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // readerClient is the client a reader's browser is: it follows no redirect. |
| 122 | var readerClient = &http.Client{Timeout: requestTimeout, Transport: client.Transport, |
| 123 | CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| 124 | |
| 125 | // redeem redeems l at the valley-web at addr, as a reader page's script |
| 126 | // does, and returns the session token it sets. |
| 127 | func redeem(t *testing.T, addr string, l readerLink) string { |
| 128 | t.Helper() |
| 129 | r, _ := http.NewRequest("POST", "http://"+addr+"/-/redeem", strings.NewReader(`{"credential":"`+l.credential+`"}`)) |
| 130 | r.Header.Set("Origin", readerOrigin) |
| 131 | r.Header.Set("Content-Type", "application/json") |
| 132 | r.Header.Set("X-Valley-Reader", "1") |
| 133 | res, err := readerClient.Do(r) |
| 134 | if err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | io.Copy(io.Discard, res.Body) |
| 138 | res.Body.Close() |
| 139 | for _, c := range res.Cookies() { |
| 140 | if c.Name == "__Host-valley-reader" && res.StatusCode == http.StatusNoContent { |
| 141 | return c.Value |
| 142 | } |
| 143 | } |
| 144 | t.Fatalf("redeeming %s's link answered %d with no session", l.binding.Principal, res.StatusCode) |
| 145 | return "" |
| 146 | } |
| 147 | |
| 148 | // read is a GET of target at the valley-web at addr, carrying token's |
| 149 | // session cookie when token is not "": its status, its body, and whether |
| 150 | // it renewed the session. |
| 151 | func read(t *testing.T, addr, target, token string) (int, string, bool) { |
| 152 | t.Helper() |
| 153 | r, _ := http.NewRequest("GET", "http://"+addr+target, nil) |
| 154 | if token != "" { |
| 155 | r.Header.Set("Cookie", "__Host-valley-reader="+token) |
| 156 | } |
| 157 | res, err := readerClient.Do(r) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | body, _ := io.ReadAll(res.Body) |
| 162 | res.Body.Close() |
| 163 | renewed := token != "" && strings.HasPrefix(res.Header.Get("Set-Cookie"), "__Host-valley-reader="+token+";") |
| 164 | return res.StatusCode, string(body), renewed |
| 165 | } |