From 96e56989d0b49fbbf967da7ee46815e81e82d11e Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Thu, 2 Feb 2023 13:53:19 +0100 Subject: [PATCH 1/7] Provide the ability to set password hash algorithm parameters (#22942) Backport #22942 This PR refactors and improves the password hashing code within gitea and makes it possible for server administrators to set the password hashing parameters In addition it takes the opportunity to adjust the settings for `pbkdf2` in order to make the hashing a little stronger. The majority of this work was inspired by PR #14751 and I would like to thank @boppy for their work on this. Thanks to @gusted for the suggestion to adjust the `pbkdf2` hashing parameters. Close #14751 Signed-off-by: Andrew Thornton --- cmd/admin_user_change_password.go | 2 +- .../doc/advanced/config-cheat-sheet.en-us.md | 16 +- models/user/user.go | 75 +------ models/user/user_test.go | 3 +- modules/auth/password/hash/argon2.go | 76 +++++++ modules/auth/password/hash/bcrypt.go | 51 +++++ modules/auth/password/hash/common.go | 28 +++ modules/auth/password/hash/hash.go | 147 ++++++++++++++ modules/auth/password/hash/hash_test.go | 186 ++++++++++++++++++ modules/auth/password/hash/pbkdf2.go | 61 ++++++ modules/auth/password/hash/scrypt.go | 63 ++++++ modules/auth/password/hash/setting.go | 40 ++++ modules/auth/password/hash/setting_test.go | 38 ++++ modules/{ => auth}/password/password.go | 8 +- modules/{ => auth}/password/password_test.go | 0 modules/{ => auth}/password/pwn.go | 0 modules/setting/setting.go | 10 +- routers/api/v1/admin/user.go | 2 +- routers/install/install.go | 3 +- routers/web/admin/users.go | 2 +- routers/web/auth/auth.go | 2 +- routers/web/auth/password.go | 2 +- routers/web/user/setting/account.go | 2 +- 23 files changed, 732 insertions(+), 85 deletions(-) create mode 100644 modules/auth/password/hash/argon2.go create mode 100644 modules/auth/password/hash/bcrypt.go create mode 100644 modules/auth/password/hash/common.go create mode 100644 modules/auth/password/hash/hash.go create mode 100644 modules/auth/password/hash/hash_test.go create mode 100644 modules/auth/password/hash/pbkdf2.go create mode 100644 modules/auth/password/hash/scrypt.go create mode 100644 modules/auth/password/hash/setting.go create mode 100644 modules/auth/password/hash/setting_test.go rename modules/{ => auth}/password/password.go (93%) rename modules/{ => auth}/password/password_test.go (100%) rename modules/{ => auth}/password/pwn.go (100%) diff --git a/cmd/admin_user_change_password.go b/cmd/admin_user_change_password.go index 1b7c73370d43..7866bde9128b 100644 --- a/cmd/admin_user_change_password.go +++ b/cmd/admin_user_change_password.go @@ -9,7 +9,7 @@ import ( "fmt" user_model "code.gitea.io/gitea/models/user" - pwd "code.gitea.io/gitea/modules/password" + pwd "code.gitea.io/gitea/modules/auth/password" "code.gitea.io/gitea/modules/setting" "github.com/urfave/cli" diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index bb1ae7b4ab90..6a1854abab0f 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -523,7 +523,21 @@ Certain queues have defaults that override the defaults set in `[queue]` (this o - `IMPORT_LOCAL_PATHS`: **false**: Set to `false` to prevent all users (including admin) from importing local path on server. - `INTERNAL_TOKEN`: **\**: Secret used to validate communication within Gitea binary. - `INTERNAL_TOKEN_URI`: ****: Instead of defining INTERNAL_TOKEN in the configuration, this configuration option can be used to give Gitea a path to a file that contains the internal token (example value: `file:/etc/gitea/internal_token`) -- `PASSWORD_HASH_ALGO`: **pbkdf2**: The hash algorithm to use \[argon2, pbkdf2, scrypt, bcrypt\], argon2 will spend more memory than others. +- `PASSWORD_HASH_ALGO`: **pbkdf2**: The hash algorithm to use \[argon2, pbkdf2, pbkdf2_v1, scrypt, bcrypt\], argon2 and scrypt will spend significant amounts of memory. + - Note: The default parameters for `pbkdf2` hashing have changed - the previous settings are available as `pbkdf2_v1` but are not recommended. + - The hash functions may be tuned by using `$` after the algorithm: + - `argon2$