oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "io"
5 "log/slog"
6 "strings"
7 "testing"
8)
9
10func quietLogger() *slog.Logger {
11 return slog.New(slog.NewTextHandler(io.Discard, nil))
12}
13
14// setEnv clears every variable LoadAuthEnv reads, then applies the given
15// ones. Clearing first matters: the process running these tests may have
16// real values in its environment, and a test that passed only because a
17// developer had SHEETS_ORIGIN exported would be worthless.
18func setEnv(t *testing.T, kv map[string]string) {
19 t.Helper()
20 for _, k := range []string{
21 "SHEETS_ORIGIN", "SHEETS_INSTANCE_KEY", "SHEETS_OWNERS",
22 "SHEETS_AGENT_ISSUER", "SHEETS_AGENT_TEAM",
23 "SHEETS_HOME_ISSUER", "SHEETS_HOME_CONSUMER_ID", "SHEETS_HOME_KEYS_BASE64", "SHEETS_HOME_DEK_BASE64",
24 "CARLOS_SMTP_HOST", "CARLOS_SMTP_FROM",
25 "CARLOS_SMTP_PORT", "CARLOS_SMTP_USER", "CARLOS_SMTP_PASS",
26 "CARLOS_MAIL", "CARLOS_CONTROL_SOCKET", "CARLOS_ADMIN_TOKEN",
27 "CARLOS_MAIL_FROM",
28 } {
29 t.Setenv(k, "")
30 }
31 for k, v := range kv {
32 t.Setenv(k, v)
33 }
34}
35
36func goodEnv() map[string]string {
37 return map[string]string{
38 "SHEETS_ORIGIN": "https://sheets.example.com",
39 "SHEETS_INSTANCE_KEY": "not-empty",
40 "SHEETS_OWNERS": "paul@example.com",
41 "CARLOS_SMTP_HOST": "smtp.example.com",
42 "CARLOS_SMTP_FROM": "noreply@sheets.example.com",
43 }
44}
45
46func TestLoadAuthEnvAcceptsCompleteConfig(t *testing.T) {
47 setEnv(t, goodEnv())
48 env, err := LoadAuthEnv(quietLogger())
49 if err != nil {
50 t.Fatalf("LoadAuthEnv: %v", err)
51 }
52 if env.Origin != "https://sheets.example.com" {
53 t.Errorf("Origin = %q", env.Origin)
54 }
55 if env.Mailer == nil {
56 t.Error("Mailer is nil")
57 }
58}
59
60// TestLoadAuthEnvRefusesEachMissingVar is the boot gate. Each of these
61// would otherwise produce an instance that serves pages and answers
62// /healthz while being unable to sign anyone in safely.
63func TestLoadAuthEnvRefusesEachMissingVar(t *testing.T) {
64 for _, missing := range []string{"SHEETS_ORIGIN", "SHEETS_INSTANCE_KEY", "SHEETS_OWNERS"} {
65 t.Run(missing, func(t *testing.T) {
66 env := goodEnv()
67 delete(env, missing)
68 setEnv(t, env)
69 _, err := LoadAuthEnv(quietLogger())
70 if err == nil {
71 t.Fatalf("missing %s: accepted, want refusal", missing)
72 }
73 if !strings.Contains(err.Error(), missing) {
74 t.Errorf("error does not name %s: %v", missing, err)
75 }
76 })
77 }
78}
79
80// TestLoadAuthEnvRefusesMissingMail: mail.FromEnv degrades to a logging
81// sender with a warning. The link is a live credential and the log is
82// platform-held storage, so degrading is worse than refusing.
83func TestLoadAuthEnvRefusesMissingMail(t *testing.T) {
84 for _, missing := range []string{"CARLOS_SMTP_HOST", "CARLOS_SMTP_FROM"} {
85 t.Run(missing, func(t *testing.T) {
86 env := goodEnv()
87 delete(env, missing)
88 setEnv(t, env)
89 if _, err := LoadAuthEnv(quietLogger()); err == nil {
90 t.Fatalf("missing %s: accepted, want refusal", missing)
91 }
92 })
93 }
94}
95
96// TestLoadAuthEnvAllowsLoggedMailOnLoopback pins the one relaxation, and
97// pins that it is bound to the origin rather than to a switch. A
98// deployed instance's origin is its public https host, so it cannot
99// reach this branch.
100func TestLoadAuthEnvAllowsLoggedMailOnLoopback(t *testing.T) {
101 for _, origin := range []string{"http://localhost:8080", "http://127.0.0.1:8080"} {
102 t.Run(origin, func(t *testing.T) {
103 env := goodEnv()
104 env["SHEETS_ORIGIN"] = origin
105 delete(env, "CARLOS_SMTP_HOST")
106 delete(env, "CARLOS_SMTP_FROM")
107 setEnv(t, env)
108 if _, err := LoadAuthEnv(quietLogger()); err != nil {
109 t.Fatalf("loopback origin with no SMTP: %v, want accepted", err)
110 }
111 })
112 }
113}
114
115// TestLoopbackIsNotPrefixSpoofable: the relaxation must not be reachable
116// by a hostname that merely starts with the loopback text.
117func TestLoopbackIsNotPrefixSpoofable(t *testing.T) {
118 for _, origin := range []string{
119 "https://localhost.attacker.example",
120 "http://127.0.0.1.attacker.example",
121 "http://localhost.attacker.example",
122 "http://127.0.0.1.example.com",
123 "http://notlocalhost",
124 } {
125 t.Run(origin, func(t *testing.T) {
126 env := goodEnv()
127 env["SHEETS_ORIGIN"] = origin
128 delete(env, "CARLOS_SMTP_HOST")
129 delete(env, "CARLOS_SMTP_FROM")
130 setEnv(t, env)
131 if _, err := LoadAuthEnv(quietLogger()); err == nil {
132 t.Errorf("%s reached the loopback relaxation", origin)
133 }
134 })
135 }
136}
137
138func TestAdmitsIsFailClosed(t *testing.T) {
139 allow := Admits(splitOwners("Paul@Example.com, other@example.com,"))
140 for _, tc := range []struct {
141 address string
142 want bool
143 }{
144 {"paul@example.com", true},
145 {"PAUL@EXAMPLE.COM", true},
146 {" paul@example.com ", true},
147 {"other@example.com", true},
148 {"stranger@example.com", false},
149 {"", false},
150 } {
151 if got := allow(tc.address); got != tc.want {
152 t.Errorf("Admits(%q) = %v, want %v", tc.address, got, tc.want)
153 }
154 }
155}
156
157// TestSplitOwnersDropsBlanks: a trailing comma or a stray space must not
158// put an empty string in the allowlist, which Admits("") would then let
159// through.
160func TestSplitOwnersDropsBlanks(t *testing.T) {
161 for _, raw := range []string{"", " ", ",", " , , "} {
162 if got := splitOwners(raw); len(got) != 0 {
163 t.Errorf("splitOwners(%q) = %v, want empty", raw, got)
164 }
165 }
166}
167