Skip to content

Commit 3fe86b3

Browse files
yschimkeswankjesse
authored andcommitted
256kB header limit (square#3602)
* 100k header limit * 256k limit
1 parent 1d9c158 commit 3fe86b3

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

okhttp-tests/src/test/java/okhttp3/CallTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,25 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
18711871
.assertFailure("HTTP 205 had non-zero Content-Length: 39");
18721872
}
18731873

1874+
@Test public void httpWithExcessiveHeaders() throws IOException {
1875+
String longLine = "HTTP/1.1 200 " + stringFill('O', 256 * 1024) + "K";
1876+
1877+
server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
1878+
1879+
server.enqueue(new MockResponse()
1880+
.setStatus(longLine)
1881+
.setBody("I'm not even supposed to be here today."));
1882+
1883+
executeSynchronously("/")
1884+
.assertFailureMatches(".*unexpected end of stream on Connection.*");
1885+
}
1886+
1887+
private String stringFill(char fillChar, int length) {
1888+
char[] value = new char[length];
1889+
Arrays.fill(value, fillChar);
1890+
return new String(value);
1891+
}
1892+
18741893
@Test public void canceledBeforeExecute() throws Exception {
18751894
Call call = client.newCall(new Request.Builder().url(server.url("/a")).build());
18761895
call.cancel();

okhttp-tests/src/test/java/okhttp3/RecordedResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public RecordedResponse assertFailure(Class<?>... allowedExceptionTypes) {
157157
}
158158

159159
public RecordedResponse assertFailure(String... messages) {
160-
assertNotNull(failure);
160+
assertNotNull("No failure found", failure);
161161
assertTrue(failure.getMessage(), Arrays.asList(messages).contains(failure.getMessage()));
162162
return this;
163163
}

okhttp/src/main/java/okhttp3/internal/http1/Http1Codec.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public final class Http1Codec implements HttpCodec {
7474
private static final int STATE_OPEN_RESPONSE_BODY = 4;
7575
private static final int STATE_READING_RESPONSE_BODY = 5;
7676
private static final int STATE_CLOSED = 6;
77+
private static final int HEADER_LIMIT = Integer.getInteger("okhttp.headerlimit", 256 * 1024);
7778

7879
/** The client that configures this stream. May be null for HTTPS proxy tunnels. */
7980
final OkHttpClient client;
@@ -83,6 +84,7 @@ public final class Http1Codec implements HttpCodec {
8384
final BufferedSource source;
8485
final BufferedSink sink;
8586
int state = STATE_IDLE;
87+
private long headerLimit = HEADER_LIMIT;
8688

8789
public Http1Codec(OkHttpClient client, StreamAllocation streamAllocation, BufferedSource source,
8890
BufferedSink sink) {
@@ -184,7 +186,7 @@ public void writeRequest(Headers headers, String requestLine) throws IOException
184186
}
185187

186188
try {
187-
StatusLine statusLine = StatusLine.parse(source.readUtf8LineStrict());
189+
StatusLine statusLine = StatusLine.parse(readHeaderLine());
188190

189191
Response.Builder responseBuilder = new Response.Builder()
190192
.protocol(statusLine.protocol)
@@ -206,11 +208,17 @@ public void writeRequest(Headers headers, String requestLine) throws IOException
206208
}
207209
}
208210

211+
private String readHeaderLine() throws IOException {
212+
String line = source.readUtf8LineStrict(headerLimit);
213+
headerLimit -= line.length();
214+
return line;
215+
}
216+
209217
/** Reads headers or trailers. */
210218
public Headers readHeaders() throws IOException {
211219
Headers.Builder headers = new Headers.Builder();
212220
// parse the result headers until the first blank line
213-
for (String line; (line = source.readUtf8LineStrict()).length() != 0; ) {
221+
for (String line; (line = readHeaderLine()).length() != 0; ) {
214222
Internal.instance.addLenient(headers, line);
215223
}
216224
return headers.build();

0 commit comments

Comments
 (0)