按成员版本与设备名单限制群组密文投递
This commit is contained in:
1 parent
43216c205c
commit
636419e948
12 files changed
+592
-7
No files matched your search
@@ -26,6 +26,11 @@ server {
|
||||
proxy_pass http://api:8080;
|
||||
proxy_set_header Host $http_host;
|
||||
}
|
||||
location ~ ^/api/v1/groups/[0-9a-fA-F-]+/(epochs|messages)$ {
|
||||
client_max_body_size 3m;
|
||||
proxy_pass http://api:8080;
|
||||
proxy_set_header Host $http_host;
|
||||
}
|
||||
|
||||
add_header Content-Security-Policy "default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self'; connect-src 'self'; img-src 'self' blob:; media-src blob:; form-action 'self'; base-uri 'none'; frame-ancestors 'none'" always;
|
||||
add_header Referrer-Policy no-referrer always;
|
||||
|
||||
+5
-1
@@ -44,4 +44,8 @@ MLS 凭据由标准 Base64 的 publicKey 和 signature 组成,使用 UTF-8 前
|
||||
|
||||
群组详情包含十进制字符串 membershipVersion。已接受成员变化会递增版本,待处理邀请和角色变化保留版本。active 成员可以查看其他有效成员,群主与管理员还可查看待处理邀请。群主可解散、调整管理员和移除成员。管理员可改名、邀请好友和移除普通成员。
|
||||
|
||||
下一阶段将增加 /conversations、群组密文投递、/calls、/posts、/backups、/push 与独立的管理接口。
|
||||
群组密文投递使用 `/groups/{id}/epochs` 和 `/groups/{id}/messages`。群主发布带 membershipVersion 的世代,选取每个有效成员的一台已登记 MLS 设备,提交其他设备的 Welcome。GET epochs 只返回当前设备被编入的世代及其 Welcome。POST messages 仅接受当前世代的 MLS PrivateMessage,GET messages 通过 after 游标返回当前设备有权读取的密文。
|
||||
|
||||
成员变更后旧世代不能发消息。绑定设备被撤销后需以替代设备发布新世代。重新加入的成员只能补拉本次加入以来的世代。服务端校验外层格式和投递授权,MLS 内容验证由客户端执行。
|
||||
|
||||
下一阶段将增加 /conversations、/calls、/posts、/backups、/push 与独立的管理接口。
|
||||
@@ -96,3 +96,7 @@ Windows 和 Web 使用 MediaRecorder 录制 Opus,支持取消、计时和最
|
||||
## 群组成员管理
|
||||
|
||||
Web 和 Windows 可创建群组、邀请已接受的联系人、接受或拒绝邀请、管理群名称及管理员、移除成员、退出与解散。未接受邀请的账号无法查看成员名单。成员加入和离开在事务内递增成员版本,后续 MLS 投递将以此拒绝过期名单。群组消息仍待接入。
|
||||
|
||||
## 群组密文投递
|
||||
|
||||
服务端已接入群世代登记、设备定向 Welcome、群密文写入、游标补拉与保留期。集成测试覆盖名单外设备拒绝、成员移除后拒绝旧世代、重新加入后的历史隔离和绑定设备撤销。当前群世代限每账户一台设备,客户端 MLS 群会话仍待接入。
|
||||
@@ -66,6 +66,7 @@ func run() error {
|
||||
cancel()
|
||||
accounts := account.New(db, cfg.SessionTTL)
|
||||
envelopes := delivery.New(db)
|
||||
groupStore := groups.New(db)
|
||||
go func() {
|
||||
ticker := time.NewTicker(15 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
@@ -81,12 +82,15 @@ func run() error {
|
||||
if err := envelopes.Cleanup(cleanup, cfg.EnvelopeRetention); err != nil {
|
||||
slog.Warn("delivery cleanup failed")
|
||||
}
|
||||
if err := groupStore.Cleanup(cleanup, cfg.EnvelopeRetention); err != nil {
|
||||
slog.Warn("group cleanup failed")
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}()
|
||||
server := &http.Server{
|
||||
Addr: cfg.Addr, Handler: httpapi.New(accounts, contacts.New(db), envelopes, groups.New(db), cache, cfg.Origins, ready),
|
||||
Addr: cfg.Addr, Handler: httpapi.New(accounts, contacts.New(db), envelopes, groupStore, cache, cfg.Origins, ready),
|
||||
ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 15 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 16 << 10,
|
||||
BaseContext: func(net.Listener) context.Context { return ctx },
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
ALTER TABLE group_members ADD COLUMN joined_version bigint;
|
||||
UPDATE group_members m SET joined_version=g.membership_version
|
||||
FROM chat_groups g WHERE g.id=m.group_id AND m.state='active';
|
||||
ALTER TABLE group_members ADD CHECK ((state='active') = (joined_version IS NOT NULL));
|
||||
|
||||
CREATE TABLE group_epochs (
|
||||
group_id uuid NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
|
||||
id uuid NOT NULL,
|
||||
membership_version bigint NOT NULL,
|
||||
creator_device uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
retired_at timestamptz,
|
||||
PRIMARY KEY (group_id,id)
|
||||
);
|
||||
CREATE UNIQUE INDEX group_epochs_current ON group_epochs(group_id,membership_version) WHERE retired_at IS NULL;
|
||||
|
||||
CREATE TABLE group_epoch_devices (
|
||||
group_id uuid NOT NULL,
|
||||
epoch_id uuid NOT NULL,
|
||||
device_id uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
welcome bytea CHECK (octet_length(welcome) BETWEEN 16 AND 65536),
|
||||
PRIMARY KEY (group_id,epoch_id,device_id),
|
||||
FOREIGN KEY (group_id,epoch_id) REFERENCES group_epochs(group_id,id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE group_messages (
|
||||
sequence bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
group_id uuid NOT NULL,
|
||||
epoch_id uuid NOT NULL,
|
||||
sender_device uuid NOT NULL,
|
||||
message_id uuid NOT NULL,
|
||||
ciphertext bytea NOT NULL CHECK (octet_length(ciphertext) BETWEEN 16 AND 65536),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
FOREIGN KEY (group_id,epoch_id,sender_device) REFERENCES group_epoch_devices(group_id,epoch_id,device_id) ON DELETE CASCADE,
|
||||
UNIQUE (group_id,epoch_id,sender_device,message_id)
|
||||
);
|
||||
CREATE INDEX group_messages_cursor ON group_messages(group_id,sequence);
|
||||
CREATE INDEX group_messages_expiry ON group_messages(created_at);
|
||||
@@ -28,6 +28,9 @@ var attachmentsMigration string
|
||||
//go:embed 007_groups.sql
|
||||
var groupsMigration string
|
||||
|
||||
//go:embed 008_group_transport.sql
|
||||
var groupTransportMigration string
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -40,7 +43,7 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
if _, err = tx.Exec(ctx, "CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY)"); err != nil {
|
||||
return err
|
||||
}
|
||||
for index, migration := range []string{identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration, attachmentsMigration, groupsMigration} {
|
||||
for index, migration := range []string{identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration, attachmentsMigration, groupsMigration, groupTransportMigration} {
|
||||
version := index + 1
|
||||
var applied bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version=$1)", version).Scan(&applied); err != nil {
|
||||
|
||||
@@ -46,8 +46,8 @@ func (s *Store) Create(ctx context.Context, owner, name string) (Group, error) {
|
||||
result := Group{Name: name, State: "active", Role: "owner", Version: "1"}
|
||||
err := s.db.QueryRow(ctx, `
|
||||
WITH created AS (INSERT INTO chat_groups(name,owner_id) VALUES($2,$1) RETURNING id)
|
||||
INSERT INTO group_members(group_id,user_id,state)
|
||||
SELECT id,$1,'active' FROM created RETURNING group_id
|
||||
INSERT INTO group_members(group_id,user_id,state,joined_version)
|
||||
SELECT id,$1,'active',1 FROM created RETURNING group_id
|
||||
`, owner, name).Scan(&result.ID)
|
||||
return result, err
|
||||
}
|
||||
@@ -124,7 +124,8 @@ func (s *Store) Change(ctx context.Context, user, id string, change Change) erro
|
||||
|
||||
// Serialize membership and version changes on the group row.
|
||||
var owner string
|
||||
err = tx.QueryRow(ctx, "SELECT owner_id FROM chat_groups WHERE id=$1 FOR UPDATE", id).Scan(&owner)
|
||||
var version int64
|
||||
err = tx.QueryRow(ctx, "SELECT owner_id,membership_version FROM chat_groups WHERE id=$1 FOR UPDATE", id).Scan(&owner, &version)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrUnavailable
|
||||
}
|
||||
@@ -147,7 +148,7 @@ func (s *Store) Change(ctx context.Context, user, id string, change Change) erro
|
||||
if state == "invited" {
|
||||
switch change.Action {
|
||||
case "accept":
|
||||
_, err = tx.Exec(ctx, "UPDATE group_members SET state='active' WHERE group_id=$1 AND user_id=$2", id, user)
|
||||
_, err = tx.Exec(ctx, "UPDATE group_members SET state='active',joined_version=$3 WHERE group_id=$1 AND user_id=$2", id, user, version+1)
|
||||
activeChanged = true
|
||||
case "decline":
|
||||
_, err = tx.Exec(ctx, "DELETE FROM group_members WHERE group_id=$1 AND user_id=$2", id, user)
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var ErrVersion = errors.New("group membership or device set changed")
|
||||
|
||||
type DeviceWelcome struct {
|
||||
Device string `json:"device"`
|
||||
Wire []byte `json:"wire,omitempty"`
|
||||
}
|
||||
|
||||
type Epoch struct {
|
||||
ID string `json:"id"`
|
||||
Version string `json:"membershipVersion"`
|
||||
Devices []DeviceWelcome `json:"devices"`
|
||||
}
|
||||
|
||||
type AvailableEpoch struct {
|
||||
ID string `json:"id"`
|
||||
Version string `json:"membershipVersion"`
|
||||
Creator string `json:"creatorDevice"`
|
||||
Welcome []byte `json:"welcome,omitempty"`
|
||||
Current bool `json:"current"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Sequence string `json:"sequence"`
|
||||
Epoch string `json:"epoch"`
|
||||
ID string `json:"id"`
|
||||
Sender string `json:"senderDevice"`
|
||||
Wire []byte `json:"wire"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
func lockTransport(ctx context.Context, tx pgx.Tx, group, user, device string) (string, bool, error) {
|
||||
var version string
|
||||
var owner bool
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT g.membership_version::text,g.owner_id=$2
|
||||
FROM chat_groups g JOIN group_members m ON m.group_id=g.id
|
||||
JOIN devices d ON d.user_id=m.user_id
|
||||
WHERE g.id=$1 AND m.user_id=$2 AND m.state='active' AND d.id=$3 AND d.revoked_at IS NULL
|
||||
FOR UPDATE OF g
|
||||
`, group, user, device).Scan(&version, &owner)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
err = ErrUnavailable
|
||||
}
|
||||
return version, owner, err
|
||||
}
|
||||
|
||||
func (s *Store) PublishEpoch(ctx context.Context, group, user, sender string, epoch Epoch) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
version, owner, err := lockTransport(ctx, tx, group, user, sender)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !owner {
|
||||
return ErrAction
|
||||
}
|
||||
if version != epoch.Version {
|
||||
return ErrVersion
|
||||
}
|
||||
|
||||
// Lock the selected devices so a revocation cannot interleave with enrollment.
|
||||
devices := make([]string, 0, len(epoch.Devices))
|
||||
for _, welcome := range epoch.Devices {
|
||||
devices = append(devices, welcome.Device)
|
||||
}
|
||||
var size, people, active int
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT count(*), count(DISTINCT user_id),
|
||||
(SELECT count(*) FROM group_members WHERE group_id=$1 AND state='active')
|
||||
FROM (
|
||||
SELECT d.user_id FROM devices d JOIN group_members m ON m.user_id=d.user_id
|
||||
JOIN device_mls_credentials c ON c.device_id=d.id
|
||||
WHERE m.group_id=$1 AND m.state='active' AND d.revoked_at IS NULL AND d.id=ANY($2::uuid[])
|
||||
ORDER BY d.id FOR UPDATE OF d
|
||||
) AS locked
|
||||
`, group, devices).Scan(&size, &people, &active)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if size != len(devices) || people != active || people != size {
|
||||
return ErrVersion
|
||||
}
|
||||
|
||||
if _, err = tx.Exec(ctx, `
|
||||
UPDATE group_epochs g SET retired_at=now()
|
||||
WHERE g.group_id=$1 AND g.membership_version=$2::bigint AND g.retired_at IS NULL AND EXISTS (
|
||||
SELECT 1 FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
WHERE e.group_id=g.group_id AND e.epoch_id=g.id AND d.revoked_at IS NOT NULL
|
||||
)
|
||||
`, group, version); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.Exec(ctx, `
|
||||
INSERT INTO group_epochs(group_id,id,membership_version,creator_device) VALUES($1,$2,$3::bigint,$4)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, group, epoch.ID, version, sender)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.RowsAffected() == 0 {
|
||||
return retryEpoch(ctx, tx, group, sender, epoch)
|
||||
}
|
||||
for _, welcome := range epoch.Devices {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO group_epoch_devices VALUES($1,$2,$3,$4)", group, epoch.ID, welcome.Device, welcome.Wire); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func retryEpoch(ctx context.Context, tx pgx.Tx, group, sender string, input Epoch) error {
|
||||
var matches bool
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM group_epochs WHERE group_id=$1 AND id=$2 AND membership_version=$3::bigint AND creator_device=$4 AND retired_at IS NULL
|
||||
) AND (SELECT count(*) FROM group_epoch_devices WHERE group_id=$1 AND epoch_id=$2)=$5
|
||||
`, group, input.ID, input.Version, sender, len(input.Devices)).Scan(&matches)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !matches {
|
||||
return ErrVersion
|
||||
}
|
||||
for _, item := range input.Devices {
|
||||
var wire []byte
|
||||
err = tx.QueryRow(ctx, "SELECT welcome FROM group_epoch_devices WHERE group_id=$1 AND epoch_id=$2 AND device_id=$3", group, input.ID, item.Device).Scan(&wire)
|
||||
if errors.Is(err, pgx.ErrNoRows) || err == nil && !bytes.Equal(wire, item.Wire) {
|
||||
return ErrVersion
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
version, _, err := lockTransport(ctx, tx, group, user, device)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var enrolled bool
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM group_epoch_devices e JOIN group_epochs g
|
||||
ON g.group_id=e.group_id AND g.id=e.epoch_id
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 AND e.device_id=$3 AND g.membership_version=$4::bigint AND g.retired_at IS NULL)
|
||||
`, group, message.Epoch, device, version).Scan(&enrolled)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !enrolled {
|
||||
return "", ErrVersion
|
||||
}
|
||||
|
||||
// Match the device revocation transaction's row lock. Once a bound device is
|
||||
// revoked the generation cannot accept any further application ciphertext.
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT d.revoked_at IS NULL FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 ORDER BY d.id FOR UPDATE OF d
|
||||
`, group, message.Epoch)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for rows.Next() {
|
||||
var active bool
|
||||
if err = rows.Scan(&active); err != nil || !active {
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", ErrVersion
|
||||
}
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var sequence string
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO group_messages(group_id,epoch_id,sender_device,message_id,ciphertext)
|
||||
VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING RETURNING sequence::text
|
||||
`, group, message.Epoch, device, message.ID, message.Wire).Scan(&sequence)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
var previous []byte
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT sequence::text,ciphertext FROM group_messages
|
||||
WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND message_id=$4
|
||||
`, group, message.Epoch, device, message.ID).Scan(&sequence, &previous)
|
||||
if err == nil && !bytes.Equal(previous, message.Wire) {
|
||||
return "", ErrVersion
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sequence, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Inbox(ctx context.Context, group, user, device string, after int64) ([]Message, error) {
|
||||
tx, err := s.db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = readAccess(ctx, tx, group, user, device); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT x.sequence::text,x.epoch_id,x.message_id,x.sender_device,x.ciphertext,x.created_at
|
||||
FROM group_messages x JOIN group_epochs g ON g.group_id=x.group_id AND g.id=x.epoch_id
|
||||
JOIN group_epoch_devices e ON e.group_id=g.group_id AND e.epoch_id=g.id AND e.device_id=$3
|
||||
JOIN group_members m ON m.group_id=x.group_id AND m.user_id=$2 AND m.state='active'
|
||||
JOIN devices d ON d.id=$3 AND d.user_id=m.user_id AND d.revoked_at IS NULL
|
||||
WHERE x.group_id=$1 AND x.sequence>$4 AND g.membership_version>=m.joined_version
|
||||
ORDER BY x.sequence LIMIT 64
|
||||
`, group, user, device, after)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]Message, 0)
|
||||
for rows.Next() {
|
||||
var m Message
|
||||
if err := rows.Scan(&m.Sequence, &m.Epoch, &m.ID, &m.Sender, &m.Wire, &m.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, m)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func readAccess(ctx context.Context, tx pgx.Tx, group, user, device string) error {
|
||||
var ok bool
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM group_members m JOIN devices d ON d.user_id=m.user_id
|
||||
WHERE m.group_id=$1 AND m.user_id=$2 AND m.state='active' AND d.id=$3 AND d.revoked_at IS NULL)
|
||||
`, group, user, device).Scan(&ok)
|
||||
if err == nil && !ok {
|
||||
return ErrUnavailable
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Epochs(ctx context.Context, group, user, device string) ([]AvailableEpoch, error) {
|
||||
tx, err := s.db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = readAccess(ctx, tx, group, user, device); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT e.id,e.membership_version::text,e.creator_device,d.welcome,
|
||||
e.membership_version=g.membership_version AND e.retired_at IS NULL AND NOT EXISTS (
|
||||
SELECT 1 FROM group_epoch_devices x JOIN devices bound ON bound.id=x.device_id
|
||||
WHERE x.group_id=e.group_id AND x.epoch_id=e.id AND bound.revoked_at IS NOT NULL
|
||||
)
|
||||
FROM group_epochs e JOIN chat_groups g ON g.id=e.group_id
|
||||
JOIN group_epoch_devices d ON d.group_id=e.group_id AND d.epoch_id=e.id AND d.device_id=$3
|
||||
JOIN group_members m ON m.group_id=e.group_id AND m.user_id=$2
|
||||
WHERE e.group_id=$1 AND e.membership_version>=m.joined_version
|
||||
ORDER BY e.created_at DESC,e.id LIMIT 200
|
||||
`, group, user, device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]AvailableEpoch, 0)
|
||||
for rows.Next() {
|
||||
var e AvailableEpoch
|
||||
if err = rows.Scan(&e.ID, &e.Version, &e.Creator, &e.Welcome, &e.Current); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, e)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Cleanup(ctx context.Context, retention time.Duration) error {
|
||||
cutoff := time.Now().Add(-retention)
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM group_messages WHERE created_at<$1", cutoff); err != nil {
|
||||
return err
|
||||
}
|
||||
// Keep the current epoch and any old epoch with retained application messages.
|
||||
_, err = tx.Exec(ctx, `
|
||||
DELETE FROM group_epochs e USING chat_groups g
|
||||
WHERE e.group_id=g.id AND (e.membership_version<g.membership_version OR e.retired_at IS NOT NULL) AND e.created_at<$1
|
||||
AND NOT EXISTS(SELECT 1 FROM group_messages m WHERE m.group_id=e.group_id AND m.epoch_id=e.id)
|
||||
`, cutoff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
@@ -64,6 +64,10 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/groups", a.authorized(a.createGroup))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}", a.authorized(a.groupDetail))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}", a.authorized(a.changeGroup))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/epochs", a.authorized(a.groupEpochs))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/epochs", a.authorized(a.publishGroupEpoch))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/messages", a.authorized(a.groupInbox))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/messages", a.authorized(a.sendGroupMessage))
|
||||
return a.boundary(mux)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xhchat/server/internal/account"
|
||||
"xhchat/server/internal/groups"
|
||||
)
|
||||
|
||||
func (a *API) groupEpochs(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
epochs, err := a.groups.Epochs(r.Context(), id, actor.User.ID, actor.Device.ID)
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, epochs)
|
||||
}
|
||||
|
||||
func (a *API) publishGroupEpoch(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-epoch:"+actor.Device.ID, 12) {
|
||||
return
|
||||
}
|
||||
|
||||
var input groups.Epoch
|
||||
if !parseLimit(w, r, &input, 3<<20) {
|
||||
return
|
||||
}
|
||||
version, err := strconv.ParseInt(input.Version, 10, 64)
|
||||
if !uuidPattern.MatchString(input.ID) || err != nil || version < 1 || strconv.FormatInt(version, 10) != input.Version ||
|
||||
len(input.Devices) < 1 || len(input.Devices) > 32 {
|
||||
problem(w, 400, "invalid_group_epoch", "群加密世代信息无效")
|
||||
return
|
||||
}
|
||||
seen := make(map[string]bool, len(input.Devices))
|
||||
for index := range input.Devices {
|
||||
device := &input.Devices[index]
|
||||
device.Device = strings.ToLower(device.Device)
|
||||
if !uuidPattern.MatchString(device.Device) || seen[device.Device] ||
|
||||
(device.Device == actor.Device.ID && len(device.Wire) != 0) ||
|
||||
(device.Device != actor.Device.ID && (!mlsFrame(device.Wire) || device.Wire[3] != 3)) {
|
||||
problem(w, 400, "invalid_group_epoch", "群设备或 Welcome 无效")
|
||||
return
|
||||
}
|
||||
seen[device.Device] = true
|
||||
}
|
||||
if !seen[actor.Device.ID] {
|
||||
problem(w, 400, "invalid_group_epoch", "创建设备必须加入群加密世代")
|
||||
return
|
||||
}
|
||||
if err := a.groups.PublishEpoch(r.Context(), id, actor.User.ID, actor.Device.ID, input); err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
func (a *API) sendGroupMessage(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-send:"+actor.Device.ID, 180) {
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
ID string `json:"id"`
|
||||
Epoch string `json:"epoch"`
|
||||
Wire []byte `json:"wire"`
|
||||
}
|
||||
if !parseLimit(w, r, &input, 96<<10) {
|
||||
return
|
||||
}
|
||||
if !uuidPattern.MatchString(input.ID) || !uuidPattern.MatchString(input.Epoch) || !mlsFrame(input.Wire) || input.Wire[3] != 2 {
|
||||
problem(w, 400, "invalid_group_message", "群消息密文无效")
|
||||
return
|
||||
}
|
||||
sequence, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire})
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]string{"sequence": sequence})
|
||||
}
|
||||
|
||||
func (a *API) groupInbox(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 180) {
|
||||
return
|
||||
}
|
||||
after := int64(0)
|
||||
if raw := r.URL.Query().Get("after"); raw != "" {
|
||||
var err error
|
||||
after, err = strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || after < 0 {
|
||||
problem(w, 400, "invalid_cursor", "无效的投递游标")
|
||||
return
|
||||
}
|
||||
}
|
||||
items, err := a.groups.Inbox(r.Context(), id, actor.User.ID, actor.Device.ID, after)
|
||||
if errors.Is(err, groups.ErrUnavailable) {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, items)
|
||||
}
|
||||
@@ -23,6 +23,8 @@ func groupError(w http.ResponseWriter, err error) {
|
||||
problem(w, 409, "group_action_unavailable", "当前无法执行此群操作")
|
||||
case errors.Is(err, groups.ErrLimit):
|
||||
problem(w, 409, "group_member_limit", "群成员与待处理邀请已达上限")
|
||||
case errors.Is(err, groups.ErrVersion):
|
||||
problem(w, 409, "group_epoch_changed", "群成员或设备版本已变更")
|
||||
default:
|
||||
unavailable(w, err)
|
||||
}
|
||||
|
||||
@@ -89,6 +89,74 @@ test('群组邀请、授权、成员移除与版本变更', { timeout: 25_000 },
|
||||
await json(path, 'GET', owner.token, undefined, 404)
|
||||
})
|
||||
|
||||
test('群密文只投递到当前名单中的设备,离开或撤销后阻断旧世代', { timeout: 25_000 }, async () => {
|
||||
const enroll = async () => {
|
||||
const keys = generateKeyPairSync('ed25519')
|
||||
const actor = await register(keys)
|
||||
const publicKey = randomBytes(32)
|
||||
const bytes = Buffer.concat([Buffer.from('XHChat MLS credential v1\n' + actor.user.id + '\n' + actor.device.id + '\n'), publicKey])
|
||||
const response = await call('/mls-credential', 'POST', actor.token, {
|
||||
publicKey: publicKey.toString('base64'), signature: sign(null, bytes, keys.privateKey).toString('base64'),
|
||||
})
|
||||
assert.equal(response.status, 204)
|
||||
return actor
|
||||
}
|
||||
const owner = await enroll()
|
||||
const member = await enroll()
|
||||
const outsider = await enroll()
|
||||
const group = await json('/groups', 'POST', owner.token, { name: '密文投递验收' }, 201)
|
||||
const path = '/groups/' + group.id
|
||||
const publish = async (actor, input, expected = 204) => {
|
||||
const response = await call(path + '/epochs', 'POST', actor.token, input)
|
||||
assert.equal(response.status, expected, await response.text())
|
||||
}
|
||||
const change = async (actor, action, target) => {
|
||||
const response = await call(path, 'POST', actor.token, { action, target })
|
||||
assert.equal(response.status, 204, await response.text())
|
||||
}
|
||||
const welcome = Buffer.concat([Buffer.from([0, 1, 0, 3]), randomBytes(96)]).toString('base64')
|
||||
const first = { id: randomUUID(), membershipVersion: '1', devices: [{ device: owner.device.id }] }
|
||||
await publish(owner, first)
|
||||
await publish(owner, first)
|
||||
const firstMessage = { id: randomUUID(), epoch: first.id, wire: frame() }
|
||||
await json(path + '/messages', 'POST', owner.token, firstMessage)
|
||||
await contact(owner, member, 'request')
|
||||
await contact(member, owner, 'accept')
|
||||
await change(owner, 'invite', member.user.id)
|
||||
await json(path + '/epochs', 'GET', member.token, undefined, 404)
|
||||
await change(member, 'accept')
|
||||
await json(path + '/messages', 'POST', owner.token, { ...firstMessage, id: randomUUID() }, 409)
|
||||
assert.deepEqual(await json(path + '/messages', 'GET', member.token), [])
|
||||
const second = { id: randomUUID(), membershipVersion: '2', devices: [
|
||||
{ device: owner.device.id }, { device: member.device.id, wire: welcome },
|
||||
] }
|
||||
await publish(owner, { ...second, devices: [second.devices[0], { device: outsider.device.id, wire: welcome }] }, 409)
|
||||
await publish(member, { ...second, devices: [{ device: member.device.id }, { device: owner.device.id, wire: welcome }] }, 409)
|
||||
await publish(owner, second)
|
||||
await publish(owner, second)
|
||||
await publish(owner, { ...second, id: randomUUID() }, 409)
|
||||
const epochs = await json(path + '/epochs', 'GET', member.token)
|
||||
assert.deepEqual(epochs, [{ id: second.id, membershipVersion: '2', creatorDevice: owner.device.id, welcome, current: true }])
|
||||
await json(path + '/messages', 'GET', outsider.token, undefined, 404)
|
||||
const message = { id: randomUUID(), epoch: second.id, wire: frame() }
|
||||
const sequence = await json(path + '/messages', 'POST', member.token, message)
|
||||
assert.deepEqual(await json(path + '/messages', 'POST', member.token, message), sequence)
|
||||
const received = await json(path + '/messages', 'GET', member.token)
|
||||
assert.deepEqual(received.map((item) => item.wire), [message.wire])
|
||||
await change(owner, 'remove', member.user.id)
|
||||
await json(path + '/messages', 'GET', member.token, undefined, 404)
|
||||
await json(path + '/messages', 'POST', owner.token, { ...message, id: randomUUID() }, 409)
|
||||
await change(owner, 'invite', member.user.id)
|
||||
await change(member, 'accept')
|
||||
assert.deepEqual(await json(path + '/messages', 'GET', member.token), [])
|
||||
assert.deepEqual(await json(path + '/epochs', 'GET', member.token), [])
|
||||
const next = { ...second, id: randomUUID(), membershipVersion: '4' }
|
||||
await publish(owner, next)
|
||||
assert.equal((await call('/devices/' + member.device.id, 'DELETE', member.token)).status, 204)
|
||||
await json(path + '/messages', 'POST', owner.token, { id: randomUUID(), epoch: next.id, wire: frame() }, 409)
|
||||
assert.equal((await json(path + '/epochs', 'GET', owner.token)).every((epoch) => !epoch.current), true)
|
||||
})
|
||||
|
||||
test('设备密文投递、幂等、离线补拉、实时提示和撤销', { timeout: 25000 }, async () => {
|
||||
const sender = await register()
|
||||
const recipient = await register()
|
||||
|
||||
Reference in new issue
Block a user