Skip to content

Commit df3ea76

Browse files
authored
Merge pull request #221 from d-hat/CVE-2020-35381
Attempt to fix #219 and introduce a test. The only error that can easily be returned in this case is `KeyPathNotFoundError`, which is reasonable if you squint (a malformed key can not be found). Note I'm far from fluent in golang so this should be reviewed with some care 😄
2 parents e015c37 + 1e1db9e commit df3ea76

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

parser.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,11 @@ func searchKeys(data []byte, keys ...string) int {
308308
case '[':
309309
// If we want to get array element by index
310310
if keyLevel == level && keys[level][0] == '[' {
311-
aIdx, err := strconv.Atoi(keys[level][1 : len(keys[level])-1])
311+
var keyLen = len(keys[level])
312+
if keyLen < 3 || keys[level][0] != '[' || keys[level][keyLen-1] != ']' {
313+
return -1
314+
}
315+
aIdx, err := strconv.Atoi(keys[level][1 : keyLen-1])
312316
if err != nil {
313317
return -1
314318
}

parser_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,18 @@ var getStringTests = []GetTest{
988988
path: []string{"c"},
989989
isErr: true,
990990
},
991+
{
992+
desc: `empty array index`,
993+
json: `[""]`,
994+
path: []string{"[]"},
995+
isFound: false,
996+
},
997+
{
998+
desc: `malformed array index`,
999+
json: `[""]`,
1000+
path: []string{"["},
1001+
isFound: false,
1002+
},
9911003
}
9921004

9931005
var getUnsafeStringTests = []GetTest{

0 commit comments

Comments
 (0)