| 1 |
From: James Hillyerd <[email protected]> |
| 2 |
Date: Thu, 2 Feb 2023 11:50:20 -0800 |
| 3 |
Subject: fix: Reject invalid MIME header name characters to support Go 1.20 |
| 4 |
(#270) |
| 5 |
|
| 6 |
* Test with go 1.20 release candidate |
| 7 |
* header: Detect invalid header name characters for Go 1.20 |
| 8 |
* header: test for permitted special chars |
| 9 |
|
| 10 |
Origin: backport, https://github.com/jhillyerd/enmime/commit/415ff72f |
| 11 |
--- |
| 12 |
header.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++--------- |
| 13 |
header_test.go | 18 +++++++++++++----- |
| 14 |
2 files changed, 63 insertions(+), 14 deletions(-) |
| 15 |
|
| 16 |
diff --git a/header.go b/header.go |
| 17 |
index cd9d28b..628e765 100644 |
| 18 |
--- a/header.go |
| 19 |
+++ b/header.go |
| 20 |
@@ -81,9 +81,9 @@ var AddressHeaders = map[string]bool{ |
| 21 |
// ParseMediaType is a more tolerant implementation of Go's mime.ParseMediaType function. |
| 22 |
// |
| 23 |
// Tolerances accounted for: |
| 24 |
-// * Missing ';' between content-type and media parameters |
| 25 |
-// * Repeating media parameters |
| 26 |
-// * Unquoted values in media parameters containing 'tspecials' characters |
| 27 |
+// - Missing ';' between content-type and media parameters |
| 28 |
+// - Repeating media parameters |
| 29 |
+// - Unquoted values in media parameters containing 'tspecials' characters |
| 30 |
func ParseMediaType(ctype string) (mtype string, params map[string]string, invalidParams []string, |
| 31 |
err error) { |
| 32 |
// Export of internal function. |
| 33 |
@@ -97,6 +97,7 @@ func readHeader(r *bufio.Reader, p *Part) (textproto.MIMEHeader, error) { |
| 34 |
buf := &bytes.Buffer{} |
| 35 |
tp := textproto.NewReader(r) |
| 36 |
firstHeader := true |
| 37 |
+line: |
| 38 |
for { |
| 39 |
// Pull out each line of the headers as a temporary slice s |
| 40 |
s, err := tp.ReadLineBytes() |
| 41 |
@@ -119,18 +120,29 @@ func readHeader(r *bufio.Reader, p *Part) (textproto.MIMEHeader, error) { |
| 42 |
continue |
| 43 |
} |
| 44 |
if firstColon > 0 { |
| 45 |
- // Contains a colon, treat as a new header line |
| 46 |
- if !firstHeader { |
| 47 |
- // New Header line, end the previous |
| 48 |
- buf.Write([]byte{'\r', '\n'}) |
| 49 |
- } |
| 50 |
- |
| 51 |
// Behavior change in net/textproto package in Golang 1.12.10 and 1.13.1: |
| 52 |
// A space preceding the first colon in a header line is no longer handled |
| 53 |
// automatically due to CVE-2019-16276 which takes advantage of this |
| 54 |
// particular violation of RFC-7230 to exploit HTTP/1.1 |
| 55 |
if bytes.Contains(s[:firstColon+1], []byte{' ', ':'}) { |
| 56 |
s = bytes.Replace(s, []byte{' ', ':'}, []byte{':'}, 1) |
| 57 |
+ firstColon = bytes.IndexByte(s, ':') |
| 58 |
+ } |
| 59 |
+ |
| 60 |
+ // Behavior change in net/textproto package in Golang 1.20: invalid characters |
| 61 |
+ // in header keys are no longer allowed; https://github.com/golang/go/issues/53188 |
| 62 |
+ for _, c := range s[:firstColon] { |
| 63 |
+ if c != ' ' && !validHeaderFieldByte(c) { |
| 64 |
+ p.addError( |
| 65 |
+ ErrorMalformedHeader, "Header name %q contains invalid character %q", s, c) |
| 66 |
+ continue line |
| 67 |
+ } |
| 68 |
+ } |
| 69 |
+ |
| 70 |
+ // Contains a colon, treat as a new header line |
| 71 |
+ if !firstHeader { |
| 72 |
+ // New Header line, end the previous |
| 73 |
+ buf.Write([]byte{'\r', '\n'}) |
| 74 |
} |
| 75 |
|
| 76 |
s = textproto.TrimBytes(s) |
| 77 |
@@ -213,3 +225,32 @@ func quotedDisplayName(s string) string { |
| 78 |
func whiteSpaceRune(r rune) bool { |
| 79 |
return r == ' ' || r == '\t' || r == '\r' || r == '\n' |
| 80 |
} |
| 81 |
+ |
| 82 |
+// Mirror of func in net/textproto/reader.go; from go 1.20.0-rc.2 |
| 83 |
+func validHeaderFieldByte(c byte) bool { |
| 84 |
+ // mask is a 128-bit bitmap with 1s for allowed bytes, |
| 85 |
+ // so that the byte c can be tested with a shift and an and. |
| 86 |
+ // If c >= 128, then 1<<c and 1<<(c-64) will both be zero, |
| 87 |
+ // and this function will return false. |
| 88 |
+ const mask = 0 | |
| 89 |
+ (1<<(10)-1)<<'0' | |
| 90 |
+ (1<<(26)-1)<<'a' | |
| 91 |
+ (1<<(26)-1)<<'A' | |
| 92 |
+ 1<<'!' | |
| 93 |
+ 1<<'#' | |
| 94 |
+ 1<<'$' | |
| 95 |
+ 1<<'%' | |
| 96 |
+ 1<<'&' | |
| 97 |
+ 1<<'\'' | |
| 98 |
+ 1<<'*' | |
| 99 |
+ 1<<'+' | |
| 100 |
+ 1<<'-' | |
| 101 |
+ 1<<'.' | |
| 102 |
+ 1<<'^' | |
| 103 |
+ 1<<'_' | |
| 104 |
+ 1<<'`' | |
| 105 |
+ 1<<'|' | |
| 106 |
+ 1<<'~' |
| 107 |
+ return ((uint64(1)<<c)&(mask&(1<<64-1)) | |
| 108 |
+ (uint64(1)<<(c-64))&(mask>>64)) != 0 |
| 109 |
+} |
| 110 |
diff --git a/header_test.go b/header_test.go |
| 111 |
index 01055be..5b18568 100644 |
| 112 |
--- a/header_test.go |
| 113 |
+++ b/header_test.go |
| 114 |
@@ -99,8 +99,6 @@ func TestReadHeader(t *testing.T) { |
| 115 |
{ |
| 116 |
label: "missing name", |
| 117 |
input: ": line1=foo\r\n", |
| 118 |
- hname: "", |
| 119 |
- want: "", |
| 120 |
correct: false, |
| 121 |
}, |
| 122 |
{ |
| 123 |
@@ -127,12 +125,17 @@ func TestReadHeader(t *testing.T) { |
| 124 |
correct: true, |
| 125 |
}, |
| 126 |
{ |
| 127 |
- label: "equals in name", |
| 128 |
- input: "name=value:text\n", |
| 129 |
- hname: "name=value", |
| 130 |
+ label: "permitted specials in name", |
| 131 |
+ input: "Begin!#$%&'*+-.^_`|~End: text\n", |
| 132 |
+ hname: "Begin!#$%&'*+-.^_`|~End", |
| 133 |
want: "text", |
| 134 |
correct: true, |
| 135 |
}, |
| 136 |
+ { |
| 137 |
+ label: "equals in name", |
| 138 |
+ input: "name=value:text\n", |
| 139 |
+ correct: false, |
| 140 |
+ }, |
| 141 |
{ |
| 142 |
label: "no space before continuation", |
| 143 |
input: "X-Bad-Continuation: line1=foo;\nline2=bar\n", |
| 144 |
@@ -236,6 +239,11 @@ func TestReadHeader(t *testing.T) { |
| 145 |
gotErrs := len(p.Errors) |
| 146 |
if gotErrs != wantErrs { |
| 147 |
t.Errorf("Got %v p.Errors, want %v", gotErrs, wantErrs) |
| 148 |
+ if gotErrs > 0 { |
| 149 |
+ for _, e := range p.Errors { |
| 150 |
+ t.Log(e) |
| 151 |
+ } |
| 152 |
+ } |
| 153 |
} |
| 154 |
|
| 155 |
// Check for extra headers by removing expected ones. |