Fix slice panic in PolledClient.GetMessages for out-of-range start/count - #3063
Draft
rootkiller6788 wants to merge 1 commit into
Draft
Fix slice panic in PolledClient.GetMessages for out-of-range start/count#3063rootkiller6788 wants to merge 1 commit into
rootkiller6788 wants to merge 1 commit into
Conversation
The /polled_connections/{connId}/messages endpoint parses start and count from the request query and passes them to GetMessages unchecked. A negative start or a start beyond the number of buffered messages caused make([]interface{}, count) or c.messages[start:end] to panic with a slice out-of-range error, killing the request handler.
Clamp start to [0, len(messages)] and count to the remaining messages so the method returns a sensible slice for any input instead of panicking. Adds TestGetMessagesBounds covering negative start, start past the end, and an oversized count.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
/polled_connections/{connId}/messagesHTTP endpoint parsesstartandcountfrom the request query string and passes them toPolledClient.GetMessageswithout any range validation. A negativestart(e.g.?start=-1) or astartbeyond the number of buffered messages (e.g.?start=100) causedmake([]interface{}, count)orc.messages[start:end]to panic with a slice out-of-range error, terminating the request handler.Change
Clamp
startto[0, len(messages)]andcountto the remaining messages (len(messages)-start) inGetMessages, so the method returns a sensible slice for any input:start < 0is treated as0start > len(messages)returns an empty slicecount < 0(meaning "all remaining") orcountlarger than the remaining messages is clamped to the remaining countThis also avoids the
start + countinteger-overflow edge case for very largecountvalues by usinglen(messages)-startinstead of computingend := start + count.Verification
TestGetMessagesBoundscovering negativestart,startpast the end (with and withoutcount), and an oversizedcount.TestMessagesbehavior is preserved.