| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // The native exchange with rastrillo/native's browser approval mounted, as |
| 14 | // main mounts both: POST /native/oficina-id still reaches connect ahead of |
| 15 | // the /native/* wildcard, and a native session can never approve another |
| 16 | // device. The approval mints a copy with a fresh creation time, so a |
| 17 | // native session approving itself would outlive its seven days (Fable, |
| 18 | // sheets b942939). |
| 19 | func TestNativeSessionCannotApproveTheBrowserSignIn(t *testing.T) { |
| 20 | f := newHomeFixture(t) |
| 21 | if err := f.app.ConfigureNative(); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | f.local(t, signedInAddress) |
| 25 | f.link(t, "pr_linked") |
| 26 | local := f.jar[f.app.homeSessions.CookieName()] |
| 27 | f.jar = map[string]*http.Cookie{} |
| 28 | |
| 29 | rec, answer := f.nativeExchange(t, "pr_linked", nil) |
| 30 | if rec.Code != http.StatusOK || answer.State != "ok" { |
| 31 | t.Fatalf("Oficina ID exchange with native routes mounted = %d %s", rec.Code, rec.Body.String()) |
| 32 | } |
| 33 | native := &http.Cookie{Name: answer.Name, Value: answer.Value} |
| 34 | |
| 35 | // rastrillo/native's browser approval, at this instance's origin. |
| 36 | post := func(path string, body any, cookie *http.Cookie) *httptest.ResponseRecorder { |
| 37 | var reader *strings.Reader |
| 38 | r := httptest.NewRequest("POST", testOrigin+path, nil) |
| 39 | if body != nil { |
| 40 | data, _ := json.Marshal(body) |
| 41 | reader = strings.NewReader(string(data)) |
| 42 | r = httptest.NewRequest("POST", testOrigin+path, reader) |
| 43 | r.Header.Set("Content-Type", "application/json") |
| 44 | } |
| 45 | r.Header.Set("Origin", testOrigin) |
| 46 | if cookie != nil { |
| 47 | r.AddCookie(cookie) |
| 48 | } |
| 49 | w := httptest.NewRecorder() |
| 50 | f.app.Router().ServeHTTP(w, r) |
| 51 | return w |
| 52 | } |
| 53 | request := func() (string, string) { |
| 54 | verifier := randomProof(t) |
| 55 | sum := sha256.Sum256([]byte(verifier)) |
| 56 | rec := post("/native/request", map[string]string{"challenge": base64.RawURLEncoding.EncodeToString(sum[:])}, nil) |
| 57 | var out struct{ ID string } |
| 58 | if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &out) != nil || out.ID == "" { |
| 59 | t.Fatalf("POST /native/request = %d %s", rec.Code, rec.Body.String()) |
| 60 | } |
| 61 | return out.ID, verifier |
| 62 | } |
| 63 | |
| 64 | id, verifier := request() |
| 65 | if approve := post("/native/authorize?id="+id, nil, native); approve.Code == http.StatusOK || strings.Contains(approve.Body.String(), "is signed in") { |
| 66 | t.Fatalf("a native session approved a device: %d %s", approve.Code, approve.Body.String()) |
| 67 | } |
| 68 | if ex := post("/native/exchange", map[string]string{"id": id, "verifier": verifier}, nil); ex.Code == http.StatusOK { |
| 69 | t.Fatalf("exchange after a native session's approval = %d %s", ex.Code, ex.Body.String()) |
| 70 | } |
| 71 | |
| 72 | // A browser's own session still approves, as before. |
| 73 | id, verifier = request() |
| 74 | if approve := post("/native/authorize?id="+id, nil, local); approve.Code != http.StatusOK { |
| 75 | t.Fatalf("a browser session's approval = %d %s", approve.Code, approve.Body.String()) |
| 76 | } |
| 77 | if ex := post("/native/exchange", map[string]string{"id": id, "verifier": verifier}, nil); ex.Code != http.StatusOK { |
| 78 | t.Fatalf("exchange after a browser approval = %d %s", ex.Code, ex.Body.String()) |
| 79 | } |
| 80 | } |
| 81 | |