Skip to content

Commit 434eadc

Browse files
rolandshoemakergopherbot
authored andcommitted
language: reject excessively large Accept-Language strings
The BCP 47 tag parser has quadratic time complexity due to inherent aspects of its design. Since the parser is, by design, exposed to untrusted user input, this can be leveraged to force a program to consume significant time parsing Accept-Language headers. The parser cannot be easily rewritten to fix this behavior for various reasons. Instead the solution implemented in this CL is to limit the total complexity of tags passed into ParseAcceptLanguage by limiting the number of dashes in the string to 1000. This should be more than enough for the majority of real world use cases, where the number of tags being sent is likely to be in the single digits. Thanks to the OSS-Fuzz project for discovering this issue and to Adam Korczynski (ADA Logics) for writing the fuzz case and for reporting the issue. Fixes CVE-2022-32149 Fixes golang/go#56152 Change-Id: I7bda1d84cee2b945039c203f26869d58ee9374ae Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1565112 Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Tatiana Bradley <tatianabradley@google.com> Reviewed-on: https://go-review.googlesource.com/c/text/+/442235 TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Roland Shoemaker <roland@golang.org> Run-TryBot: Roland Shoemaker <roland@golang.org>
1 parent 23407e7 commit 434eadc

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

language/parse.go

+5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func update(b *language.Builder, part ...interface{}) (err error) {
147147
}
148148

149149
var errInvalidWeight = errors.New("ParseAcceptLanguage: invalid weight")
150+
var errTagListTooLarge = errors.New("tag list exceeds max length")
150151

151152
// ParseAcceptLanguage parses the contents of an Accept-Language header as
152153
// defined in http://www.ietf.org/rfc/rfc2616.txt and returns a list of Tags and
@@ -164,6 +165,10 @@ func ParseAcceptLanguage(s string) (tag []Tag, q []float32, err error) {
164165
}
165166
}()
166167

168+
if strings.Count(s, "-") > 1000 {
169+
return nil, nil, errTagListTooLarge
170+
}
171+
167172
var entry string
168173
for s != "" {
169174
if entry, s = split(s, ','); entry == "" {

language/parse_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,16 @@ func TestParseAcceptLanguage(t *testing.T) {
394394
}
395395
}
396396
}
397+
398+
func TestParseAcceptLanguageTooBig(t *testing.T) {
399+
s := strings.Repeat("en-x-a-", 333)
400+
_, _, err := ParseAcceptLanguage(s)
401+
if err != language.ErrSyntax {
402+
t.Errorf("ParseAcceptLanguage() unexpected error: got %v, want %v", err, language.ErrSyntax)
403+
}
404+
s += "en-x-a"
405+
_, _, err = ParseAcceptLanguage(s)
406+
if err != errTagListTooLarge {
407+
t.Errorf("ParseAcceptLanguage() unexpected error: got %v, want %v", err, errTagListTooLarge)
408+
}
409+
}

0 commit comments

Comments
 (0)