mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-02 00:48:52 +00:00
chore: update passwap v0.12.1 and align hash validation defaults/errors (#12179)
# Which Problems Are Solved - Upgrading to `zitadel/passwap` v0.12.1 introduced new encoded-hash validation paths that still had review feedback open. - Secret hasher defaults were internally inconsistent (`Hasher.Cost: 4` vs `Limits.Bcrypt.MinCost: 10`), which could reject hashes created by the configured hasher. - New validation error IDs/messages and test coverage needed to be aligned with project conventions and expected behavior branches. # How the Problems Are Solved - Kept the dependency upgrade to `zitadel/passwap` v0.12.1 and completed the validation integration. - Updated `ValidateEncodedHash` error handling in `internal/crypto/passwap.go` to: - use unique random-style error IDs, - return `Errors.Hash.NotSupported` for no-verifier cases, - keep invalid-hash branches mapped to invalid argument errors. - Expanded `TestHasher_ValidateEncodedHash` in `internal/crypto/passwap_test.go` to cover and assert: - bounds error branch, - no-verifier branch, - generic invalid-hash branch, - expected ZITADEL error IDs/messages. - Restored lost inline verifier-context comments for argon2 and md5plain verifier entries. # Additional Changes - Added the missing explanatory `Limits` comment for `SecretHasher` in `cmd/defaults.yaml`. - Corrected `SecretHasher.Limits.Bcrypt.MinCost` from `10` to `4` to match the configured default bcrypt cost and avoid configuration footguns. # Additional Context - Follow-up for PR review feedback in https://github.com/zitadel/zitadel/pull/12179#pullrequestreview-4313121965 --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
@@ -4,10 +4,12 @@ import (
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/zitadel/passwap"
|
||||
"github.com/zitadel/passwap/argon2"
|
||||
"github.com/zitadel/passwap/bcrypt"
|
||||
"github.com/zitadel/passwap/drupal7"
|
||||
@@ -16,8 +18,25 @@ import (
|
||||
"github.com/zitadel/passwap/pbkdf2"
|
||||
"github.com/zitadel/passwap/scrypt"
|
||||
"github.com/zitadel/passwap/sha2"
|
||||
"github.com/zitadel/passwap/verifier"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type skipHasher struct{}
|
||||
|
||||
func (skipHasher) Hash(string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (skipHasher) Validate(string) (verifier.Result, error) {
|
||||
return verifier.Skip, nil
|
||||
}
|
||||
|
||||
func (skipHasher) Verify(string, string) (verifier.Result, error) {
|
||||
return verifier.Skip, nil
|
||||
}
|
||||
|
||||
func TestPasswordHasher_EncodingSupported(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -460,6 +479,85 @@ func TestPasswordHashConfig_PasswordHasher(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasher_ValidateEncodedHash(t *testing.T) {
|
||||
validateCfg := &HashConfig{
|
||||
Hasher: HasherConfig{
|
||||
Algorithm: HashNameBcrypt,
|
||||
Params: map[string]any{
|
||||
"cost": 12,
|
||||
},
|
||||
},
|
||||
Limits: HashLimitsConfig{
|
||||
Bcrypt: BcryptLimitsConfig{MinCost: 10, MaxCost: 16},
|
||||
},
|
||||
}
|
||||
hasher, err := validateCfg.NewHasher()
|
||||
require.NoError(t, err)
|
||||
|
||||
valid, err := hasher.Hash("password")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, hasher.ValidateEncodedHash(valid))
|
||||
|
||||
lowCostCfg := &HashConfig{
|
||||
Hasher: HasherConfig{
|
||||
Algorithm: HashNameBcrypt,
|
||||
Params: map[string]any{
|
||||
"cost": 4,
|
||||
},
|
||||
},
|
||||
Limits: HashLimitsConfig{
|
||||
Bcrypt: BcryptLimitsConfig{MinCost: 4, MaxCost: 16},
|
||||
},
|
||||
}
|
||||
lowCostHasher, err := lowCostCfg.NewHasher()
|
||||
require.NoError(t, err)
|
||||
outOfBounds, err := lowCostHasher.Hash("password")
|
||||
require.NoError(t, err)
|
||||
noVerifierHasher := &Hasher{Swapper: passwap.NewSwapper(skipHasher{})}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
hasher *Hasher
|
||||
encoded string
|
||||
wantErr error
|
||||
wantParent error
|
||||
}{
|
||||
{
|
||||
name: "bounds error",
|
||||
hasher: hasher,
|
||||
encoded: outOfBounds,
|
||||
wantErr: zerrors.ThrowInvalidArgument(nil, "CRYPT-5uV9n", "Errors.User.Password.Invalid"),
|
||||
wantParent: &verifier.BoundsError{},
|
||||
},
|
||||
{
|
||||
name: "no verifier",
|
||||
hasher: noVerifierHasher,
|
||||
encoded: "$unknown$hash",
|
||||
wantErr: zerrors.ThrowInvalidArgument(nil, "CRYPT-2xK7d", "Errors.Hash.NotSupported"),
|
||||
wantParent: passwap.ErrNoVerifier,
|
||||
},
|
||||
{
|
||||
name: "invalid hash",
|
||||
hasher: hasher,
|
||||
encoded: "not-a-hash",
|
||||
wantErr: zerrors.ThrowInvalidArgument(nil, "CRYPT-r8Qm2", "Errors.User.Password.Invalid"),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.hasher.ValidateEncodedHash(tt.encoded)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, tt.wantErr)
|
||||
if tt.name == "bounds error" {
|
||||
var bounds *verifier.BoundsError
|
||||
assert.True(t, errors.As(err, &bounds))
|
||||
} else if tt.wantParent != nil {
|
||||
assert.ErrorIs(t, err, tt.wantParent)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasherConfig_decodeParams(t *testing.T) {
|
||||
type dst struct {
|
||||
A int
|
||||
@@ -666,7 +764,7 @@ func TestHasherConfig_scryptParams(t *testing.T) {
|
||||
},
|
||||
},
|
||||
want: scrypt.Params{
|
||||
N: 4,
|
||||
LN: 2,
|
||||
R: 8,
|
||||
P: 1,
|
||||
KeyLen: 32,
|
||||
|
||||
Reference in New Issue
Block a user