File tree Expand file tree Collapse file tree 1 file changed +18
-5
lines changed Expand file tree Collapse file tree 1 file changed +18
-5
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,9 @@ import (
13
13
"github.com/emersion/go-imap/v2/internal/utf7"
14
14
)
15
15
16
+ // This limits the max list nesting depth to prevent stack overflow.
17
+ const maxListDepth = 1000
18
+
16
19
// IsAtomChar returns true if ch is an ATOM-CHAR.
17
20
func IsAtomChar (ch byte ) bool {
18
21
switch ch {
@@ -45,11 +48,12 @@ type Decoder struct {
45
48
// and needs to be fully buffered in memory.
46
49
CheckBufferedLiteralFunc func (size int64 , nonSync bool ) error
47
50
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
53
57
}
54
58
55
59
// NewDecoder creates a new decoder.
@@ -440,6 +444,15 @@ func (dec *Decoder) List(f func() error) (isList bool, err error) {
440
444
return true , nil
441
445
}
442
446
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
+
443
456
for {
444
457
if err := f (); err != nil {
445
458
return true , err
You can’t perform that action at this time.
0 commit comments