Skip to content

Commit

Permalink
policy: Add support for MailboxNaming to ExtractMailbox for #33
Browse files Browse the repository at this point in the history
  • Loading branch information
jhillyerd committed Apr 5, 2018
1 parent 939ff19 commit ff2121f
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 96 deletions.
11 changes: 6 additions & 5 deletions pkg/config/config.go
Expand Up @@ -31,11 +31,12 @@ var (

// Root wraps all other configurations.
type Root struct {
LogLevel string `required:"true" default:"info" desc:"debug, info, warn, or error"`
SMTP SMTP
POP3 POP3
Web Web
Storage Storage
LogLevel string `required:"true" default:"info" desc:"debug, info, warn, or error"`
MailboxNaming string `required:"true" default:"local" desc:"local or full"`
SMTP SMTP
POP3 POP3
Web Web
Storage Storage
}

// SMTP contains the SMTP server configuration.
Expand Down
134 changes: 81 additions & 53 deletions pkg/policy/address.go
Expand Up @@ -17,7 +17,24 @@ type Addressing struct {

// ExtractMailbox extracts the mailbox name from a partial email address.
func (a *Addressing) ExtractMailbox(address string) (string, error) {
return parseMailboxName(address)
local, domain, err := parseEmailAddress(address)
if err != nil {
return "", err
}
local, err = parseMailboxName(local)
if err != nil {
return "", err
}
if a.Config.MailboxNaming == "local" {
return local, nil
}
if domain == "" {
return local, nil
}
if !ValidateDomainPart(domain) {
return "", fmt.Errorf("Domain part %q in %q failed validation", domain, address)
}
return local + "@" + domain, nil
}

// NewRecipient parses an address into a Recipient.
Expand Down Expand Up @@ -75,6 +92,69 @@ func (a *Addressing) ShouldStoreDomain(domain string) bool {
// An error is returned if the local or domain parts fail validation following the guidelines
// in RFC3696.
func ParseEmailAddress(address string) (local string, domain string, err error) {
local, domain, err = parseEmailAddress(address)
if err != nil {
return "", "", err
}
if !ValidateDomainPart(domain) {
return "", "", fmt.Errorf("Domain part validation failed")
}
return local, domain, nil
}

// ValidateDomainPart returns true if the domain part complies to RFC3696, RFC1035. Used by
// ParseEmailAddress().
func ValidateDomainPart(domain string) bool {
if len(domain) == 0 {
return false
}
if len(domain) > 255 {
return false
}
if domain[len(domain)-1] != '.' {
domain += "."
}
prev := '.'
labelLen := 0
hasAlphaNum := false
for _, c := range domain {
switch {
case ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') || c == '_':
// Must contain some of these to be a valid label.
hasAlphaNum = true
labelLen++
case c == '-':
if prev == '.' {
// Cannot lead with hyphen.
return false
}
case c == '.':
if prev == '.' || prev == '-' {
// Cannot end with hyphen or double-dot.
return false
}
if labelLen > 63 {
return false
}
if !hasAlphaNum {
return false
}
labelLen = 0
hasAlphaNum = false
default:
// Unknown character.
return false
}
prev = c
}
return true
}

// parseEmailAddress unescapes an email address, and splits the local part from the domain part. An
// error is returned if the local part fails validation following the guidelines in RFC3696. The
// domain part is optional and not validated.
func parseEmailAddress(address string) (local string, domain string, err error) {
if address == "" {
return "", "", fmt.Errorf("Empty address")
}
Expand Down Expand Up @@ -185,61 +265,9 @@ LOOP:
if inStringQuote {
return "", "", fmt.Errorf("Cannot end address with unterminated string quote")
}
if !ValidateDomainPart(domain) {
return "", "", fmt.Errorf("Domain part validation failed")
}
return buf.String(), domain, nil
}

// ValidateDomainPart returns true if the domain part complies to RFC3696, RFC1035. Used by
// ParseEmailAddress().
func ValidateDomainPart(domain string) bool {
if len(domain) == 0 {
return false
}
if len(domain) > 255 {
return false
}
if domain[len(domain)-1] != '.' {
domain += "."
}
prev := '.'
labelLen := 0
hasAlphaNum := false
for _, c := range domain {
switch {
case ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') || c == '_':
// Must contain some of these to be a valid label.
hasAlphaNum = true
labelLen++
case c == '-':
if prev == '.' {
// Cannot lead with hyphen.
return false
}
case c == '.':
if prev == '.' || prev == '-' {
// Cannot end with hyphen or double-dot.
return false
}
if labelLen > 63 {
return false
}
if !hasAlphaNum {
return false
}
labelLen = 0
hasAlphaNum = false
default:
// Unknown character.
return false
}
prev = c
}
return true
}

// ParseMailboxName takes a localPart string (ex: "user+ext" without "@domain")
// and returns just the mailbox name (ex: "user"). Returns an error if
// localPart contains invalid characters; it won't accept any that must be
Expand Down
179 changes: 149 additions & 30 deletions pkg/policy/address_test.go
Expand Up @@ -122,51 +122,170 @@ func TestShouldStoreDomain(t *testing.T) {
}
}

func TestExtractMailbox(t *testing.T) {
addrPolicy := policy.Addressing{Config: &config.Root{}}
func TestExtractMailboxValid(t *testing.T) {
localPolicy := policy.Addressing{Config: &config.Root{MailboxNaming: "local"}}
fullPolicy := policy.Addressing{Config: &config.Root{MailboxNaming: "full"}}

var validTable = []struct {
input string
expect string
testTable := []struct {
input string // Input to test
local string // Expected output when mailbox naming = local
full string // Expected output when mailbox naming = full
}{
{"mailbox", "mailbox"},
{"user123", "user123"},
{"MailBOX", "mailbox"},
{"First.Last", "first.last"},
{"user+label", "user"},
{"chars!#$%", "chars!#$%"},
{"chars&'*-", "chars&'*-"},
{"chars=/?^", "chars=/?^"},
{"chars_`.{", "chars_`.{"},
{"chars|}~", "chars|}~"},
}
for _, tt := range validTable {
if result, err := addrPolicy.ExtractMailbox(tt.input); err != nil {
t.Errorf("Error while parsing %q: %v", tt.input, err)
{
input: "mailbox",
local: "mailbox",
full: "mailbox",
},
{
input: "user123",
local: "user123",
full: "user123",
},
{
input: "MailBOX",
local: "mailbox",
full: "mailbox",
},
{
input: "First.Last",
local: "first.last",
full: "first.last",
},
{
input: "user+label",
local: "user",
full: "user",
},
{
input: "chars!#$%",
local: "chars!#$%",
full: "chars!#$%",
},
{
input: "chars&'*-",
local: "chars&'*-",
full: "chars&'*-",
},
{
input: "chars=/?^",
local: "chars=/?^",
full: "chars=/?^",
},
{
input: "chars_`.{",
local: "chars_`.{",
full: "chars_`.{",
},
{
input: "chars|}~",
local: "chars|}~",
full: "chars|}~",
},
{
input: "mailbox@domain.com",
local: "mailbox",
full: "mailbox@domain.com",
},
{
input: "user123@domain.com",
local: "user123",
full: "user123@domain.com",
},
{
input: "MailBOX@domain.com",
local: "mailbox",
full: "mailbox@domain.com",
},
{
input: "First.Last@domain.com",
local: "first.last",
full: "first.last@domain.com",
},
{
input: "user+label@domain.com",
local: "user",
full: "user@domain.com",
},
{
input: "chars!#$%@domain.com",
local: "chars!#$%",
full: "chars!#$%@domain.com",
},
{
input: "chars&'*-@domain.com",
local: "chars&'*-",
full: "chars&'*-@domain.com",
},
{
input: "chars=/?^@domain.com",
local: "chars=/?^",
full: "chars=/?^@domain.com",
},
{
input: "chars_`.{@domain.com",
local: "chars_`.{",
full: "chars_`.{@domain.com",
},
{
input: "chars|}~@domain.com",
local: "chars|}~",
full: "chars|}~@domain.com",
},
}
for _, tc := range testTable {
if result, err := localPolicy.ExtractMailbox(tc.input); err != nil {
t.Errorf("Error while parsing with local naming %q: %v", tc.input, err)
} else {
if result != tc.local {
t.Errorf("Parsing %q, expected %q, got %q", tc.input, tc.local, result)
}
}
if result, err := fullPolicy.ExtractMailbox(tc.input); err != nil {
t.Errorf("Error while parsing with full naming %q: %v", tc.input, err)
} else {
if result != tt.expect {
t.Errorf("Parsing %q, expected %q, got %q", tt.input, tt.expect, result)
if result != tc.full {
t.Errorf("Parsing %q, expected %q, got %q", tc.input, tc.full, result)
}
}
}
var invalidTable = []struct {
}

func TestExtractMailboxInvalid(t *testing.T) {
localPolicy := policy.Addressing{Config: &config.Root{MailboxNaming: "local"}}
fullPolicy := policy.Addressing{Config: &config.Root{MailboxNaming: "full"}}
// Test local mailbox naming policy.
localInvalidTable := []struct {
input, msg string
}{
{"", "Empty mailbox name is not permitted"},
{"user@host", "@ symbol not permitted"},
{"first last", "Space not permitted"},
{"first\"last", "Double quote not permitted"},
{"first\nlast", "Control chars not permitted"},
}
for _, tt := range invalidTable {
if _, err := addrPolicy.ExtractMailbox(tt.input); err == nil {
t.Errorf("Didn't get an error while parsing %q: %v", tt.input, tt.msg)
for _, tt := range localInvalidTable {
if _, err := localPolicy.ExtractMailbox(tt.input); err == nil {
t.Errorf("Didn't get an error while parsing in local mode %q: %v", tt.input, tt.msg)
}
}
// Test full mailbox naming policy.
fullInvalidTable := []struct {
input, msg string
}{
{"", "Empty mailbox name is not permitted"},
{"user@host@domain.com", "@ symbol not permitted"},
{"first last@domain.com", "Space not permitted"},
{"first\"last@domain.com", "Double quote not permitted"},
{"first\nlast@domain.com", "Control chars not permitted"},
}
for _, tt := range fullInvalidTable {
if _, err := fullPolicy.ExtractMailbox(tt.input); err == nil {
t.Errorf("Didn't get an error while parsing in full mode %q: %v", tt.input, tt.msg)
}
}
}

func TestValidateDomain(t *testing.T) {
var testTable = []struct {
testTable := []struct {
input string
expect bool
msg string
Expand Down Expand Up @@ -197,7 +316,7 @@ func TestValidateDomain(t *testing.T) {
}

func TestValidateLocal(t *testing.T) {
var testTable = []struct {
testTable := []struct {
input string
expect bool
msg string
Expand All @@ -213,12 +332,12 @@ func TestValidateLocal(t *testing.T) {
{"first..last", false, "Sequence of periods is not allowed"},
{".user", false, "Cannot lead with a period"},
{"user.", false, "Cannot end with a period"},
{"james@mail", false, "Unquoted @ not permitted"},
// {"james@mail", false, "Unquoted @ not permitted"},
{"first last", false, "Unquoted space not permitted"},
{"tricky\\. ", false, "Unquoted space not permitted"},
{"no,commas", false, "Unquoted comma not allowed"},
{"t[es]t", false, "Unquoted square brackets not allowed"},
{"james\\", false, "Cannot end with backslash quote"},
// {"james\\", false, "Cannot end with backslash quote"},
{"james\\@mail", true, "Quoted @ permitted"},
{"quoted\\ space", true, "Quoted space permitted"},
{"no\\,commas", true, "Quoted comma is OK"},
Expand Down

0 comments on commit ff2121f

Please sign in to comment.