Skip to content

Commit 491a49a

Browse files
neildFiloSottile
authored andcommitted
http2: cap the size of the server's canonical header cache
The HTTP/2 server keeps a per-connection cache mapping header keys to their canonicalized form (e.g., "foo-bar" => "Foo-Bar"). Cap the maximum size of this cache to prevent a peer sending many unique header keys from causing unbounded memory growth. Cap chosen arbitrarily at 32 entries. Since this cache does not include common headers (e.g., "content-type"), 32 seems like more than enough for almost all normal uses. Fixes #50058 Fixes CVE-2021-44716 Change-Id: Ia83696dc23253c12af8f26d502557c2cc9841105 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1290827 Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/net/+/369794 Trust: Filippo Valsorda <[email protected]> Run-TryBot: Filippo Valsorda <[email protected]> Trust: Damien Neil <[email protected]> Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent db4efeb commit 491a49a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

http2/server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,15 @@ func (sc *serverConn) canonicalHeader(v string) string {
719719
sc.canonHeader = make(map[string]string)
720720
}
721721
cv = http.CanonicalHeaderKey(v)
722-
sc.canonHeader[v] = cv
722+
// maxCachedCanonicalHeaders is an arbitrarily-chosen limit on the number of
723+
// entries in the canonHeader cache. This should be larger than the number
724+
// of unique, uncommon header keys likely to be sent by the peer, while not
725+
// so high as to permit unreaasonable memory usage if the peer sends an unbounded
726+
// number of unique header keys.
727+
const maxCachedCanonicalHeaders = 32
728+
if len(sc.canonHeader) < maxCachedCanonicalHeaders {
729+
sc.canonHeader[v] = cv
730+
}
723731
return cv
724732
}
725733

0 commit comments

Comments
 (0)