Skip to content

Commit 61b9de9

Browse files
authored
Merge pull request #3 from qhenkart/optional-dkim
optional dkim
2 parents d858e52 + d4e23bc commit 61b9de9

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

helpers/inbound/inbound.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,37 @@ func (email *ParsedEmail) parseHeaders(headers string) {
233233
}
234234
}
235235

236+
// ValidateConfig adds configuration options for validating a parsed email. Specifically turning DKIM as optional. DKIM validates emails in transit from tampering. However many valid email clients
237+
// do not have it configured
238+
// SPF could potentially be added but it poses a more significant security risk
239+
type ValidateConfig struct {
240+
dkimOptional bool
241+
}
242+
243+
// NewValidateConfig returns a configuration struct for validating the parsed email. Specifically turning DKIM as optional. DKIM validates emails in transit from tampering. However many valid email clients
244+
// do not have it configured
245+
func NewValidateConfig() ValidateConfig {
246+
return ValidateConfig{}
247+
}
248+
249+
// WithDKIMOptional validates DKIM values only if they exist. This allows the inbound client to support more email clients that might not support this configuration
250+
func (vc ValidateConfig) WithDKIMOptional() ValidateConfig {
251+
vc.dkimOptional = true
252+
return vc
253+
}
254+
236255
// Validate validates the DKIM and SPF scores to ensure that the email client and address was not spoofed
237-
func (email *ParsedEmail) Validate() error {
238-
if len(email.rawValues["dkim"]) == 0 || len(email.rawValues["SPF"]) == 0 {
239-
return fmt.Errorf("missing DKIM and SPF score")
256+
func (email *ParsedEmail) Validate(config ...ValidateConfig) error {
257+
var dkimOptional bool
258+
if len(config) > 0 {
259+
dkimOptional = config[0].dkimOptional
260+
}
261+
if len(email.rawValues["SPF"]) == 0 {
262+
return fmt.Errorf("missing SPF score")
263+
}
264+
265+
if !dkimOptional && len(email.rawValues["dkim"]) == 0 {
266+
return fmt.Errorf("missing DKIM score")
240267
}
241268

242269
for _, val := range email.rawValues["dkim"] {

helpers/inbound/inbound_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,19 @@ func TestValidate(t *testing.T) {
136136
name string
137137
values map[string][]string
138138
expectedError error
139+
config ValidateConfig
139140
}{
140141
{
141-
name: "MissingHeaders",
142+
name: "MissingSPFHeaders",
142143
values: map[string][]string{},
143-
expectedError: fmt.Errorf("missing DKIM and SPF score"),
144+
expectedError: fmt.Errorf("missing SPF score"),
144145
},
146+
{
147+
name: "MissingDKIMHeaders",
148+
values: map[string][]string{"SPF": {"pass"}},
149+
expectedError: fmt.Errorf("missing DKIM score"),
150+
},
151+
145152
{
146153
name: "FailedDkim",
147154
values: map[string][]string{"dkim": {"pass", "fail", "pass"}, "SPF": {"pass"}},
@@ -161,13 +168,29 @@ func TestValidate(t *testing.T) {
161168
name: "success",
162169
values: map[string][]string{"dkim": {"pass", "pass", "pass"}, "SPF": {"pass", "pass", "pass"}},
163170
},
171+
{
172+
name: "dkimOptionalAndPresent",
173+
values: map[string][]string{"dkim": {"pass", "pass", "pass"}, "SPF": {"pass", "pass", "pass"}},
174+
config: NewValidateConfig().WithDKIMOptional(),
175+
},
176+
{
177+
name: "dkimOptionalWithFailure",
178+
values: map[string][]string{"dkim": {"fail", "pass", "pass"}, "SPF": {"pass", "pass", "pass"}},
179+
config: NewValidateConfig().WithDKIMOptional(),
180+
expectedError: fmt.Errorf("DKIM validation failed"),
181+
},
182+
{
183+
name: "dkimOptionalAndAbsent",
184+
values: map[string][]string{"SPF": {"pass", "pass", "pass"}},
185+
config: NewValidateConfig().WithDKIMOptional(),
186+
},
164187
}
165188

166189
for _, test := range tests {
167190
t.Run(test.name, func(subTest *testing.T) {
168191
//Load POST body
169192
email := ParsedEmail{rawValues: test.values}
170-
err := email.Validate()
193+
err := email.Validate(test.config)
171194

172195
if test.expectedError != nil {
173196
assert.EqualError(subTest, test.expectedError, err.Error())

0 commit comments

Comments
 (0)