Skip to content

net/textproto: CanonicalMIMEHeaderKey panic on strings with spaces #6712

Description

@bradfitz
CanonicalMIMEHeaderKey panics on certain inputs containing spaces.

Reported from a Google user.

e.g. "C Ontent-Transfer-Encoding" will crash.

I'm getting hg codereview SSL errors now (gogo-inflight SSL MITM?) but fix is here:

diff -r 78cebfb89b21 src/pkg/net/textproto/reader.go
--- a/src/pkg/net/textproto/reader.go   Fri Nov 01 09:18:35 2013 -0700
+++ b/src/pkg/net/textproto/reader.go   Sun Nov 03 13:58:32 2013 -0800
@@ -574,13 +574,10 @@
        // and upper case after each dash.
        // (Host, User-Agent, If-Modified-Since).
        // MIME headers are ASCII only, so no Unicode issues.
-       if a[i] == ' ' {
-           a[i] = '-'
-           upper = true
-           continue
-       }
        c := a[i]
-       if upper && 'a' <= c && c <= 'z' {
+       if c == ' ' {
+           c = '-'
+       } else if upper && 'a' <= c && c <= 'z' {
            c -= toLower
        } else if !upper && 'A' <= c && c <= 'Z' {
            c += toLower
diff -r 78cebfb89b21 src/pkg/net/textproto/reader_test.go
--- a/src/pkg/net/textproto/reader_test.go  Fri Nov 01 09:18:35 2013 -0700
+++ b/src/pkg/net/textproto/reader_test.go  Sun Nov 03 13:58:32 2013 -0800
@@ -25,6 +25,10 @@
    {"user-agent", "User-Agent"},
    {"USER-AGENT", "User-Agent"},
    {"üser-agenT", "üser-Agent"}, // non-ASCII unchanged
+
+   // This caused a panic due to mishandling of a space:
+   {"C Ontent-Transfer-Encoding", "C-Ontent-Transfer-Encoding"},
+   {"foo bar", "Foo-Bar"},
 }

 func TestCanonicalMIMEHeaderKey(t *testing.T) {

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions