为双端群聊增加端到端加密附件

This commit is contained in:
xhgit committed 2026-11-14 09:05:40 +08:00
1 parent 5988ad6ed6
commit a95a007768
21 files changed
+405 -84

No files matched your search

@@ -0,0 +1,17 @@
CREATE TABLE group_attachments (
group_id uuid NOT NULL,
id uuid NOT NULL,
epoch_id uuid NOT NULL,
sender_device uuid NOT NULL,
ciphertext bytea NOT NULL CHECK (octet_length(ciphertext) BETWEEN 16 AND 8388624),
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (group_id,id),
UNIQUE (group_id,epoch_id,sender_device,id),
FOREIGN KEY (group_id,epoch_id,sender_device) REFERENCES group_epoch_devices(group_id,epoch_id,device_id) ON DELETE CASCADE
);
ALTER TABLE group_messages ADD COLUMN attachment_id uuid;
ALTER TABLE group_messages ADD FOREIGN KEY (group_id,epoch_id,sender_device,attachment_id)
REFERENCES group_attachments(group_id,epoch_id,sender_device,id);
CREATE UNIQUE INDEX group_messages_attachment ON group_messages(group_id,attachment_id) WHERE attachment_id IS NOT NULL;
CREATE INDEX group_attachments_expiry ON group_attachments(created_at);
+7 -1
View File
@@ -31,6 +31,9 @@ var groupsMigration string
//go:embed 008_group_transport.sql
var groupTransportMigration string
//go:embed 009_group_attachments.sql
var groupAttachmentsMigration string
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
tx, err := pool.Begin(ctx)
if err != nil {
@@ -43,7 +46,10 @@ 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, groupTransportMigration} {
for index, migration := range []string{
identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration,
attachmentsMigration, groupsMigration, groupTransportMigration, groupAttachmentsMigration,
} {
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 {
+58
View File
@@ -0,0 +1,58 @@
package groups
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
var ErrAttachment = errors.New("group attachment unavailable or already used")
func (s *Store) UploadAttachment(ctx context.Context, group, user, device, epoch, id string, ciphertext []byte) error {
tx, err := s.db.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
if err = lockSendingEpoch(ctx, tx, group, user, device, epoch); err != nil {
return err
}
inserted, err := tx.Exec(ctx, `
INSERT INTO group_attachments(group_id,id,epoch_id,sender_device,ciphertext)
VALUES($1,$2,$3,$4,$5) ON CONFLICT(group_id,id) DO NOTHING
`, group, id, epoch, device, ciphertext)
if err != nil {
return err
}
if inserted.RowsAffected() == 0 {
var matches bool
err = tx.QueryRow(ctx, `
SELECT ciphertext=$5 FROM group_attachments WHERE group_id=$1 AND id=$2 AND epoch_id=$3 AND sender_device=$4
`, group, id, epoch, device, ciphertext).Scan(&matches)
if errors.Is(err, pgx.ErrNoRows) || err == nil && !matches {
return ErrAttachment
}
if err != nil {
return err
}
}
return tx.Commit(ctx)
}
func (s *Store) DownloadAttachment(ctx context.Context, group, user, device, id string) ([]byte, error) {
var ciphertext []byte
err := s.db.QueryRow(ctx, `
SELECT a.ciphertext FROM group_attachments a
JOIN group_messages x ON x.group_id=a.group_id AND x.attachment_id=a.id
JOIN group_epochs g ON g.group_id=a.group_id AND g.id=a.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=g.group_id AND m.user_id=$2 AND m.state='active'
JOIN devices d ON d.id=e.device_id AND d.user_id=m.user_id AND d.revoked_at IS NULL
WHERE a.group_id=$1 AND a.id=$4 AND g.membership_version>=m.joined_version
`, group, user, device, id).Scan(&ciphertext)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrUnavailable
}
return ciphertext, err
}
+61 -37
View File
@@ -32,12 +32,13 @@ type AvailableEpoch struct {
}
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"`
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"`
AttachmentID *string `json:"attachmentId,omitempty"`
}
func lockTransport(ctx context.Context, tx pgx.Tx, group, user, device string) (string, bool, error) {
@@ -149,27 +150,22 @@ func retryEpoch(ctx context.Context, tx pgx.Tx, group, sender string, input Epoc
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)
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) error {
version, _, err := lockTransport(ctx, tx, group, user, device)
if err != nil {
return "", err
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)
`, group, epoch, device, version).Scan(&enrolled)
if err != nil {
return "", err
return err
}
if !enrolled {
return "", ErrVersion
return ErrVersion
}
// Match the device revocation transaction's row lock. Once a bound device is
@@ -177,36 +173,57 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
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)
`, group, epoch)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var active bool
if err = rows.Scan(&active); err != nil {
return err
}
if !active {
return ErrVersion
}
}
return rows.Err()
}
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
}
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 {
defer tx.Rollback(ctx)
if err = lockSendingEpoch(ctx, tx, group, user, device, message.Epoch); err != nil {
return "", err
}
if message.AttachmentID != nil {
var found string
err = tx.QueryRow(ctx, `
SELECT id FROM group_attachments WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND id=$4 FOR KEY SHARE
`, group, message.Epoch, device, message.AttachmentID).Scan(&found)
if errors.Is(err, pgx.ErrNoRows) {
return "", ErrAttachment
}
if 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)
INSERT INTO group_messages(group_id,epoch_id,sender_device,message_id,ciphertext,attachment_id)
VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING sequence::text
`, group, message.Epoch, device, message.ID, message.Wire, message.AttachmentID).Scan(&sequence)
if errors.Is(err, pgx.ErrNoRows) {
var previous []byte
var same bool
err = tx.QueryRow(ctx, `
SELECT sequence::text,ciphertext FROM group_messages
SELECT sequence::text,ciphertext=$5 AND attachment_id IS NOT DISTINCT FROM $6::uuid 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) {
`, group, message.Epoch, device, message.ID, message.Wire, message.AttachmentID).Scan(&sequence, &same)
if errors.Is(err, pgx.ErrNoRows) || err == nil && !same {
return "", ErrVersion
}
}
@@ -226,7 +243,7 @@ func (s *Store) Inbox(ctx context.Context, group, user, device string, after int
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
SELECT x.sequence::text,x.epoch_id,x.message_id,x.sender_device,x.ciphertext,x.created_at,x.attachment_id
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'
@@ -241,7 +258,7 @@ func (s *Store) Inbox(ctx context.Context, group, user, device string, after int
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 {
if err := rows.Scan(&m.Sequence, &m.Epoch, &m.ID, &m.Sender, &m.Wire, &m.CreatedAt, &m.AttachmentID); err != nil {
return nil, err
}
result = append(result, m)
@@ -317,6 +334,13 @@ func (s *Store) Cleanup(ctx context.Context, retention time.Duration) error {
if _, err = tx.Exec(ctx, "DELETE FROM group_messages WHERE created_at<$1", cutoff); err != nil {
return err
}
if _, err = tx.Exec(ctx, `
DELETE FROM group_attachments a WHERE created_at<$1 AND NOT EXISTS (
SELECT 1 FROM group_messages m WHERE m.group_id=a.group_id AND m.attachment_id=a.id
)
`, 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
+2
View File
@@ -68,6 +68,8 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
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))
mux.HandleFunc("POST /api/v1/groups/{id}/attachments/{attachment}", a.authorized(a.uploadGroupAttachment))
mux.HandleFunc("GET /api/v1/groups/{id}/attachments/{attachment}", a.authorized(a.downloadGroupAttachment))
mux.HandleFunc("GET /api/v1/groups/{id}/devices", a.authorized(a.groupDevices))
mux.HandleFunc("POST /api/v1/groups/{id}/devices/{device}/claim", a.authorized(a.claimGroupPackage))
return a.boundary(mux)
+25 -12
View File
@@ -19,17 +19,11 @@ func (a *API) uploadAttachment(w http.ResponseWriter, r *http.Request, _ string,
if !a.limit(w, r, "attachment-upload:"+identity.Device.ID, 12) {
return
}
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, delivery.AttachmentLimit))
var tooLarge *http.MaxBytesError
switch {
case errors.As(err, &tooLarge):
problem(w, 413, "attachment_too_large", "附件超过 8 MiB")
return
case err != nil || len(body) < 16:
problem(w, 400, "invalid_attachment", "附件请求格式无效")
body, ok := readAttachment(w, r)
if !ok {
return
}
err = a.delivery.UploadAttachment(r.Context(), identity.Device.ID, recipient, id, body)
err := a.delivery.UploadAttachment(r.Context(), identity.Device.ID, recipient, id, body)
switch {
case errors.Is(err, delivery.ErrUnavailable):
problem(w, 404, "attachment_unavailable", "无法向指定设备发送附件")
@@ -42,6 +36,20 @@ func (a *API) uploadAttachment(w http.ResponseWriter, r *http.Request, _ string,
}
}
func readAttachment(w http.ResponseWriter, r *http.Request) ([]byte, bool) {
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, delivery.AttachmentLimit))
var tooLarge *http.MaxBytesError
switch {
case errors.As(err, &tooLarge):
problem(w, 413, "attachment_too_large", "附件超过 8 MiB")
return nil, false
case err != nil || len(body) < 16:
problem(w, 400, "invalid_attachment", "附件请求格式无效")
return nil, false
}
return body, true
}
func (a *API) downloadAttachment(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
id := r.PathValue("id")
if !uuidPattern.MatchString(id) {
@@ -58,8 +66,13 @@ func (a *API) downloadAttachment(w http.ResponseWriter, r *http.Request, _ strin
case err != nil:
unavailable(w, err)
default:
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment")
_, _ = w.Write(body)
writeAttachment(w, body)
}
}
func writeAttachment(w http.ResponseWriter, body []byte) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment")
w.Header().Set("Cache-Control", "no-store")
_, _ = w.Write(body)
}
@@ -0,0 +1,50 @@
package httpapi
import (
"errors"
"net/http"
"xhchat/server/internal/account"
"xhchat/server/internal/groups"
)
func (a *API) uploadGroupAttachment(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
group, id, epoch := r.PathValue("id"), r.PathValue("attachment"), r.URL.Query().Get("epoch")
if !uuidPattern.MatchString(group) || !uuidPattern.MatchString(id) || !uuidPattern.MatchString(epoch) ||
r.Header.Get("Content-Type") != "application/octet-stream" {
problem(w, 400, "invalid_attachment", "群附件请求格式无效")
return
}
if !a.limit(w, r, "attachment-upload:"+actor.Device.ID, 12) {
return
}
body, ok := readAttachment(w, r)
if !ok {
return
}
if err := a.groups.UploadAttachment(r.Context(), group, actor.User.ID, actor.Device.ID, epoch, id, body); err != nil {
groupError(w, err)
return
}
w.WriteHeader(204)
}
func (a *API) downloadGroupAttachment(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
group, id := r.PathValue("id"), r.PathValue("attachment")
if !uuidPattern.MatchString(group) || !uuidPattern.MatchString(id) {
problem(w, 404, "attachment_unavailable", "群附件不可用")
return
}
if !a.limit(w, r, "attachment-download:"+actor.Device.ID, 30) {
return
}
body, err := a.groups.DownloadAttachment(r.Context(), group, actor.User.ID, actor.Device.ID, id)
switch {
case errors.Is(err, groups.ErrUnavailable):
problem(w, 404, "attachment_unavailable", "群附件不可用或已过期")
case err != nil:
unavailable(w, err)
default:
writeAttachment(w, body)
}
}
+8 -5
View File
@@ -108,18 +108,21 @@ func (a *API) sendGroupMessage(w http.ResponseWriter, r *http.Request, _ string,
return
}
var input struct {
ID string `json:"id"`
Epoch string `json:"epoch"`
Wire []byte `json:"wire"`
ID string `json:"id"`
Epoch string `json:"epoch"`
Wire []byte `json:"wire"`
AttachmentID *string `json:"attachmentId,omitempty"`
}
if !parseLimit(w, r, &input, 96<<10) {
return
}
if !uuidPattern.MatchString(input.ID) || !uuidPattern.MatchString(input.Epoch) || !mlsFrame(input.Wire) || input.Wire[3] != 2 {
if !uuidPattern.MatchString(input.ID) || !uuidPattern.MatchString(input.Epoch) || !mlsFrame(input.Wire) || input.Wire[3] != 2 ||
input.AttachmentID != nil && !uuidPattern.MatchString(*input.AttachmentID) {
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})
message := groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire, AttachmentID: input.AttachmentID}
sequence, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
if err != nil {
groupError(w, err)
return
+2
View File
@@ -25,6 +25,8 @@ func groupError(w http.ResponseWriter, err error) {
problem(w, 409, "group_member_limit", "群成员与待处理邀请已达上限")
case errors.Is(err, groups.ErrVersion):
problem(w, 409, "group_epoch_changed", "群成员或设备版本已变更")
case errors.Is(err, groups.ErrAttachment):
problem(w, 409, "group_attachment_conflict", "群附件不可用或已绑定到其他消息")
default:
unavailable(w, err)
}