Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion language/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ func ParseAcceptLanguage(s string) (tag []Tag, q []float32, err error) {
}
}()

if strings.Count(s, "-") > 1000 {
// The BCP 47 scanner aliases '_' to '-' in scanner.init
// (internal/language/parse.go); the guard must count both.
if strings.Count(s, "-")+strings.Count(s, "_") > 1000 {
return nil, nil, errTagListTooLarge
}

Expand Down
15 changes: 15 additions & 0 deletions language/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,18 @@ func TestParseAcceptLanguageTooBig(t *testing.T) {
t.Errorf("ParseAcceptLanguage() unexpected error: got %v, want %v", err, errTagListTooLarge)
}
}

func TestParseAcceptLanguageUnderscoreGuard(t *testing.T) {
// Build a payload that would trigger the quadratic path with
// '_' but is blocked by the dash-count guard. Verify the guard
// now also counts '_'.
parts := make([]string, 0, 1002)
parts = append(parts, "en")
for i := 0; i < 1001; i++ {
parts = append(parts, "abcdefghi")
}
s := strings.Join(parts, "_")
if _, _, err := ParseAcceptLanguage(s); err != errTagListTooLarge {
t.Errorf("ParseAcceptLanguage with 1001 '_' separators: got err=%v, want errTagListTooLarge", err)
}
}