diff --git a/.gitleaks.toml b/.gitleaks.toml index 28487fb2a..90dc9f7cc 100644 --- a/.gitleaks.toml +++ b/.gitleaks.toml @@ -31,3 +31,9 @@ id = "local-workstation-path" description = "Local workstation home directory path" regex = '''(/Users|/home)/[A-Za-z0-9_.-]+/''' keywords = ["/users/", "/home/"] + +[[rules]] +id = "fanout-bearer-secret" +description = "Fanout opaque bearer secret" +regex = '''fo[iacr]_[A-Za-z0-9_-]{43}\b''' +keywords = ["foi_", "foa_", "for_", "foc_"] diff --git a/docs/authentication.md b/docs/authentication.md index 9fa660717..81e58cce3 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -22,7 +22,7 @@ stderr containing a **setup URL**: Setup credential behavior: -- The credential carries 128 bits of entropy and lives only in memory. It is +- The credential carries 256 bits of entropy and lives only in memory. It is never written to the database. - It is consumed the moment the first administrator is committed, before any other setup work. A later failure in that request does not leave a live diff --git a/docs/proposals/0001-logged-in-ui-shell.md b/docs/proposals/0001-logged-in-ui-shell.md index a09f6bda1..18f5d8852 100644 --- a/docs/proposals/0001-logged-in-ui-shell.md +++ b/docs/proposals/0001-logged-in-ui-shell.md @@ -125,7 +125,7 @@ URLs are unchanged: `/chat`, `/chat/$threadId`, `/dashboards`, `/dashboards/$das - Code step replaces the `TextInput` with Mantine `PinInput`: `length={6}`, `type="number"`, `oneTimeCode`, autofocus, `aria-label="Verification code"`. When six digits are entered the form submits itself. A failed verify clears the input, shows the server message in the existing `Alert`, and refocuses. - Under the input: "Sent to · Change email" (returns to the email step and clears the code), a "Resend code" `Button variant="subtle"` with a 30-second client cooldown that shows "Resend in 12s", and the hint "Codes expire in 5 minutes" (the server TTL in `internal/auth/code.go`). Server rate-limit responses from `/api/auth/start` are shown verbatim in the alert. - Headings on every auth surface: 28px at base, 32px from `sm`. Copy: "Sign in" (no self-signup), "Sign in or create an account" (self-signup) stays but at the smaller size, "Create the first admin", "Save your ingest token". -- Setup success: the copy button copies the full header line (`Authorization: Bearer fo_…`) and the button label becomes "Copy header". A second, smaller "Copy token only" link copies the bare token. +- Setup success: the copy button copies the full header line (`Authorization: Bearer foi_…`) and the button label becomes "Copy header". A second, smaller "Copy token only" link copies the bare token. ## 5. Defects fixed as part of the work diff --git a/internal/api/auth_test.go b/internal/api/auth_test.go index 9bf98fb29..a949981ba 100644 --- a/internal/api/auth_test.go +++ b/internal/api/auth_test.go @@ -672,7 +672,7 @@ func TestSetupLifecycleAndIngestToken(t *testing.T) { if body["access_token"] != "" { t.Fatal("legacy access token must not be returned") } - if !strings.HasPrefix(body["ingest_token"], "fo_") { + if !strings.HasPrefix(body["ingest_token"], "foi_") { t.Fatalf("ingest token = %q", body["ingest_token"]) } if body["ingest_header_name"] != "Authorization" { diff --git a/internal/auth/bearer_test.go b/internal/auth/bearer_test.go new file mode 100644 index 000000000..77acafa4c --- /dev/null +++ b/internal/auth/bearer_test.go @@ -0,0 +1,51 @@ +package auth + +import ( + "context" + "errors" + "strings" + "testing" +) + +// Nil stores make any database access fail: malformed credentials must return +// the path's normal denial before opening a connection or computing a lookup. +func TestMalformedBearerSecretsSkipDatabase(t *testing.T) { + ctx := context.Background() + store := &OAuthStore{} + links := &CodeStore{} + for _, value := range []string{"", "bad", strings.Repeat("a", 32), strings.Repeat("a", 48), "fo_" + strings.Repeat("a", 48), "foa_" + strings.Repeat("!", 43)} { + if _, err := store.VerifyAccessToken(ctx, value, ""); !errors.Is(err, ErrInvalidOAuthToken) { + t.Fatalf("access: %v", err) + } + if _, err := store.RotateRefreshToken(ctx, "", value, "", ""); !errors.Is(err, ErrInvalidOAuthGrant) { + t.Fatalf("refresh: %v", err) + } + if _, err := store.ConsumeAuthorizationCode(ctx, value); !errors.Is(err, ErrInvalidOAuthGrant) { + t.Fatalf("code: %v", err) + } + if _, ok, err := links.VerifyLoginLink(value); ok || err != nil { + t.Fatalf("login link: %v, %v", ok, err) + } + } + for _, prefix := range []string{"foi_", "foa_", "for_", "foc_"} { + value := prefix + strings.Repeat("A", 43) + if prefix != "foa_" { + if _, err := store.VerifyAccessToken(ctx, value, ""); !errors.Is(err, ErrInvalidOAuthToken) { + t.Fatal(err) + } + } + if prefix != "for_" { + if _, err := store.RotateRefreshToken(ctx, "", value, "", ""); !errors.Is(err, ErrInvalidOAuthGrant) { + t.Fatal(err) + } + } + if prefix != "foc_" { + if _, err := store.ConsumeAuthorizationCode(ctx, value); !errors.Is(err, ErrInvalidOAuthGrant) { + t.Fatal(err) + } + } + if _, ok, err := links.VerifyLoginLink(value); ok || err != nil { + t.Fatalf("login link: %v, %v", ok, err) + } + } +} diff --git a/internal/auth/code.go b/internal/auth/code.go index ad0381f4a..997170d1c 100644 --- a/internal/auth/code.go +++ b/internal/auth/code.go @@ -14,15 +14,15 @@ import ( "github.com/labstack/fanout/internal/db/generated" appid "github.com/labstack/fanout/internal/id" + "github.com/labstack/fanout/internal/secret" ) const ( - codeTTL = 5 * time.Minute - loginLinkTTL = 15 * time.Minute - loginLinkTokenBytes = 16 - maxAttempts = 3 - purposeEmailCode = "email_code" - purposeLoginLink = "login_link" + codeTTL = 5 * time.Minute + loginLinkTTL = 15 * time.Minute + maxAttempts = 3 + purposeEmailCode = "email_code" + purposeLoginLink = "login_link" ) // GenerateCode returns a random 6-digit code. @@ -34,13 +34,9 @@ func GenerateCode() (string, error) { return fmt.Sprintf("%06d", n.Int64()), nil } -// GenerateLoginLinkToken returns a credential with 128 bits of entropy. +// GenerateLoginLinkToken returns a credential with 256 bits of entropy. func GenerateLoginLinkToken() (string, error) { - buf := make([]byte, loginLinkTokenBytes) - if _, err := rand.Read(buf); err != nil { - return "", fmt.Errorf("auth: generate login link token: %w", err) - } - return hex.EncodeToString(buf), nil + return secret.NewSecret() } // HashCode produces an HMAC-SHA256 hex digest of the code. @@ -188,6 +184,9 @@ func (s *CodeStore) Verify(email, code string) (bool, error) { // email it was issued for. Login links are purpose-bound and cannot be used as // ordinary email verification codes. func (s *CodeStore) VerifyLoginLink(token string) (string, bool, error) { + if !secret.ValidSecret(token, "") { + return "", false, nil + } ctx := context.Background() row, err := s.q.GetUnusedLoginLink(ctx, HashCode(token, s.secret)) if errors.Is(err, sql.ErrNoRows) { diff --git a/internal/auth/code_test.go b/internal/auth/code_test.go index 36c0d2722..a7e8eadab 100644 --- a/internal/auth/code_test.go +++ b/internal/auth/code_test.go @@ -8,6 +8,8 @@ import ( "time" appstore "github.com/labstack/fanout/internal/store" + + "github.com/labstack/fanout/internal/secret" ) func newTestCodeStore(t *testing.T) *CodeStore { @@ -30,13 +32,13 @@ func TestGenerateCode_SixDigits(t *testing.T) { } } -func TestGenerateLoginLinkTokenHas128BitsOfEntropy(t *testing.T) { +func TestGenerateLoginLinkTokenHas256BitsOfEntropy(t *testing.T) { token, err := GenerateLoginLinkToken() if err != nil { t.Fatalf("GenerateLoginLinkToken: %v", err) } - if len(token) != 2*loginLinkTokenBytes { - t.Fatalf("token length = %d, want %d hex characters", len(token), 2*loginLinkTokenBytes) + if !secret.ValidSecret(token, "") { + t.Fatal("login token must encode 32 bytes as unpadded base64url") } } diff --git a/internal/auth/oauth_store.go b/internal/auth/oauth_store.go index 24961bb94..ec7f1c04f 100644 --- a/internal/auth/oauth_store.go +++ b/internal/auth/oauth_store.go @@ -2,10 +2,8 @@ package auth import ( "context" - "crypto/rand" "crypto/sha256" "database/sql" - "encoding/base64" "encoding/hex" "encoding/json" "errors" @@ -13,6 +11,8 @@ import ( "time" appid "github.com/labstack/fanout/internal/id" + + "github.com/labstack/fanout/internal/secret" ) const ( @@ -270,7 +270,8 @@ func (s *OAuthStore) CreateAuthorizationCode(ctx context.Context, code OAuthAuth return "", ErrInvalidOAuthGrant } code.Scope = canonicalScope - raw, err := randomOAuthValue("foc_") + value, err := secret.NewSecret() + raw := "foc_" + value if err != nil { return "", err } @@ -290,6 +291,9 @@ func (s *OAuthStore) CreateAuthorizationCode(ctx context.Context, code OAuthAuth } func (s *OAuthStore) ConsumeAuthorizationCode(ctx context.Context, raw string) (OAuthAuthorizationCode, error) { + if !secret.ValidSecret(raw, "foc_") { + return OAuthAuthorizationCode{}, ErrInvalidOAuthGrant + } conn, err := s.db.Conn(ctx) if err != nil { return OAuthAuthorizationCode{}, fmt.Errorf("oauth: open code transaction: %w", err) @@ -353,6 +357,9 @@ func (s *OAuthStore) IssueTokenPair(ctx context.Context, clientID, userID, scope } func (s *OAuthStore) RotateRefreshToken(ctx context.Context, clientID, raw, resource, requestedScope string) (OAuthTokenPair, error) { + if !secret.ValidSecret(raw, "for_") { + return OAuthTokenPair{}, ErrInvalidOAuthGrant + } conn, err := s.db.Conn(ctx) if err != nil { return OAuthTokenPair{}, fmt.Errorf("oauth: open refresh transaction: %w", err) @@ -466,6 +473,9 @@ func (s *OAuthStore) RotateRefreshToken(ctx context.Context, clientID, raw, reso // database failure is returned as a wrapped infrastructure error so callers // can fail closed without treating an outage as a bad credential. func (s *OAuthStore) VerifyAccessToken(ctx context.Context, raw, resource string) (OAuthTokenRecord, error) { + if !secret.ValidSecret(raw, "foa_") { + return OAuthTokenRecord{}, ErrInvalidOAuthToken + } record, err := readOAuthToken(ctx, s.db, raw) if errors.Is(err, ErrInvalidOAuthGrant) { return OAuthTokenRecord{}, ErrInvalidOAuthToken @@ -544,11 +554,13 @@ type oauthDB interface { } func (s *OAuthStore) insertTokenPair(ctx context.Context, db oauthDB, familyID, clientID, userID, scope, resource string) (OAuthTokenPair, error) { - access, err := randomOAuthValue("foa_") + accessValue, err := secret.NewSecret() + access := "foa_" + accessValue if err != nil { return OAuthTokenPair{}, err } - refresh, err := randomOAuthValue("for_") + refreshValue, err := secret.NewSecret() + refresh := "for_" + refreshValue if err != nil { return OAuthTokenPair{}, err } @@ -595,14 +607,6 @@ func readOAuthToken(ctx context.Context, db oauthDB, raw string) (OAuthTokenReco return record, nil } -func randomOAuthValue(prefix string) (string, error) { - b := make([]byte, 32) - if _, err := rand.Read(b); err != nil { - return "", fmt.Errorf("oauth: generate random value: %w", err) - } - return prefix + base64.RawURLEncoding.EncodeToString(b), nil -} - func oauthHash(value string) string { sum := sha256.Sum256([]byte(value)) return hex.EncodeToString(sum[:]) diff --git a/internal/auth/oauth_store_test.go b/internal/auth/oauth_store_test.go index 73d0bdff4..ec64f7dbf 100644 --- a/internal/auth/oauth_store_test.go +++ b/internal/auth/oauth_store_test.go @@ -555,9 +555,9 @@ func TestOAuthStoreCleanupKeepsLiveFamiliesAndActiveClients(t *testing.T) { } func TestOAuthTokenPairStringRedactsSecrets(t *testing.T) { - pair := OAuthTokenPair{AccessToken: "foa_secret", RefreshToken: "for_secret", ExpiresIn: 900, Scope: MCPScopeTelemetryRead} + pair := OAuthTokenPair{AccessToken: "foa_" + strings.Repeat("A", 43), RefreshToken: "for_" + strings.Repeat("B", 43), ExpiresIn: 900, Scope: MCPScopeTelemetryRead} got := fmt.Sprintf("%v", pair) - if strings.Contains(got, "foa_secret") || strings.Contains(got, "for_secret") { + if strings.Contains(got, pair.AccessToken) || strings.Contains(got, pair.RefreshToken) { t.Fatalf("String() leaked token material: %s", got) } if !strings.Contains(got, "REDACTED") { diff --git a/internal/auth/setup.go b/internal/auth/setup.go index e3e992899..c22db2173 100644 --- a/internal/auth/setup.go +++ b/internal/auth/setup.go @@ -1,12 +1,11 @@ package auth import ( - "crypto/rand" "crypto/subtle" - "encoding/base64" - "fmt" "sync" "time" + + "github.com/labstack/fanout/internal/secret" ) const SetupTTL = time.Hour @@ -47,7 +46,7 @@ func NewSetup() *Setup { } func (s *Setup) Rotate() (string, time.Time, error) { - token, err := generateSetupToken() + token, err := secret.NewSecret() if err != nil { return "", time.Time{}, err } @@ -74,7 +73,7 @@ func (s *Setup) Verify(token string) SetupStatus { if time.Now().UTC().After(expires) { return SetupStatusExpired } - if subtle.ConstantTimeCompare([]byte(token), []byte(current)) != 1 { + if !secret.ValidSecret(token, "") || subtle.ConstantTimeCompare([]byte(token), []byte(current)) != 1 { return SetupStatusWrong } return SetupStatusOK @@ -86,16 +85,3 @@ func (s *Setup) Clear() { s.expires = time.Time{} s.mu.Unlock() } - -// setupTokenBytes is the entropy floor for a credential that creates an -// administrator. The token is delivered inside the setup URL rather than typed, -// so length costs nothing in usability. -const setupTokenBytes = 16 - -func generateSetupToken() (string, error) { - buf := make([]byte, setupTokenBytes) - if _, err := rand.Read(buf); err != nil { - return "", fmt.Errorf("generate setup token: %w", err) - } - return base64.RawURLEncoding.EncodeToString(buf), nil -} diff --git a/internal/auth/setup_test.go b/internal/auth/setup_test.go index b900417d1..76f2afeb8 100644 --- a/internal/auth/setup_test.go +++ b/internal/auth/setup_test.go @@ -3,6 +3,7 @@ package auth import ( "encoding/base64" "regexp" + "strings" "sync" "testing" "time" @@ -10,7 +11,7 @@ import ( // The setup credential is delivered inside a URL, so it is URL-safe and // unpunctuated rather than grouped for typing. -var tokenShape = regexp.MustCompile(`^[A-Za-z0-9_-]{22,}$`) +var tokenShape = regexp.MustCompile(`^[A-Za-z0-9_-]{43}$`) func TestSetupVerifyAcceptsFreshToken(t *testing.T) { s := NewSetup() @@ -113,7 +114,7 @@ func TestSetupConcurrentAccess(t *testing.T) { wg.Wait() } -func TestSetupTokenCarriesAtLeast128BitsOfEntropy(t *testing.T) { +func TestSetupTokenCarries256BitsOfEntropy(t *testing.T) { s := NewSetup() token, _, err := s.Rotate() if err != nil { @@ -123,7 +124,16 @@ func TestSetupTokenCarriesAtLeast128BitsOfEntropy(t *testing.T) { if err != nil { t.Fatalf("setup token %q is not raw base64url: %v", token, err) } - if len(decoded) < 16 { - t.Fatalf("setup token carries %d bits, want at least 128: a credential that creates an administrator needs a conservative entropy floor", len(decoded)*8) + if len(decoded) != 32 { + t.Fatalf("setup token carries %d bits, want 256", len(decoded)*8) + } +} + +func TestSetupRejectsMatchingMalformedToken(t *testing.T) { + for _, token := range []string{strings.Repeat("a", 22), strings.Repeat("!", 43)} { + setup := &Setup{token: token, expires: time.Now().Add(time.Minute)} + if status := setup.Verify(token); status != SetupStatusWrong { + t.Fatalf("malformed setup token returned %v", status) + } } } diff --git a/internal/ingest/auth.go b/internal/ingest/auth.go index 1a42f1c17..e279840d3 100644 --- a/internal/ingest/auth.go +++ b/internal/ingest/auth.go @@ -12,6 +12,7 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" + "github.com/labstack/fanout/internal/secret" "github.com/labstack/fanout/internal/settings" ) @@ -65,6 +66,9 @@ func (a *ingestAuthorizer) authorize(ctx context.Context) error { // authorizeToken owns the transport-neutral credential policy. gRPC and HTTP // extract tokens from their respective headers, then share this verification. func (a *ingestAuthorizer) authorizeToken(ctx context.Context, token string) error { + if !secret.ValidSecret(token, "foi_") { + return errInvalidIngestToken + } if a.settingsStore == nil { // Defensive: production always wires a store (cmd/fanout/main.go). Reaching // this path means a mis-wired init; fail closed and log so it surfaces. diff --git a/internal/ingest/auth_test.go b/internal/ingest/auth_test.go index f9182461b..a3b7ab8c4 100644 --- a/internal/ingest/auth_test.go +++ b/internal/ingest/auth_test.go @@ -2,6 +2,8 @@ package ingest import ( "context" + "errors" + "strings" "testing" "google.golang.org/grpc/codes" @@ -59,7 +61,7 @@ func TestAuthorize_RejectsWrongToken(t *testing.T) { } authorizer := newIngestAuthorizer(store) - ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer fo_wrong")) + ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer foi_"+strings.Repeat("A", 43))) err = authorizer.authorize(ctx) if status.Code(err) != codes.Unauthenticated { @@ -132,3 +134,12 @@ func newRuntimeStore(t *testing.T) *settings.Store { t.Cleanup(func() { sqlite.Close() }) return settings.NewStore(sqlite.DB) } + +func TestMalformedIngestSecretsSkipSettingsLookup(t *testing.T) { + a := newIngestAuthorizer(nil) + for _, token := range []string{"", "fo_" + strings.Repeat("a", 48), "foi_short", "foa_" + strings.Repeat("A", 43), "foi_" + strings.Repeat("!", 43)} { + if err := a.authorizeToken(context.Background(), token); !errors.Is(err, errInvalidIngestToken) { + t.Fatalf("malformed token: %v", err) + } + } +} diff --git a/internal/ingest/http_test.go b/internal/ingest/http_test.go index 037cfe8c2..8a3148a93 100644 --- a/internal/ingest/http_test.go +++ b/internal/ingest/http_test.go @@ -12,6 +12,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "strings" "testing" collectorlogs "go.opentelemetry.io/proto/otlp/collector/logs/v1" @@ -131,7 +132,7 @@ func TestHTTPIngestAuthentication(t *testing.T) { }) t.Run("wrong token", func(t *testing.T) { f := newHTTPIngestFixture(t, true) - assertOTLPHTTPStatus(t, f.handler, http.MethodPost, "/v1/traces", testTraceRequest(), "fo_wrong", http.StatusUnauthorized) + assertOTLPHTTPStatus(t, f.handler, http.MethodPost, "/v1/traces", testTraceRequest(), "foi_"+strings.Repeat("A", 43), http.StatusUnauthorized) }) t.Run("legacy Fanout header", func(t *testing.T) { f := newHTTPIngestFixture(t, true) diff --git a/internal/secret/secret.go b/internal/secret/secret.go new file mode 100644 index 000000000..02926b21e --- /dev/null +++ b/internal/secret/secret.go @@ -0,0 +1,28 @@ +// Package secret generates and validates opaque bearer secrets. +package secret + +import ( + "crypto/rand" + "encoding/base64" + "fmt" + "strings" +) + +// NewSecret returns 32 random bytes as unpadded base64url. +func NewSecret() (string, error) { + value := make([]byte, 32) + if _, err := rand.Read(value); err != nil { + return "", fmt.Errorf("generate secret: %w", err) + } + return base64.RawURLEncoding.EncodeToString(value), nil +} + +// ValidSecret reports whether value has NewSecret's shape after prefix. +func ValidSecret(value, prefix string) bool { + rest, ok := strings.CutPrefix(value, prefix) + if !ok || len(rest) != 43 { + return false + } + decoded, err := base64.RawURLEncoding.DecodeString(rest) + return err == nil && len(decoded) == 32 +} diff --git a/internal/secret/secret_test.go b/internal/secret/secret_test.go new file mode 100644 index 000000000..b3eec0edc --- /dev/null +++ b/internal/secret/secret_test.go @@ -0,0 +1,35 @@ +package secret + +import ( + "strings" + "testing" +) + +func TestSecretShape(t *testing.T) { + first, err := NewSecret() + if err != nil { + t.Fatal(err) + } + second, err := NewSecret() + if err != nil { + t.Fatal(err) + } + if first == second { + t.Fatal("generated secrets must differ") + } + for _, prefix := range []string{"", "foi_", "foa_", "for_", "foc_"} { + if !ValidSecret(prefix+first, prefix) { + t.Errorf("generated secret rejected for prefix %q", prefix) + } + } + for _, prefix := range []string{"", "foi_", "foa_", "for_", "foc_"} { + for _, rest := range []string{"", first[:42], first + "A", first + "=", strings.Repeat("!", 43), strings.Repeat("A", 42) + "+", strings.Repeat("A", 42) + "/", first[:42] + "\n"} { + if ValidSecret(prefix+rest, prefix) { + t.Errorf("malformed secret accepted for prefix %q", prefix) + } + } + } + if ValidSecret("foa_"+first, "foi_") || ValidSecret("fo_"+strings.Repeat("a", 48), "foi_") { + t.Fatal("wrong prefix accepted") + } +} diff --git a/internal/settings/ingest.go b/internal/settings/ingest.go index 571a371f2..ab5a9b53e 100644 --- a/internal/settings/ingest.go +++ b/internal/settings/ingest.go @@ -8,6 +8,7 @@ import ( "github.com/labstack/fanout/internal/auth" "github.com/labstack/fanout/internal/db/generated" + "github.com/labstack/fanout/internal/secret" ) const ingestKey = "ingest" @@ -64,11 +65,11 @@ func (s *Store) SetIngestWithAudit(ctx context.Context, ingest Ingest, audit *au } func GenerateIngestToken() (string, string, error) { - raw, err := randomHexToken(24) + raw, err := secret.NewSecret() if err != nil { return "", "", fmt.Errorf("generate ingest token: %w", err) } - token := "fo_" + raw + token := "foi_" + raw return token, HashIngestToken(token), nil } @@ -77,5 +78,5 @@ func HashIngestToken(token string) string { } func CheckIngestToken(token, hash string) bool { - return checkToken(token, hash) + return secret.ValidSecret(token, "foi_") && checkToken(token, hash) } diff --git a/internal/settings/store_test.go b/internal/settings/store_test.go index 00d0bd4e1..dfcd15c4b 100644 --- a/internal/settings/store_test.go +++ b/internal/settings/store_test.go @@ -2,9 +2,11 @@ package settings import ( "context" + "strings" "testing" "github.com/labstack/fanout/internal/auth" + "github.com/labstack/fanout/internal/secret" appstore "github.com/labstack/fanout/internal/store" ) @@ -22,7 +24,7 @@ func TestGetIngest_EmptyBeforeSetup(t *testing.T) { func TestSetIngest_RoundTripsTokenHash(t *testing.T) { store := newTestStore(t) - want := Ingest{TokenHash: HashIngestToken("fo_test")} + want := Ingest{TokenHash: HashIngestToken("foi_test")} if err := store.SetIngest(context.Background(), want); err != nil { t.Fatalf("SetIngest: %v", err) @@ -46,7 +48,7 @@ func TestSetIngestWithAuditSurvivesRequestCancellation(t *testing.T) { store := NewStore(sqlite.DB) ctx, cancel := context.WithCancel(context.Background()) cancel() - want := Ingest{TokenHash: HashIngestToken("fo_detached")} + want := Ingest{TokenHash: HashIngestToken("foi_detached")} if err := store.SetIngestWithAudit(ctx, want, auth.NewAuditStore(sqlite.DB), auth.AuditEvent{ EventType: "ingest_key.rotated", Outcome: "success", TargetType: "ingest", TargetID: "default", }); err != nil { @@ -70,13 +72,13 @@ func TestGenerateAndCheckIngestToken(t *testing.T) { if err != nil { t.Fatalf("GenerateIngestToken: %v", err) } - if token == "" || hash == "" { + if !secret.ValidSecret(token, "foi_") || len(hash) != 64 { t.Fatal("token or hash is empty") } if !CheckIngestToken(token, hash) { t.Fatal("CheckIngestToken returned false for the generated token") } - if CheckIngestToken("fo_other", hash) { + if CheckIngestToken("foi_"+strings.Repeat("A", 43), hash) { t.Fatal("CheckIngestToken returned true for the wrong token") } } @@ -91,3 +93,10 @@ func newTestStore(t *testing.T) *Store { t.Cleanup(func() { sqlite.Close() }) return NewStore(sqlite.DB) } + +func TestIngestRejectsRetiredTokenEvenWithMatchingDigest(t *testing.T) { + token := "fo_" + strings.Repeat("a", 48) + if CheckIngestToken(token, HashIngestToken(token)) { + t.Fatal("retired ingest token accepted") + } +} diff --git a/internal/settings/tokens.go b/internal/settings/tokens.go index 2a5b8630d..49ace1618 100644 --- a/internal/settings/tokens.go +++ b/internal/settings/tokens.go @@ -1,21 +1,11 @@ package settings import ( - "crypto/rand" "crypto/sha256" "crypto/subtle" "encoding/hex" - "fmt" ) -func randomHexToken(byteLen int) (string, error) { - buf := make([]byte, byteLen) - if _, err := rand.Read(buf); err != nil { - return "", fmt.Errorf("generate token: %w", err) - } - return hex.EncodeToString(buf), nil -} - func hashToken(token string) string { sum := sha256.Sum256([]byte(token)) return hex.EncodeToString(sum[:]) diff --git a/site/src/content/docs/docs/get-started/send-telemetry.mdx b/site/src/content/docs/docs/get-started/send-telemetry.mdx index 67a237458..3fa1f78be 100644 --- a/site/src/content/docs/docs/get-started/send-telemetry.mdx +++ b/site/src/content/docs/docs/get-started/send-telemetry.mdx @@ -16,7 +16,9 @@ same port as the application: `7520` by default. Both require the ingest token. ## The ingest token The raw ingest token is shown once, immediately after you create the first -administrator. It is `fo_`-prefixed, and Fanout stores only its hash. +administrator. It is `foi_` followed by 43 unpadded base64url characters (32 random bytes). +Fanout stores only its SHA-256 digest. Existing `fo_` tokens are rejected; +rotate the ingest token in **Connect telemetry → Ingest token** and update every collector. An administrator can issue a replacement in **Connect telemetry → Ingest token** in the account menu, or with `POST /api/settings/ingest/token/rotate`. Save the new token when it is @@ -26,7 +28,7 @@ exporters until you update their credentials. Send it as a bearer token; this is the only accepted ingest credential form: ```text -Authorization: Bearer fo_ +Authorization: Bearer foi_ ``` Ingest is available after administrator setup. Exporters require the ingest @@ -37,7 +39,7 @@ token; browser sessions and MCP OAuth tokens cannot authorize exports. ```sh export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:7520 export OTEL_EXPORTER_OTLP_PROTOCOL=grpc -export OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer%20fo_" +export OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer%20foi_" export OTEL_SERVICE_NAME=checkout export OTEL_RESOURCE_ATTRIBUTES=service.namespace=storefront ``` diff --git a/site/src/content/docs/docs/guides/troubleshooting.mdx b/site/src/content/docs/docs/guides/troubleshooting.mdx index 3e265d792..b55a9224f 100644 --- a/site/src/content/docs/docs/guides/troubleshooting.mdx +++ b/site/src/content/docs/docs/guides/troubleshooting.mdx @@ -106,7 +106,7 @@ Work down this list in order: ingest token to check against, and ingest rejects everything. A collector started ahead of setup fails until you complete it. 2. **Is it the right credential, in the right header?** The ingest token is - `fo_`-prefixed and goes in `Authorization: Bearer`. Browser sessions and MCP + `foi_`-prefixed and goes in `Authorization: Bearer`. Browser sessions and MCP tokens are not ingest credentials. 3. **Is the listener reachable?** Both OTLP transports use `FANOUT_ADDR` (`127.0.0.1:7520` by default; the image sets `:7520`). diff --git a/site/src/content/docs/docs/guides/upgrade.mdx b/site/src/content/docs/docs/guides/upgrade.mdx index d12d01b2e..20b4cc674 100644 --- a/site/src/content/docs/docs/guides/upgrade.mdx +++ b/site/src/content/docs/docs/guides/upgrade.mdx @@ -42,3 +42,11 @@ See [storage](/docs/concepts/storage) for the current format contract. This release also renames API and MCP contracts without aliases. Update scripts and clients using the release notes and [HTTP routes](/docs/reference/api). + +## When a release changes credential formats + +Ingest tokens use the `foi_` prefix. Fanout rejects tokens issued with the +earlier `fo_` prefix. After upgrading, an administrator rotates the token in +**Connect telemetry → Ingest token** and updates every collector. Unused login +links issued before the upgrade stop working; request a new one. Connected MCP +agents keep their OAuth tokens. diff --git a/ui/host/e2e/support/harness.test.ts b/ui/host/e2e/support/harness.test.ts index 8ebd062d6..cf8b29287 100644 --- a/ui/host/e2e/support/harness.test.ts +++ b/ui/host/e2e/support/harness.test.ts @@ -10,12 +10,13 @@ import FailureReport from "../failure-report"; describe("setup safety and readiness", () => { it("redacts credentials before printing the last fifty diagnostic lines", () => { const lines = Array.from({ length: 60 }, (_, i) => `line ${i}`); - lines.push('Bearer fo_ingest.abc-123 setup_token=secret%20token&next=/ cookie: fanout_session=session-secret; other=second-secret'); + const tokens = ["foi_", "foa_", "for_", "foc_"].map((prefix) => prefix + "A".repeat(43)); + lines.push(`${tokens.join(" ")} setup_token=secret%20token&next=/ cookie: fanout_session=session-secret; other=second-secret`); lines.push('Set-Cookie: fanout_session=response-secret; Path=/; HttpOnly'); const tail = diagnosticTail(lines.join("\n")); expect(tail.split("\n")).toHaveLength(50); expect(tail).toContain("line 59"); - for (const secret of ["fo_ingest", "secret%20token", "session-secret", "second-secret", "response-secret"]) expect(tail).not.toContain(secret); + for (const secret of [...tokens, "secret%20token", "session-secret", "second-secret", "response-secret"]) expect(tail).not.toContain(secret); expect(tail).toContain("[REDACTED]"); expect(diagnosticTail('{"cookie":"session=json-secret"}')).not.toContain("json-secret"); }); diff --git a/ui/host/e2e/support/setup-support.ts b/ui/host/e2e/support/setup-support.ts index 9667eb0b6..33fecbe07 100644 --- a/ui/host/e2e/support/setup-support.ts +++ b/ui/host/e2e/support/setup-support.ts @@ -2,7 +2,7 @@ import type { EventEmitter } from "node:events"; export function diagnosticTail(output: string): string { return output - .replace(/fo_[A-Za-z0-9_.%~+\/=-]+/g, "[REDACTED]") + .replace(/fo[iacr]_[A-Za-z0-9_.%~+\/=-]+/g, "[REDACTED]") .replace(/setup_token=[^\s&"'<>]+/gi, "setup_token=[REDACTED]") .replace(/((?:set-cookie|cookie)["']?\s*[:=]\s*)[^\r\n]+/gi, "$1[REDACTED]") .trimEnd().split(/\r?\n/).slice(-50).join("\n"); diff --git a/ui/host/src/settings.test.tsx b/ui/host/src/settings.test.tsx index bf2c637c1..2e6cffcc3 100644 --- a/ui/host/src/settings.test.tsx +++ b/ui/host/src/settings.test.tsx @@ -122,7 +122,7 @@ describe("connect telemetry", () => { }); it("shows the new token once after rotating", async () => { - fetchMock.mockImplementation(async (_url, init) => json((init as RequestInit | undefined)?.method === "POST" ? { ...connection, ingest_token: "fo_newtoken" } : connection)); + fetchMock.mockImplementation(async (_url, init) => json((init as RequestInit | undefined)?.method === "POST" ? { ...connection, ingest_token: `foi_${"A".repeat(43)}` } : connection)); const root = await mount(); await vi.waitFor(() => expect(document.body.textContent).toContain("Rotate token")); const open = [...document.querySelectorAll("button")].find((button) => button.textContent?.trim() === "Rotate token"); @@ -130,7 +130,7 @@ describe("connect telemetry", () => { await vi.waitFor(() => expect(document.querySelector('[role="dialog"]')).not.toBeNull()); const confirm = [...document.querySelectorAll('[role="dialog"] button')].find((button) => button.textContent?.trim() === "Rotate token"); await act(async () => confirm?.click()); - await vi.waitFor(() => expect(document.body.textContent).toContain("fo_newtoken")); + await vi.waitFor(() => expect(document.body.textContent).toContain(`foi_${"A".repeat(43)}`)); expect(document.body.textContent).toContain("Fanout shows this once"); await act(async () => root.unmount()); }); @@ -176,7 +176,7 @@ describe("connect telemetry", () => { it("stops offering to issue a token once one has been issued", async () => { fetchMock.mockImplementation(async (_url, init) => json((init as RequestInit | undefined)?.method === "POST" - ? { ...connection, token_required: true, ingest_token: "fo_firsttoken" } + ? { ...connection, token_required: true, ingest_token: `foi_${"B".repeat(43)}` } : { ...connection, token_required: false })); const root = await mount(); await vi.waitFor(() => expect(document.body.textContent).toContain("rejecting telemetry")); @@ -184,15 +184,15 @@ describe("connect telemetry", () => { await act(async () => open?.click()); const confirm = [...document.querySelectorAll('[role="dialog"] button')].find((button) => button.textContent?.trim() === "Issue token"); await act(async () => confirm?.click()); - await vi.waitFor(() => expect(document.body.textContent).toContain("fo_firsttoken")); + await vi.waitFor(() => expect(document.body.textContent).toContain(`foi_${"B".repeat(43)}`)); // The page has changed the fact it describes: leaving the old copy up // invites the admin to rotate away the token they were just handed. expect(document.body.textContent).not.toContain("rejecting telemetry"); const done = [...document.querySelectorAll("button")].find((button) => button.textContent?.trim() === "Done"); await act(async () => done?.click()); - expect(document.body.textContent).not.toContain("fo_firsttoken"); + expect(document.body.textContent).not.toContain(`foi_${"B".repeat(43)}`); // "Shown once" has to be true of the cache as well as the screen. - expect(JSON.stringify(client.getMutationCache().getAll().map((mutation) => mutation.state.data))).not.toContain("fo_firsttoken"); + expect(JSON.stringify(client.getMutationCache().getAll().map((mutation) => mutation.state.data))).not.toContain(`foi_${"B".repeat(43)}`); expect(document.body.textContent).toContain("Rotate when a token may have been exposed"); await act(async () => root.unmount()); });