| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net" |
| 8 | "net/url" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "time" |
| 12 | |
| 13 | // Time zones are a person's setting (§6), so every IANA name must |
| 14 | // load on any host, with or without a zoneinfo directory. |
| 15 | _ "time/tzdata" |
| 16 | |
| 17 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 18 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 19 | ) |
| 20 | |
| 21 | // Config is everything Mapa needs from platform config. A type with a |
| 22 | // constructor, not os.Getenv in main, because every refusal below is a |
| 23 | // safety property and a safety property needs a test (Fichas' config.go |
| 24 | // records the false pass that made the case). |
| 25 | type Config struct { |
| 26 | Origin string |
| 27 | Sealer *seal.Sealer |
| 28 | // InstanceKey seals rastrillo's pending sign-in blob. |
| 29 | InstanceKey string |
| 30 | // Owners is the lone shape's whole admission list (ruling 5). |
| 31 | Owners []string |
| 32 | // AllowLoggedMail permits booting with no mail, so magic links are |
| 33 | // written to the log: loopback origins only. |
| 34 | AllowLoggedMail bool |
| 35 | // MailConfigured is true when the platform gave Mapa a way to send: |
| 36 | // CARLOS mail submission or SMTP. Mailer builds the sender. |
| 37 | MailConfigured bool |
| 38 | // Directory is the hub directory's origin, the menu's "All apps". |
| 39 | Directory string |
| 40 | // TimeZone is the team's default time zone, an instance setting |
| 41 | // (§3.1): "today" for anyone who has not chosen their own. |
| 42 | TimeZone *time.Location |
| 43 | // Importers may import from Notion (Notion import spec §7, D4). Empty |
| 44 | // means the import does not exist: its routes are not mounted. |
| 45 | Importers []string |
| 46 | // Locale is the team's locale, MAPA_LOCALE (en-IE unless set): how |
| 47 | // dates and numbers are written for a reader whose browser asks for |
| 48 | // none Mapa offers (level-up build note 85). |
| 49 | Locale string |
| 50 | // Images is the object store and the allowances (config_images.go). |
| 51 | Images ImageSetup |
| 52 | } |
| 53 | |
| 54 | // ErrMissing is returned when required platform config is absent. |
| 55 | var ErrMissing = errors.New("mapa: required configuration is missing") |
| 56 | |
| 57 | // ErrSuiteNotBuilt is MAPA_SHAPE=suite. The suite shape needs the home's |
| 58 | // identity bridge and roster pull; Mapa runs lone until they exist, and |
| 59 | // says so rather than booting a roster invented in the wrong app. |
| 60 | var ErrSuiteNotBuilt = errors.New("mapa: MAPA_SHAPE=suite is not " + |
| 61 | "built yet; it needs the home's identity bridge and roster pull") |
| 62 | |
| 63 | // defaultOrigin is local only: the origin is the CSRF yardstick. |
| 64 | const defaultOrigin = "http://127.0.0.1:8834" |
| 65 | |
| 66 | // LoadConfig reads and validates configuration. getenv is a parameter so |
| 67 | // a test supplies an environment without mutating the process's. |
| 68 | // |
| 69 | // There is deliberately no generated fallback DEK: it would seal this |
| 70 | // boot's pages under a key the next boot cannot derive. |
| 71 | func LoadConfig(getenv func(string) string) (Config, error) { |
| 72 | var config Config |
| 73 | var err error |
| 74 | if config.Sealer, err = loadSealer(getenv("MAPA_DEK")); err != nil { |
| 75 | return Config{}, err |
| 76 | } |
| 77 | config.Origin = getenv("MAPA_ORIGIN") |
| 78 | if config.Origin == "" { |
| 79 | config.Origin = defaultOrigin |
| 80 | } |
| 81 | config.InstanceKey = getenv("MAPA_INSTANCE_KEY") |
| 82 | if config.InstanceKey == "" { |
| 83 | return Config{}, fmt.Errorf("%w: MAPA_INSTANCE_KEY", ErrMissing) |
| 84 | } |
| 85 | // No default admission list: an empty one on a public host admits |
| 86 | // anyone who can receive mail. |
| 87 | config.Owners = ParseOwners(getenv("MAPA_OWNERS")) |
| 88 | if len(config.Owners) == 0 { |
| 89 | return Config{}, fmt.Errorf("%w: MAPA_OWNERS", ErrMissing) |
| 90 | } |
| 91 | if err := config.loadMail(getenv); err != nil { |
| 92 | return Config{}, err |
| 93 | } |
| 94 | if config.Images, err = loadImages(getenv); err != nil { |
| 95 | return Config{}, err |
| 96 | } |
| 97 | if err := config.loadImporters(getenv("MAPA_IMPORTERS")); err != nil { |
| 98 | return Config{}, err |
| 99 | } |
| 100 | return config, config.loadRest(getenv) |
| 101 | } |
| 102 | |
| 103 | // loadImporters reads MAPA_IMPORTERS and refuses one who is not an |
| 104 | // owner: Mapa has no roles, and an importer outside the admission list |
| 105 | // could never sign in to use it (Notion import spec §7). |
| 106 | func (config *Config) loadImporters(raw string) error { |
| 107 | config.Importers = ParseOwners(raw) |
| 108 | owners := map[string]bool{} |
| 109 | for _, owner := range config.Owners { |
| 110 | owners[owner] = true |
| 111 | } |
| 112 | for _, importer := range config.Importers { |
| 113 | if !owners[importer] { |
| 114 | return fmt.Errorf("mapa: MAPA_IMPORTERS names %s, who is not "+ |
| 115 | "in MAPA_OWNERS", importer) |
| 116 | } |
| 117 | } |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | // Importer reports whether address may import from Notion. |
| 122 | func (config Config) Importer(address string) bool { |
| 123 | address = normaliseAddress(address) |
| 124 | for _, importer := range config.Importers { |
| 125 | if importer == address { |
| 126 | return true |
| 127 | } |
| 128 | } |
| 129 | return false |
| 130 | } |
| 131 | |
| 132 | func loadSealer(raw string) (*seal.Sealer, error) { |
| 133 | if raw == "" { |
| 134 | return nil, fmt.Errorf("%w: MAPA_DEK", ErrMissing) |
| 135 | } |
| 136 | dek, err := base64.StdEncoding.DecodeString(raw) |
| 137 | if err != nil { |
| 138 | return nil, fmt.Errorf("mapa: MAPA_DEK is not base64: %w", err) |
| 139 | } |
| 140 | sealer, err := seal.New(dek) |
| 141 | if err != nil { |
| 142 | return nil, fmt.Errorf("mapa: MAPA_DEK rejected: %w", err) |
| 143 | } |
| 144 | return sealer, nil |
| 145 | } |
| 146 | |
| 147 | // loadMail refuses to boot where every magic link, a live credential, |
| 148 | // would be written to platform logs we cannot retract. Mail is either |
| 149 | // channel mail.FromCarlos takes: CARLOS mail submission, which the |
| 150 | // agent turns on with CARLOS_MAIL=submit, or CARLOS_SMTP_*. |
| 151 | func (config *Config) loadMail(getenv func(string) string) error { |
| 152 | config.AllowLoggedMail = getenv("MAPA_ALLOW_LOGGED_MAIL") == "1" |
| 153 | if config.AllowLoggedMail && !isLocal(config.Origin) { |
| 154 | return fmt.Errorf("mapa: MAPA_ALLOW_LOGGED_MAIL writes live "+ |
| 155 | "sign-in links to the log and is loopback only, but "+ |
| 156 | "MAPA_ORIGIN is %s", config.Origin) |
| 157 | } |
| 158 | submit := getenv("CARLOS_MAIL") == "submit" |
| 159 | smtp := getenv("CARLOS_SMTP_HOST") != "" && |
| 160 | getenv("CARLOS_SMTP_FROM") != "" |
| 161 | config.MailConfigured = submit || smtp |
| 162 | if !config.MailConfigured && !config.AllowLoggedMail { |
| 163 | return fmt.Errorf("%w: CARLOS_MAIL=submit, or CARLOS_SMTP_HOST "+ |
| 164 | "and CARLOS_SMTP_FROM; without one every sign-in link "+ |
| 165 | "goes to the log. Set MAPA_ALLOW_LOGGED_MAIL=1 to accept "+ |
| 166 | "that on loopback", ErrMissing) |
| 167 | } |
| 168 | return nil |
| 169 | } |
| 170 | |
| 171 | func (config *Config) loadRest(getenv func(string) string) error { |
| 172 | var err error |
| 173 | config.Directory, err = directoryOrigin(getenv("HOME_DIRECTORY_ORIGIN")) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | switch shape := strings.TrimSpace(getenv("MAPA_SHAPE")); shape { |
| 178 | case "", "lone": |
| 179 | case "suite": |
| 180 | return ErrSuiteNotBuilt |
| 181 | default: |
| 182 | return fmt.Errorf("mapa: MAPA_SHAPE is %q; it is lone or suite", |
| 183 | shape) |
| 184 | } |
| 185 | name := strings.TrimSpace(getenv("MAPA_TIME_ZONE")) |
| 186 | if name == "" { |
| 187 | name = "UTC" |
| 188 | } |
| 189 | config.TimeZone, err = time.LoadLocation(name) |
| 190 | if err != nil || name == "Local" { |
| 191 | return fmt.Errorf("mapa: MAPA_TIME_ZONE %q is not an IANA "+ |
| 192 | "time zone name", name) |
| 193 | } |
| 194 | return config.loadLocale(getenv) |
| 195 | } |
| 196 | |
| 197 | // loadLocale refuses a team locale Mapa cannot write in, naming the |
| 198 | // spelling to use when it is only miswritten. |
| 199 | func (config *Config) loadLocale(getenv func(string) string) error { |
| 200 | config.Locale = strings.TrimSpace(getenv("MAPA_LOCALE")) |
| 201 | if config.Locale == "" { |
| 202 | config.Locale = locale.Default |
| 203 | } |
| 204 | if err := locale.Check(config.Locale); err != nil { |
| 205 | return fmt.Errorf("mapa: MAPA_LOCALE: %w", err) |
| 206 | } |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | // isLocal reports whether an origin is a loopback address, strictly. |
| 211 | func isLocal(origin string) bool { |
| 212 | parsed, err := url.Parse(origin) |
| 213 | if err != nil || parsed.Scheme != "http" || parsed.Path != "" { |
| 214 | return false |
| 215 | } |
| 216 | return loopbackHost(parsed.Hostname()) |
| 217 | } |
| 218 | |
| 219 | // UsedDefaultOrigin reports whether Origin fell back, so main says so. |
| 220 | func (config Config) UsedDefaultOrigin() bool { |
| 221 | return config.Origin == defaultOrigin |
| 222 | } |
| 223 | |
| 224 | // directoryOrigin validates HOME_DIRECTORY_ORIGIN: empty, or an origin |
| 225 | // and nothing else, https (or http on loopback), no trailing slash. |
| 226 | // Fichas' rule, unchanged: it becomes a link on every page. |
| 227 | func directoryOrigin(value string) (string, error) { |
| 228 | value = strings.TrimSuffix(strings.TrimSpace(value), "/") |
| 229 | if value == "" { |
| 230 | return "", nil |
| 231 | } |
| 232 | refuse := fmt.Errorf("mapa: HOME_DIRECTORY_ORIGIN is %q; it is "+ |
| 233 | "the hub directory's origin, https://host", value) |
| 234 | parsed, err := url.Parse(value) |
| 235 | if err != nil || parsed.Opaque != "" || parsed.User != nil || |
| 236 | parsed.Path != "" || parsed.RawQuery != "" || |
| 237 | parsed.Fragment != "" || parsed.ForceQuery || |
| 238 | parsed.Hostname() == "" || strings.HasSuffix(parsed.Host, ":") { |
| 239 | return "", refuse |
| 240 | } |
| 241 | if port := parsed.Port(); port != "" { |
| 242 | number, err := strconv.Atoi(port) |
| 243 | if err != nil || number < 1 || number > 65535 { |
| 244 | return "", refuse |
| 245 | } |
| 246 | } |
| 247 | secure := parsed.Scheme == "https" |
| 248 | local := parsed.Scheme == "http" && loopbackHost(parsed.Hostname()) |
| 249 | if !secure && !local { |
| 250 | return "", refuse |
| 251 | } |
| 252 | return parsed.Scheme + "://" + parsed.Host, nil |
| 253 | } |
| 254 | |
| 255 | // loopbackHost is localhost or a loopback IP address, exactly. |
| 256 | func loopbackHost(host string) bool { |
| 257 | if host == "localhost" { |
| 258 | return true |
| 259 | } |
| 260 | address := net.ParseIP(host) |
| 261 | return address != nil && address.IsLoopback() |
| 262 | } |
| 263 | |