Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ static ParsedScrollId parseScrollId(String scrollId) {
includeContextUUID = false;
type = firstChunk;
}
SearchContextIdForNode[] context = new SearchContextIdForNode[in.readVInt()];
int count = in.readVInt();
if (count < 0 || count > bytes.length) {
throw new IllegalArgumentException("Invalid scroll id");
}
SearchContextIdForNode[] context = new SearchContextIdForNode[count];
for (int i = 0; i < context.length; ++i) {
final String contextUUID = includeContextUUID ? in.readString() : "";
long id = in.readLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.VersionUtils;

import java.io.ByteArrayOutputStream;
import java.util.Base64;

import static org.hamcrest.Matchers.equalTo;

public class TransportSearchHelperTests extends OpenSearchTestCase {
Expand Down Expand Up @@ -92,4 +95,23 @@ public void testParseScrollId() {
assertEquals(42, parseScrollId.getContext()[2].getSearchContextId().getId());
assertThat(parseScrollId.getContext()[2].getSearchContextId().getSessionId(), equalTo("c"));
}

public void testParseScrollIdRejectsOversizedCount() throws Exception {
// Craft a scroll_id with a varint-encoded count far exceeding the payload size.
// This would previously cause an OOM by allocating a huge array.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Write type string: length=1, char='q' (any valid type)
baos.write(1); // vint string length
baos.write('q');
// Write a massive vint count (Integer.MAX_VALUE encoded as 5-byte varint)
baos.write(0xFF);
baos.write(0xFF);
baos.write(0xFF);
baos.write(0xFF);
baos.write(0x07);
String scrollId = Base64.getUrlEncoder().encodeToString(baos.toByteArray());

IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TransportSearchHelper.parseScrollId(scrollId));
assertEquals("Cannot parse scroll id", e.getMessage());
}
}
Loading