oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package adopt
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/alta/carlosop"
12 "amadan.net/oficina/alta/provision"
13)
14
15const signInNonce = "NONCE-MUST-NOT-TRAVEL"
16
17// redirectTo answers status with one Location.
18func redirectTo(status int, location string) func(http.ResponseWriter) {
19 return func(w http.ResponseWriter) {
20 w.Header().Set("Location", location)
21 w.WriteHeader(status)
22 }
23}
24
25// signInFor is the redirect production answers for slug.
26func signInFor(slug string) func(http.ResponseWriter) {
27 return redirectTo(http.StatusSeeOther, HomeIssuerOrigin+"/authorize?consumer="+slug+"&nonce="+signInNonce)
28}
29
30// secretHome stores the fixture's Home issuer and consumer as production
31// does, as secrets in the default layer, and serves the live sign-in.
32func secretHome(x *carlosFixture) {
33 x.f.SetSecret(x.app.Slug, "", x.app.Prefix+"_HOME_ISSUER")
34 x.f.SetSecret(x.app.Slug, "", x.app.Prefix+"_HOME_CONSUMER_ID")
35 x.site.signIn = signInFor(x.app.Slug)
36}
37
38// The adoption tuple's consumer is the bare slug for each of the six apps.
39func TestAdoptionConsumerIsTheBareSlug(t *testing.T) {
40 want := []string{"docs", "sheets", "slides", "calendar", "calls", "memoria", "equipo", "base"}
41 if len(provision.Apps) != len(want) {
42 t.Fatalf("%d apps", len(provision.Apps))
43 }
44 for _, slug := range want {
45 if _, ok := provision.AppBySlug(slug); !ok {
46 t.Fatalf("no app %s", slug)
47 }
48 if got := provision.ConsumerID(provision.AdoptionLabel, slug); got != slug {
49 t.Fatalf("ConsumerID(%s) = %q", slug, got)
50 }
51 }
52}
53
54// Production's shape passes for every app: both values secret, both read
55// from the sign-in redirect, which is never followed.
56func TestPreflightReadsSecretHomeIdentityFromSignIn(t *testing.T) {
57 for _, a := range provision.Apps {
58 t.Run(a.Slug, func(t *testing.T) {
59 x := newCarlosFixture(t, a.Slug)
60 secretHome(x)
61 snap, codes := x.preflight(t, nil)
62 wantCodes(t, codes)
63 if snap.HomeIssuer != HomeIssuerOrigin || snap.HomeConsumer != a.Slug {
64 t.Fatalf("issuer %q consumer %q", snap.HomeIssuer, snap.HomeConsumer)
65 }
66 if x.site.signIns.Load() != 1 || x.site.followed.Load() != 0 {
67 t.Fatalf("sign-in read %d times, followed %d times", x.site.signIns.Load(), x.site.followed.Load())
68 }
69 if strings.Contains(fmt.Sprintf("%+v", snap), signInNonce) {
70 t.Fatal("the snapshot carries the nonce")
71 }
72 })
73 }
74}
75
76func TestPreflightAcceptsAFoundRedirect(t *testing.T) {
77 x := newCarlosFixture(t, "docs")
78 secretHome(x)
79 x.site.signIn = redirectTo(http.StatusFound, HomeIssuerOrigin+"/authorize?nonce=n&consumer=docs")
80 _, codes := x.preflight(t, nil)
81 wantCodes(t, codes)
82}
83
84// Everything short of the exact redirect leaves both values empty.
85func TestPreflightRefusesSignInLookalikes(t *testing.T) {
86 const good = "/authorize?consumer=docs&nonce=n"
87 both := []string{"home-consumer-mismatch", "home-issuer-mismatch"}
88 cases := map[string]struct {
89 answer func(http.ResponseWriter)
90 want []string
91 }{
92 "other origin": {redirectTo(303, "https://id.example"+good), both},
93 "issuer subdomain": {redirectTo(303, "https://x.id.oficina.works"+good), both},
94 "issuer as a prefix": {redirectTo(303, "https://id.oficina.works.example"+good), both},
95 "explicit port": {redirectTo(303, "https://id.oficina.works:443"+good), both},
96 "http scheme": {redirectTo(303, "http://id.oficina.works"+good), both},
97 "userinfo": {redirectTo(303, "https://user@id.oficina.works"+good), both},
98 "userinfo lookalike": {redirectTo(303, "https://id.oficina.works@evil.example"+good), both},
99 "wrong path": {redirectTo(303, HomeIssuerOrigin+"/login?consumer=docs"), both},
100 "trailing slash": {redirectTo(303, HomeIssuerOrigin+"/authorize/?consumer=docs"), both},
101 "encoded path": {redirectTo(303, HomeIssuerOrigin+"/%61uthorize?consumer=docs"), both},
102 "fragment": {redirectTo(303, HomeIssuerOrigin+good+"#consumer=docs"), both},
103 "empty fragment": {redirectTo(303, HomeIssuerOrigin+good+"#"), both},
104 "backslash in host": {redirectTo(303, "https://id.oficina.works\\@evil.example"+good), both},
105 "backslash for path": {redirectTo(303, "https://id.oficina.works\\authorize?consumer=docs"), both},
106 "backslash in path": {redirectTo(303, HomeIssuerOrigin+"/authorize\\?consumer=docs"), both},
107 "backslash in query": {redirectTo(303, HomeIssuerOrigin+good+"\\"), both},
108 "backslash consumer": {redirectTo(303, HomeIssuerOrigin+"/authorize?consumer=docs\\"), both},
109 "repeated consumer": {redirectTo(303, HomeIssuerOrigin+"/authorize?consumer=docs&consumer=docs"), both},
110 "empty consumer": {redirectTo(303, HomeIssuerOrigin+"/authorize?consumer=&nonce=n"), both},
111 "no consumer": {redirectTo(303, HomeIssuerOrigin+"/authorize?nonce=n"), both},
112 "malformed query": {redirectTo(303, HomeIssuerOrigin+"/authorize?consumer=docs&x=%zz"), both},
113 "semicolon query": {redirectTo(303, HomeIssuerOrigin+"/authorize?consumer=docs;consumer=x"), both},
114 "relative location": {redirectTo(303, good), both},
115 "scheme-relative": {redirectTo(303, "//id.oficina.works"+good), both},
116 "no location": {func(w http.ResponseWriter) { w.WriteHeader(303) }, both},
117 "two locations": {func(w http.ResponseWriter) {
118 w.Header().Add("Location", HomeIssuerOrigin+good)
119 w.Header().Add("Location", HomeIssuerOrigin+good)
120 w.WriteHeader(303)
121 }, both},
122 "200 with a location": {redirectTo(200, HomeIssuerOrigin+good), both},
123 "301": {redirectTo(301, HomeIssuerOrigin+good), both},
124 "307": {redirectTo(307, HomeIssuerOrigin+good), both},
125 "oversized body": {func(w http.ResponseWriter) {
126 w.Header().Set("Location", HomeIssuerOrigin+good)
127 w.WriteHeader(303)
128 _, _ = w.Write(make([]byte, bodyLimit+1))
129 }, both},
130 "connection dropped": {func(w http.ResponseWriter) { panic(http.ErrAbortHandler) }, both},
131 "wrong consumer": {signInFor("sheets"), []string{"home-consumer-mismatch"}},
132 }
133 for name, c := range cases {
134 t.Run(name, func(t *testing.T) {
135 x := newCarlosFixture(t, "docs")
136 secretHome(x)
137 x.site.signIn = c.answer
138 _, codes := x.preflight(t, nil)
139 wantCodes(t, codes, c.want...)
140 if x.site.followed.Load() != 0 {
141 t.Fatal("the redirect was followed")
142 }
143 })
144 }
145 t.Run("no sign-in door", func(t *testing.T) {
146 x := newCarlosFixture(t, "docs")
147 secretHome(x)
148 x.site.signIn = nil
149 _, codes := x.preflight(t, nil)
150 wantCodes(t, codes, both...)
151 })
152}
153
154// A plain var is compared as it is: the redirect never rescues it.
155func TestPreflightNeverFallsBackFromAPlainVar(t *testing.T) {
156 t.Run("issuer", func(t *testing.T) {
157 x := newCarlosFixture(t, "docs")
158 secretHome(x)
159 x.f.SetVar("docs", "", "DOCS_HOME_ISSUER", "https://id.example")
160 _, codes := x.preflight(t, nil)
161 wantCodes(t, codes, "home-issuer-mismatch")
162 })
163 t.Run("consumer", func(t *testing.T) {
164 x := newCarlosFixture(t, "docs")
165 secretHome(x)
166 x.f.SetVar("docs", "", "DOCS_HOME_CONSUMER_ID", "oficina-docs")
167 _, codes := x.preflight(t, nil)
168 wantCodes(t, codes, "home-consumer-mismatch")
169 })
170 t.Run("consumer absent", func(t *testing.T) {
171 x := newCarlosFixture(t, "docs")
172 secretHome(x)
173 x.f.Unset("docs", "", "DOCS_HOME_CONSUMER_ID")
174 _, codes := x.preflight(t, nil)
175 wantCodes(t, codes, "home-consumer-mismatch")
176 })
177 t.Run("both plain makes no sign-in read", func(t *testing.T) {
178 x := newCarlosFixture(t, "docs")
179 x.site.signIn = signInFor("docs")
180 _, codes := x.preflight(t, nil)
181 wantCodes(t, codes)
182 if x.site.signIns.Load() != 0 {
183 t.Fatal("plain vars read the sign-in door")
184 }
185 })
186}
187
188// A secret in the overlay is not the app's own stored identity.
189func TestPreflightRefusesASecretConsumerInTheOverlay(t *testing.T) {
190 x := newCarlosFixture(t, "docs")
191 secretHome(x)
192 x.f.SetOverlay("docs", docsRoute, "DOCS_HOME_CONSUMER_ID", "docs")
193 _, codes := x.preflight(t, nil)
194 wantCodes(t, codes, "home-consumer-mismatch")
195}
196
197// A secret is only believed when the stored layer agrees with the
198// effective view about it.
199func TestStoredSecretNeedsTheSameKindAndDigest(t *testing.T) {
200 l := carlosop.Layer{Vars: []carlosop.Var{{Key: "K", Kind: "secret", Digest: "d1"}, {Key: "P", Kind: "env", Digest: "d1"}}}
201 cases := map[string]struct {
202 ev *carlosop.EffectiveVar
203 want bool
204 }{
205 "held": {&carlosop.EffectiveVar{Key: "K", Kind: "secret", Digest: "d1"}, true},
206 "absent": {nil, false},
207 "other digest": {&carlosop.EffectiveVar{Key: "K", Kind: "secret", Digest: "d2"}, false},
208 "effective plain": {&carlosop.EffectiveVar{Key: "K", Kind: "env", Digest: "d1"}, false},
209 "stored plain": {&carlosop.EffectiveVar{Key: "P", Kind: "secret", Digest: "d1"}, false},
210 "not stored": {&carlosop.EffectiveVar{Key: "X", Kind: "secret", Digest: "d1"}, false},
211 }
212 for name, c := range cases {
213 if got := storedSecret(l, c.ev); got != c.want {
214 t.Errorf("%s: %v", name, got)
215 }
216 }
217}
218
219// The sign-in is only believed from an https origin.
220func TestSignInIdentityRefusesAPlainHTTPOrigin(t *testing.T) {
221 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { signInFor("docs")(w) }))
222 t.Cleanup(srv.Close)
223 if iss, cons := signInIdentity(context.Background(), srv.Client(), srv.URL); iss != "" || cons != "" {
224 t.Fatalf("issuer %q consumer %q", iss, cons)
225 }
226}
227
228// configure runs Preflight again before writing: a sign-in that has moved
229// sends nothing.
230func TestConfigureRefusesAMovedSignInBeforeWriting(t *testing.T) {
231 w := newWriteFixture(t, "docs")
232 secretHome(w.carlosFixture)
233 w.site.signIn = signInFor("sheets")
234 if _, err := w.configure(true); err == nil || !strings.Contains(err.Error(), "home-consumer-mismatch") {
235 t.Fatalf("configure: %v", err)
236 }
237 if w.puts() != 0 {
238 t.Fatal("configure wrote past a moved sign-in")
239 }
240}
241
242func secretHomeBefore(w *writeFixture) { secretHome(w.carlosFixture) }
243
244// The activation gate holds the live sign-in to the same rule.
245func TestObserverRefusesAMovedSignIn(t *testing.T) {
246 x := newObserverFixture(t, "calendar", secretHomeBefore)
247 if _, err := x.observe(); err != nil {
248 t.Fatalf("Observe with production's shape: %v", err)
249 }
250 x.site.signIn = signInFor("sheets")
251 if _, err := x.observe(); err == nil || !strings.Contains(err.Error(), "home-consumer-mismatch") {
252 t.Fatalf("Observe: %v", err)
253 }
254}
255
256// The secrets themselves stay under digest preservation: a reseal is
257// drift even though the sign-in still answers the same.
258func TestObserverRefusesResealedHomeSecrets(t *testing.T) {
259 for _, key := range []string{"CALENDAR_HOME_ISSUER", "CALENDAR_HOME_CONSUMER_ID"} {
260 t.Run(key, func(t *testing.T) {
261 x := newObserverFixture(t, "calendar", secretHomeBefore)
262 x.f.SetSecret("calendar", "", key)
263 if _, err := x.observe(); err == nil || !strings.Contains(err.Error(), "drift-changed:"+key) {
264 t.Fatalf("Observe: %v", err)
265 }
266 })
267 }
268}
269
270// A rollback has to reach an app that is down, so it never asks the
271// sign-in door, and a broken one does not stop it.
272func TestRollbackDoesNotNeedTheSignIn(t *testing.T) {
273 w := newWriteFixture(t, "docs")
274 secretHome(w.carlosFixture)
275 w.configured(t)
276 w.site.signIn = nil
277 before := w.site.signIns.Load()
278 if err := w.rollback(true); err != nil {
279 t.Fatalf("RollbackAgentKeys: %v", err)
280 }
281 if w.site.signIns.Load() != before {
282 t.Fatal("the rollback read the sign-in door")
283 }
284}
285