Skip to content

Commit 0277a4f

Browse files
committed
Merge branch '4.0.x'
Closes gh-50163
2 parents 6de5f60 + ad25c28 commit 0277a4f

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

module/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/ConnectionInputStream.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class ConnectionInputStream extends FilterInputStream {
3131

3232
private static final int BUFFER_SIZE = 4096;
3333

34+
private static final int MAX_HEADER_SIZE = 8192;
35+
3436
ConnectionInputStream(InputStream in) {
3537
super(in);
3638
}
@@ -45,11 +47,16 @@ class ConnectionInputStream extends FilterInputStream {
4547
String readHeader() throws IOException {
4648
byte[] buffer = new byte[BUFFER_SIZE];
4749
StringBuilder content = new StringBuilder(BUFFER_SIZE);
48-
while (content.indexOf(HEADER_END) == -1) {
50+
while (content.indexOf(HEADER_END) == -1 && content.length() < MAX_HEADER_SIZE) {
4951
int amountRead = checkedRead(buffer, 0, BUFFER_SIZE);
5052
content.append(new String(buffer, 0, amountRead));
5153
}
52-
return content.substring(0, content.indexOf(HEADER_END));
54+
55+
int endIndex = content.indexOf(HEADER_END);
56+
if (endIndex == -1) {
57+
throw new IOException("Malformed header");
58+
}
59+
return content.substring(0, endIndex);
5360
}
5461

5562
/**

module/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/ConnectionInputStreamTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.FilterInputStream;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23+
import java.util.Random;
2324

2425
import org.junit.jupiter.api.Test;
2526

@@ -47,6 +48,14 @@ void readHeader() throws Exception {
4748
assertThat(inputStream.readHeader()).isEqualTo(header);
4849
}
4950

51+
@Test
52+
void readHeaderThrowsWhenHeaderIsMalformed() {
53+
byte[] header = new byte[10000];
54+
new Random().nextBytes(header);
55+
ConnectionInputStream inputStream = new ConnectionInputStream(new ByteArrayInputStream(header));
56+
assertThatIOException().isThrownBy(inputStream::readHeader).withMessage("Malformed header");
57+
}
58+
5059
@Test
5160
void readFully() throws Exception {
5261
byte[] bytes = "the data that we want to read fully".getBytes();

0 commit comments

Comments
 (0)