package config import ( "errors" "fmt" "strings" "time" ) const ( // defaultVerificationMailMinInterval is the shortest time between two // verification messages to one address, counting the registration and // every resend. defaultVerificationMailMinInterval = time.Minute // minVerificationMailMinInterval and maxVerificationMailMinInterval bound // the setting: a shorter interval is no brake, a longer one is a // lock-out that a person who mistyped cannot wait out. minVerificationMailMinInterval = time.Second maxVerificationMailMinInterval = time.Hour // defaultVerificationMailDailyLimit is the number of verification // messages to one address per 24 hours. defaultVerificationMailDailyLimit = 5 maxVerificationMailDailyLimit = 50 // defaultIdentityClientHourlyLimit is the number of registration, // confirmation, resend, and login attempts per client network address per // hour. It is a brake on one source reaching many addresses; the per-address // limits above protect one address from many sources. defaultIdentityClientHourlyLimit = 30 maxIdentityClientHourlyLimit = 10000 // Login limits count attempts against both the client and normalized // address buckets, regardless of whether the address has an account. defaultIdentityLoginEmailHourlyLimit = 10 maxIdentityLoginEmailHourlyLimit = 10000 defaultIdentitySessionIdleTimeout = 24 * time.Hour defaultIdentitySessionAbsoluteLifetime = 7 * 24 * time.Hour minIdentitySessionTimeout = time.Minute maxIdentitySessionIdleTimeout = 30 * 24 * time.Hour maxIdentitySessionAbsoluteLifetime = 365 * 24 * time.Hour // MinIdentityKeySecretBytes is the shortest accepted IDENTITY_KEY_SECRET. MinIdentityKeySecretBytes = 32 // examplePlaceholderPrefix starts the placeholder values of the example // environment file; a secret that keeps it is public. examplePlaceholderPrefix = "replace-this" ) // loadIdentity reads the identity settings. IDENTITY_KEY_SECRET is // required: without it ValidateIdentity refuses the configuration, unless // IDENTITY_ALLOW_EPHEMERAL_KEY is true, which lets a development stand draw a // random secret for the life of the process, so idempotency records and rate // limit buckets change and session CSRF tokens differ after a restart. // // Limit and timeout settings treat an empty value as unset, as the mail quota // does: Compose passes an unset variable as an empty string. func (config *Config) loadIdentity(lookup LookupEnv) error { config.IdentityKeySecret = strings.TrimSpace(valueOrDefault(lookup, "IDENTITY_KEY_SECRET", "")) var err error config.IdentityAllowEphemeralKey, err = boolOrDefault(lookup, "IDENTITY_ALLOW_EPHEMERAL_KEY", false) if err != nil { return err } setOnly := func(key string) (string, bool) { value, ok := lookup(key) if strings.TrimSpace(value) == "" { return "", false } return value, ok } config.VerificationMailMinInterval, err = durationOrDefault(setOnly, "VERIFICATION_MAIL_MIN_INTERVAL", defaultVerificationMailMinInterval) if err != nil { return err } config.VerificationMailDailyLimit, err = intOrDefault(setOnly, "VERIFICATION_MAIL_DAILY_LIMIT", defaultVerificationMailDailyLimit) if err != nil { return err } if config.IdentityClientHourlyLimit, err = intOrDefault(setOnly, "IDENTITY_CLIENT_HOURLY_LIMIT", defaultIdentityClientHourlyLimit); err != nil { return err } if config.IdentityLoginEmailHourlyLimit, err = intOrDefault(setOnly, "IDENTITY_LOGIN_EMAIL_HOURLY_LIMIT", defaultIdentityLoginEmailHourlyLimit); err != nil { return err } if config.IdentitySessionIdleTimeout, err = durationOrDefault(setOnly, "IDENTITY_SESSION_IDLE_TIMEOUT", defaultIdentitySessionIdleTimeout); err != nil { return err } config.IdentitySessionAbsoluteLifetime, err = durationOrDefault(setOnly, "IDENTITY_SESSION_ABSOLUTE_LIFETIME", defaultIdentitySessionAbsoluteLifetime) return err } // ValidateIdentity checks the registration settings. Errors never include // the secret. func (config Config) ValidateIdentity() error { if err := config.validateIdentitySecret(); err != nil { return err } if err := config.validateVerificationMailSettings(); err != nil { return err } if err := config.validateIdentityLimits(); err != nil { return err } return config.validateSessionTimeouts() } func (config Config) validateIdentitySecret() error { if config.IdentityKeySecret == "" && !config.IdentityAllowEphemeralKey { return errors.New("Config.ValidateIdentity - IDENTITY_KEY_SECRET is required (generate one with: openssl rand -base64 48); " + "set IDENTITY_ALLOW_EPHEMERAL_KEY=true only on a development stand") } if strings.HasPrefix(config.IdentityKeySecret, examplePlaceholderPrefix) { return errors.New("Config.ValidateIdentity - IDENTITY_KEY_SECRET is an example placeholder; generate a real one with: openssl rand -base64 48") } if config.IdentityKeySecret != "" && len(config.IdentityKeySecret) < MinIdentityKeySecretBytes { return fmt.Errorf("Config.ValidateIdentity - IDENTITY_KEY_SECRET must have at least %d bytes", MinIdentityKeySecretBytes) } return nil } func (config Config) validateVerificationMailSettings() error { if config.VerificationMailMinInterval < minVerificationMailMinInterval || config.VerificationMailMinInterval > maxVerificationMailMinInterval { return fmt.Errorf("Config.ValidateIdentity - VERIFICATION_MAIL_MIN_INTERVAL must be between %s and %s", minVerificationMailMinInterval, maxVerificationMailMinInterval) } if config.VerificationMailDailyLimit < 1 || config.VerificationMailDailyLimit > maxVerificationMailDailyLimit { return fmt.Errorf("Config.ValidateIdentity - VERIFICATION_MAIL_DAILY_LIMIT must be between 1 and %d", maxVerificationMailDailyLimit) } if config.VerificationMailMinInterval >= 24*time.Hour { return errors.New("Config.ValidateIdentity - VERIFICATION_MAIL_MIN_INTERVAL must be shorter than the daily window") } return nil } func (config Config) validateIdentityLimits() error { if config.IdentityClientHourlyLimit < 1 || config.IdentityClientHourlyLimit > maxIdentityClientHourlyLimit { return fmt.Errorf("Config.ValidateIdentity - IDENTITY_CLIENT_HOURLY_LIMIT must be between 1 and %d", maxIdentityClientHourlyLimit) } if config.IdentityLoginEmailHourlyLimit < 1 || config.IdentityLoginEmailHourlyLimit > maxIdentityLoginEmailHourlyLimit { return fmt.Errorf("Config.ValidateIdentity - IDENTITY_LOGIN_EMAIL_HOURLY_LIMIT must be between 1 and %d", maxIdentityLoginEmailHourlyLimit) } return nil } func (config Config) validateSessionTimeouts() error { if config.IdentitySessionIdleTimeout < minIdentitySessionTimeout || config.IdentitySessionIdleTimeout > maxIdentitySessionIdleTimeout { return fmt.Errorf("Config.ValidateIdentity - IDENTITY_SESSION_IDLE_TIMEOUT must be between %s and %s", minIdentitySessionTimeout, maxIdentitySessionIdleTimeout) } if config.IdentitySessionAbsoluteLifetime < minIdentitySessionTimeout || config.IdentitySessionAbsoluteLifetime > maxIdentitySessionAbsoluteLifetime { return fmt.Errorf("Config.ValidateIdentity - IDENTITY_SESSION_ABSOLUTE_LIFETIME must be between %s and %s", minIdentitySessionTimeout, maxIdentitySessionAbsoluteLifetime) } if config.IdentitySessionIdleTimeout > config.IdentitySessionAbsoluteLifetime { return errors.New("Config.ValidateIdentity - IDENTITY_SESSION_IDLE_TIMEOUT must not exceed IDENTITY_SESSION_ABSOLUTE_LIFETIME") } return nil }