api tests hash passwords at bcrypt's lowest cost; one genre splitter (#8330, #8331)
release / web (push) Successful in 1m30s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 2m43s
release / android (push) Successful in 4m31s
release / Build signed APK (push) Successful in 5m0s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 13s
release / Verify release artifacts (tag releases only) (push) Skipped
release / web (push) Successful in 1m30s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 2m43s
release / android (push) Successful in 4m31s
release / Build signed APK (push) Successful in 5m0s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 13s
release / Verify release artifacts (tag releases only) (push) Skipped
#8330: every password the api handlers store goes through hashPassword at bcrypt.DefaultCost, most of a second per hash under -race, so the password tests took about 1.4s each, roughly 15s of the package. The cost is now a package variable the tests' TestMain lowers to MinCost, after refusing a production default below bcrypt.DefaultCost. #8331: recommendation and taste each had a splitGenres and subsonic's hasGenre split inline, the same rule three times. internal/genretag.Split is the one copy, a stdlib-only leaf, and the two TestSplitGenres tables are one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// The tests hash passwords at bcrypt's lowest cost: what they check is which
|
||||
// password is stored and who is signed out, not how slow a guess is. The
|
||||
// cost the server uses is checked first, so lowering it here can't hide a
|
||||
// weaker one there.
|
||||
func TestMain(m *testing.M) {
|
||||
if passwordCost < bcrypt.DefaultCost {
|
||||
fmt.Fprintf(os.Stderr, "passwordCost is %d; stored passwords need at least bcrypt.DefaultCost (%d)\n",
|
||||
passwordCost, bcrypt.DefaultCost)
|
||||
os.Exit(1)
|
||||
}
|
||||
passwordCost = bcrypt.MinCost
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
@@ -43,9 +43,14 @@ func checkNewPassword(w http.ResponseWriter, pw string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// passwordCost is bcrypt's work factor for stored passwords. The package's
|
||||
// tests lower it, since each hash at the default cost takes most of a second
|
||||
// under the race detector; their TestMain refuses a default below bcrypt's.
|
||||
var passwordCost = bcrypt.DefaultCost
|
||||
|
||||
// hashPassword is the one place a password is hashed for storage.
|
||||
func hashPassword(pw string) ([]byte, error) {
|
||||
return bcrypt.GenerateFromPassword([]byte(pw), bcrypt.DefaultCost)
|
||||
return bcrypt.GenerateFromPassword([]byte(pw), passwordCost)
|
||||
}
|
||||
|
||||
// requireURLUUID parses the chi URL parameter named key as a UUID. On
|
||||
|
||||
Reference in New Issue
Block a user