保存认证容量保护整改检查点待远程复审

This commit is contained in:
xuans committed 2026-09-17 12:00:45 +08:00
1 parent e49e392fd1
commit e30f21849f
27 files changed
+721 -53

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
package password
import "errors"
var ErrBusy = errors.New("password computation is busy")
// 所有协议和恢复流程共享一个计算名额;满载立即拒绝,不积压等待请求。
var computationSlot = make(chan struct{}, 1)
func acquireComputation() (func(), error) {
select {
case computationSlot <- struct{}{}:
return func() { <-computationSlot }, nil
default:
return nil, ErrBusy
}
}
+105
View File
@@ -0,0 +1,105 @@
package password_test
import (
"errors"
"strings"
"sync"
"sync/atomic"
"testing"
"git.pcux.cn/xuans/Smail/internal/passwordtest"
"git.pcux.cn/xuans/Smail/utils/password"
)
func TestCapacityRejectsBurstAndReleasesAfterPanic(t *testing.T) {
encoded, err := password.Hash("StrongPass12")
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(encoded, "$argon2id$v=19$m=65536,t=3,p=1$") {
t.Fatal("密码算法强度发生变化")
}
release := passwordtest.Hold(t)
var wg sync.WaitGroup
for index := 0; index < 64; index++ {
wg.Add(1)
go func() {
defer wg.Done()
if _, err := password.Hash("StrongPass12"); !errors.Is(err, password.ErrBusy) {
t.Errorf("并发哈希未返回繁忙:%v", err)
}
if matched, err := password.Check(encoded, "StrongPass12"); matched || !errors.Is(err, password.ErrBusy) {
t.Errorf("并发校验未返回繁忙:%v %v", matched, err)
}
if err := password.CheckDummy("StrongPass12"); !errors.Is(err, password.ErrBusy) {
t.Errorf("模拟校验绕过计算名额:%v", err)
}
}()
}
wg.Wait()
release()
if matched, err := password.Check(encoded, "StrongPass12"); !matched || err != nil {
t.Fatalf("异常释放后不能继续认证:%v %v", matched, err)
}
if matched, err := password.Check(encoded, "WrongPass12"); matched || err != nil {
t.Fatalf("密码错误与服务繁忙未区分:%v %v", matched, err)
}
}
func TestCapacityValidationDoesNotLeakSlot(t *testing.T) {
if _, err := password.Hash(""); !errors.Is(err, password.ErrInvalidLength) {
t.Fatal(err)
}
if matched, err := password.Check(strings.Repeat("x", 256), "StrongPass12"); matched || err != nil {
t.Fatal("非法哈希应被直接拒绝")
}
if err := password.CheckDummy("StrongPass12"); err != nil {
t.Fatal(err)
}
if _, err := password.Hash("StrongPass12"); err != nil {
t.Fatal(err)
}
}
func TestCapacityRealConcurrentComputations(t *testing.T) {
encoded, err := password.Hash("StrongPass12")
if err != nil {
t.Fatal(err)
}
var succeeded, rejected atomic.Int32
var wg sync.WaitGroup
start := make(chan struct{})
for index := 0; index < 64; index++ {
wg.Add(1)
go func(mode int) {
defer wg.Done()
<-start
var err error
switch mode % 3 {
case 0:
_, err = password.Hash("StrongPass12")
case 1:
var matched bool
matched, err = password.Check(encoded, "StrongPass12")
if err == nil && !matched {
t.Error("真实并发校验密码不匹配")
}
default:
err = password.CheckDummy("StrongPass12")
}
if errors.Is(err, password.ErrBusy) {
rejected.Add(1)
} else if err == nil {
succeeded.Add(1)
} else {
t.Errorf("真实并发计算异常:%v", err)
}
}(index)
}
close(start)
wg.Wait()
if succeeded.Load() == 0 || rejected.Load() == 0 || succeeded.Load()+rejected.Load() != 64 {
t.Fatalf("并发保护未生效:成功=%d,繁忙=%d", succeeded.Load(), rejected.Load())
}
t.Logf("64 路真实计算:成功=%d,繁忙=%d", succeeded.Load(), rejected.Load())
}
+37 -6
View File
@@ -43,6 +43,11 @@ func Hash(plain string) (string, error) {
if plainSize < minSecretSize {
return "", ErrInvalidLength
}
release, err := acquireComputation()
if err != nil {
return "", err
}
defer release()
salt := make([]byte, saltLength)
if _, err := rand.Read(salt); err != nil {
return "", fmt.Errorf("generate password salt: %w", err)
@@ -82,28 +87,54 @@ func HashUserPassword(plain string) (string, error) {
// Verify 校验 Argon2id 密码哈希,不接受旧算法格式。
func Verify(encoded, plain string) bool {
matched, _ := Check(encoded, plain)
return matched
}
// Check 区分密码不匹配与计算资源繁忙,业务入口必须保留错误信息。
func Check(encoded, plain string) (bool, error) {
if len([]byte(plain)) < minSecretSize {
return false
return false, nil
}
if len(encoded) > maxHashSize {
return false, nil
}
parts := strings.Split(encoded, "$")
if len(parts) != 6 || parts[1] != format || parts[2] != fmt.Sprintf("v=%d", version) || parts[3] != fmt.Sprintf("m=%d,t=%d,p=%d", memory, iterations, parallelism) {
return false
return false, nil
}
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
if err != nil || len(salt) != saltLength {
return false
return false, nil
}
expected, err := base64.RawStdEncoding.DecodeString(parts[5])
if err != nil || len(expected) != keyLength {
return false
return false, nil
}
release, err := acquireComputation()
if err != nil {
return false, err
}
defer release()
actual := argon2.IDKey([]byte(plain), salt, iterations, memory, parallelism, keyLength)
return subtle.ConstantTimeCompare(actual, expected) == 1
return subtle.ConstantTimeCompare(actual, expected) == 1, nil
}
func VerifyDummy(plain string) bool {
_ = CheckDummy(plain)
return false
}
// CheckDummy 为不存在的凭据执行同等强度计算,并传递过载状态。
func CheckDummy(plain string) error {
release, err := acquireComputation()
if err != nil {
return err
}
defer release()
salt := [saltLength]byte{0x53, 0x6d, 0x61, 0x69, 0x6c, 0x2d, 0x61, 0x75, 0x74, 0x68, 0x2d, 0x64, 0x75, 0x6d, 0x6d, 0x79}
expected := [keyLength]byte{0x91, 0xa4, 0x65, 0x82, 0x31, 0x07, 0x44, 0xc8, 0x7a, 0x22, 0xe1, 0x59, 0x3c, 0x5b, 0x8d, 0xf1, 0xd4, 0x3a, 0x68, 0x9c, 0xee, 0x20, 0x57, 0x61, 0x45, 0x3a, 0x71, 0x10, 0x5b, 0x26, 0xb6, 0xa0}
actual := argon2.IDKey([]byte(plain), salt[:], iterations, memory, parallelism, keyLength)
return subtle.ConstantTimeCompare(actual, expected[:]) == 1
_ = subtle.ConstantTimeCompare(actual, expected[:])
return nil
}