From 56e7fdd4b849c5a7582fb4b7adf4d4065437f828 Mon Sep 17 00:00:00 2001 From: "fox.cpp" Date: Mon, 21 Oct 2019 01:47:09 +0300 Subject: [PATCH] authres: Don't quote address-like property values --- authres/format.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/authres/format.go b/authres/format.go index f907b4a..a922904 100644 --- a/authres/format.go +++ b/authres/format.go @@ -109,13 +109,26 @@ func formatValue(s string) string { } if shouldQuote { - // None of involved specs specify how to handle " in quoted strings - // so we just drop them and hope they are not criticial. - return `"` + strings.Replace(s, `"`, ``, -1) + `"` + return `"` + strings.Replace(s, `"`, `\"`, -1) + `"` } return s } +var addressOk = map[rune]struct{}{ + // Most ASCII punctuation except for: + // ( ) = " + // as these can cause issues due to ambiguous ABNF rules. + // I.e. technically mentioned characters can be left unquoted, but they can + // be interpreted as parts of non-quoted parameters or comments so it is + // better to quote them. + '#': {}, '$': {}, '%': {}, '&': {}, + '\'': {}, '*': {}, '+': {}, ',': {}, + '.': {}, '/': {}, '-': {}, '@': {}, + '[': {}, ']': {}, '\\': {}, '^': {}, + '_': {}, '`': {}, '{': {}, '|': {}, + '}': {}, '~': {}, +} + func formatPvalue(s string) string { // pvalue = [CFWS] ( value / [ [ local-part ] "@" ] domain-name ) // [CFWS] @@ -129,7 +142,7 @@ func formatPvalue(s string) string { // for others. addressLike := true for _, ch := range s { - if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) || ch != '@' { + if _, ok := addressOk[ch]; !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && !ok { addressLike = false } }