Skip to content

Commit cb06bc5

Browse files
authored
🩹 Fix: handle un-matched open brackets in the query params (#3121)
* Add logic for counting open brackets * Add UTs * update increment/decrement syntax with ++/-- * Update UT to remove duplicate
1 parent bfcf91d commit cb06bc5

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

ctx.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,15 +1306,24 @@ func parseParamSquareBrackets(k string) (string, error) {
13061306
defer bytebufferpool.Put(bb)
13071307

13081308
kbytes := []byte(k)
1309+
openBracketsCount := 0
13091310

13101311
for i, b := range kbytes {
1311-
if b == '[' && kbytes[i+1] != ']' {
1312-
if err := bb.WriteByte('.'); err != nil {
1313-
return "", fmt.Errorf("failed to write: %w", err)
1312+
if b == '[' {
1313+
openBracketsCount++
1314+
if i+1 < len(kbytes) && kbytes[i+1] != ']' {
1315+
if err := bb.WriteByte('.'); err != nil {
1316+
return "", fmt.Errorf("failed to write: %w", err)
1317+
}
13141318
}
1319+
continue
13151320
}
13161321

1317-
if b == '[' || b == ']' {
1322+
if b == ']' {
1323+
openBracketsCount--
1324+
if openBracketsCount < 0 {
1325+
return "", errors.New("unmatched brackets")
1326+
}
13181327
continue
13191328
}
13201329

@@ -1323,6 +1332,10 @@ func parseParamSquareBrackets(k string) (string, error) {
13231332
}
13241333
}
13251334

1335+
if openBracketsCount > 0 {
1336+
return "", errors.New("unmatched brackets")
1337+
}
1338+
13261339
return bb.String(), nil
13271340
}
13281341

ctx_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4508,6 +4508,10 @@ func Test_Ctx_QueryParser(t *testing.T) {
45084508
utils.AssertEqual(t, nil, c.QueryParser(empty))
45094509
utils.AssertEqual(t, 0, len(empty.Hobby))
45104510

4511+
c.Request().URI().SetQueryString("id=1&name[=tom")
4512+
q = new(Query)
4513+
utils.AssertEqual(t, "unmatched brackets", c.QueryParser(q).Error())
4514+
45114515
type Query2 struct {
45124516
Bool bool
45134517
ID int
@@ -4790,6 +4794,10 @@ func Test_Ctx_QueryParser_Schema(t *testing.T) {
47904794
utils.AssertEqual(t, "doe", cq.Data[1].Name)
47914795
utils.AssertEqual(t, 12, cq.Data[1].Age)
47924796

4797+
c.Request().URI().SetQueryString("data[0][name]=john&data[0][age]=10&data[1]name]=doe&data[1][age]=12")
4798+
cq = new(CollectionQuery)
4799+
utils.AssertEqual(t, "unmatched brackets", c.QueryParser(cq).Error())
4800+
47934801
c.Request().URI().SetQueryString("data.0.name=john&data.0.age=10&data.1.name=doe&data.1.age=12")
47944802
cq = new(CollectionQuery)
47954803
utils.AssertEqual(t, nil, c.QueryParser(cq))

0 commit comments

Comments
 (0)