Skip to content

Commit f789966

Browse files
committed
imapwire: limit max list nesting depth
1 parent de699b6 commit f789966

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

internal/imapwire/decoder.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
"github.com/emersion/go-imap/v2/internal/utf7"
1414
)
1515

16+
// This limits the max list nesting depth to prevent stack overflow.
17+
const maxListDepth = 1000
18+
1619
// IsAtomChar returns true if ch is an ATOM-CHAR.
1720
func IsAtomChar(ch byte) bool {
1821
switch ch {
@@ -45,11 +48,12 @@ type Decoder struct {
4548
// and needs to be fully buffered in memory.
4649
CheckBufferedLiteralFunc func(size int64, nonSync bool) error
4750

48-
r *bufio.Reader
49-
side ConnSide
50-
err error
51-
literal bool
52-
crlf bool
51+
r *bufio.Reader
52+
side ConnSide
53+
err error
54+
literal bool
55+
crlf bool
56+
listDepth int
5357
}
5458

5559
// NewDecoder creates a new decoder.
@@ -440,6 +444,15 @@ func (dec *Decoder) List(f func() error) (isList bool, err error) {
440444
return true, nil
441445
}
442446

447+
dec.listDepth++
448+
defer func() {
449+
dec.listDepth--
450+
}()
451+
452+
if dec.listDepth >= maxListDepth {
453+
return false, fmt.Errorf("imapwire: exceeded max depth")
454+
}
455+
443456
for {
444457
if err := f(); err != nil {
445458
return true, err

0 commit comments

Comments
 (0)