Skip to content

Commit

Permalink
Add message length check in spamcheck history to prevent memory issues
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 20, 2025
1 parent 5e34ea0 commit f9ea04f
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/spamcheck/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type LastRequests struct {
lock sync.RWMutex
}

const maxMsgLen = 1024

// NewLastRequests creates new requests tracker
func NewLastRequests(size int) *LastRequests {
// minimum size is 1
Expand All @@ -29,6 +31,11 @@ func (h *LastRequests) Push(req Request) {
h.lock.Lock()
defer h.lock.Unlock()

if len(req.Msg) > maxMsgLen {
// truncate the message if it's too long to prevent memory exhaustion attacks
req.Msg = req.Msg[:maxMsgLen]
}

h.requests.Value = req
h.requests = h.requests.Next()
}
Expand Down

0 comments on commit f9ea04f

Please sign in to comment.