Skip to content

Commit 5ede095

Browse files
neildgopherbot
authored andcommitted
net/textproto: avoid quadratic complexity in Reader.ReadResponse
Reader.ReadResponse constructed a response string from repeated string concatenation, permitting a malicious sender to cause excessive memory allocation and CPU consumption by sending a response consisting of many short lines. Use a strings.Builder to construct the string instead. Thanks to Jakub Ciolek for reporting this issue. Fixes CVE-2025-61724 Fixes golang#75716 Change-Id: I1a98ce85a21b830cb25799f9ac9333a67400d736 Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/2940 Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/709859 TryBot-Bypass: Michael Pratt <[email protected]> Auto-Submit: Michael Pratt <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 5ce8cd1 commit 5ede095

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/net/textproto/reader.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@ func (r *Reader) ReadCodeLine(expectCode int) (code int, message string, err err
285285
//
286286
// An expectCode <= 0 disables the check of the status code.
287287
func (r *Reader) ReadResponse(expectCode int) (code int, message string, err error) {
288-
code, continued, message, err := r.readCodeLine(expectCode)
288+
code, continued, first, err := r.readCodeLine(expectCode)
289289
multi := continued
290+
var messageBuilder strings.Builder
291+
messageBuilder.WriteString(first)
290292
for continued {
291293
line, err := r.ReadLine()
292294
if err != nil {
@@ -297,12 +299,15 @@ func (r *Reader) ReadResponse(expectCode int) (code int, message string, err err
297299
var moreMessage string
298300
code2, continued, moreMessage, err = parseCodeLine(line, 0)
299301
if err != nil || code2 != code {
300-
message += "\n" + strings.TrimRight(line, "\r\n")
302+
messageBuilder.WriteByte('\n')
303+
messageBuilder.WriteString(strings.TrimRight(line, "\r\n"))
301304
continued = true
302305
continue
303306
}
304-
message += "\n" + moreMessage
307+
messageBuilder.WriteByte('\n')
308+
messageBuilder.WriteString(moreMessage)
305309
}
310+
message = messageBuilder.String()
306311
if err != nil && multi && message != "" {
307312
// replace one line error message with all lines (full message)
308313
err = &Error{code, message}

0 commit comments

Comments
 (0)