Skip to content

Commit 5c2c9d7

Browse files
committed
fix panic on invalid accept-language header
1 parent 7c8e0bc commit 5c2c9d7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

pkg/appsec/ja4h/ja4h.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
ja4hFullHashLength = 51
2626
ja4hSubHashLength = 12
2727
defaultLang = "0000"
28+
emptyCookiesHash = "000000000000"
2829
)
2930

3031
// httpMethod extracts the first two lowercase characters of the HTTP method.
@@ -77,6 +78,7 @@ func primaryLanguage(headers http.Header) string {
7778
//cf. https://github.com/FoxIO-LLC/ja4/blob/main/python/ja4h.py#L13
7879
lang = strings.ReplaceAll(lang, "-", "")
7980
lang = strings.ReplaceAll(lang, ";", ",")
81+
lang = lang[:min(len(lang), 4)]
8082

8183
return strings.Split(lang, ",")[0] + strings.Repeat("0", 4-len(lang))
8284
}
@@ -123,7 +125,7 @@ func hashTruncated(input string) string {
123125
// jA4H_c computes a truncated SHA256 hash of sorted cookie names.
124126
func jA4H_c(cookies []*http.Cookie) string {
125127
if len(cookies) == 0 {
126-
return strings.Repeat("0", truncatedHashLength)
128+
return emptyCookiesHash
127129
}
128130
var builder strings.Builder
129131
for i, cookie := range cookies {
@@ -138,7 +140,7 @@ func jA4H_c(cookies []*http.Cookie) string {
138140
// jA4H_d computes a truncated SHA256 hash of cookie name-value pairs.
139141
func jA4H_d(cookies []*http.Cookie) string {
140142
if len(cookies) == 0 {
141-
return strings.Repeat("0", truncatedHashLength)
143+
return emptyCookiesHash
142144
}
143145
var builder strings.Builder
144146
for i, cookie := range cookies {

pkg/appsec/ja4h/ja4h_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ func TestJA4H_A(t *testing.T) {
4040
},
4141
expectedResult: "po11cr000000",
4242
},
43+
{
44+
name: "bad accept-language header",
45+
request: func() *http.Request {
46+
req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil)
47+
req.Header.Set("Accept-Language", "aksjdhaslkdhalkjsd")
48+
return req
49+
},
50+
expectedResult: "ge11nn01aksj",
51+
},
4352
}
4453

4554
for _, tt := range tests {

0 commit comments

Comments
 (0)