A continuation of HomeBox the inventory and organization system built for the Home User
fix: hashing
Matthew Kilgore66809ce3583c
diff --git a/backend/internal/core/services/service_user.go b/backend/internal/core/services/service_user.goindex 492e8c10..4b64b5e1 100644--- a/backend/internal/core/services/service_user.go+++ b/backend/internal/core/services/service_user.go@@ -373,10 +373,12 @@ func (svc *UserService) Login(ctx context.Context, username, password string, exattribute.Bool("user.found", false),attribute.String("login.outcome", "user_not_found"),)- // SECURITY: Perform hash to ensure response times are the same+ // SECURITY: Perform a real argon2id comparison against a valid dummy hash so+ // response timing matches the "user found" path and accounts cannot be+ // enumerated by measuring how long login takes._, dummySpan := entityServiceTracer().Start(ctx, "service.UserService.Login.timingDummy",trace.WithAttributes(attribute.String("reason", "user_not_found")))- hasher.CheckPasswordHashCtx(ctx, "not-a-real-password", "not-a-real-password")+ hasher.CheckDummyPasswordHashCtx(ctx)dummySpan.End()return UserAuthTokenDetail{}, ErrorInvalidLogin}@@ -393,7 +395,7 @@ func (svc *UserService) Login(ctx context.Context, username, password string, exspan.SetAttributes(attribute.String("login.outcome", "blocked_no_password_hash"))_, dummySpan := entityServiceTracer().Start(ctx, "service.UserService.Login.timingDummy",trace.WithAttributes(attribute.String("reason", "no_password_hash")))- hasher.CheckPasswordHashCtx(ctx, "not-a-real-password", "not-a-real-password")+ hasher.CheckDummyPasswordHashCtx(ctx)dummySpan.End()return UserAuthTokenDetail{}, ErrorInvalidLogin}diff --git a/backend/internal/core/services/service_user_login_test.go b/backend/internal/core/services/service_user_login_test.gonew file mode 100644index 00000000..90d52331--- /dev/null+++ b/backend/internal/core/services/service_user_login_test.go@@ -0,0 +1,44 @@+package services++import (+ "context"+ "errors"+ "testing"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++// TestLogin_EndToEnd verifies that after the account-enumeration timing fix,+// legitimate login still works and every rejection path returns the same generic+// error, regardless of whether the account exists.+func TestLogin_EndToEnd(t *testing.T) {+ ctx := context.Background()++ const password = "correct-horse-battery-staple"+ reg := UserRegistration{+ Name: fk.Str(8),+ Email: fk.Email(),+ Password: password,+ }+ _, err := tSvc.User.RegisterUser(ctx, reg)+ require.NoError(t, err)++ t.Run("valid credentials succeed", func(t *testing.T) {+ tok, err := tSvc.User.Login(ctx, reg.Email, password, false)+ require.NoError(t, err)+ assert.NotEmpty(t, tok.Raw)+ })++ t.Run("wrong password is rejected with generic error", func(t *testing.T) {+ _, err := tSvc.User.Login(ctx, reg.Email, "wrong-password", false)+ require.Error(t, err)+ assert.True(t, errors.Is(err, ErrorInvalidLogin))+ })++ t.Run("nonexistent user is rejected with the same generic error", func(t *testing.T) {+ _, err := tSvc.User.Login(ctx, "[email protected]", "any-password", false)+ require.Error(t, err)+ assert.True(t, errors.Is(err, ErrorInvalidLogin))+ })+}diff --git a/backend/pkgs/hasher/password.go b/backend/pkgs/hasher/password.goindex d2675c3b..990e8d29 100644--- a/backend/pkgs/hasher/password.go+++ b/backend/pkgs/hasher/password.go@@ -9,6 +9,7 @@ import ("fmt""os""strings"+ "sync""go.opentelemetry.io/otel""go.opentelemetry.io/otel/attribute"@@ -133,6 +134,53 @@ func CheckPasswordHash(password, hash string) (bool, bool) {return CheckPasswordHashCtx(context.Background(), password, hash)}+// staticDummyHash is a valid argon2id-encoded hash used as a fallback for timing+// equalization if hashing a placeholder at runtime ever fails. Because decodeHash+// reads the argon2 cost parameters from the encoded hash itself, comparing against+// this runs the full KDF at the same cost as a real login. The plaintext behind it+// is intentionally unknown and irrelevant — it is only ever compared against a+// deliberately-wrong guess.+const staticDummyHash = "$argon2id$v=19$m=65536,t=3,p=2$sD6MJ4qEDD8pJGFFb8Ew7A$v0FY6vLHFJyJUm2cAa8qEYLd1x5WlCQ01B24AJ/Tcxs"++var (+ dummyHashOnce sync.Once+ dummyHash string+)++// timingEqualizationHash returns a valid argon2id hash generated with the active+// params, computed once on first use. Comparing a password against it forces the+// same argon2id work a real login performs.+func timingEqualizationHash() string {+ dummyHashOnce.Do(func() {+ h, err := HashPassword("homebox-timing-equalization-placeholder")+ if err != nil {+ dummyHash = staticDummyHash+ return+ }+ dummyHash = h+ })+ return dummyHash+}++// CheckDummyPasswordHashCtx performs a password comparison against a fixed, valid+// argon2id hash purely to equalize response timing. Callers use it on authentication+// paths that reject *before* a real password comparison (unknown user, user with no+// stored password) so that an attacker cannot distinguish those paths from a real+// wrong-password attempt by measuring response time — the classic account-enumeration+// side channel. The result is intentionally discarded.+//+// This must run the same KDF as the real path, which is why it compares against a+// genuine argon2id hash rather than an arbitrary string (an invalid "hash" would fail+// to decode instantly and perform no cryptographic work, leaving the timing leak open).+func CheckDummyPasswordHashCtx(ctx context.Context) {+ if !enabled {+ // With protection disabled the real path is an instant plaintext compare,+ // so there is no timing to equalize.+ return+ }+ CheckPasswordHashCtx(ctx, "not-a-real-password", timingEqualizationHash())+}+// CheckPasswordHashCtx checks the password and emits spans for each branch (argon2id// success, argon2id mismatch, bcrypt fallback, both-failed). Errors from the argon2// decode path are recorded on the span instead of being silently swallowed — thatdiff --git a/backend/pkgs/hasher/timing_equalization_test.go b/backend/pkgs/hasher/timing_equalization_test.gonew file mode 100644index 00000000..13cfa403--- /dev/null+++ b/backend/pkgs/hasher/timing_equalization_test.go@@ -0,0 +1,38 @@+package hasher++import (+ "context"+ "testing"+)++// TestTimingEqualizationHashIsValidArgon2 guards against regressing the login+// account-enumeration timing side channel. The "user not found" / "no password"+// login paths call CheckDummyPasswordHashCtx to equalize response timing with a+// real login. That only works if the dummy comparison runs the argon2id KDF, which+// requires a *valid* argon2id hash — an arbitrary string would fail to decode+// instantly and perform no cryptographic work, reopening the side channel.+func TestTimingEqualizationHashIsValidArgon2(t *testing.T) {+ h := timingEqualizationHash()+ if h == "" {+ t.Fatal("timing equalization hash is empty")+ }+ if _, _, _, err := decodeHash(h); err != nil {+ t.Fatalf("timing equalization hash must be a decodable argon2id hash, got decode error: %v (hash=%q)", err, h)+ }+}++// TestStaticDummyHashIsValid ensures the compiled-in fallback used when runtime+// hash generation fails is itself a valid argon2id hash.+func TestStaticDummyHashIsValid(t *testing.T) {+ if _, _, _, err := decodeHash(staticDummyHash); err != nil {+ t.Fatalf("staticDummyHash must decode as argon2id, got: %v", err)+ }+}++// TestCheckDummyPasswordHashDoesRealWork sanity-checks that the dummy comparison+// exercises the real comparison path (returns a no-match result rather than+// short-circuiting) when protection is enabled.+func TestCheckDummyPasswordHashDoesRealWork(t *testing.T) {+ // Should not panic and should complete a real argon2id comparison.+ CheckDummyPasswordHashCtx(context.Background())+}