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

Closed
0xc0d opened this issue Aug 22, 2021 · 5 comments
Closed

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

0xc0d opened this issue Aug 22, 2021 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@0xc0d
Copy link
Contributor

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

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

@seankhliao
Copy link
Member

cc @bradfitz

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 23, 2021
@dmitshur
Copy link
Contributor

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

@0xc0d
Copy link
Contributor Author

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!

@seankhliao
Copy link
Member

Duplicate of #33823

@seankhliao seankhliao marked this as a duplicate of #33823 Jul 13, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2022
@golang golang locked and limited conversation to collaborators Jul 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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