oficina / fichas Public

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

Plain git — no account needed to clone.

Download

Download this file

1package fichas
2
3import (
4 "encoding/base64"
5 "errors"
6 "testing"
7
8 "amadan.net/oficina/suite/server"
9)
10
11func env(m map[string]string) func(string) string {
12 // Every test here supplies the required set unless it is the thing
13 // under test, so a new required variable does not silently rewrite
14 // what the other cases are asserting.
15 full := map[string]string{
16 "FICHAS_DEK": goodDEK(),
17 "FICHAS_INSTANCE_KEY": "k",
18 "FICHAS_OWNERS": "ana@example.com",
19 "CARLOS_SMTP_HOST": "smtp.example.com",
20 "CARLOS_SMTP_FROM": "fichas@example.com",
21 }
22 for k, v := range m {
23 if v == "" {
24 delete(full, k)
25 continue
26 }
27 full[k] = v
28 }
29 return func(k string) string { return full[k] }
30}
31
32// unset marks a variable as deliberately absent.
33const unset = ""
34
35func goodDEK() string {
36 return base64.StdEncoding.EncodeToString(make([]byte, 32))
37}
38
39// The refusal that matters most: no DEK, no boot. A generated fallback
40// would seal this boot's cards under a key the next boot cannot derive.
41func TestLoadConfigRefusesAMissingDEK(t *testing.T) {
42 _, err := LoadConfig(env(map[string]string{"FICHAS_DEK": unset}))
43 if !errors.Is(err, ErrMissing) {
44 t.Fatalf("booting with no FICHAS_DEK returned %v, want ErrMissing", err)
45 }
46}
47
48func TestLoadConfigRefusesAMalformedDEK(t *testing.T) {
49 for name, v := range map[string]string{
50 "not base64": "!!!!not base64!!!!",
51 "too short": base64.StdEncoding.EncodeToString(make([]byte, 16)),
52 "too long": base64.StdEncoding.EncodeToString(make([]byte, 64)),
53 "empty bytes": base64.StdEncoding.EncodeToString(nil),
54 } {
55 if _, err := LoadConfig(env(map[string]string{"FICHAS_DEK": v})); err == nil {
56 t.Errorf("a DEK that is %s was accepted", name)
57 }
58 }
59}
60
61func TestLoadConfigAcceptsAGoodDEK(t *testing.T) {
62 c, err := LoadConfig(env(map[string]string{"FICHAS_DEK": goodDEK()}))
63 if err != nil {
64 t.Fatalf("LoadConfig: %v", err)
65 }
66 if c.Sealer == nil {
67 t.Fatal("a config with no Sealer was returned without an error")
68 }
69 if !c.UsedDefaultOrigin() {
70 t.Fatalf("Origin is %q; an unset FICHAS_ORIGIN should report as defaulted", c.Origin)
71 }
72}
73
74func TestLoadConfigTakesTheOriginWhenSet(t *testing.T) {
75 c, err := LoadConfig(env(map[string]string{
76 "FICHAS_DEK": goodDEK(),
77 "FICHAS_ORIGIN": "https://fichas.oficina.works",
78 }))
79 if err != nil {
80 t.Fatalf("LoadConfig: %v", err)
81 }
82 if c.Origin != "https://fichas.oficina.works" {
83 t.Fatalf("Origin is %q", c.Origin)
84 }
85 if c.UsedDefaultOrigin() {
86 t.Fatal("an explicitly set origin reports as defaulted")
87 }
88}
89
90// No owners is an empty admission gate, which on a public hostname admits
91// anyone who can receive mail. Refusing to start beats serving in that
92// state.
93func TestLoadConfigRefusesAnEmptyOwnerList(t *testing.T) {
94 if _, err := LoadConfig(env(map[string]string{"FICHAS_OWNERS": unset})); !errors.Is(err, ErrMissing) {
95 t.Fatalf("an empty FICHAS_OWNERS returned %v, want ErrMissing", err)
96 }
97}
98
99// auth.New refuses an empty instance key, and rightly: an empty input
100// hashes to a fixed, publicly computable value identical across every
101// deployment.
102func TestLoadConfigRefusesAnEmptyInstanceKey(t *testing.T) {
103 if _, err := LoadConfig(env(map[string]string{"FICHAS_INSTANCE_KEY": unset})); !errors.Is(err, ErrMissing) {
104 t.Fatalf("an empty FICHAS_INSTANCE_KEY returned %v, want ErrMissing", err)
105 }
106}
107
108func TestOwnersAreNormalised(t *testing.T) {
109 c, err := LoadConfig(env(map[string]string{"FICHAS_OWNERS": " Ana@Example.com , BOB@example.com ,, "}))
110 if err != nil {
111 t.Fatalf("LoadConfig: %v", err)
112 }
113 want := []string{"ana@example.com", "bob@example.com"}
114 if len(c.Owners) != len(want) {
115 t.Fatalf("owners are %q", c.Owners)
116 }
117 for i := range want {
118 if c.Owners[i] != want[i] {
119 t.Fatalf("owners are %q, want %q", c.Owners, want)
120 }
121 }
122}
123
124// mail.Logged writes the magic link — a live credential — into the process
125// log, and platform log storage is not something we can grep and retract.
126func TestLoggedMailIsRefusedOffLocalhost(t *testing.T) {
127 if _, err := LoadConfig(env(map[string]string{
128 "FICHAS_ORIGIN": "https://fichas.oficina.works",
129 "FICHAS_ALLOW_LOGGED_MAIL": "1",
130 })); err == nil {
131 t.Fatal("FICHAS_ALLOW_LOGGED_MAIL was accepted on a public origin")
132 }
133}
134
135// Ruling 5's two shapes. Suite is the intended default and is not built;
136// booting into it silently would mean an admission gate that is the owners
137// list while the operator believes it is the home's roster.
138func TestTheSuiteShapeRefusesRatherThanPretending(t *testing.T) {
139 _, err := LoadConfig(env(map[string]string{"FICHAS_SHAPE": "suite"}))
140 if !errors.Is(err, ErrSuiteNotBuilt) {
141 t.Fatalf("FICHAS_SHAPE=suite returned %v, want ErrSuiteNotBuilt", err)
142 }
143}
144
145func TestTheDefaultShapeIsLone(t *testing.T) {
146 c, err := LoadConfig(env(map[string]string{}))
147 if err != nil {
148 t.Fatalf("LoadConfig: %v", err)
149 }
150 if c.Shape != ShapeLone {
151 t.Fatalf("the default shape is %q", c.Shape)
152 }
153}
154
155func TestAnUnknownShapeIsRefused(t *testing.T) {
156 if _, err := LoadConfig(env(map[string]string{"FICHAS_SHAPE": "federated"})); err == nil {
157 t.Fatal("an unknown shape was accepted")
158 }
159}
160
161// The guard the first deploy needed and did not have.
162//
163// mail.FromEnv falls back to mail.Logged with a Warn when SMTP is unset,
164// so the app booted on a public origin wired to write every magic link —
165// a live credential — into platform log storage. Nothing failed. A WARN
166// scrolled past.
167func TestBootIsRefusedWithNoMailer(t *testing.T) {
168 for _, missing := range []string{"CARLOS_SMTP_HOST", "CARLOS_SMTP_FROM"} {
169 _, err := LoadConfig(env(map[string]string{missing: unset}))
170 if !errors.Is(err, ErrMissing) {
171 t.Errorf("booting with no %s returned %v, want ErrMissing", missing, err)
172 }
173 }
174}
175
176// The escape hatch works, and only on loopback.
177func TestLoggedMailIsAllowedOnLoopbackWhenAskedFor(t *testing.T) {
178 c, err := LoadConfig(env(map[string]string{
179 "CARLOS_SMTP_HOST": unset,
180 "CARLOS_SMTP_FROM": unset,
181 "FICHAS_ORIGIN": "http://127.0.0.1:8824",
182 "FICHAS_ALLOW_LOGGED_MAIL": "1",
183 }))
184 if err != nil {
185 t.Fatalf("LoadConfig: %v", err)
186 }
187 if c.MailConfigured {
188 t.Fatal("MailConfigured is true with no SMTP")
189 }
190}
191
192// And the hatch cannot be used to get a logging mailer onto a real host.
193func TestLoggedMailCannotBeAskedForOffLoopback(t *testing.T) {
194 if _, err := LoadConfig(env(map[string]string{
195 "CARLOS_SMTP_HOST": unset,
196 "CARLOS_SMTP_FROM": unset,
197 "FICHAS_ORIGIN": "https://fichas.bds.oncarlos.com",
198 "FICHAS_ALLOW_LOGGED_MAIL": "1",
199 })); err == nil {
200 t.Fatal("a logging mailer was accepted on a public origin")
201 }
202}
203
204// HOME_DIRECTORY_ORIGIN is the account menu's "All apps": an origin and
205// nothing else, https unless it is loopback, and absent means none.
206func TestLoadConfigTakesTheDirectoryOrigin(t *testing.T) {
207 c, err := LoadConfig(env(map[string]string{}))
208 if err != nil || c.Directory != "" {
209 t.Fatalf("unset: Directory %q, err %v; want empty", c.Directory, err)
210 }
211 c, err = LoadConfig(env(map[string]string{"HOME_DIRECTORY_ORIGIN": "https://oficina.works/"}))
212 if err != nil || c.Directory != "https://oficina.works" {
213 t.Fatalf("Directory %q, err %v; want https://oficina.works", c.Directory, err)
214 }
215 // Loopback http is allowed for local development, with or without a
216 // port, and the hostname is judged on its own, not by a prefix.
217 for in, want := range map[string]string{
218 "http://127.0.0.1": "http://127.0.0.1",
219 "http://localhost/": "http://localhost",
220 "http://[::1]": "http://[::1]",
221 "http://127.0.0.1:8746": "http://127.0.0.1:8746",
222 "https://oficina.works:8443": "https://oficina.works:8443",
223 } {
224 if c, err := LoadConfig(env(map[string]string{"HOME_DIRECTORY_ORIGIN": in})); err != nil || c.Directory != want {
225 t.Errorf("HOME_DIRECTORY_ORIGIN=%q: Directory %q, err %v; want %q", in, c.Directory, err, want)
226 }
227 }
228 for _, bad := range []string{"oficina.works", "http://oficina.works", "https://oficina.works/apps", "https://oficina.works?x=1", "javascript:alert(1)", "https://",
229 // A port with no host, and a port that is not one.
230 "https://:443", "http://:8080", "https://oficina.works:", "https://oficina.works:x", "https://oficina.works:99999",
231 // Loopback-looking names that are not loopback.
232 "http://127.0.0.1.evil.example", "http://localhost.evil.example:80", "http://[::1].evil"} {
233 if _, err := LoadConfig(env(map[string]string{"HOME_DIRECTORY_ORIGIN": bad})); err == nil {
234 t.Errorf("HOME_DIRECTORY_ORIGIN=%q accepted; want a refusal", bad)
235 }
236 }
237}
238
239// FICHAS_SUITE_CONSUMERS (spec §6.5): unset is no consumers; a good list is
240// read; anything malformed refuses boot rather than silently serving
241// nobody.
242//
243// MUTATION-TESTED 2026-11-01, reverted: the ConsumersFromEnv error
244// discarded (boot continues with no consumers) → "malformed".
245func TestSuiteConsumersAreReadAtBoot(t *testing.T) {
246 c, err := LoadConfig(env(map[string]string{}))
247 if err != nil || c.SuiteConsumers != nil {
248 t.Fatalf("unset: %+v, %v", c.SuiteConsumers, err)
249 }
250 good := `[{"consumer_id":"memoria","tokens":[{"epoch":1,"sha256":"` + server.TokenHash("t") + `"}]}]`
251 c, err = LoadConfig(env(map[string]string{"FICHAS_SUITE_CONSUMERS": good}))
252 if err != nil || len(c.SuiteConsumers) != 1 || c.SuiteConsumers[0].ID != "memoria" {
253 t.Fatalf("good: %+v, %v", c.SuiteConsumers, err)
254 }
255 for name, bad := range map[string]string{
256 "not JSON": "memoria:abc",
257 "a plaintext token": `[{"consumer_id":"memoria","tokens":[{"epoch":1,"sha256":"the-token"}]}]`,
258 "no tokens": `[{"consumer_id":"memoria","tokens":[]}]`,
259 "an unknown field": `[{"consumer_id":"memoria","origin":"x","tokens":[{"epoch":1,"sha256":"` + server.TokenHash("t") + `"}]}]`,
260 } {
261 if _, err := LoadConfig(env(map[string]string{"FICHAS_SUITE_CONSUMERS": bad})); err == nil {
262 t.Errorf("malformed (%s) was accepted", name)
263 }
264 }
265}
266