Skip to content

net/textproto: canonicalMIMEHeaderKey returns a "malformed MIME header line:" for header containing / #68590

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

Closed
kb4rodrigog opened this issue Jul 25, 2024 · 7 comments
Labels
Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@kb4rodrigog
Copy link

Go version

go1.22.5

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/user/Library/Caches/go-build'
GOENV='/Users/user/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
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='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/user/projects/project/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/g0/r8qb78z13c3_x62jqrr_6xqm0000gp/T/go-build1410450898=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I tried to get the mime headers passing an email header

	tr := textproto.NewReader(bufio.NewReader(bytes.NewReader(b)))
	headers, err := tr.ReadMIMEHeader()

What did you see happen?

I got this error:
malformed MIME header line: X-Hdr-a/b

What did you expect to see?

I expect to see this mime header along with the others in the headers object.

The issue happens because the function validHeaderFieldByte is missing the / character in the list of valid ones

https://datatracker.ietf.org/doc/html/rfc2822#section-3.6.8

optional-field  =       field-name ":" unstructured CRLF

field-name      =       1*ftext

ftext           =       %d33-57 /               ; Any character except
                        %d59-126                ;  controls, SP, and
                                                ;  ":".

The validation was already missing the character in version 1.19, but the canonicalMIMEHeaderKey function was not returning the boolean value to indicate as a !ok to the readMIMEHeader function.

// validHeaderFieldByte reports whether b is a valid byte in a header
// field name. RFC 7230 says:
//
//	header-field   = field-name ":" OWS field-value OWS
//	field-name     = token
//	tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
//	        "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
//	token = 1*tchar
func validHeaderFieldByte(b byte) bool {
	return int(b) < len(isTokenTable) && isTokenTable[b]
}

// canonicalMIMEHeaderKey is like CanonicalMIMEHeaderKey but is
// allowed to mutate the provided byte slice before returning the
// string.
//
// For invalid inputs (if a contains spaces or non-token bytes), a
// is unchanged and a string copy is returned.
func canonicalMIMEHeaderKey(a []byte) string {
	// See if a looks like a header key. If not, return it unchanged.
	for _, c := range a {
		if validHeaderFieldByte(c) {
			continue
		}
		// Don't canonicalize.
		return string(a)
	}
  ...
}
@dcormier
Copy link
Contributor

dcormier commented Jul 25, 2024

Seeing as the textproto package docs state it implements support for the likes of SMTP, it seems like headers from those relevant RFCs should be supported.

Package textproto implements generic support for text-based request/response protocols in the style of HTTP, NNTP, and SMTP.

@seankhliao seankhliao changed the title textproto/reader: canonicalMIMEHeaderKey returns a "malformed MIME header line:" error for a valid header net/textproto: canonicalMIMEHeaderKey returns a "malformed MIME header line:" for header containing / Jul 25, 2024
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Jul 25, 2024

For #53188 we restricted the set of permitted characters in the textproto package to conform to RFC 7230. We aren't going to go back on that. For the specific case of mail messages we added CL 504416 to permit more characters for mail messages, as part of #58862 and #60332.

I don't think there is anything to do here. Sorry.

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 25, 2024
@dcormier
Copy link
Contributor

dcormier commented Jul 26, 2024

The docs on textproto state it's intended for use with SMTP. If that's not the case, should those docs be updated to reflect that?

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/601555 mentions this issue: net/textproto: document enforcement of RFC 7230 for headers

@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2024
gopherbot pushed a commit that referenced this issue Mar 4, 2025
Fixes #68590

Change-Id: Ie7cf1fe8379182f86317d5ebb7f45a404ecd70e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/601555
Auto-Submit: Damien Neil <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
@dmitshur dmitshur added Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Mar 4, 2025
@dmitshur dmitshur added this to the Go1.25 milestone Mar 4, 2025
@dmitshur
Copy link
Member

dmitshur commented Mar 4, 2025

Closed by merging CL 601555 (commit 3b9d10c).

@dmitshur dmitshur closed this as completed Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants