Bound assertion display fields, check origins at boot
The issuer cuts name, email and Base name to a share of rastrillo's 1 KiB limit on Ext, counted as encoded JSON and at a character boundary, so a very long name no longer breaks the handoff with a 500. With BASE_APPS_DOMAIN set, config.Load now asks rastrillo/assertion to build a verifier for Base's origin and each catalogue app's origin, so an explicit :443, an IP or a bad label fails at boot, not at the first handoff. Loopback http stays allowed for local runs. golang.org/x/text goes to v0.41.0, with the x/mod, x/sync and x/tools minimums it requires; govulncheck now finds nothing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+183
−13
go.mod+4 −4go.sum+8 −8internal/config/config.go+41 −0internal/config/config_test.go+40 −0internal/issuer/issuer.go+38 −1internal/issuer/issuer_test.go+52 −0
diff --git a/go.mod b/go.mod| index 9afcb53..ea4a130 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -23,11 +23,11 @@ require ( |
| github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect |
| github.com/tdewolff/minify/v2 v2.24.19 // indirect |
| github.com/tdewolff/parse/v2 v2.8.16 // indirect |
| - golang.org/x/mod v0.37.0 // indirect |
| - golang.org/x/sync v0.21.0 // indirect |
| + golang.org/x/mod v0.38.0 // indirect |
| + golang.org/x/sync v0.22.0 // indirect |
| golang.org/x/sys v0.47.0 // indirect |
| - golang.org/x/text v0.39.0 // indirect |
| - golang.org/x/tools v0.47.0 // indirect |
| + golang.org/x/text v0.41.0 // indirect |
| + golang.org/x/tools v0.48.0 // indirect |
| gorm.io/gorm v1.31.2 // indirect |
| gorm.io/plugin/dbresolver v1.6.2 // indirect |
| modernc.org/libc v1.74.1 // indirect |
diff --git a/go.sum b/go.sum| index f11ac52..8e1b17f 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -46,17 +46,17 @@ github.com/tdewolff/parse/v2 v2.8.16 h1:bLk5svUOQRkW/Y2SJ+DeENSIkZBcTIkq+Atyv5D8 |
| github.com/tdewolff/parse/v2 v2.8.16/go.mod h1:XdsoSFThlVIRIajAuqz1evNY7bagZS8LBOPA3aVopwQ= |
| github.com/tdewolff/test v1.0.12 h1:7F21DqIajswxuche0geHdrUZRCWE4oko4b7bcmkkrxk= |
| github.com/tdewolff/test v1.0.12/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8= |
| -golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ= |
| -golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0= |
| -golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM= |
| -golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= |
| +golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk= |
| +golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40= |
| +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= |
| +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= |
| golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
| golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= |
| golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= |
| -golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus= |
| -golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM= |
| -golang.org/x/tools v0.47.0 h1:7Kn5x/d1svx/PzryTsqeoZN4TZwqeH5pGWjefhLi/1Q= |
| -golang.org/x/tools v0.47.0/go.mod h1:dFHnyTvFWY212G+h7ZY4Vsp/K3U4/7W9TyVaAul8uCA= |
| +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= |
| +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= |
| +golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE= |
| +golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk= |
| gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= |
| gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= |
| gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= |
diff --git a/internal/config/config.go b/internal/config/config.go| index bbeb56a..b6aabfc 100644 |
| --- a/internal/config/config.go |
| +++ b/internal/config/config.go |
| @@ -2,6 +2,7 @@ |
| package config |
| import ( |
| + "crypto/elliptic" |
| "encoding/base64" |
| "errors" |
| "fmt" |
| @@ -12,7 +13,10 @@ import ( |
| "time" |
| _ "time/tzdata" // the release binary must not depend on host zoneinfo |
| + "amadan.net/rastrillo/rastrillo/assertion" |
| "amadan.net/rastrillo/rastrillo/crypto" |
| + |
| + "amadan.net/carlos/base/internal/catalog" |
| ) |
| // A host label is "<slug>-<appid>", at most 32 characters. App ids in the |
| @@ -97,9 +101,46 @@ func Load(getenv func(string) string) (Config, error) { |
| return Config{}, fmt.Errorf("BASE_ASSERTION_KEY: %w", err) |
| } |
| } |
| + if cfg.AppsDomain != "" { |
| + if err := checkAssertionOrigins(cfg, local); err != nil { |
| + return Config{}, err |
| + } |
| + } |
| return cfg, nil |
| } |
| +// checkAssertionOrigins holds, with installs configured, Base's origin and |
| +// each catalogue app's origin to the rules rastrillo/assertion applies to an |
| +// assertion's issuer and audience (https, a DNS hostname, no :443), by asking |
| +// it to build a verifier for each pair. A loopback http Base origin is the |
| +// local-run mode and is not checked; handoffs cannot work there. |
| +func checkAssertionOrigins(cfg Config, local bool) error { |
| + issuer := cfg.Origin |
| + if local && strings.HasPrefix(issuer, "http://") { |
| + issuer = "https://base.invalid" // stands in, so the app origins are still checked |
| + } else if _, err := assertion.NewVerifier(issuer, "https://app.invalid", probeKeys); err != nil { |
| + return fmt.Errorf("BASE_ORIGIN %q cannot issue assertions: it must be https with a DNS hostname and no :443", cfg.Origin) |
| + } |
| + for _, e := range catalog.All() { |
| + app := "https://" + cfg.Slug + "-" + e.ID + "." + cfg.AppsDomain |
| + if _, err := assertion.NewVerifier(issuer, app, probeKeys); err != nil { |
| + return fmt.Errorf("BASE_APPS_DOMAIN %q gives %s the origin %q, which cannot receive assertions: it must be a DNS hostname with no :443", cfg.AppsDomain, e.ID, app) |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// probeKeys is a key set the verifier accepts (P-256's base point), for |
| +// checking origins only; nothing is ever verified with it. |
| +var probeKeys = func() map[string][]byte { |
| + p := elliptic.P256().Params() |
| + pub := make([]byte, 65) |
| + pub[0] = 4 |
| + p.Gx.FillBytes(pub[1:33]) |
| + p.Gy.FillBytes(pub[33:]) |
| + return map[string][]byte{"probe": pub} |
| +}() |
| + |
| // originIsLocal checks BASE_ORIGIN is https, or http on a loopback host, |
| // and reports whether it is loopback. |
| func originIsLocal(origin string) (bool, error) { |
diff --git a/internal/config/config_test.go b/internal/config/config_test.go| index 08c6743..732acb5 100644 |
| --- a/internal/config/config_test.go |
| +++ b/internal/config/config_test.go |
| @@ -82,3 +82,43 @@ func TestLoadRefusesIPOrigins(t *testing.T) { |
| t.Errorf("loopback http refused: %v", err) |
| } |
| } |
| + |
| +// Finding 10: with installs configured, Base's origin and every app origin |
| +// it would hand out must be ones the assertion package accepts, so a bad |
| +// setting fails at boot rather than at the first handoff. HTTP on loopback |
| +// stays allowed for local runs. |
| +func TestLoadChecksOriginsAgainstTheAssertionRules(t *testing.T) { |
| + cases := []struct { |
| + name, origin, domain string |
| + ok bool |
| + }{ |
| + {"https hostnames", "https://base.example.com", "apps.example.com", true}, |
| + {"a port other than 443", "https://base.example.com:8443", "apps.example.com:8443", true}, |
| + {"loopback http for local runs", "http://127.0.0.1:8830", "apps.example.com", true}, |
| + {"explicit :443 on Base", "https://base.example.com:443", "apps.example.com", false}, |
| + {"explicit :443 on apps", "https://base.example.com", "apps.example.com:443", false}, |
| + {"apps domain is an IP", "https://base.example.com", "203.0.113.7", false}, |
| + {"apps domain numeric suffix", "https://base.example.com", "apps.127", false}, |
| + {"apps domain bad label", "https://base.example.com", "apps_x.example.com", false}, |
| + {"apps domain with a path", "https://base.example.com", "apps.example.com/x", false}, |
| + {"apps domain empty label", "https://base.example.com", "apps..example.com", false}, |
| + {"Base hostname with underscore", "https://base_x.example.com", "apps.example.com", false}, |
| + } |
| + for _, tc := range cases { |
| + t.Run(tc.name, func(t *testing.T) { |
| + m := valid() |
| + m["BASE_ORIGIN"] = tc.origin |
| + m["BASE_APPS_DOMAIN"] = tc.domain |
| + _, err := Load(env(m)) |
| + if (err == nil) != tc.ok { |
| + t.Errorf("origin %q, apps domain %q: err = %v, want ok=%v", tc.origin, tc.domain, err, tc.ok) |
| + } |
| + }) |
| + } |
| + // Without an apps domain nothing is installed, and the checks wait. |
| + m := valid() |
| + m["BASE_ORIGIN"] = "https://base.example.com:443" |
| + if _, err := Load(env(m)); err != nil { |
| + t.Errorf("no installs configured: %v", err) |
| + } |
| +} |
diff --git a/internal/issuer/issuer.go b/internal/issuer/issuer.go| index 92ee6cd..281aa3f 100644 |
| --- a/internal/issuer/issuer.go |
| +++ b/internal/issuer/issuer.go |
| @@ -17,6 +17,7 @@ import ( |
| "io/fs" |
| "log/slog" |
| "net/http" |
| + "strings" |
| "time" |
| "amadan.net/rastrillo/rastrillo/assertion" |
| @@ -116,7 +117,7 @@ func (s *issuer) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| Name string `json:"name"` |
| Email string `json:"email"` |
| Base string `json:"base"` |
| - }{m.Name, m.Email, s.cfg.Name}) |
| + }{clip(m.Name, extName), clip(m.Email, extEmail), clip(s.cfg.Name, extBase)}) |
| if err != nil { |
| s.fail(w, app, err) |
| return |
| @@ -193,6 +194,42 @@ func (s *issuer) installedOrigin(r *http.Request, app string) (string, bool, err |
| return "", false, nil |
| } |
| +// Ext is display only, and rastrillo refuses one over 1 KiB. Each field |
| +// gets a share of that, counted as encoded JSON; with the object's own 32 |
| +// bytes the three add up to 992. |
| +const ( |
| + extName = 384 |
| + extEmail = 320 |
| + extBase = 256 |
| +) |
| + |
| +// clip returns s if its JSON encoding (without quotes) fits in max bytes, and |
| +// otherwise its longest whole-character prefix that fits with a trailing |
| +// "…". Escapes count at their encoded size. |
| +func clip(s string, max int) string { |
| + if encodedLen(s) <= max { |
| + return s |
| + } |
| + const ellipsis = "…" // 3 bytes, not escaped |
| + var b strings.Builder |
| + used := len(ellipsis) |
| + for _, r := range s { |
| + n := encodedLen(string(r)) |
| + if used+n > max { |
| + break |
| + } |
| + used += n |
| + b.WriteRune(r) // an invalid byte becomes U+FFFD, a valid character |
| + } |
| + return b.String() + ellipsis |
| +} |
| + |
| +// encodedLen is the length of s as encoding/json writes it, less the quotes. |
| +func encodedLen(s string) int { |
| + b, _ := json.Marshal(s) |
| + return len(b) - 2 |
| +} |
| + |
| // nonceShape accepts exactly what assertion.RandomID produces: 16 bytes as |
| // 22 unpadded, canonical base64url characters. |
| func nonceShape(s string) bool { |
diff --git a/internal/issuer/issuer_test.go b/internal/issuer/issuer_test.go| index 5a8cbb2..6d0ceb4 100644 |
| --- a/internal/issuer/issuer_test.go |
| +++ b/internal/issuer/issuer_test.go |
| @@ -18,6 +18,7 @@ import ( |
| "strings" |
| "testing" |
| "time" |
| + "unicode/utf8" |
| "amadan.net/rastrillo/rastrillo/assertion" |
| "amadan.net/rastrillo/rastrillo/budget" |
| @@ -510,3 +511,54 @@ func TestAuthorizeUsesRecordedHost(t *testing.T) { |
| t.Errorf("CSP %q", got) |
| } |
| } |
| + |
| +// Finding 10: display fields are cut to fit rastrillo's 1 KiB limit on Ext, |
| +// at a character boundary, so a very long name never breaks the handoff. |
| +func TestAuthorizeBoundsDisplayFields(t *testing.T) { |
| + for name, tc := range map[string]struct{ member, base string }{ |
| + "long name": {strings.Repeat("Ana María ", 300), "Acme"}, |
| + "name that escapes": {strings.Repeat("<&>", 400), "Acme"}, |
| + "long base name": {"Paul", strings.Repeat("Acme Holdings ", 200)}, |
| + "multibyte at the edge": {strings.Repeat("é", 1000), strings.Repeat("日本", 400)}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + cfg := testConfig(t) |
| + cfg.Name = tc.base |
| + d := openDB(t) |
| + setInstall(t, d.Writer(), cfg, "scratchpad", "installed") |
| + m := owner(t, d.Writer()) |
| + m.Name = tc.member |
| + m.Email = strings.Repeat("x", 400) + "@acme.test" |
| + h, err := issuer.New(d.Writer(), cfg, |
| + func(*http.Request) (store.Member, bool) { return m, true }, |
| + func(*http.Request) (time.Time, bool) { return time.Time{}, false }, |
| + slog.New(slog.NewTextHandler(io.Discard, nil))) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + w := httptest.NewRecorder() |
| + h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, authorizePath("scratchpad", nonce(t)), nil)) |
| + v, ok := fields(w.Body.String(), appOrigin+"/auth/assertion") |
| + if w.Code != 200 || !ok { |
| + t.Fatalf("authorize: %d\n%s", w.Code, w.Body) |
| + } |
| + c, err := verifier(t, cfg).Verify(v.Get("assertion"), time.Now()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var ext map[string]string |
| + if err := json.Unmarshal(c.Ext, &ext); err != nil || len(c.Ext) > 1024 { |
| + t.Fatalf("ext %d bytes: %v", len(c.Ext), err) |
| + } |
| + for field, full := range map[string]string{"name": tc.member, "email": m.Email, "base": tc.base} { |
| + got := ext[field] |
| + if got == "" || !utf8.ValidString(got) { |
| + t.Errorf("%s = %q", field, got) |
| + } |
| + if got != full && !strings.HasPrefix(full, strings.TrimSuffix(got, "…")) { |
| + t.Errorf("%s %q is not a prefix of the original", field, got) |
| + } |
| + } |
| + }) |
| + } |
| +} |