Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/mail: AddressParser wrong name after decoding #19363

Closed
minaevmike opened this issue Mar 2, 2017 · 5 comments
Closed

net/mail: AddressParser wrong name after decoding #19363

minaevmike opened this issue Mar 2, 2017 · 5 comments
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@minaevmike
Copy link
Contributor

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/prog/GO"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build757954020=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

I am trying to decode email header with mail.AddressParser but when i have multiply rfc2047 decoded strings a space appears. As i undestand this happens because of joining atom's in header with space: https://github.com/golang/go/blob/master/src/net/mail/message.go#L421. I am not sure but mb it is better to decode whole header before it's parsing?
https://play.golang.org/p/GjZ2HqFgT8

What did you expect to see?

I expect to see that o.Name is equal to Михаил Минаев

What did you see instead?

i see Михаил Мина ев (with space)

@josharian josharian changed the title net/mail AddressParser wrong name after decoding net/mail: AddressParser wrong name after decoding Mar 2, 2017
@minaevmike
Copy link
Contributor Author

mb possible fix can be like this

diff --git a/src/net/mail/message.go b/src/net/mail/message.go
index 702b765..88d32f5 100644
--- a/src/net/mail/message.go
+++ b/src/net/mail/message.go
@@ -402,9 +402,6 @@ func (p *addrParser) consumePhrase() (phrase string, err error) {
                        // We actually parse dot-atom here to be more permissive
                        // than what RFC 5322 specifies.
                        word, err = p.consumeAtom(true, true)
-                       if err == nil {
-                               word, err = p.decodeRFC2047Word(word)
-                       }
                }
 
                if err != nil {
@@ -419,7 +416,7 @@ func (p *addrParser) consumePhrase() (phrase string, err error) {
                return "", fmt.Errorf("mail: missing word in phrase: %v", err)
        }
        phrase = strings.Join(words, " ")
-       return phrase, nil
+       return p.decodeRFC2047Word(phrase)
 }
 
 // consumeQuotedString parses the quoted string at the start of p.
@@ -545,11 +542,10 @@ func (p *addrParser) decodeRFC2047Word(s string) (string, error) {
                return p.dec.DecodeHeader(s)
        }
 
-       dec, err := rfc2047Decoder.Decode(s)
+       dec, err := rfc2047Decoder.DecodeHeader(s)
        if err == nil {
                return dec, nil
        }
-
        if _, ok := err.(charsetError); ok {
                return s, err
        }
diff --git a/src/net/mail/message_test.go b/src/net/mail/message_test.go
index f0761ab..3abd8c1 100644
--- a/src/net/mail/message_test.go
+++ b/src/net/mail/message_test.go
@@ -565,10 +565,6 @@ func TestAddressString(t *testing.T) {
                        `=?utf-8?b?QsO2YiwgSmFjw7Zi?= <bob@example.com>`,
                },
                {
-                       &Address{Name: "=??Q?x?=", Address: "hello@world.com"},
-                       `"=??Q?x?=" <hello@world.com>`,
-               },
-               {
                        &Address{Name: "=?hello", Address: "hello@world.com"},
                        `"=?hello" <hello@world.com>`,
                },

@bradfitz
Copy link
Contributor

bradfitz commented Mar 3, 2017

Sorry, we can't accept or discuss code on the issue tracker.

Please upload to Gerrit. See https://golang.org/doc/contribute.html

@bradfitz bradfitz added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. help wanted labels Mar 3, 2017
@bradfitz bradfitz added this to the Go1.9 milestone Mar 3, 2017
@hirochachacha
Copy link
Contributor

@minaevmike rfc2047 says: + An 'encoded-word' MUST NOT appear within a 'quoted-string'.
So, I sent https://go-review.googlesource.com/#/c/37811/. Let me know if you disagree.

@gopherbot
Copy link

CL https://golang.org/cl/37811 mentions this issue.

@minaevmike
Copy link
Contributor Author

minaevmike commented Mar 6, 2017

@hirochachacha thank you for your fix. I think that it's good enough.

@golang golang locked and limited conversation to collaborators Mar 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants