Skip to content

Commit ffa6b91

Browse files
authored
Merge pull request #27 from gofiber/codex/2025-06-05-05-02-46
🧹 chore: Add upper index limit for parsers
2 parents a8dd97a + eb02845 commit ffa6b91

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

cache.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import (
1212
"sync"
1313
)
1414

15-
var errInvalidPath = errors.New("schema: invalid path")
15+
const maxParserIndex = 1000
16+
17+
var (
18+
errInvalidPath = errors.New("schema: invalid path")
19+
errIndexTooLarge = errors.New("schema: index exceeds parser limit")
20+
)
1621

1722
// newCache returns a new cache.
1823
func newCache() *cache {
@@ -78,6 +83,9 @@ func (c *cache) parsePath(p string, t reflect.Type) ([]pathPart, error) {
7883
if index64, err = strconv.ParseInt(keys[i], 10, 0); err != nil {
7984
return nil, errInvalidPath
8085
}
86+
if index64 > maxParserIndex {
87+
return nil, errIndexTooLarge
88+
}
8189
parts = append(parts, pathPart{
8290
path: path,
8391
field: field,

decoder_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,29 @@ func TestDecodePanicIsCaughtAndReturnedAsError(t *testing.T) {
31503150
}
31513151
}
31523152

3153+
func TestDecodeIndexExceedsParserLimit(t *testing.T) {
3154+
type R struct {
3155+
N1 []*struct {
3156+
Value string
3157+
}
3158+
}
3159+
data := map[string][]string{
3160+
"n1.1001.value": {"Foo"},
3161+
}
3162+
3163+
s := new(R)
3164+
decoder := NewDecoder()
3165+
err := decoder.Decode(s, data)
3166+
if err == nil {
3167+
t.Fatal("Expected an error when index exceeds parser limit")
3168+
}
3169+
3170+
expected := MultiError{"n1.1001.value": UnknownKeyError{Key: "n1.1001.value"}}
3171+
if !reflect.DeepEqual(err, expected) {
3172+
t.Fatalf("Expected %v, got: %v", expected, err)
3173+
}
3174+
}
3175+
31533176
func BenchmarkHandleMultipartField(b *testing.B) {
31543177
// Create dummy file headers for testing
31553178
dummyFile := &multipart.FileHeader{

0 commit comments

Comments
 (0)