From 0679b99a65fc95eae848659efc2ab9b39a26d29c Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Tue, 12 Jan 2021 15:51:14 +0100 Subject: [PATCH] feat: reject messages which are too large --- internal/imap/store.go | 2 +- internal/smtp/store.go | 1 + internal/smtp/user.go | 21 +++++++++++++++++++++ internal/store/user.go | 6 +++--- unreleased.md | 6 ++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/internal/imap/store.go b/internal/imap/store.go index 005108c3..137c916c 100644 --- a/internal/imap/store.go +++ b/internal/imap/store.go @@ -31,7 +31,7 @@ import ( type storeUserProvider interface { UserID() string GetSpace() (usedSpace, maxSpace uint, err error) - GetMaxUpload() (uint, error) + GetMaxUpload() (int64, error) GetAddress(addressID string) (storeAddressProvider, error) diff --git a/internal/smtp/store.go b/internal/smtp/store.go index 4f0218a6..42a1bbd0 100644 --- a/internal/smtp/store.go +++ b/internal/smtp/store.go @@ -33,4 +33,5 @@ type storeUserProvider interface { attachedPublicKeyName string, parentID string) (*pmapi.Message, []*pmapi.Attachment, error) SendMessage(messageID string, req *pmapi.SendMessageReq) error + GetMaxUpload() (int64, error) } diff --git a/internal/smtp/user.go b/internal/smtp/user.go index 202c3166..39655e17 100644 --- a/internal/smtp/user.go +++ b/internal/smtp/user.go @@ -329,6 +329,12 @@ func (su *smtpUser) Send(returnPath string, to []string, messageReader io.Reader su.backend.sendRecorder.setMessageID(sendRecorderMessageHash, message.ID) log.WithField("messageID", message.ID).Debug("Draft was created successfully") + if ok, err := su.isTotalSizeOkay(message, atts); err != nil { + return err + } else if !ok { + return errors.New("message is too large") + } + // We always have to create a new draft even if there already is one, // because clients don't necessarily save the draft before sending, which // can lead to sending the wrong message. Also clients do not necessarily @@ -527,3 +533,18 @@ func (su *smtpUser) Logout() error { log.Debug("SMTP client logged out user ", su.addressID) return nil } + +func (su *smtpUser) isTotalSizeOkay(message *pmapi.Message, attachments []*pmapi.Attachment) (bool, error) { + maxUpload, err := su.storeUser.GetMaxUpload() + if err != nil { + return false, err + } + + var attSize int64 + + for _, att := range attachments { + attSize += att.Size + } + + return message.Size+attSize <= maxUpload, nil +} diff --git a/internal/store/user.go b/internal/store/user.go index b0b92150..d234776b 100644 --- a/internal/store/user.go +++ b/internal/store/user.go @@ -31,11 +31,11 @@ func (store *Store) GetSpace() (usedSpace, maxSpace uint, err error) { return uint(apiUser.UsedSpace), uint(apiUser.MaxSpace), nil } -// GetMaxUpload returns max size of attachment in bytes. -func (store *Store) GetMaxUpload() (uint, error) { +// GetMaxUpload returns max size of message + all attachments in bytes. +func (store *Store) GetMaxUpload() (int64, error) { apiUser, err := store.client().CurrentUser() if err != nil { return 0, err } - return uint(apiUser.MaxUpload), nil + return apiUser.MaxUpload, nil } diff --git a/unreleased.md b/unreleased.md index 3d2a49cd..2f8de02f 100644 --- a/unreleased.md +++ b/unreleased.md @@ -14,3 +14,9 @@ Changelog [format](http://keepachangelog.com/en/1.0.0/) * GODT-787 GODT-978 Fix IE and Bridge importing to sent not showing up in inbox (setting up flags properly). * GODT-1006 Use correct macOS keychain name. * GODT-1009 Set ContentID if present and not explicitly attachment. +* GODT-900 Remove \Deleted flag after re-importing the message (do not delete messages by moving to local folder and back). +* GODT-908 Do not unpause event loop if other mailbox is still fetching. +* Check deprecated status code first to better determine API error. +* GODT-732 Fix usage of fontawesome +* GODT-915 Bump go-imap dependency and remove go-imap-specialuse dependency. +* GODT-928 Reject messages which are too large.