Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: ignore password length for demos

Matthew Kilgore049fd4c1ab5a
diff --git a/backend/app/api/demo.go b/backend/app/api/demo.go
index 00086b24..679dc03b 100644
--- a/backend/app/api/demo.go
+++ b/backend/app/api/demo.go
@@ -15,14 +15,12 @@ import (
const (
// demoPasswordEnv is the env var operators set when running demo mode in
// production, overriding the hardcoded development default below.
+ // When set, the value is accepted verbatim regardless of length —
+ // PasswordMinLength is bypassed for demo seeding (see SetupDemo).
demoPasswordEnv = "HBOX_DEMO_PASSWORD"
- // demoPasswordDefault is the password used when demo mode runs outside
- // production. Public knowledge — never used unless cfg.Mode is development.
- // Must satisfy services.PasswordMinLength or registration will reject it.
+ // demoPasswordDefault is the password used when demoPasswordEnv is unset.
+ // Public knowledge — only safe to leave at the default in development.
demoPasswordDefault = "demodemo"
- // demoPasswordMinLength is the minimum acceptable length for a demo
- // password supplied via demoPasswordEnv in production mode.
- demoPasswordMinLength = 12
)
func (a *app) SetupDemo() error {
@@ -55,9 +53,10 @@ func (a *app) SetupDemo() error {
return nil
}
- // Otherwise, register the demo user
+ // Otherwise, register the demo user. Skip PasswordMinLength so operators
+ // can use any HBOX_DEMO_PASSWORD; public registration still enforces it.
log.Debug().Msg("Registering demo user")
- _, err := a.services.User.RegisterUser(ctx, registration)
+ _, err := a.services.User.RegisterUser(ctx, registration, services.SkipPasswordValidation())
if err != nil {
if ent.IsConstraintError(err) {
// Concurrent creation race: treat as exists and skip
diff --git a/backend/app/api/main.go b/backend/app/api/main.go
index 30838a94..d3e0b8ca 100644
--- a/backend/app/api/main.go
+++ b/backend/app/api/main.go
@@ -122,19 +122,6 @@ func run(cfg *config.Config) error {
}
hasher.SetAPIKeyPepper([]byte(cfg.Auth.APIKeyPepper))
- // Demo mode seeds a known user ([email protected]). In production mode the
- // hardcoded "demo" password would be publicly guessable, so require an
- // explicit non-trivial HBOX_DEMO_PASSWORD before letting the app start.
- if cfg.Demo && cfg.Mode == config.ModeProduction {
- if len(os.Getenv(demoPasswordEnv)) < demoPasswordMinLength {
- return fmt.Errorf(
- "refusing to start: demo mode enabled in production but %s is unset or shorter than %d characters. "+
- "Set %s to a strong password or disable demo mode (HBOX_DEMO=false)",
- demoPasswordEnv, demoPasswordMinLength, demoPasswordEnv,
- )
- }
- }
-
// =========================================================================
// Initialize OpenTelemetry
otelProvider, err := otel.NewProvider(context.Background(), &cfg.Otel, version)
diff --git a/backend/internal/core/services/service_user.go b/backend/internal/core/services/service_user.go
index 257994b3..ef22dc24 100644
--- a/backend/internal/core/services/service_user.go
+++ b/backend/internal/core/services/service_user.go
@@ -56,9 +56,28 @@ type (
}
)
+// RegisterOption customizes RegisterUser behavior.
+type RegisterOption func(*registerOptions)
+
+type registerOptions struct {
+ skipPasswordValidation bool
+}
+
+// SkipPasswordValidation bypasses the PasswordMinLength floor for this
+// registration. Reserved for demo seeding, where the operator-supplied
+// HBOX_DEMO_PASSWORD must be accepted as-is regardless of length.
+func SkipPasswordValidation() RegisterOption {
+ return func(o *registerOptions) { o.skipPasswordValidation = true }
+}
+
// RegisterUser creates a new user and group in the data with the provided data. It also bootstraps the user's group
// with default Tags and Locations.
-func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration) (repo.UserOut, error) {
+func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration, opts ...RegisterOption) (repo.UserOut, error) {
+ options := registerOptions{}
+ for _, o := range opts {
+ o(&options)
+ }
+
ctx, span := entityServiceTracer().Start(ctx, "service.UserService.RegisterUser",
trace.WithAttributes(
attribute.Int("user.name.length", len(data.Name)),
@@ -70,7 +89,7 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)
// Reject too-short (and empty) passwords before any DB work. The reset and
// change-password flows enforce the same floor; registration was the gap.
- if len(data.Password) < PasswordMinLength {
+ if !options.skipPasswordValidation && len(data.Password) < PasswordMinLength {
span.SetAttributes(attribute.String("registration.outcome", "password_too_short"))
return repo.UserOut{}, ErrorPasswordTooShort
}