From 3916ddc8e4a06c321b08cd6d3753ad8995b0666f Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Tue, 25 Aug 2020 16:29:51 +0200 Subject: [PATCH] fix: allow overriding sign via contact settings if set --- internal/smtp/preferences.go | 19 ++++++++--------- internal/smtp/preferences_test.go | 8 ++++---- internal/smtp/vcard_tools.go | 34 +++++++++++++++---------------- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/internal/smtp/preferences.go b/internal/smtp/preferences.go index 03f84185..97f84bfb 100644 --- a/internal/smtp/preferences.go +++ b/internal/smtp/preferences.go @@ -95,9 +95,8 @@ func (b *sendPreferencesBuilder) shouldEncrypt() bool { return false } -func (b *sendPreferencesBuilder) withSign() { - v := true - b.sign = &v +func (b *sendPreferencesBuilder) withSign(sign bool) { + b.sign = &sign } func (b *sendPreferencesBuilder) withSignDefault() { @@ -258,7 +257,7 @@ func (b *sendPreferencesBuilder) setInternalPGPSettings( // We always encrypt and sign internal mail. b.withEncrypt(true) - b.withSign() + b.withSign(true) // We use a custom scheme for internal messages. b.withScheme(pmInternal) @@ -369,7 +368,7 @@ func (b *sendPreferencesBuilder) setExternalPGPSettingsWithWKDKeys( // We always encrypt and sign external mail if WKD keys are present. b.withEncrypt(true) - b.withSign() + b.withSign(true) // If the contact has a specific Scheme preference, we set it (otherwise we // leave it unset to allow it to be filled in with the default value later). @@ -402,13 +401,13 @@ func (b *sendPreferencesBuilder) setExternalPGPSettingsWithoutWKDKeys( ) (err error) { b.withEncrypt(vCardData.Encrypt) - if !vCardData.SignMissing && vCardData.Sign { - b.withSign() + if vCardData.SignIsSet { + b.withSign(vCardData.Sign) } // Sign must be enabled whenever encrypt is. - if vCardData.Sign || vCardData.Encrypt { - b.withSign() + if vCardData.Encrypt { + b.withSign(true) } // If the contact has a specific Scheme preference, we set it (otherwise we @@ -479,7 +478,7 @@ func (b *sendPreferencesBuilder) setEncryptionPreferences(mailSettings pmapi.Mai } if b.shouldEncrypt() { - b.withSign() + b.withSign(true) } // If undefined, default to the user mail setting "Default PGP scheme". diff --git a/internal/smtp/preferences_test.go b/internal/smtp/preferences_test.go index 59a1937f..45f172f9 100644 --- a/internal/smtp/preferences_test.go +++ b/internal/smtp/preferences_test.go @@ -243,7 +243,7 @@ func TestPreferencesBuilder(t *testing.T) { { name: "external with sign enabled", - contactMeta: &ContactMetadata{Sign: true}, + contactMeta: &ContactMetadata{Sign: true, SignIsSet: true}, receivedKeys: []pmapi.PublicKey{}, isInternal: false, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"}, @@ -272,7 +272,7 @@ func TestPreferencesBuilder(t *testing.T) { { name: "external with pinned contact public key, encrypted and signed", - contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true}, + contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, SignIsSet: true}, receivedKeys: []pmapi.PublicKey{}, isInternal: false, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"}, @@ -287,7 +287,7 @@ func TestPreferencesBuilder(t *testing.T) { { name: "external with pinned contact public key, encrypted and signed using contact-specific pgp-inline", - contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, Scheme: pgpInline}, + contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, Scheme: pgpInline, SignIsSet: true}, receivedKeys: []pmapi.PublicKey{}, isInternal: false, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"}, @@ -302,7 +302,7 @@ func TestPreferencesBuilder(t *testing.T) { { name: "external with pinned contact public key, encrypted and signed using global pgp-inline", - contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true}, + contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, SignIsSet: true}, receivedKeys: []pmapi.PublicKey{}, isInternal: false, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPInlinePackage, DraftMIMEType: "text/html"}, diff --git a/internal/smtp/vcard_tools.go b/internal/smtp/vcard_tools.go index e84456c9..90cf5743 100644 --- a/internal/smtp/vcard_tools.go +++ b/internal/smtp/vcard_tools.go @@ -27,13 +27,13 @@ import ( ) type ContactMetadata struct { - Email string - Keys []string - Scheme string - Sign bool - SignMissing bool - Encrypt bool - MIMEType string + Email string + Keys []string + Scheme string + Sign bool + SignIsSet bool + Encrypt bool + MIMEType string } const ( @@ -72,22 +72,22 @@ func GetContactMetadataFromVCards(cards []pmapi.Card, email string) (contactMeta // Warn: ParseBool treats 1, T, True, true as true and 0, F, Fale, false as false. // However PMEL declares 'true' is true, 'false' is false. every other string is true encrypt, _ := strconv.ParseBool(parsedCard.GetValueByGroup(FieldPMEncrypt, group)) - var sign, signMissing bool + var sign, signIsSet bool if len(parsedCard[FieldPMSign]) == 0 { - signMissing = true + signIsSet = false } else { sign, _ = strconv.ParseBool(parsedCard.GetValueByGroup(FieldPMSign, group)) - signMissing = false + signIsSet = true } mimeType := parsedCard.GetValueByGroup(FieldPMMIMEType, group) return &ContactMetadata{ - Email: email, - Keys: keys, - Scheme: scheme, - Sign: sign, - SignMissing: signMissing, - Encrypt: encrypt, - MIMEType: mimeType, + Email: email, + Keys: keys, + Scheme: scheme, + Sign: sign, + SignIsSet: signIsSet, + Encrypt: encrypt, + MIMEType: mimeType, }, nil } return &ContactMetadata{}, nil