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

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

+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