Sending: do not send empty objects to API

This commit is contained in:
Michal Horejsek
2021-01-28 10:09:46 +01:00
committed by Jakub
parent 7065211064
commit 50d167a983
3 changed files with 40 additions and 22 deletions

View File

@ -97,18 +97,18 @@ type AlgoKey struct {
type MessageAddress struct {
Type PackageFlag
EncryptedBodyKeyPacket string `json:"BodyKeyPacket"` // base64-encoded key packet.
EncryptedBodyKeyPacket string `json:"BodyKeyPacket,omitempty"` // base64-encoded key packet.
Signature SignatureFlag
EncryptedAttachmentKeyPackets map[string]string `json:"AttachmentKeyPackets"`
EncryptedAttachmentKeyPackets map[string]string `json:"AttachmentKeyPackets,omitempty"`
}
type MessagePackage struct {
Addresses map[string]*MessageAddress
Type PackageFlag
MIMEType string
EncryptedBody string `json:"Body"` // base64-encoded encrypted data packet.
DecryptedBodyKey AlgoKey `json:"BodyKey"` // base64-encoded session key (only if cleartext recipients).
DecryptedAttachmentKeys map[string]AlgoKey `json:"AttachmentKeys"` // Only include if cleartext & attachments.
EncryptedBody string `json:"Body"` // base64-encoded encrypted data packet.
DecryptedBodyKey *AlgoKey `json:"BodyKey,omitempty"` // base64-encoded session key (only if cleartext recipients).
DecryptedAttachmentKeys map[string]AlgoKey `json:"AttachmentKeys,omitempty"` // Only include if cleartext & attachments.
}
func newMessagePackage(
@ -123,8 +123,10 @@ func newMessagePackage(
}
if send.sharedScheme.HasAtLeastOne(ClearPackage | ClearMIMEPackage) {
pkg.DecryptedBodyKey.Key = send.decryptedBodyKey.GetBase64Key()
pkg.DecryptedBodyKey.Algorithm = send.decryptedBodyKey.Algo
pkg.DecryptedBodyKey = &AlgoKey{
Key: send.decryptedBodyKey.GetBase64Key(),
Algorithm: send.decryptedBodyKey.Algo,
}
}
if len(attKeys) != 0 && send.sharedScheme.Has(ClearPackage) {
@ -148,7 +150,7 @@ type SendMessageReq struct {
// AutoSaveContacts int `json:",omitempty"`
// Data for encrypted recipients.
Packages []*MessagePackage
Packages []*MessagePackage `json:",omitempty"`
mime, plain, rich sendData
attKeys map[string]*crypto.SessionKey

View File

@ -19,6 +19,7 @@ package pmapi
import (
"encoding/base64"
"encoding/json"
"testing"
"github.com/ProtonMail/gopenpgp/v2/crypto"
@ -126,8 +127,13 @@ func (td *testData) prepareAndCheck(t *testing.T) {
wantBodyKey := wantPackage.DecryptedBodyKey
haveBodyKey := havePackage.DecryptedBodyKey
matchPresence(wantBodyKey.Algorithm)(t, haveBodyKey.Algorithm, "pkg %d", i)
matchPresence(wantBodyKey.Key)(t, haveBodyKey.Key, "pkg %d", i)
if wantBodyKey == nil {
r.Nil(haveBodyKey, "pkg %d: expected empty body key but got %v", i, haveBodyKey)
} else {
r.NotNil(haveBodyKey, "pkg %d: expected body key but got nil", i)
r.NotEmpty(haveBodyKey.Algorithm, "pkg %d", i)
r.NotEmpty(haveBodyKey.Key, "pkg %d", i)
}
if len(td.attKeys) == 0 {
r.Len(havePackage.DecryptedAttachmentKeys, 0)
@ -145,6 +151,15 @@ func (td *testData) prepareAndCheck(t *testing.T) {
}
}
}
haveBytes, err := json.Marshal(have)
r.NoError(err)
haveString := string(haveBytes)
// Added `:` to avoid false-fail if the whole output results to empty object.
r.NotContains(haveString, ":\"\"", "found empty string: %s", haveString)
r.NotContains(haveString, ":[]", "found empty list: %s", haveString)
r.NotContains(haveString, ":{}", "found empty object: %s", haveString)
r.NotContains(haveString, ":null", "found null: %s", haveString)
}
func TestSendReq(t *testing.T) {
@ -330,7 +345,7 @@ func TestSendReq(t *testing.T) {
Type: ClearPackage,
MIMEType: ContentTypeHTML,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
},
@ -346,7 +361,7 @@ func TestSendReq(t *testing.T) {
Type: ClearPackage,
MIMEType: ContentTypePlainText,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
},
@ -361,7 +376,7 @@ func TestSendReq(t *testing.T) {
Type: ClearMIMEPackage,
MIMEType: ContentTypeMultipartMixed,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
},
},
},
@ -406,7 +421,7 @@ func TestSendReq(t *testing.T) {
Type: InternalPackage | ClearPackage,
MIMEType: ContentTypePlainText,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
},
@ -424,7 +439,7 @@ func TestSendReq(t *testing.T) {
Type: InternalPackage | ClearPackage,
MIMEType: ContentTypeHTML,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
},
@ -454,7 +469,7 @@ func TestSendReq(t *testing.T) {
Type: ClearMIMEPackage | PGPMIMEPackage,
MIMEType: ContentTypeMultipartMixed,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
},
},
},
@ -496,7 +511,7 @@ func TestSendReq(t *testing.T) {
Type: ClearMIMEPackage,
MIMEType: ContentTypeMultipartMixed,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
},
{
Addresses: map[string]*MessageAddress{
@ -506,7 +521,7 @@ func TestSendReq(t *testing.T) {
Type: ClearPackage,
MIMEType: ContentTypePlainText,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
{
@ -517,7 +532,7 @@ func TestSendReq(t *testing.T) {
Type: ClearPackage,
MIMEType: ContentTypeHTML,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
},
@ -568,7 +583,7 @@ func TestSendReq(t *testing.T) {
Type: ClearMIMEPackage | PGPMIMEPackage,
MIMEType: ContentTypeMultipartMixed,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
},
{
Addresses: map[string]*MessageAddress{
@ -580,7 +595,7 @@ func TestSendReq(t *testing.T) {
Type: InternalPackage | ClearPackage | PGPInlinePackage,
MIMEType: ContentTypePlainText,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
{
@ -593,7 +608,7 @@ func TestSendReq(t *testing.T) {
Type: InternalPackage | ClearPackage,
MIMEType: ContentTypeHTML,
EncryptedBody: "non-empty",
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
DecryptedAttachmentKeys: attAlgoKeys,
},
},

View File

@ -13,3 +13,4 @@ Changelog [format](http://keepachangelog.com/en/1.0.0/)
### Changed
### Fixed
* GODT-999 Sending: do not send empty objects to API.