为双端群聊增加端到端加密附件
This commit is contained in:
1 parent
5988ad6ed6
commit
a95a007768
21 files changed
+405
-84
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user