mime_header_parse.mx raw

   1  // Regression: every letter is a valid header-field byte.
   2  //
   3  // validHeaderFieldByte built its bitmap as ONE 128-bit untyped constant:
   4  //
   5  //	const mask = 0 | (1<<(10)-1)<<'0' | (1<<(26)-1)<<'a' | (1<<(26)-1)<<'A' | ...
   6  //	return ((uint64(1)<<c)&(mask&(1<<64-1)) | (uint64(1)<<(c-64))&(mask>>64)) != 0
   7  //
   8  // Moxie does not evaluate a constant wider than 64 bits exactly. Both halves of
   9  // that one came back zero - mask>>64 == 0 AND mask&(1<<64-1) == 0 - so every
  10  // letter was rejected, canonicalMIMEHeaderKey returned ok=false for every real
  11  // header name, and http.ReadRequest failed on a well-formed request with
  12  //
  13  //	malformed MIME header line: Host: example.com
  14  //
  15  // This is the same constant-folding precision bug that makes time.Date and
  16  // time.Parse wrong, reached through a different expression. The bitmap is now
  17  // two 64-bit halves, neither of which needs more than 64 bits.
  18  //
  19  // This pins the whole tchar set from RFC 7230, so a future change that drops a
  20  // group of characters fails here instead of in every HTTP request.
  21  package main
  22  
  23  import (
  24  	"bufio"
  25  	"bytes"
  26  	"net/textproto"
  27  )
  28  
  29  func parse(raw string) (h textproto.MIMEHeader, err error) {
  30  	return textproto.NewReader(bufio.NewReader(bytes.NewBuffer([]byte(raw)))).ReadMIMEHeader()
  31  }
  32  
  33  func main() {
  34  	// Termination: a batch program must still be stoppable, so the entry
  35  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  36  	select {
  37  	case <-shutdown:
  38  		return
  39  	default:
  40  	}
  41  
  42  	// Every category: lowercase, uppercase, digits, and each punctuator that
  43  	// the mask carries. Keys are canonicalized, so expect Host/User-Agent etc.
  44  	raw := "content-type: text/plain\r\n" |
  45  		"X-Test-Case: v\r\n" |
  46  		"X-0123456789: v\r\n" |
  47  		"a!#$%&'*+-.: v\r\n" |
  48  		"x^_`|~: v\r\n" |
  49  		"\r\n"
  50  	h, err := parse(raw)
  51  	if err != nil {
  52  		panic("ReadMIMEHeader: " | err.Error())
  53  	}
  54  	if h.Get("Content-Type") != "text/plain" {
  55  		panic("Content-Type: [" | h.Get("Content-Type") | "]")
  56  	}
  57  	if h.Get("X-Test-Case") != "v" {
  58  		panic("X-Test-Case")
  59  	}
  60  	if h.Get("X-0123456789") != "v" {
  61  		panic("X-0123456789")
  62  	}
  63  	// The punctuator key is not canonicalized (it has no letters to fold), so
  64  	// read it back exactly as written.
  65  	if h.Get("a!#$%&'*+-.") != "v" {
  66  		panic("punctuator key")
  67  	}
  68  	if h.Get("x^_`|~") != "v" {
  69  		panic("x^_`|~ key")
  70  	}
  71  
  72  	// A blank line ends the header block and an empty block is not an error.
  73  	h2, err2 := parse("\r\n")
  74  	if err2 != nil {
  75  		panic("empty block: " | err2.Error())
  76  	}
  77  	if len(h2) != 0 {
  78  		panic("empty block not empty")
  79  	}
  80  
  81  	println("ok")
  82  }
  83