oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "regexp"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/home/internal/home/sealtest"
13 "amadan.net/rastrillo/rastrillo/sessions"
14 "amadan.net/rastrillo/rastrillo/webauthn"
15)
16
17// §13 Revocation: a first passkey enrolment on the web ends every
18// passkey-less grant; later enrolments end nothing.
19func TestNativeFirstEnrolmentRevokesPasskeylessGrants(t *testing.T) {
20 t.Parallel()
21 app := newNativeApp(t)
22 d := newDevice(t)
23 g1, _ := d.signInByCode(t, app, sealtest.CanaryAddress)
24 session := signInFully(t, app, sealtest.CanaryAddress)
25 enrol(t, app, session)
26 if ended, reason := grantState(t, app, g1); !ended || reason != "enrolment" {
27 t.Fatal("code grant survived first enrolment", reason)
28 }
29 // It may sign in again, now with a passkey; a second enrolment leaves
30 // that grant alone.
31 app = app.restart(t)
32 start := d.start(t, app, sealtest.CanaryAddress)
33 if out := decodeReply(t, d.verify(t, app, start, lastCode(t, app))); out["pending"] == nil {
34 t.Fatal("no pending", out)
35 }
36 if _, err := app.db.Writer().Exec("INSERT INTO home_id_grants(grant_hash,person_ref,identity_id,device_key,platform,method,auth_time,created_at,last_used_day,expires_at) VALUES('pk',?,'i','k','macos','passkey',1,1,?,?)", subjectOf(t, app, session), dayOf(time.Now().Unix()), time.Now().Unix()+day); err != nil {
37 t.Fatal(err)
38 }
39 enrol(t, app, session)
40 if n := count(t, app, "SELECT count(*) FROM home_id_grants WHERE grant_hash='pk' AND ended_at IS NULL"); n != 1 {
41 t.Fatal("a second enrolment ended a passkey grant")
42 }
43}
44
45// §13 Revocation, with Paul's 2026-11-14 ruling: "Sign out all devices" ends
46// every grant and every Home browser session of that person, on id.* in one
47// transaction with the grants, and on the directory in one transaction with
48// the signed receipt this browser carries across. Behind a fresh session and
49// request-forgery protection.
50func TestNativeSignOutAllDevices(t *testing.T) {
51 t.Parallel()
52 f := newMountedChooser(t)
53 app := f.p.credential
54 g1, _ := newDevice(t).signInByCode(t, app, sealtest.CanaryAddress)
55 app = app.restart(t)
56 g2, _ := newDevice(t).signInByCode(t, app, sealtest.CanaryAddress)
57 other, _ := newDevice(t).signInByCode(t, app, "other@example.invalid")
58 otherBrowser := signInFully(t, app, "other@example.invalid")
59 elsewhere := signInFully(t, app, sealtest.CanaryAddress) // another browser
60 session := signInFully(t, app.restart(t), sealtest.CanaryAddress)
61 person := subjectOf(t, app, session)
62
63 page := exchangeRequest(app.App, "GET", Origin+signoutAllPath, "", nil, session)
64 if page.Code != 200 || !strings.Contains(page.Body.String(), `action="/devices/sign-out-all"`) {
65 t.Fatal("page", page.Code)
66 }
67 if forged := exchangeRequest(app.App, "POST", Origin+signoutAllPath, "https://evil.example", url.Values{}, session); forged.Code != 403 {
68 t.Fatal("cross-origin sign-out-all", forged.Code)
69 }
70 if anon := exchangeRequest(app.App, "POST", Origin+signoutAllPath, Origin, url.Values{}); anon.Code == 200 {
71 t.Fatal("no session ended devices")
72 }
73 // A stale session must re-authenticate first.
74 stamp := time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)
75 if _, err := app.db.Writer().Exec("UPDATE sessions SET auth_time=?, created_at=? WHERE token_hash=?", stamp, stamp, sessions.HashToken(session.Value)); err != nil {
76 t.Fatal(err)
77 }
78 if stale := exchangeRequest(app.App, "POST", Origin+signoutAllPath, Origin, url.Values{}, session); stale.Code == 200 {
79 t.Fatal("a stale session ended devices")
80 }
81 if n := count(t, app, "SELECT count(*) FROM home_id_grants WHERE ended_at IS NOT NULL"); n != 0 {
82 t.Fatal("grants ended without a fresh session", n)
83 }
84 session = signInFully(t, app.restart(t), sealtest.CanaryAddress)
85
86 // The person's directory sessions, in two browsers and one native app,
87 // and someone else's.
88 dirA, dirB, dirOther := f.session(t, person), f.session(t, person), f.session(t, chooserOther)
89 if _, err := f.p.directory.db.Writer().Exec("INSERT INTO sessions(token_hash,subject,method,auth_time,created_at,expires_at) VALUES('native-dir',?,?,?,?,?)", person, nativeSessionMethod, stamp, stamp, time.Now().Add(time.Hour).UTC().Format(time.RFC3339)); err != nil {
90 t.Fatal(err)
91 }
92
93 done := exchangeRequest(app.App, "POST", Origin+signoutAllPath, Origin, url.Values{}, session)
94 if done.Code != 200 || !strings.Contains(done.Body.String(), `action="`+directoryOrigin+signedOutAllPath+`"`) {
95 t.Fatal("sign out all", done.Code)
96 }
97 if c := namedCookieOrNil(done, app.sessions.CookieName()); c == nil || c.MaxAge >= 0 {
98 t.Fatal("this browser's id.* cookie was not cleared")
99 }
100 // id.*: every grant and every session of the person, at once.
101 for _, g := range []string{g1, g2} {
102 if ended, reason := grantState(t, app, g); !ended || reason != "all_devices" {
103 t.Fatal("grant survived", reason)
104 }
105 }
106 if n := count(t, app, "SELECT count(*) FROM sessions WHERE subject=?", person); n != 0 {
107 t.Fatal("id.* sessions survived", n)
108 }
109 if r := exchangeRequest(app.App, "GET", Origin+signoutAllPath, "", nil, elsewhere); r.Code == 200 {
110 t.Fatal("the person's other id.* browser is still signed in")
111 }
112 if ended, _ := grantState(t, app, other); ended {
113 t.Fatal("another person's grant ended")
114 }
115 if subjectOf(t, app, otherBrowser) == "" {
116 t.Fatal("another person's session ended")
117 }
118
119 // The directory: the receipt from the hop page ends every session of
120 // the person there, once.
121 token := regexp.MustCompile(`name="assertion" value="([^"]+)"`).FindStringSubmatch(done.Body.String())[1]
122 if bad := exchangeRequest(f.p.directory, "POST", directoryOrigin+signedOutAllPath, directoryOrigin, url.Values{"assertion": {token}}, dirA); bad.Code == 303 {
123 t.Fatal("receipt accepted from the wrong origin")
124 }
125 hop := exchangeRequest(f.p.directory, "POST", directoryOrigin+signedOutAllPath, Origin, url.Values{"assertion": {token}}, dirA)
126 if hop.Code != 303 || hop.Header().Get("Location") != "/signed-out" {
127 t.Fatal("directory hop", hop.Code, hop.Body.String())
128 }
129 n := 0
130 f.p.directory.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE subject=?", person).Scan(&n)
131 if n != 0 {
132 t.Fatal("directory sessions survived", n)
133 }
134 for _, c := range []*http.Cookie{dirA, dirB} {
135 if w := exchangeRequest(f.p.directory, "GET", directoryOrigin+"/", "", nil, c); strings.Contains(w.Body.String(), "Sign out all devices") {
136 t.Fatal("a directory browser is still signed in")
137 }
138 }
139 if w := exchangeRequest(f.p.directory, "GET", directoryOrigin+"/", "", nil, dirOther); !strings.Contains(w.Body.String(), "Sign out all devices") {
140 t.Fatal("control: a signed-in directory page shows the menu item")
141 }
142 f.p.directory.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE subject=?", chooserOther).Scan(&n)
143 if n != 1 {
144 t.Fatal("another person's directory session ended")
145 }
146 landed := exchangeRequest(f.p.directory, "GET", directoryOrigin+"/signed-out", "", nil, namedCookie(t, hop, "__Host-oficina-signed-out-all"))
147 if !strings.Contains(landed.Body.String(), "All devices are signed out") {
148 t.Fatal("signed-out page", landed.Code)
149 }
150 if again := exchangeRequest(f.p.directory, "POST", directoryOrigin+signedOutAllPath, Origin, url.Values{"assertion": {token}}); again.Code == 303 {
151 t.Fatal("receipt replayed")
152 }
153 // A sign-in assertion is not a sign-out receipt.
154 fresh := signInFully(t, app.restart(t), "victim@example.invalid")
155 _, intent := f.p.start(t)
156 signin, _ := f.p.mint(t, intent, fresh)
157 if w := exchangeRequest(f.p.directory, "POST", directoryOrigin+signedOutAllPath, Origin, url.Values{"assertion": {signin}}); w.Code == 303 {
158 t.Fatal("a sign-in assertion signed someone out")
159 }
160}
161
162func namedCookieOrNil(r *httptest.ResponseRecorder, name string) *http.Cookie {
163 for _, c := range r.Result().Cookies() {
164 if c.Name == name {
165 return c
166 }
167 }
168 return nil
169}
170
171// §13 Revocation: recovery-based replacement ends every grant.
172func TestNativeReplacementRevokesEveryGrant(t *testing.T) {
173 t.Parallel()
174 app := newNativeApp(t)
175 d := newDevice(t)
176 g, _ := d.signInByCode(t, app, sealtest.CanaryAddress)
177 session := signInFully(t, app, sealtest.CanaryAddress)
178 subject := subjectOf(t, app, session)
179 // A passkey grant, as native passkey sign-in would leave.
180 if _, err := app.db.Writer().Exec("INSERT INTO home_id_grants(grant_hash,person_ref,identity_id,device_key,platform,method,auth_time,created_at,last_used_day,expires_at) VALUES('pk',?,'i','k','macos','passkey',1,1,?,?)", subject, dayOf(time.Now().Unix()), time.Now().Unix()+day); err != nil {
181 t.Fatal(err)
182 }
183 tx, now, err := app.idTransaction(t.Context())
184 if err != nil {
185 t.Fatal(err)
186 }
187 cred := webauthn.Credential{ID: []byte("replacement-credential"), PublicKey: []byte{1}}
188 if err := app.replaceCredential(tx, nil, subject, cred, now); err != nil {
189 t.Fatal(err)
190 }
191 if err := tx.Commit(); err != nil {
192 t.Fatal(err)
193 }
194 if n := count(t, app, "SELECT count(*) FROM home_id_grants WHERE ended_at IS NULL"); n != 0 {
195 t.Fatal("grants survived replacement", n)
196 }
197 if _, reason := grantState(t, app, g); reason != "replacement" {
198 t.Fatal(reason)
199 }
200}
201