forked from Silverfish/proton-bridge
Sending: do not send empty objects to API
This commit is contained in:
@ -97,18 +97,18 @@ type AlgoKey struct {
|
|||||||
|
|
||||||
type MessageAddress struct {
|
type MessageAddress struct {
|
||||||
Type PackageFlag
|
Type PackageFlag
|
||||||
EncryptedBodyKeyPacket string `json:"BodyKeyPacket"` // base64-encoded key packet.
|
EncryptedBodyKeyPacket string `json:"BodyKeyPacket,omitempty"` // base64-encoded key packet.
|
||||||
Signature SignatureFlag
|
Signature SignatureFlag
|
||||||
EncryptedAttachmentKeyPackets map[string]string `json:"AttachmentKeyPackets"`
|
EncryptedAttachmentKeyPackets map[string]string `json:"AttachmentKeyPackets,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessagePackage struct {
|
type MessagePackage struct {
|
||||||
Addresses map[string]*MessageAddress
|
Addresses map[string]*MessageAddress
|
||||||
Type PackageFlag
|
Type PackageFlag
|
||||||
MIMEType string
|
MIMEType string
|
||||||
EncryptedBody string `json:"Body"` // base64-encoded encrypted data packet.
|
EncryptedBody string `json:"Body"` // base64-encoded encrypted data packet.
|
||||||
DecryptedBodyKey AlgoKey `json:"BodyKey"` // base64-encoded session key (only if cleartext recipients).
|
DecryptedBodyKey *AlgoKey `json:"BodyKey,omitempty"` // base64-encoded session key (only if cleartext recipients).
|
||||||
DecryptedAttachmentKeys map[string]AlgoKey `json:"AttachmentKeys"` // Only include if cleartext & attachments.
|
DecryptedAttachmentKeys map[string]AlgoKey `json:"AttachmentKeys,omitempty"` // Only include if cleartext & attachments.
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMessagePackage(
|
func newMessagePackage(
|
||||||
@ -123,8 +123,10 @@ func newMessagePackage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if send.sharedScheme.HasAtLeastOne(ClearPackage | ClearMIMEPackage) {
|
if send.sharedScheme.HasAtLeastOne(ClearPackage | ClearMIMEPackage) {
|
||||||
pkg.DecryptedBodyKey.Key = send.decryptedBodyKey.GetBase64Key()
|
pkg.DecryptedBodyKey = &AlgoKey{
|
||||||
pkg.DecryptedBodyKey.Algorithm = send.decryptedBodyKey.Algo
|
Key: send.decryptedBodyKey.GetBase64Key(),
|
||||||
|
Algorithm: send.decryptedBodyKey.Algo,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(attKeys) != 0 && send.sharedScheme.Has(ClearPackage) {
|
if len(attKeys) != 0 && send.sharedScheme.Has(ClearPackage) {
|
||||||
@ -148,7 +150,7 @@ type SendMessageReq struct {
|
|||||||
// AutoSaveContacts int `json:",omitempty"`
|
// AutoSaveContacts int `json:",omitempty"`
|
||||||
|
|
||||||
// Data for encrypted recipients.
|
// Data for encrypted recipients.
|
||||||
Packages []*MessagePackage
|
Packages []*MessagePackage `json:",omitempty"`
|
||||||
|
|
||||||
mime, plain, rich sendData
|
mime, plain, rich sendData
|
||||||
attKeys map[string]*crypto.SessionKey
|
attKeys map[string]*crypto.SessionKey
|
||||||
|
|||||||
@ -19,6 +19,7 @@ package pmapi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||||
@ -126,8 +127,13 @@ func (td *testData) prepareAndCheck(t *testing.T) {
|
|||||||
wantBodyKey := wantPackage.DecryptedBodyKey
|
wantBodyKey := wantPackage.DecryptedBodyKey
|
||||||
haveBodyKey := havePackage.DecryptedBodyKey
|
haveBodyKey := havePackage.DecryptedBodyKey
|
||||||
|
|
||||||
matchPresence(wantBodyKey.Algorithm)(t, haveBodyKey.Algorithm, "pkg %d", i)
|
if wantBodyKey == nil {
|
||||||
matchPresence(wantBodyKey.Key)(t, haveBodyKey.Key, "pkg %d", i)
|
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 {
|
if len(td.attKeys) == 0 {
|
||||||
r.Len(havePackage.DecryptedAttachmentKeys, 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) {
|
func TestSendReq(t *testing.T) {
|
||||||
@ -330,7 +345,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: ClearPackage,
|
Type: ClearPackage,
|
||||||
MIMEType: ContentTypeHTML,
|
MIMEType: ContentTypeHTML,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -346,7 +361,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: ClearPackage,
|
Type: ClearPackage,
|
||||||
MIMEType: ContentTypePlainText,
|
MIMEType: ContentTypePlainText,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -361,7 +376,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: ClearMIMEPackage,
|
Type: ClearMIMEPackage,
|
||||||
MIMEType: ContentTypeMultipartMixed,
|
MIMEType: ContentTypeMultipartMixed,
|
||||||
EncryptedBody: "non-empty",
|
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,
|
Type: InternalPackage | ClearPackage,
|
||||||
MIMEType: ContentTypePlainText,
|
MIMEType: ContentTypePlainText,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -424,7 +439,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: InternalPackage | ClearPackage,
|
Type: InternalPackage | ClearPackage,
|
||||||
MIMEType: ContentTypeHTML,
|
MIMEType: ContentTypeHTML,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -454,7 +469,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: ClearMIMEPackage | PGPMIMEPackage,
|
Type: ClearMIMEPackage | PGPMIMEPackage,
|
||||||
MIMEType: ContentTypeMultipartMixed,
|
MIMEType: ContentTypeMultipartMixed,
|
||||||
EncryptedBody: "non-empty",
|
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,
|
Type: ClearMIMEPackage,
|
||||||
MIMEType: ContentTypeMultipartMixed,
|
MIMEType: ContentTypeMultipartMixed,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Addresses: map[string]*MessageAddress{
|
Addresses: map[string]*MessageAddress{
|
||||||
@ -506,7 +521,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: ClearPackage,
|
Type: ClearPackage,
|
||||||
MIMEType: ContentTypePlainText,
|
MIMEType: ContentTypePlainText,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -517,7 +532,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: ClearPackage,
|
Type: ClearPackage,
|
||||||
MIMEType: ContentTypeHTML,
|
MIMEType: ContentTypeHTML,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -568,7 +583,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: ClearMIMEPackage | PGPMIMEPackage,
|
Type: ClearMIMEPackage | PGPMIMEPackage,
|
||||||
MIMEType: ContentTypeMultipartMixed,
|
MIMEType: ContentTypeMultipartMixed,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Addresses: map[string]*MessageAddress{
|
Addresses: map[string]*MessageAddress{
|
||||||
@ -580,7 +595,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: InternalPackage | ClearPackage | PGPInlinePackage,
|
Type: InternalPackage | ClearPackage | PGPInlinePackage,
|
||||||
MIMEType: ContentTypePlainText,
|
MIMEType: ContentTypePlainText,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -593,7 +608,7 @@ func TestSendReq(t *testing.T) {
|
|||||||
Type: InternalPackage | ClearPackage,
|
Type: InternalPackage | ClearPackage,
|
||||||
MIMEType: ContentTypeHTML,
|
MIMEType: ContentTypeHTML,
|
||||||
EncryptedBody: "non-empty",
|
EncryptedBody: "non-empty",
|
||||||
DecryptedBodyKey: AlgoKey{"non-empty", "non-empty"},
|
DecryptedBodyKey: &AlgoKey{"non-empty", "non-empty"},
|
||||||
DecryptedAttachmentKeys: attAlgoKeys,
|
DecryptedAttachmentKeys: attAlgoKeys,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@ -13,3 +13,4 @@ Changelog [format](http://keepachangelog.com/en/1.0.0/)
|
|||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
* GODT-999 Sending: do not send empty objects to API.
|
||||||
|
|||||||
Reference in New Issue
Block a user