From a1a7b2874264c7b8ba700663f6998110ffd49621 Mon Sep 17 00:00:00 2001 From: dojutsu-user Date: Sat, 7 Sep 2024 16:55:09 +0530 Subject: [PATCH 1/4] Add logic for handling unmatched square brackets in query params --- binder/mapping.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/binder/mapping.go b/binder/mapping.go index 07af94a152..36821be087 100644 --- a/binder/mapping.go +++ b/binder/mapping.go @@ -1,13 +1,15 @@ package binder import ( + "errors" "reflect" "strings" "sync" - "github.com/gofiber/fiber/v3/internal/schema" "github.com/gofiber/utils/v2" "github.com/valyala/bytebufferpool" + + "github.com/gofiber/fiber/v3/internal/schema" ) // ParserConfig form decoder config for SetParserDecoder @@ -132,15 +134,24 @@ func parseParamSquareBrackets(k string) (string, error) { defer bytebufferpool.Put(bb) kbytes := []byte(k) + openBracketsCount := 0 for i, b := range kbytes { - if b == '[' && kbytes[i+1] != ']' { - if err := bb.WriteByte('.'); err != nil { - return "", err //nolint:wrapcheck // unnecessary to wrap it + if b == '[' { + openBracketsCount++ + if i+1 < len(kbytes) && kbytes[i+1] != ']' { + if err := bb.WriteByte('.'); err != nil { + return "", err //nolint:wrapcheck // unnecessary to wrap it + } } + continue } - if b == '[' || b == ']' { + if b == ']' { + openBracketsCount-- + if openBracketsCount < 0 { + return "", errors.New("unmatched brackets") + } continue } @@ -149,6 +160,10 @@ func parseParamSquareBrackets(k string) (string, error) { } } + if openBracketsCount > 0 { + return "", errors.New("unmatched brackets") + } + return bb.String(), nil } From 22b9490f67d44784820f261541656ec3cfe0393e Mon Sep 17 00:00:00 2001 From: dojutsu-user Date: Sat, 7 Sep 2024 16:55:22 +0530 Subject: [PATCH 2/4] Add UTs --- binder/mapping_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/binder/mapping_test.go b/binder/mapping_test.go index aec91ff2be..af29b16c00 100644 --- a/binder/mapping_test.go +++ b/binder/mapping_test.go @@ -1,6 +1,7 @@ package binder import ( + "errors" "reflect" "testing" @@ -29,3 +30,50 @@ func Test_EqualFieldType(t *testing.T) { require.True(t, equalFieldType(&user, reflect.Int, "AGE")) require.True(t, equalFieldType(&user, reflect.Int, "age")) } + +func Test_ParseParamSquareBrackets(t *testing.T) { + tests := []struct { + input string + expected string + err error + }{ + { + input: "foo[bar]", + expected: "foo.bar", + err: nil, + }, + { + input: "foo[bar][baz]", + expected: "foo.bar.baz", + err: nil, + }, + { + input: "foo[bar", + expected: "", + err: errors.New("unmatched brackets"), + }, + { + input: "foo[bar][baz", + expected: "", + err: errors.New("unmatched brackets"), + }, + { + input: "foo]bar[", + expected: "", + err: errors.New("unmatched brackets"), + }, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + result, err := parseParamSquareBrackets(tt.input) + if tt.err != nil { + require.Error(t, err) + require.EqualError(t, err, tt.err.Error()) + } else { + require.NoError(t, err) + require.Equal(t, tt.expected, result) + } + }) + } +} From 40f35051333a658c029cbc261bafe73be9d59280 Mon Sep 17 00:00:00 2001 From: dojutsu-user Date: Sat, 7 Sep 2024 17:06:12 +0530 Subject: [PATCH 3/4] Fix lint: fieldalignment error --- binder/mapping_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/binder/mapping_test.go b/binder/mapping_test.go index af29b16c00..4a8432da19 100644 --- a/binder/mapping_test.go +++ b/binder/mapping_test.go @@ -33,34 +33,34 @@ func Test_EqualFieldType(t *testing.T) { func Test_ParseParamSquareBrackets(t *testing.T) { tests := []struct { + err error input string expected string - err error }{ { + err: nil, input: "foo[bar]", expected: "foo.bar", - err: nil, }, { + err: nil, input: "foo[bar][baz]", expected: "foo.bar.baz", - err: nil, }, { + err: errors.New("unmatched brackets"), input: "foo[bar", expected: "", - err: errors.New("unmatched brackets"), }, { + err: errors.New("unmatched brackets"), input: "foo[bar][baz", expected: "", - err: errors.New("unmatched brackets"), }, { + err: errors.New("unmatched brackets"), input: "foo]bar[", expected: "", - err: errors.New("unmatched brackets"), }, } From 3b43e4feaed49eb28299d735bf465719169d0edc Mon Sep 17 00:00:00 2001 From: dojutsu-user Date: Sun, 8 Sep 2024 19:05:57 +0530 Subject: [PATCH 4/4] Add UTs --- binder/mapping_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/binder/mapping_test.go b/binder/mapping_test.go index 4a8432da19..e6fc8146f7 100644 --- a/binder/mapping_test.go +++ b/binder/mapping_test.go @@ -62,6 +62,26 @@ func Test_ParseParamSquareBrackets(t *testing.T) { input: "foo]bar[", expected: "", }, + { + err: nil, + input: "foo[bar[baz]]", + expected: "foo.bar.baz", + }, + { + err: nil, + input: "", + expected: "", + }, + { + err: nil, + input: "[]", + expected: "", + }, + { + err: nil, + input: "foo[]", + expected: "foo", + }, } for _, tt := range tests {