fix: 建群事务内复核创建者避免停用竞态留下非法群主
This commit is contained in:
@@ -1767,3 +1767,13 @@ issue #3 未关闭,`feat/fix-3-downlink-deadlock` 未合入 `main`。下面是
|
|||||||
- 原因:原先抛错后工作线程仍按退避重连;Java 限速把在途计数减了两次。
|
- 原因:原先抛错后工作线程仍按退避重连;Java 限速把在途计数减了两次。
|
||||||
- 备选方案:在后台 `_attempt_connect`/`attemptConnect` 内首次失败即停(否决:应用再次 `connect()` 才应恢复,标志应在对外 `connect` 失败路径统一置位)。
|
- 备选方案:在后台 `_attempt_connect`/`attemptConnect` 内首次失败即停(否决:应用再次 `connect()` 才应恢复,标志应在对外 `connect` 失败路径统一置位)。
|
||||||
- 影响:仅 `sdk/python`、`sdk/java`;应用需再次调用 `connect()` 才会重连。
|
- 影响:仅 `sdk/python`、`sdk/java`;应用需再次调用 `connect()` 才会重连。
|
||||||
|
|
||||||
|
### 复审修复 R3-07
|
||||||
|
|
||||||
|
1. **客户端建群事务内复核创建者**
|
||||||
|
- 日期:2026-09-30
|
||||||
|
- 原条款:Gitea #71。`group.Create` 在 `Queue.Do` 里直接 `insertMemberTx` 群主,不跑 `endpointCheckTx`;停用/删除与建群抢在事务外对话密码窗口时,可插入已停用甚至刚删除的 `owner_id`。
|
||||||
|
- 实际做法:`Create` 同一写事务里,插入群与群主成员之前对 `actorID` 做与 `createAdmin` 相同的 `endpointCheckTx`;已停用 `endpoint_disabled`,不存在 `invalid_target`,且不插入群。不改 `emit`、不改迁移编号。
|
||||||
|
- 原因:成员侧已有事务内复核,群主侧缺对称校验,竞态会留下非法群主。
|
||||||
|
- 备选方案:事务外再查一次创建者(否决,无法覆盖密码窗口与写事务之间的竞态)。
|
||||||
|
- 影响:创建者在进入写事务前被停用/删除时建群失败且无新群行;与后台建群错误码对齐。
|
||||||
|
|||||||
@@ -147,6 +147,15 @@ func (a *App) Create(ctx context.Context, actorID string, req *protocol.GroupCre
|
|||||||
|
|
||||||
var inserted []string
|
var inserted []string
|
||||||
err := a.db.Queue.Do(ctx, func(tx *sql.Tx) error {
|
err := a.db.Queue.Do(ctx, func(tx *sql.Tx) error {
|
||||||
|
if e := endpointCheckTx(tx, actorID); e != nil {
|
||||||
|
if protoCode(e) == protocol.CodeInvalidTarget {
|
||||||
|
return errCode(protocol.CodeInvalidTarget, "owner not found")
|
||||||
|
}
|
||||||
|
if protoCode(e) == protocol.CodeEndpointDisabled {
|
||||||
|
return errCode(protocol.CodeEndpointDisabled, "owner disabled")
|
||||||
|
}
|
||||||
|
return e
|
||||||
|
}
|
||||||
var exists int
|
var exists int
|
||||||
qErr := tx.QueryRow(`SELECT 1 FROM groups WHERE id = ?`, gid).Scan(&exists)
|
qErr := tx.QueryRow(`SELECT 1 FROM groups WHERE id = ?`, gid).Scan(&exists)
|
||||||
if qErr == nil {
|
if qErr == nil {
|
||||||
|
|||||||
@@ -604,6 +604,106 @@ func TestU02CreateDedupesMembers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// raceTalk 在对话密码校验成功后执行一次 mutate,模拟事务外密码窗口里创建者被停用/删除。
|
||||||
|
type raceTalk struct {
|
||||||
|
inner group.TalkGate
|
||||||
|
mutate func()
|
||||||
|
once sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *raceTalk) CheckTalkPasswordForJoin(ctx context.Context, actorID, targetID, talkPassword, remoteIP string) error {
|
||||||
|
err := r.inner.CheckTalkPasswordForJoin(ctx, actorID, targetID, talkPassword, remoteIP)
|
||||||
|
if err == nil && r.mutate != nil {
|
||||||
|
r.once.Do(r.mutate)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestR307CreateRejectsActorDisabledOrDeletedBeforeWrite(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
code string
|
||||||
|
mutate func(ctx context.Context, t *testing.T, idApp *identity.App, db *store.DB)
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "disabled",
|
||||||
|
code: protocol.CodeEndpointDisabled,
|
||||||
|
mutate: func(ctx context.Context, t *testing.T, idApp *identity.App, _ *store.DB) {
|
||||||
|
t.Helper()
|
||||||
|
if err := idApp.Disable(ctx, "alice"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "deleted",
|
||||||
|
code: protocol.CodeInvalidTarget,
|
||||||
|
mutate: func(ctx context.Context, t *testing.T, idApp *identity.App, _ *store.DB) {
|
||||||
|
t.Helper()
|
||||||
|
if err := idApp.Delete(ctx, "alice"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
db, err := store.Open(filepath.Join(t.TempDir(), "data"), "FULL")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = db.Close() })
|
||||||
|
fixed := time.UnixMilli(1_700_000_000_000)
|
||||||
|
locks := auth.NewLoginLocks()
|
||||||
|
idApp := identity.New(identity.Config{
|
||||||
|
DB: db, Hash: auth.NewStubHashPool(), Locks: locks,
|
||||||
|
Sessions: auth.NewSessionTokens(),
|
||||||
|
Now: func() time.Time { return fixed },
|
||||||
|
})
|
||||||
|
gate := &raceTalk{inner: idApp}
|
||||||
|
gApp := group.New(group.Config{
|
||||||
|
DB: db, Talk: gate, MaxGroupMembers: 1000,
|
||||||
|
Now: func() time.Time { return fixed }, DefaultRemoteIP: "1.1.1.1",
|
||||||
|
})
|
||||||
|
ctx := context.Background()
|
||||||
|
insertEP(t, db, "alice", 1)
|
||||||
|
insertEP(t, db, "bob", 1)
|
||||||
|
if err := idApp.SelfSetTalkPassword(ctx, "bob", "secret"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
gate.mutate = func() { tc.mutate(ctx, t, idApp, db) }
|
||||||
|
|
||||||
|
var before int
|
||||||
|
if qErr := db.Read.QueryRow(`SELECT COUNT(*) FROM groups`).Scan(&before); qErr != nil {
|
||||||
|
t.Fatal(qErr)
|
||||||
|
}
|
||||||
|
_, err = gApp.Create(ctx, "alice", &protocol.GroupCreate{
|
||||||
|
V: protocol.Version, Type: protocol.TypeGroupCreate, RID: "1",
|
||||||
|
ID: "g_r307_" + tc.name, Name: "竞态群",
|
||||||
|
Members: []protocol.GroupMemberIn{{ID: "bob", TalkPassword: "secret"}},
|
||||||
|
})
|
||||||
|
if protoCode(err) != tc.code {
|
||||||
|
t.Fatalf("want %s got %v", tc.code, err)
|
||||||
|
}
|
||||||
|
var after int
|
||||||
|
if qErr := db.Read.QueryRow(`SELECT COUNT(*) FROM groups`).Scan(&after); qErr != nil {
|
||||||
|
t.Fatal(qErr)
|
||||||
|
}
|
||||||
|
if after != before {
|
||||||
|
t.Fatalf("groups leaked: before=%d after=%d", before, after)
|
||||||
|
}
|
||||||
|
var exists int
|
||||||
|
qErr := db.Read.QueryRow(`SELECT 1 FROM groups WHERE id = ?`, "g_r307_"+tc.name).Scan(&exists)
|
||||||
|
if !errors.Is(qErr, sql.ErrNoRows) {
|
||||||
|
t.Fatalf("expected no group row, got exists=%d err=%v", exists, qErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestU02AdminCreateOwnerMustExistAndEnabled(t *testing.T) {
|
func TestU02AdminCreateOwnerMustExistAndEnabled(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
gApp, _, _, db, down := setup(t)
|
gApp, _, _, db, down := setup(t)
|
||||||
|
|||||||
Reference in New Issue
Block a user