GODT-1205: "RCPT TO" does not contain all addressed from "CC"

This commit is contained in:
Alexander Bilyak
2021-09-06 21:13:37 +00:00
committed by Jakub Cuth
parent 91dcb2f773
commit 22d2bcc21d
11 changed files with 236 additions and 91 deletions

View File

@ -362,7 +362,8 @@ func (su *smtpUser) Send(returnPath string, to []string, messageReader io.Reader
req := pmapi.NewSendMessageReq(kr, mimeBody, plainBody, richBody, attkeys)
containsUnencryptedRecipients := false
for _, email := range to {
for _, recipient := range message.Recipients() {
email := recipient.Address
if !looksLikeEmail(email) {
return errors.New(`"` + email + `" is not a valid recipient.`)
}

View File

@ -429,6 +429,14 @@ func (m *Message) Has(flag int64) bool {
return (m.Flags & flag) == flag
}
func (m *Message) Recipients() []*mail.Address {
var recipients []*mail.Address
recipients = append(recipients, m.ToList...)
recipients = append(recipients, m.CCList...)
recipients = append(recipients, m.BCCList...)
return recipients
}
// MessagesCount contains message counts for one label.
type MessagesCount struct {
LabelID string

View File

@ -37,6 +37,7 @@ func APIChecksFeatureContext(s *godog.Suite) {
s.Step(`^API endpoint "([^"]*)" is not called$`, apiIsNotCalled)
s.Step(`^API endpoint "([^"]*)" is not called with$`, apiIsNotCalledWith)
s.Step(`^message is sent with API call$`, messageIsSentWithAPICall)
s.Step(`^packages are sent with API call$`, packagesAreSentWithAPICall)
s.Step(`^API mailbox "([^"]*)" for "([^"]*)" has (\d+) message(?:s)?$`, apiMailboxForUserHasNumberOfMessages)
s.Step(`^API mailbox "([^"]*)" for address "([^"]*)" of "([^"]*)" has (\d+) message(?:s)?$`, apiMailboxForAddressOfUserHasNumberOfMessages)
s.Step(`^API mailbox "([^"]*)" for "([^"]*)" has messages$`, apiMailboxForUserHasMessages)
@ -58,6 +59,17 @@ func apiIsCalledWith(endpoint string, data *gherkin.DocString) error {
return nil
}
func apiIsCalledWithRegex(endpoint string, data *gherkin.DocString) error {
match, err := apiIsCalledWithHelperRegex(endpoint, data.Content)
if err != nil {
return err
}
if !match {
return fmt.Errorf("%s was not called with %s", endpoint, data.Content)
}
return nil
}
func apiIsNotCalled(endpoint string) error {
if apiIsCalledWithHelper(endpoint, "") {
return fmt.Errorf("%s was called", endpoint)
@ -80,6 +92,14 @@ func apiIsCalledWithHelper(endpoint string, content string) bool {
return ctx.GetPMAPIController().WasCalled(method, path, request)
}
func apiIsCalledWithHelperRegex(endpoint string, content string) (bool, error) {
split := strings.Split(endpoint, " ")
method := split[0]
path := split[1]
request := []byte(content)
return ctx.GetPMAPIController().WasCalledRegex(method, path, request)
}
func messageIsSentWithAPICall(data *gherkin.DocString) error {
endpoint := "POST /mail/v4/messages"
if err := apiIsCalledWith(endpoint, data); err != nil {
@ -94,6 +114,20 @@ func messageIsSentWithAPICall(data *gherkin.DocString) error {
return nil
}
func packagesAreSentWithAPICall(data *gherkin.DocString) error {
endpoint := "POST /mail/v4/messages/.+$"
if err := apiIsCalledWithRegex(endpoint, data); err != nil {
return err
}
for _, request := range ctx.GetPMAPIController().GetCalls("POST", "/mail/v4/messages") {
if !checkAllRequiredFieldsForSendingMessage(request) {
return fmt.Errorf("%s was not called with all required fields: %s", endpoint, request)
}
}
return nil
}
func checkAllRequiredFieldsForSendingMessage(request []byte) bool {
if matches := regexp.MustCompile(`"Subject":`).Match(request); !matches {
return false

View File

@ -0,0 +1,99 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package calls
import (
"fmt"
"regexp"
"github.com/nsf/jsondiff"
)
type CallRecord struct {
method string
path string
request []byte
}
type Calls []CallRecord
func (c *Calls) Register(method, path string, request []byte) {
*c = append(*c, CallRecord{
method: method,
path: path,
request: request,
})
}
func (c *Calls) PrintCalls() {
fmt.Println("API calls:")
for idx, call := range *c {
fmt.Printf("%02d: [%s] %s\n", idx+1, call.method, call.path)
if call.request != nil && string(call.request) != "null" {
fmt.Printf("\t%s\n", call.request)
}
}
}
func (c *Calls) WasCalled(method, path string, expectedRequest []byte) bool {
res, err := c.WasCalledRegex("^"+regexp.QuoteMeta(method)+"$", "^"+regexp.QuoteMeta(path)+"$", expectedRequest)
if err != nil {
panic(err)
}
return res
}
func (c *Calls) WasCalledRegex(methodRegex, pathRegex string, expectedRequest []byte) (bool, error) {
for _, call := range *c {
matched, err := regexp.Match(methodRegex, []byte(call.method))
if err != nil {
return false, err
}
if !matched {
continue
}
matched, err = regexp.Match(pathRegex, []byte(call.path))
if err != nil {
return false, err
}
if !matched {
continue
}
if string(expectedRequest) == "" {
return true, nil
}
diff, _ := jsondiff.Compare(call.request, expectedRequest, &jsondiff.Options{})
isSuperset := diff == jsondiff.FullMatch || diff == jsondiff.SupersetMatch
if isSuperset {
return true, nil
}
}
return false, nil
}
func (c *Calls) GetCalls(method, path string) [][]byte {
requests := [][]byte{}
for _, call := range *c {
if call.method == method && call.path == path {
requests = append(requests, call.request)
}
}
return requests
}

View File

@ -40,6 +40,7 @@ type PMAPIController interface {
ReorderAddresses(user *pmapi.User, addressIDs []string) error
PrintCalls()
WasCalled(method, path string, expectedRequest []byte) bool
WasCalledRegex(methodRegex, pathRegex string, expectedRequest []byte) (bool, error)
GetCalls(method, path string) [][]byte
}

View File

@ -21,6 +21,7 @@ import (
"sync"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/ProtonMail/proton-bridge/test/context/calls"
"github.com/sirupsen/logrus"
)
@ -30,7 +31,7 @@ type Controller struct {
// Internal states.
lock *sync.RWMutex
fakeAPIs []*FakePMAPI
calls []*fakeCall
calls calls.Calls
labelIDGenerator idGenerator
messageIDGenerator idGenerator
tokenGenerator idGenerator
@ -52,7 +53,7 @@ func NewController() (*Controller, pmapi.Manager) {
controller := &Controller{
lock: &sync.RWMutex{},
fakeAPIs: []*FakePMAPI{},
calls: []*fakeCall{},
calls: calls.Calls{},
labelIDGenerator: 100, // We cannot use system label IDs.
messageIDGenerator: 0,
tokenGenerator: 1000, // No specific reason; 1000 simply feels right.

View File

@ -19,10 +19,8 @@ package fakeapi
import (
"encoding/json"
"fmt"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/nsf/jsondiff"
)
type method string
@ -34,12 +32,6 @@ const (
DELETE method = "DELETE"
)
type fakeCall struct {
method method
path string
request []byte
}
func (ctl *Controller) checkAndRecordCall(method method, path string, req interface{}) error {
ctl.lock.Lock()
defer ctl.lock.Unlock()
@ -54,11 +46,7 @@ func (ctl *Controller) checkAndRecordCall(method method, path string, req interf
}
}
ctl.calls = append(ctl.calls, &fakeCall{
method: method,
path: path,
request: request,
})
ctl.calls.Register(string(method), path, request)
if ctl.noInternetConnection {
return pmapi.ErrNoConnection
@ -68,38 +56,17 @@ func (ctl *Controller) checkAndRecordCall(method method, path string, req interf
}
func (ctl *Controller) PrintCalls() {
fmt.Println("API calls:")
for idx, call := range ctl.calls {
fmt.Printf("%02d: [%s] %s\n", idx+1, call.method, call.path)
if call.request != nil && string(call.request) != "null" {
fmt.Printf("\t%s\n", call.request)
}
}
ctl.calls.PrintCalls()
}
func (ctl *Controller) WasCalled(method, path string, expectedRequest []byte) bool {
for _, call := range ctl.calls {
if string(call.method) != method || call.path != path {
continue
}
if string(expectedRequest) == "" {
return true
}
diff, _ := jsondiff.Compare(call.request, expectedRequest, &jsondiff.Options{})
isSuperset := diff == jsondiff.FullMatch || diff == jsondiff.SupersetMatch
if isSuperset {
return true
}
}
return false
return ctl.calls.WasCalled(method, path, expectedRequest)
}
func (ctl *Controller) WasCalledRegex(methodRegex, pathRegex string, expectedRequest []byte) (bool, error) {
return ctl.calls.WasCalledRegex(methodRegex, pathRegex, expectedRequest)
}
func (ctl *Controller) GetCalls(method, path string) [][]byte {
requests := [][]byte{}
for _, call := range ctl.calls {
if string(call.method) == method && call.path == path {
requests = append(requests, call.request)
}
}
return requests
return ctl.calls.GetCalls(method, path)
}

View File

@ -256,3 +256,69 @@ Feature: SMTP sending of plain messages
}
}
"""
Scenario: RCPT does not contain all CC
When SMTP client sends "MAIL FROM:<[userAddress]>"
Then SMTP response is "OK"
When SMTP client sends "RCPT TO:<bridgetest@protonmail.com>"
Then SMTP response is "OK"
When SMTP client sends "DATA"
Then SMTP response is "OK"
When SMTP client sends
"""
From: Bridge Test <[userAddress]>
To: Internal Bridge <bridgetest@protonmail.com>
CC: Internal Bridge 2 <bridgetest2@protonmail.com>
Content-Type: text/plain
Subject: RCPT-CC test
This is CC missing in RCPT test. Have a nice day!
.
"""
Then SMTP response is "OK"
And mailbox "Sent" for "user" has messages
| from | to | cc | subject |
| [userAddress] | bridgetest@protonmail.com | bridgetest2@protonmail.com | RCPT-CC test |
And message is sent with API call
"""
{
"Message": {
"Subject": "RCPT-CC test",
"Sender": {
"Name": "Bridge Test"
},
"ToList": [
{
"Address": "bridgetest@protonmail.com",
"Name": "Internal Bridge"
}
],
"CCList": [
{
"Address": "bridgetest2@protonmail.com",
"Name": "Internal Bridge 2"
}
],
"BCCList": []
}
}
"""
And packages are sent with API call
"""
{
"Packages":[
{
"Addresses":{
"bridgetest@protonmail.com":{
"Type":1
},
"bridgetest2@protonmail.com":{
"Type":1
}
},
"Type":1,
"MIMEType":"text/plain"
}
]
}
"""

View File

@ -17,62 +17,25 @@
package liveapi
import (
"fmt"
"github.com/nsf/jsondiff"
)
type fakeCall struct {
method string
path string
request []byte
}
func (ctl *Controller) recordCall(method, path string, request []byte) {
ctl.lock.Lock()
defer ctl.lock.Unlock()
ctl.calls = append(ctl.calls, &fakeCall{
method: method,
path: path,
request: request,
})
ctl.calls.Register(method, path, request)
}
func (ctl *Controller) PrintCalls() {
fmt.Println("API calls:")
for idx, call := range ctl.calls {
fmt.Printf("%02d: [%s] %s\n", idx+1, call.method, call.path)
if call.request != nil && string(call.request) != "null" {
fmt.Printf("\t%s\n", call.request)
}
}
ctl.calls.PrintCalls()
}
func (ctl *Controller) WasCalled(method, path string, expectedRequest []byte) bool {
for _, call := range ctl.calls {
if call.method != method || call.path != path {
continue
}
if string(expectedRequest) == "" {
return true
}
diff, _ := jsondiff.Compare(call.request, expectedRequest, &jsondiff.Options{})
isSuperset := diff == jsondiff.FullMatch || diff == jsondiff.SupersetMatch
if isSuperset {
return true
}
}
return false
return ctl.calls.WasCalled(method, path, expectedRequest)
}
func (ctl *Controller) WasCalledRegex(methodRegex, pathRegex string, expectedRequest []byte) (bool, error) {
return ctl.calls.WasCalledRegex(methodRegex, pathRegex, expectedRequest)
}
func (ctl *Controller) GetCalls(method, path string) [][]byte {
requests := [][]byte{}
for _, call := range ctl.calls {
if call.method == method && call.path == path {
requests = append(requests, call.request)
}
}
return requests
return ctl.calls.GetCalls(method, path)
}

View File

@ -22,6 +22,7 @@ import (
"sync"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/ProtonMail/proton-bridge/test/context/calls"
"github.com/sirupsen/logrus"
)
@ -30,7 +31,7 @@ type Controller struct {
log *logrus.Entry
// Internal states.
lock *sync.RWMutex
calls []*fakeCall
calls calls.Calls
messageIDsByUsername map[string][]string
// State controlled by test.
@ -41,7 +42,7 @@ func NewController(_ string) (*Controller, pmapi.Manager) {
controller := &Controller{
log: logrus.WithField("pkg", "live-controller"),
lock: &sync.RWMutex{},
calls: []*fakeCall{},
calls: calls.Calls{},
messageIDsByUsername: map[string][]string{},
noInternetConnection: false,

View File

@ -37,6 +37,7 @@ func SMTPActionsAuthFeatureContext(s *godog.Suite) {
s.Step(`^SMTP client sends message with bcc "([^"]*)"$`, smtpClientSendsMessageWithBCC)
s.Step(`^SMTP client "([^"]*)" sends message with bcc "([^"]*)"$`, smtpClientNamedSendsMessageWithBCC)
s.Step(`^SMTP client sends "([^"]*)"$`, smtpClientSendsCommand)
s.Step(`^SMTP client sends$`, smtpClientSendsCommandMultiline)
s.Step(`^SMTP client "([^"]*)" sends "([^"]*)"$`, smtpClientNamedSendsCommand)
}
@ -111,6 +112,9 @@ func smtpClientNamedSendsMessageWithBCC(clientID, bcc string, message *gherkin.D
func smtpClientSendsCommand(command string) error {
return smtpClientNamedSendsCommand("smtp", command)
}
func smtpClientSendsCommandMultiline(command *gherkin.DocString) error {
return smtpClientNamedSendsCommand("smtp", command.Content)
}
func smtpClientNamedSendsCommand(clientName, command string) error {
command = strings.ReplaceAll(command, "\\r", "\r")
command = strings.ReplaceAll(command, "\\n", "\n")