@@ -3629,6 +3629,7 @@ func testHandlerSetsBodyNil(t *testing.T, h2 bool) {
3629
3629
}
3630
3630
3631
3631
// Test that we validate the Host header.
3632
+ // Issue 11206 (invalid bytes in Host) and 13624 (Host present in HTTP/1.1)
3632
3633
func TestServerValidatesHostHeader (t * testing.T ) {
3633
3634
tests := []struct {
3634
3635
proto string
@@ -3676,6 +3677,43 @@ func TestServerValidatesHostHeader(t *testing.T) {
3676
3677
}
3677
3678
}
3678
3679
3680
+ // Test that we validate the valid bytes in HTTP/1 headers.
3681
+ // Issue 11207.
3682
+ func TestServerValidatesHeaders (t * testing.T ) {
3683
+ tests := []struct {
3684
+ header string
3685
+ want int
3686
+ }{
3687
+ {"" , 200 },
3688
+ {"Foo: bar\r \n " , 200 },
3689
+ {"X-Foo: bar\r \n " , 200 },
3690
+ {"Foo: a space\r \n " , 200 },
3691
+
3692
+ {"A space: foo\r \n " , 400 }, // space in header
3693
+ {"foo\xff bar: foo\r \n " , 400 }, // binary in header
3694
+ {"foo\x00 bar: foo\r \n " , 400 }, // binary in header
3695
+
3696
+ {"foo: foo\x00 foo\r \n " , 400 }, // binary in value
3697
+ {"foo: foo\xff foo\r \n " , 400 }, // binary in value
3698
+ }
3699
+ for _ , tt := range tests {
3700
+ conn := & testConn {closec : make (chan bool )}
3701
+ io .WriteString (& conn .readBuf , "GET / HTTP/1.1\r \n Host: foo\r \n " + tt .header + "\r \n " )
3702
+
3703
+ ln := & oneConnListener {conn }
3704
+ go Serve (ln , HandlerFunc (func (ResponseWriter , * Request ) {}))
3705
+ <- conn .closec
3706
+ res , err := ReadResponse (bufio .NewReader (& conn .writeBuf ), nil )
3707
+ if err != nil {
3708
+ t .Errorf ("For %q, ReadResponse: %v" , tt .header , res )
3709
+ continue
3710
+ }
3711
+ if res .StatusCode != tt .want {
3712
+ t .Errorf ("For %q, Status = %d; want %d" , tt .header , res .StatusCode , tt .want )
3713
+ }
3714
+ }
3715
+ }
3716
+
3679
3717
func BenchmarkClientServer (b * testing.B ) {
3680
3718
b .ReportAllocs ()
3681
3719
b .StopTimer ()
0 commit comments