交付 Windows 与 Web 端到端加密附件预览

This commit is contained in:
xhgit committed 2026-11-13 19:14:44 +08:00
1 parent d1efdedc57
commit c24960d22a
19 files changed
+421 -58

No files matched your search

+51 -39
View File
@@ -34,47 +34,10 @@ func (s *Store) Send(ctx context.Context, sender, recipient, message string, cip
}
defer tx.Rollback(ctx)
var senderUser, recipientUser string
err = tx.QueryRow(ctx, `
SELECT sender.user_id, recipient.user_id FROM devices sender CROSS JOIN devices recipient
WHERE sender.id=$1 AND recipient.id=$2 AND sender.id<>recipient.id
`, sender, recipient).Scan(&senderUser, &recipientUser)
if errors.Is(err, pgx.ErrNoRows) {
return "", ErrUnavailable
}
if err != nil {
if err = authorizeDelivery(ctx, tx, sender, recipient); err != nil {
return "", err
}
// Use the same account lock order as contact changes. Locking both devices also
// serializes writes for each recipient and prevents a cursor passing an uncommitted write.
if _, err = tx.Exec(ctx, "SELECT id FROM users WHERE id IN ($1,$2) ORDER BY id FOR UPDATE", senderUser, recipientUser); err != nil {
return "", err
}
var active int
err = tx.QueryRow(ctx, `
SELECT count(*) FROM (
SELECT id FROM devices WHERE id IN ($1,$2) AND revoked_at IS NULL ORDER BY id FOR UPDATE
) AS active_devices
`, sender, recipient).Scan(&active)
if err != nil {
return "", err
}
if active != 2 {
return "", ErrUnavailable
}
// This direct-message endpoint only connects accepted contacts.
// A separate authorization flow is needed for same-account device transfers.
var friends bool
err = tx.QueryRow(ctx, "SELECT "+acceptedContact, senderUser, recipientUser).Scan(&friends)
if err != nil {
return "", err
}
if !friends {
return "", ErrUnavailable
}
var seq string
err = tx.QueryRow(ctx, `
INSERT INTO device_envelopes(message_id, sender_device, recipient_device, ciphertext)
@@ -122,10 +85,59 @@ func (s *Store) Inbox(ctx context.Context, recipient string, after int64) ([]Env
}
func (s *Store) Cleanup(ctx context.Context, retention time.Duration) error {
if _, err := s.db.Exec(ctx, "DELETE FROM device_envelopes WHERE created_at < $1", time.Now().Add(-retention)); err != nil {
cutoff := time.Now().Add(-retention)
if _, err := s.db.Exec(ctx, "DELETE FROM device_envelopes WHERE created_at < $1", cutoff); err != nil {
return err
}
if _, err := s.db.Exec(ctx, "DELETE FROM device_attachments WHERE created_at < $1", cutoff); err != nil {
return err
}
// Keep issued package hashes past the validity window of an individual upload.
_, err := s.db.Exec(ctx, "DELETE FROM device_key_packages WHERE created_at < now() - interval '90 days'")
return err
}
func authorizeDelivery(ctx context.Context, tx pgx.Tx, sender, recipient string) error {
var senderUser, recipientUser string
err := tx.QueryRow(ctx, `
SELECT sender.user_id, recipient.user_id FROM devices sender CROSS JOIN devices recipient
WHERE sender.id=$1 AND recipient.id=$2 AND sender.id<>recipient.id
`, sender, recipient).Scan(&senderUser, &recipientUser)
if errors.Is(err, pgx.ErrNoRows) {
return ErrUnavailable
}
if err != nil {
return err
}
// Use the same account lock order as contact changes. Locking both devices also
// serializes writes for each recipient and prevents a cursor passing an uncommitted write.
if _, err = tx.Exec(ctx, "SELECT id FROM users WHERE id IN ($1,$2) ORDER BY id FOR UPDATE", senderUser, recipientUser); err != nil {
return err
}
var active int
err = tx.QueryRow(ctx, `
SELECT count(*) FROM (
SELECT id FROM devices WHERE id IN ($1,$2) AND revoked_at IS NULL ORDER BY id FOR UPDATE
) AS active_devices
`, sender, recipient).Scan(&active)
if err != nil {
return err
}
if active != 2 {
return ErrUnavailable
}
// This direct-message endpoint only connects accepted contacts.
// A separate authorization flow is needed for same-account device transfers.
var friends bool
err = tx.QueryRow(ctx, "SELECT "+acceptedContact, senderUser, recipientUser).Scan(&friends)
if err != nil {
return err
}
if !friends {
return ErrUnavailable
}
return nil
}