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: ReadMessage returns EOF in case of header-only messages. #47897

Open
0xc0d opened this issue Aug 22, 2021 · 4 comments · May be fixed by #47898
Open

net/mail: ReadMessage returns EOF in case of header-only messages. #47897

0xc0d opened this issue Aug 22, 2021 · 4 comments · May be fixed by #47898

Comments

@0xc0d
Copy link

@0xc0d 0xc0d commented Aug 22, 2021

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

$ go version
go version go1.16 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/user/Library/Caches/go-build"
GOENV="/Users/user/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/user/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/user/sdk/go1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/user/sdk/go1.16/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_r/pyfmdgr15fz3_x7hhfk7gb3r0000gp/T/go-build2586304111=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"mime"
	"mime/multipart"
	"net/mail"
	"os"
	"strings"
)

var email = strings.NewReader(`From: <abusedesk@example.com>
Date: Thu, 8 Mar 2005 17:40:36 EDT
Subject: FW: Earn money
To: <abuse@example.net>
MIME-Version: 1.0
Content-Type: multipart/report; report-type=feedback-report;
	boundary="part1_13d.2e68ed54_boundary"

--part1_13d.2e68ed54_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

This is an email abuse report for an email message received from IP
192.0.2.1 on Thu, 8 Mar 2005 14:00:00 EDT.  For more information
about this format please see http://www.mipassoc.org/arf/.

--part1_13d.2e68ed54_boundary
Content-Type: message/feedback-report

Feedback-Type: abuse
User-Agent: SomeGenerator/1.0
Version: 1

--part1_13d.2e68ed54_boundary
Content-Type: message/rfc822
Content-Disposition: inline

Received: from mailserver.example.net
	(mailserver.example.net [192.0.2.1])
	by example.com with ESMTP id M63d4137594e46;
	Thu, 08 Mar 2005 14:00:00 -0400
From: <somespammer@example.net>
To: <Undisclosed Recipients>
Subject: Earn money
MIME-Version: 1.0
Content-type: text/plain
Message-ID: 8787KJKJ3K4J3K4J3K4J3.mail@example.net
Date: Thu, 02 Sep 2004 12:31:03 -0500

Spam Spam Spam
Spam Spam Spam
Spam Spam Spam
Spam Spam Spam

--part1_13d.2e68ed54_boundary--
`)

func main() {
	msg, err := mail.ReadMessage(email)
	if err != nil {
		log.Fatal(err)
	}
	_, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
	if err != nil {
		log.Fatal(err)
	}
	mr := multipart.NewReader(msg.Body, params["boundary"])
	for {
		part, err := mr.NextPart()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		contentType := part.Header.Get("Content-Type")
		fmt.Printf("Content-Type: %v\n", contentType)

		b := new(bytes.Buffer)
		b.ReadFrom(part)
		fmt.Printf("Body: %q\n\n", b.String())


		switch {
		case strings.HasPrefix(contentType, "message"):
			msg, err = mail.ReadMessage(b)
			if err != nil {
				log.Println("error occurred:", err)
				break
			}
			io.Copy(os.Stdout, msg.Body)
		default:
			io.Copy(os.Stdout, b)
		}
		fmt.Println("-----------------------------")
	}
}

What did you expect to see?

Content-Type: text/plain; charset="US-ASCII"
Body: "This is an email abuse report for an email message received from IP\n192.0.2.1 on Thu, 8 Mar 2005 14:00:00 EDT.  For more information\nabout this format please see http://www.mipassoc.org/arf/.\n"

This is an email abuse report for an email message received from IP
192.0.2.1 on Thu, 8 Mar 2005 14:00:00 EDT.  For more information
about this format please see http://www.mipassoc.org/arf/.
-----------------------------
Content-Type: message/feedback-report
Body: "Feedback-Type: abuse\nUser-Agent: SomeGenerator/1.0\nVersion: 1\n"

-----------------------------
Content-Type: message/rfc822
Body: "Received: from mailserver.example.net\n\t(mailserver.example.net [192.0.2.1])\n\tby example.com with ESMTP id M63d4137594e46;\n\tThu, 08 Mar 2005 14:00:00 -0400\nFrom: <somespammer@example.net>\nTo: <Undisclosed Recipients>\nSubject: Earn money\nMIME-Version: 1.0\nContent-type: text/plain\nMessage-ID: 8787KJKJ3K4J3K4J3K4J3.mail@example.net\nDate: Thu, 02 Sep 2004 12:31:03 -0500\n\nSpam Spam Spam\nSpam Spam Spam\nSpam Spam Spam\nSpam Spam Spam\n"

Spam Spam Spam
Spam Spam Spam
Spam Spam Spam
Spam Spam Spam
-----------------------------

What did you see instead?

Content-Type: text/plain; charset="US-ASCII"
Body: "This is an email abuse report for an email message received from IP\n192.0.2.1 on Thu, 8 Mar 2005 14:00:00 EDT.  For more information\nabout this format please see http://www.mipassoc.org/arf/.\n"

This is an email abuse report for an email message received from IP
192.0.2.1 on Thu, 8 Mar 2005 14:00:00 EDT.  For more information
about this format please see http://www.mipassoc.org/arf/.
-----------------------------
Content-Type: message/feedback-report
Body: "Feedback-Type: abuse\nUser-Agent: SomeGenerator/1.0\nVersion: 1\n"

2021/08/23 00:47:39 error occurred: EOF
-----------------------------
Content-Type: message/rfc822
Body: "Received: from mailserver.example.net\n\t(mailserver.example.net [192.0.2.1])\n\tby example.com with ESMTP id M63d4137594e46;\n\tThu, 08 Mar 2005 14:00:00 -0400\nFrom: <somespammer@example.net>\nTo: <Undisclosed Recipients>\nSubject: Earn money\nMIME-Version: 1.0\nContent-type: text/plain\nMessage-ID: 8787KJKJ3K4J3K4J3K4J3.mail@example.net\nDate: Thu, 02 Sep 2004 12:31:03 -0500\n\nSpam Spam Spam\nSpam Spam Spam\nSpam Spam Spam\nSpam Spam Spam\n"

Spam Spam Spam
Spam Spam Spam
Spam Spam Spam
Spam Spam Spam
-----------------------------

Note that the example email is from RFC 5965.

I belive the ReadMessage should also support header-only messages.

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 22, 2021

Change https://golang.org/cl/344269 mentions this issue: net/mail: fix EOF error while reading header-only message

Loading

@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Aug 23, 2021

Loading

@dmitshur
Copy link
Contributor

@dmitshur dmitshur commented Nov 29, 2021

This may be the same as issue #33823. If so, @0xc0d, would it work well to merge this report into that issue?

Loading

@0xc0d
Copy link
Author

@0xc0d 0xc0d commented Nov 29, 2021

Yes it us the same issue, I have already opened a PR for that.
I guess we can merge this report safely. Thank you @dmitshur!

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants