Skip to content

Commit 580cba3

Browse files
neildgopherbot
authored andcommitted
http3, internal/httpcommon: improve :authority/host header interactions
Implement a variety of checks and behaviors involving the :authority pseudo-header and the Host header. When both are present, both must match. There may be no more than one Host header. When there is Host and no :authority, use Host as the authority. Validate the authority contents (same rules as used by HTTP/1 servers). For golang/go#80065 Change-Id: I01bfd826f4139384bd7910fc59a4109b6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/813520 Reviewed-by: Nicholas Husin <[email protected]> Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]>
1 parent 21c1020 commit 580cba3

4 files changed

Lines changed: 164 additions & 5 deletions

File tree

‎internal/http3/server.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ func (sc *serverConn) parseHeader(st *stream) (http.Header, pseudoHeader, error)
370370
if !httpguts.ValidHeaderFieldValue(value) {
371371
return &streamError{errH3MessageError, "invalid field value"}
372372
}
373+
if name == "" || (name[0] == ':' && value == "") {
374+
// Reject 0-length pseudo-header values up front,
375+
// to avoid any confusion down the line between
376+
// "present but zero-length" and "absent".
377+
return &streamError{errH3MessageError, "invalid field"}
378+
}
373379
switch name {
374380
case ":method":
375381
if hasMethod {
@@ -514,7 +520,7 @@ func (sc *serverConn) handleRequestStream(st *stream) error {
514520
req := (&http.Request{
515521
Proto: "HTTP/3.0",
516522
Method: pHeader.method,
517-
Host: pHeader.authority,
523+
Host: reqInfo.Host,
518524
URL: reqInfo.URL,
519525
RequestURI: reqInfo.RequestURI,
520526
Trailer: reqInfo.Trailer,

‎internal/http3/server_test.go‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,132 @@ func TestServerInvalidHeader(t *testing.T) {
372372
})
373373
}
374374

375+
func TestServerAuthorityAndHostHeader(t *testing.T) {
376+
for _, test := range []struct {
377+
name string
378+
h http.Header
379+
valid bool
380+
wantHost string
381+
}{{
382+
name: "authority host mismatch",
383+
h: http.Header{
384+
":authority": {"example.tld"},
385+
"host": {"other.tld"},
386+
},
387+
}, {
388+
// The RFCs aren't explicit on whether a :authority and host that
389+
// differ only in case is a mismatch. We treat it as a mismatch because
390+
// there doesn't seem to be a good reason not to.
391+
name: "authority host case differs",
392+
h: http.Header{
393+
":authority": {"example.tld"},
394+
"host": {"EXAMPLE.TLD"},
395+
},
396+
}, {
397+
name: "authority and multiple host",
398+
h: http.Header{
399+
":authority": {"example.tld"},
400+
"host": {"example.tld", "example.tld"},
401+
},
402+
}, {
403+
name: "multiple host only",
404+
h: http.Header{
405+
"host": {"example.tld", "example.tld"},
406+
},
407+
}, {
408+
name: "multiple authority only",
409+
h: http.Header{
410+
":authority": {"example.tld", "example.tld"},
411+
},
412+
}, {
413+
name: "empty authority",
414+
h: http.Header{
415+
":authority": {""},
416+
},
417+
}, {
418+
name: "invalid authority",
419+
h: http.Header{
420+
":authority": {"example . tld"},
421+
},
422+
}, {
423+
name: "invalid host",
424+
h: http.Header{
425+
"host": {"example . tld"},
426+
},
427+
}, {
428+
name: "authority only",
429+
h: http.Header{
430+
":authority": {"example.tld"},
431+
},
432+
valid: true,
433+
wantHost: "example.tld",
434+
}, {
435+
name: "host only",
436+
h: http.Header{
437+
"host": {"example.tld"},
438+
},
439+
valid: true,
440+
wantHost: "example.tld",
441+
}, {
442+
name: "authority host match",
443+
h: http.Header{
444+
":authority": {"example.tld"},
445+
"host": {"example.tld"},
446+
},
447+
valid: true,
448+
wantHost: "example.tld",
449+
}, {
450+
name: "authority host match with port",
451+
h: http.Header{
452+
":authority": {"example.tld:443"},
453+
"host": {"example.tld:443"},
454+
},
455+
valid: true,
456+
wantHost: "example.tld:443",
457+
}, {
458+
name: "authority host mismatch with port",
459+
h: http.Header{
460+
":authority": {"example.tld:80"},
461+
"host": {"example.tld:443"},
462+
},
463+
}, {
464+
name: "userinfo in authority",
465+
h: http.Header{
466+
":authority": {"user:[email protected]"},
467+
},
468+
}, {
469+
name: "userinfo in host",
470+
h: http.Header{
471+
"host": {"user:[email protected]"},
472+
},
473+
}, {
474+
name: "neither authority nor host",
475+
h: http.Header{},
476+
valid: true,
477+
wantHost: "",
478+
}} {
479+
synctestSubtest(t, test.name, func(t *testing.T) {
480+
ts := newTestServer(t, nil)
481+
tc := ts.connect()
482+
tc.greet()
483+
484+
reqStream := tc.newStream(streamTypeRequest)
485+
reqStream.writeHeaders(requestHeader(test.h))
486+
if test.valid {
487+
call := tc.nextHandlerCall()
488+
if call == nil {
489+
t.Fatal("no server handler call; want one")
490+
}
491+
if got, want := call.req.Host, test.wantHost; got != want {
492+
t.Errorf("handler got Host %q, want %q", got, want)
493+
}
494+
} else {
495+
reqStream.wantError(quic.StreamErrorCode(errH3MessageError))
496+
}
497+
})
498+
}
499+
}
500+
375501
func TestServerInvalidStatus(t *testing.T) {
376502
synctest.Test(t, func(t *testing.T) {
377503
gotpanic := make(chan bool)

‎internal/http3/transport_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func (ts *testQUICStream) wantIdle(reason string) {
270270
if _, err := qs.Read(make([]byte, 1)); !errors.Is(err, context.Canceled) {
271271
ts.t.Fatalf("%v: want stream to be idle, but stream has content", reason)
272272
}
273-
qs.SetReadContext(nil)
273+
qs.SetReadContext(context.Background())
274274
}
275275

276276
// wantFrameHeader calls readFrameHeader and asserts that the frame is of a given type.

‎internal/httpcommon/request.go‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ type ServerRequestParam struct {
391391
// ServerRequestResult is the result of NewServerRequest.
392392
type ServerRequestResult struct {
393393
// Various http.Request fields.
394+
Host string
394395
URL *url.URL
395396
RequestURI string
396397
Trailer map[string][]string
@@ -433,20 +434,45 @@ func NewServerRequest(rp ServerRequestParam) ServerRequestResult {
433434
}
434435
delete(rp.Header, "Trailer")
435436

437+
authority := rp.Authority
438+
if host := rp.Header["Host"]; len(host) == 1 {
439+
// HTTP/2 and HTTP/3 permit the Host header to be present,
440+
// but it must match the :authority pseudo-header.
441+
if authority == "" {
442+
authority = host[0]
443+
} else if host[0] != authority {
444+
return ServerRequestResult{
445+
InvalidReason: "authority_host_mismatch",
446+
}
447+
}
448+
} else if len(host) > 1 {
449+
// HTTP/1.1 rejects any request containing more than one Host header.
450+
// HTTP/2 and HTTP/3 don't use the Host header, but reject multiple anyway.
451+
return ServerRequestResult{
452+
InvalidReason: "multiple_host_headers",
453+
}
454+
}
455+
436456
// "':authority' MUST NOT include the deprecated userinfo subcomponent
437457
// for "http" or "https" schemed URIs."
438458
// https://www.rfc-editor.org/rfc/rfc9113.html#section-8.3.1-2.3.8
439-
if strings.IndexByte(rp.Authority, '@') != -1 && (rp.Scheme == "http" || rp.Scheme == "https") {
459+
if strings.IndexByte(authority, '@') != -1 && (rp.Scheme == "http" || rp.Scheme == "https") {
440460
return ServerRequestResult{
441461
InvalidReason: "userinfo_in_authority",
442462
}
443463
}
444464

465+
if authority != "" && !httpguts.ValidHostHeader(authority) {
466+
return ServerRequestResult{
467+
InvalidReason: "invalid_authority",
468+
}
469+
}
470+
445471
var url_ *url.URL
446472
var requestURI string
447473
if rp.Method == "CONNECT" && rp.Protocol == "" {
448-
url_ = &url.URL{Host: rp.Authority}
449-
requestURI = rp.Authority // mimic HTTP/1 server behavior
474+
url_ = &url.URL{Host: authority}
475+
requestURI = authority // mimic HTTP/1 server behavior
450476
} else {
451477
// "[The :path] pseudo-header field MUST NOT be empty [...]"
452478
// https://www.rfc-editor.org/rfc/rfc9113.html#section-8.3.1-2.4.2
@@ -467,6 +493,7 @@ func NewServerRequest(rp ServerRequestParam) ServerRequestResult {
467493
}
468494

469495
return ServerRequestResult{
496+
Host: authority,
470497
URL: url_,
471498
NeedsContinue: needsContinue,
472499
RequestURI: requestURI,

0 commit comments

Comments
 (0)