From 21ad7a24524ebf0fedb34cb15f5f3e26753f0fd0 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Mon, 4 Nov 2013 14:00:23 -0800 Subject: [PATCH] Checkpoint before converting Validator->Parser --- smtpd/utils.go | 18 +++++++++++++----- smtpd/utils_test.go | 9 ++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/smtpd/utils.go b/smtpd/utils.go index 4d5a582..9af1d3f 100644 --- a/smtpd/utils.go +++ b/smtpd/utils.go @@ -105,6 +105,7 @@ func ValidateLocalPart(local string) bool { prev := byte('.') inCharQuote := false + inStringQuote := false for i := 0; i < length; i++ { c := local[i] switch { @@ -125,18 +126,25 @@ func ValidateLocalPart(local string) bool { } case c == '\\': inCharQuote = true + case c == '"': + if inCharQuote { + inCharQuote = false + } else { + inStringQuote = !inStringQuote + } case c > 127: return false default: - if ! inCharQuote { - return false + if inCharQuote || inStringQuote { + inCharQuote = false + return true } - inCharQuote = false + return false } prev = c } - if inCharQuote { - // Can't end with unused backslash quote + if inCharQuote || inStringQuote { + // Can't end with unused backslash quote or unterminated string quote return false } diff --git a/smtpd/utils_test.go b/smtpd/utils_test.go index 6f4e642..b0d4ad4 100644 --- a/smtpd/utils_test.go +++ b/smtpd/utils_test.go @@ -82,7 +82,14 @@ func TestValidateLocal(t *testing.T) { {"one\\$\\|", true, "Should be able to quote plain specials"}, {"return\\\r", true, "Should be able to quote ASCII control chars"}, {"high\\\x80", false, "Should not accept > 7-bit quoted chars"}, - //{"\"james\"", true, "Quoted a-z is permitted"}, + {"quote\\\"", true, "Quoted double quote is permitted"}, + {"\"james\"", true, "Quoted a-z is permitted"}, + {"\"first last\"", true, "Quoted space is permitted"}, + {"\"quoted@sign\"", true, "Quoted @ is allowed"}, + {"\"qp\\\"quote\"", true, "Quoted quote within quoted string is OK"}, + {"\"unterminated", false, "Quoted string must be terminated"}, + {"\"unterminated\\\"", false, "Quoted string must be terminated"}, + {"embed\"quote\"string", false, "Embedded quoted string is illegal"}, {"user+mailbox", true, "RFC3696 test case should be valid"}, {"customer/department=shipping", true, "RFC3696 test case should be valid"},