mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-12 06:40:40 +00:00
对 #4814 的审计跟进修复: - 域名尾随点绕过:user@gmail.com. 的域名不在 gmail 家族名单内,点号折叠与 googlemail 归一被整体跳过,别名刷号原样可复现。归一化入口统一去掉 FQDN 根点。 - 误拒合法用户:剥 "+后缀" 缺空串守卫,+alice@ 与 +bob@ 都折叠成 @domain, 该域后续 "+x@" 注册会永久 EMAIL_EXISTS 且无自助恢复。改为仅当 "+" 不在首位时剥离。 - 无界不可索引全表扫描:原实现按 LOWER(email) LIKE '%@domain' 把整域邮箱读进内存, 且挂在公开未鉴权的 send-verify-code 上。改为按去点邮箱 REPLACE(LOWER(TRIM(email)), '.', '') 做等值 + "local+%@domain" 前缀探针并带 LIMIT, 新增同表达式的部分索引(migrations/190)。TRIM 口径与既有精确匹配一致, 历史带首尾空白的行同样命中;LIKE 元字符转义,% 与 _ 不会扩大匹配面。 - 并发竞态:注册改走 CreateWithEmailAliasGuard,在邮箱唯一性锁上追加收件箱身份锁并在 锁内复查,避免同一收件箱的多个别名变体同时通过服务层前置查重。管理员建号仍走 Create,不受别名限制。 - 能力断言静默 fail-open:别名查重方法上提到 UserRepository 端口(编译期强制), 移除可选接口类型断言与静默降级分支。 - OAuth 邮箱注册的两条建号路径(同样发放注册赠额)纳入同一查重口径;邮箱换绑/绑定 不纳入,否则用户把邮箱改成自己收件箱的别名会被误拒。
209 lines
6.2 KiB
Go
209 lines
6.2 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type ensureEmailCall struct {
|
|
userID int64
|
|
email string
|
|
}
|
|
|
|
type replaceEmailCall struct {
|
|
userID int64
|
|
oldEmail string
|
|
newEmail string
|
|
}
|
|
|
|
type emailSyncRepoStub struct {
|
|
user *User
|
|
nextID int64
|
|
updateCalls int
|
|
created []*User
|
|
updated []*User
|
|
ensureCalls []ensureEmailCall
|
|
replaceCalls []replaceEmailCall
|
|
ensureErr error
|
|
replaceErr error
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) CreateWithEmailAliasGuard(ctx context.Context, user *User) error {
|
|
return s.Create(ctx, user)
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) Create(_ context.Context, user *User) error {
|
|
if s.nextID != 0 && user.ID == 0 {
|
|
user.ID = s.nextID
|
|
}
|
|
s.created = append(s.created, user)
|
|
s.user = user
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetByID(_ context.Context, _ int64) (*User, error) {
|
|
if s.user == nil {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
cloned := *s.user
|
|
return &cloned, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetByEmail(_ context.Context, _ string) (*User, error) {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetFirstAdmin(context.Context) (*User, error) {
|
|
return nil, fmt.Errorf("unexpected GetFirstAdmin call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) Update(_ context.Context, user *User) error {
|
|
s.updateCalls++
|
|
s.updated = append(s.updated, user)
|
|
s.user = user
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) Delete(context.Context, int64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) GetUserAvatar(context.Context, int64) (*UserAvatar, error) {
|
|
return nil, fmt.Errorf("unexpected GetUserAvatar call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UpsertUserAvatar(context.Context, int64, UpsertUserAvatarInput) (*UserAvatar, error) {
|
|
return nil, fmt.Errorf("unexpected UpsertUserAvatar call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) DeleteUserAvatar(context.Context, int64) error {
|
|
return fmt.Errorf("unexpected DeleteUserAvatar call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) List(context.Context, pagination.PaginationParams) ([]User, *pagination.PaginationResult, error) {
|
|
return nil, nil, fmt.Errorf("unexpected List call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) ListWithFilters(context.Context, pagination.PaginationParams, UserListFilters) ([]User, *pagination.PaginationResult, error) {
|
|
return nil, nil, fmt.Errorf("unexpected ListWithFilters call")
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetLatestUsedAtByUserIDs(context.Context, []int64) (map[int64]*time.Time, error) {
|
|
return map[int64]*time.Time{}, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) GetLatestUsedAtByUserID(context.Context, int64) (*time.Time, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UpdateUserLastActiveAt(context.Context, int64, time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UpdateBalance(context.Context, int64, float64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) DeductBalance(context.Context, int64, float64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) UpdateConcurrency(context.Context, int64, int) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) ExistsByEmail(context.Context, string) (bool, error) { return false, nil }
|
|
|
|
func (s *emailSyncRepoStub) ExistsByEmailAlias(context.Context, string) (bool, error) {
|
|
return false, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) RemoveGroupFromAllowedGroups(context.Context, int64) (int64, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) BatchSetConcurrency(context.Context, []int64, int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
func (s *emailSyncRepoStub) BatchAddConcurrency(context.Context, []int64, int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
func (s *emailSyncRepoStub) BatchUpdateLimits(context.Context, []int64, *int, *int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) AddGroupToAllowedGroups(context.Context, int64, int64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) RemoveGroupFromUserAllowedGroups(context.Context, int64, int64) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) ListUserAuthIdentities(context.Context, int64) ([]UserAuthIdentityRecord, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) UnbindUserAuthProvider(context.Context, int64, string) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) UpdateTotpSecret(context.Context, int64, *string) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) EnableTotp(context.Context, int64) error { return nil }
|
|
|
|
func (s *emailSyncRepoStub) DisableTotp(context.Context, int64) error { return nil }
|
|
func (s *emailSyncRepoStub) GetByIDIncludeDeleted(ctx context.Context, id int64) (*User, error) {
|
|
return s.GetByID(ctx, id)
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) EnsureEmailAuthIdentity(_ context.Context, userID int64, email string) error {
|
|
s.ensureCalls = append(s.ensureCalls, ensureEmailCall{userID: userID, email: email})
|
|
return s.ensureErr
|
|
}
|
|
|
|
func (s *emailSyncRepoStub) ReplaceEmailAuthIdentity(_ context.Context, userID int64, oldEmail, newEmail string) error {
|
|
s.replaceCalls = append(s.replaceCalls, replaceEmailCall{
|
|
userID: userID,
|
|
oldEmail: oldEmail,
|
|
newEmail: newEmail,
|
|
})
|
|
return s.replaceErr
|
|
}
|
|
|
|
func TestAdminService_CreateUser_DoesNotReturnPartialSuccessFromEmailIdentityResync(t *testing.T) {
|
|
repo := &emailSyncRepoStub{
|
|
nextID: 55,
|
|
ensureErr: fmt.Errorf("unexpected email resync"),
|
|
}
|
|
svc := &adminServiceImpl{userRepo: repo}
|
|
|
|
user, err := svc.CreateUser(context.Background(), &CreateUserInput{
|
|
Email: "admin-created@example.com",
|
|
Password: "strong-pass",
|
|
})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, user)
|
|
require.Equal(t, int64(55), user.ID)
|
|
require.Empty(t, repo.ensureCalls)
|
|
require.Empty(t, repo.replaceCalls)
|
|
}
|
|
|
|
func TestAdminService_UpdateUser_DoesNotReturnPartialSuccessFromEmailIdentityResync(t *testing.T) {
|
|
repo := &emailSyncRepoStub{
|
|
user: &User{
|
|
ID: 91,
|
|
Email: "before@example.com",
|
|
Role: RoleUser,
|
|
Status: StatusActive,
|
|
Concurrency: 3,
|
|
},
|
|
replaceErr: fmt.Errorf("unexpected email resync"),
|
|
}
|
|
svc := &adminServiceImpl{userRepo: repo}
|
|
|
|
updated, err := svc.UpdateUser(context.Background(), 91, &UpdateUserInput{
|
|
Email: "after@example.com",
|
|
})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, updated)
|
|
require.Equal(t, "after@example.com", updated.Email)
|
|
require.Empty(t, repo.replaceCalls)
|
|
require.Empty(t, repo.ensureCalls)
|
|
}
|