mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-21 15:09:21 +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 邮箱注册的两条建号路径(同样发放注册赠额)纳入同一查重口径;邮箱换绑/绑定 不纳入,否则用户把邮箱改成自己收件箱的别名会被误拒。
101 lines
3.5 KiB
Go
101 lines
3.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func seedUserForAliasTest(t *testing.T, repo *userRepository, email string) {
|
|
t.Helper()
|
|
require.NoError(t, repo.Create(context.Background(), &service.User{
|
|
Email: email,
|
|
Username: email,
|
|
PasswordHash: "hash",
|
|
Role: service.RoleUser,
|
|
Status: service.StatusActive,
|
|
}))
|
|
}
|
|
|
|
func TestUserRepositoryExistsByEmailAlias(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
stored string
|
|
probe string
|
|
want bool
|
|
}{
|
|
{"same address", "someone@gmail.com", "someone@gmail.com", true},
|
|
{"gmail plus alias", "someone@gmail.com", "someone+bulk294@gmail.com", true},
|
|
{"gmail dot trick", "d.axis.2026@gmail.com", "daxis2026@gmail.com", true},
|
|
{"gmail dot trick both sides", "d.axis.2026@gmail.com", "da.xis.2026@gmail.com", true},
|
|
{"stored plus alias found by canonical form", "someone+tag@gmail.com", "someone@gmail.com", true},
|
|
{"googlemail is a gmail alias", "someone@googlemail.com", "some.one@gmail.com", true},
|
|
{"fqdn root dot on probe", "d.axis.2026@gmail.com", "da.xis.2026@gmail.com.", true},
|
|
{"fqdn root dot on stored row", "d.axis.2026@gmail.com.", "daxis2026@gmail.com", true},
|
|
{"legacy row with spacing and case", " D.Axis.2026@Gmail.com ", "daxis2026@gmail.com", true},
|
|
{"non-gmail plus alias", "first.last@qq.com", "first.last+tag@qq.com", true},
|
|
{"different gmail inbox", "someone@gmail.com", "someoneelse@gmail.com", false},
|
|
{"non-gmail dots are significant", "first.last@qq.com", "firstlast@qq.com", false},
|
|
{"different domain", "someone@gmail.com", "someone@qq.com", false},
|
|
{"distinct plus-prefixed locals", "+alice@gmail.com", "+bob@gmail.com", false},
|
|
{"underscore is not a wildcard", "user_x@qq.com", "userax@qq.com", false},
|
|
{"percent is not a wildcard", "a%b@qq.com", "axxb@qq.com", false},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
repo, _ := newUserEntRepo(t)
|
|
seedUserForAliasTest(t, repo, tc.stored)
|
|
|
|
got, err := repo.ExistsByEmailAlias(context.Background(), tc.probe)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUserRepositoryExistsByEmailAliasIgnoresMalformedInput(t *testing.T) {
|
|
repo, _ := newUserEntRepo(t)
|
|
seedUserForAliasTest(t, repo, "someone@gmail.com")
|
|
|
|
got, err := repo.ExistsByEmailAlias(context.Background(), "not-an-email")
|
|
require.NoError(t, err)
|
|
require.False(t, got)
|
|
}
|
|
|
|
func TestUserRepositoryCreateWithEmailAliasGuard(t *testing.T) {
|
|
repo, _ := newUserEntRepo(t)
|
|
ctx := context.Background()
|
|
seedUserForAliasTest(t, repo, "d.axis.2026@gmail.com")
|
|
|
|
// 注册路径:别名变体在唯一性锁内被拒绝。
|
|
err := repo.CreateWithEmailAliasGuard(ctx, &service.User{
|
|
Email: "da.xis.2026+free@googlemail.com",
|
|
Username: "alias-variant",
|
|
PasswordHash: "hash",
|
|
Role: service.RoleUser,
|
|
Status: service.StatusActive,
|
|
})
|
|
require.ErrorIs(t, err, service.ErrEmailExists)
|
|
|
|
// 不同收件箱仍可注册。
|
|
require.NoError(t, repo.CreateWithEmailAliasGuard(ctx, &service.User{
|
|
Email: "other.person@gmail.com",
|
|
Username: "other-person",
|
|
PasswordHash: "hash",
|
|
Role: service.RoleUser,
|
|
Status: service.StatusActive,
|
|
}))
|
|
|
|
// 管理员建号(Create)不受别名限制。
|
|
require.NoError(t, repo.Create(ctx, &service.User{
|
|
Email: "daxis2026+support@gmail.com",
|
|
Username: "admin-created",
|
|
PasswordHash: "hash",
|
|
Role: service.RoleUser,
|
|
Status: service.StatusActive,
|
|
}))
|
|
}
|