Skip to content

Commit 42e82fe

Browse files
committed
SSE: bound the line length and the event size
The parser accumulated an unterminated line, or data lines without a blank line, without any limit, so a misbehaving server could exhaust the heap. EventSourceConfig now has maxLineLength (1 MiB) and maxEventSize (8 MiB). When a limit is exceeded the stream fails with MessageConstraintException and its buffers are released.
1 parent 56f5815 commit 42e82fe

9 files changed

Lines changed: 298 additions & 19 deletions

File tree

‎httpclient5-sse/src/main/java/org/apache/hc/client5/http/sse/EventSourceConfig.java‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Objects;
3030

3131
import org.apache.hc.client5.http.sse.impl.ExponentialJitterBackoff;
32+
import org.apache.hc.core5.util.Args;
3233

3334
/**
3435
* Immutable configuration for {@link EventSource} behavior, primarily covering
@@ -43,6 +44,8 @@
4344
* The strategy can also honor server hints (SSE {@code retry:} field or HTTP {@code Retry-After}).</li>
4445
* <li>{@link #maxReconnects}: maximum number of reconnect attempts before giving up.
4546
* A value of {@code -1} means unlimited attempts.</li>
47+
* <li>{@link #maxLineLength}: maximum length of a single line of the stream.</li>
48+
* <li>{@link #maxEventSize}: maximum size of the data accumulated for a single event.</li>
4649
* </ul>
4750
*
4851
* <h3>Thread-safety</h3>
@@ -74,20 +77,55 @@ public final class EventSourceConfig {
7477
*/
7578
public final int maxReconnects;
7679

80+
/**
81+
* Default value of {@link #maxLineLength}, 1 MiB.
82+
*
83+
* @since 5.7
84+
*/
85+
public static final int DEFAULT_MAX_LINE_LENGTH = 1024 * 1024;
86+
87+
/**
88+
* Default value of {@link #maxEventSize}, 8 MiB.
89+
*
90+
* @since 5.7
91+
*/
92+
public static final int DEFAULT_MAX_EVENT_SIZE = 8 * 1024 * 1024;
93+
94+
/**
95+
* Maximum length of a single line, excluding the line terminator. Counted in characters
96+
* by the char parser and in bytes by the byte parser. A longer line fails the stream.
97+
*
98+
* @since 5.7
99+
*/
100+
public final int maxLineLength;
101+
102+
/**
103+
* Maximum size of the {@code data:} content accumulated for one event before the
104+
* terminating blank line. Counted in characters. A larger event fails the stream.
105+
*
106+
* @since 5.7
107+
*/
108+
public final int maxEventSize;
109+
77110
/**
78111
* Default configuration:
79112
* <ul>
80113
* <li>{@link #backoff} = {@code new ExponentialJitterBackoff(1000, 30000, 2.0, 250)}</li>
81114
* <li>{@link #maxReconnects} = {@code -1} (unlimited)</li>
115+
* <li>{@link #maxLineLength} = 1 MiB</li>
116+
* <li>{@link #maxEventSize} = 8 MiB</li>
82117
* </ul>
83118
*
84119
* @since 5.7
85120
*/
86121
public static final EventSourceConfig DEFAULT = builder().build();
87122

88-
private EventSourceConfig(final BackoffStrategy backoff, final int maxReconnects) {
123+
private EventSourceConfig(final BackoffStrategy backoff, final int maxReconnects,
124+
final int maxLineLength, final int maxEventSize) {
89125
this.backoff = backoff;
90126
this.maxReconnects = maxReconnects;
127+
this.maxLineLength = maxLineLength;
128+
this.maxEventSize = maxEventSize;
91129
}
92130

93131
/**
@@ -111,6 +149,8 @@ public static final class Builder {
111149
private BackoffStrategy backoff =
112150
new ExponentialJitterBackoff(1_000L, 30_000L, 2.0, 250L);
113151
private int maxReconnects = -1;
152+
private int maxLineLength = DEFAULT_MAX_LINE_LENGTH;
153+
private int maxEventSize = DEFAULT_MAX_EVENT_SIZE;
114154

115155
/**
116156
* Sets the reconnect/backoff strategy.
@@ -139,14 +179,38 @@ public Builder maxReconnects(final int nmaxReconnects) {
139179
return this;
140180
}
141181

182+
/**
183+
* Sets the maximum length of a single line.
184+
*
185+
* @param maxLineLength maximum line length, must be positive
186+
* @return this builder
187+
* @since 5.7
188+
*/
189+
public Builder maxLineLength(final int maxLineLength) {
190+
this.maxLineLength = Args.positive(maxLineLength, "Max line length");
191+
return this;
192+
}
193+
194+
/**
195+
* Sets the maximum size of the data accumulated for a single event.
196+
*
197+
* @param maxEventSize maximum event size, must be positive
198+
* @return this builder
199+
* @since 5.7
200+
*/
201+
public Builder maxEventSize(final int maxEventSize) {
202+
this.maxEventSize = Args.positive(maxEventSize, "Max event size");
203+
return this;
204+
}
205+
142206
/**
143207
* Builds an immutable {@link EventSourceConfig}.
144208
*
145209
* @return the config
146210
* @since 5.7
147211
*/
148212
public EventSourceConfig build() {
149-
return new EventSourceConfig(backoff, maxReconnects);
213+
return new EventSourceConfig(backoff, maxReconnects, maxLineLength, maxEventSize);
150214
}
151215
}
152216
}

‎httpclient5-sse/src/main/java/org/apache/hc/client5/http/sse/impl/ByteSseEntityConsumer.java‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
import java.nio.ByteBuffer;
3131
import java.nio.charset.StandardCharsets;
3232

33+
import org.apache.hc.client5.http.sse.EventSourceConfig;
3334
import org.apache.hc.core5.annotation.Internal;
3435
import org.apache.hc.core5.http.ContentType;
3536
import org.apache.hc.core5.http.HttpException;
37+
import org.apache.hc.core5.http.MessageConstraintException;
3638
import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer;
39+
import org.apache.hc.core5.util.Args;
3740

3841
/**
3942
* Low-allocation binary consumer for Server-Sent Events (SSE).
@@ -52,6 +55,8 @@
5255
* <li>Implements WHATWG SSE fields: {@code data}, {@code id}, {@code event}, {@code retry}.
5356
* Unknown fields and malformed {@code retry} values are ignored.</li>
5457
* <li>At end of stream, discards any incomplete line or event.</li>
58+
* <li>Fails the stream with {@link MessageConstraintException} if a line exceeds the maximum
59+
* line length in bytes or an event accumulates more than the maximum event size.</li>
5560
* </ul>
5661
*
5762
* <h3>Thread-safety</h3>
@@ -71,6 +76,8 @@ public final class ByteSseEntityConsumer extends AbstractBinAsyncEntityConsumer<
7176
private static final byte SPACE = (byte) ' ';
7277

7378
private final SseCallbacks cb;
79+
private final int maxLineLength;
80+
private final int maxEventSize;
7481

7582
// line accumulator
7683
private byte[] lineBuf = new byte[256];
@@ -88,7 +95,13 @@ public final class ByteSseEntityConsumer extends AbstractBinAsyncEntityConsumer<
8895
private boolean bomDone = false; // once true, no further BOM detection
8996

9097
public ByteSseEntityConsumer(final SseCallbacks callbacks) {
98+
this(callbacks, EventSourceConfig.DEFAULT_MAX_LINE_LENGTH, EventSourceConfig.DEFAULT_MAX_EVENT_SIZE);
99+
}
100+
101+
public ByteSseEntityConsumer(final SseCallbacks callbacks, final int maxLineLength, final int maxEventSize) {
91102
this.cb = callbacks;
103+
this.maxLineLength = Args.positive(maxLineLength, "Max line length");
104+
this.maxEventSize = Args.positive(maxEventSize, "Max event size");
92105
}
93106

94107
@Override
@@ -101,7 +114,16 @@ public void streamStart(final ContentType contentType) throws HttpException, IOE
101114
}
102115

103116
@Override
104-
protected void data(final ByteBuffer src, final boolean endOfStream) {
117+
protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException {
118+
try {
119+
consumeBytes(src, endOfStream);
120+
} catch (final MessageConstraintException ex) {
121+
releaseResources();
122+
throw ex;
123+
}
124+
}
125+
126+
private void consumeBytes(final ByteBuffer src, final boolean endOfStream) throws IOException {
105127
if (!bomDone) {
106128
while (src.hasRemaining() && bomMatched < 3) {
107129
final int expected = (bomMatched == 0) ? 0xEF : (bomMatched == 1 ? 0xBB : 0xBF);
@@ -142,7 +164,7 @@ protected void data(final ByteBuffer src, final boolean endOfStream) {
142164
}
143165
}
144166

145-
private void processByte(final byte b) {
167+
private void processByte(final byte b) throws IOException {
146168
if (b == LF) {
147169
if (pendingCr) {
148170
// LF completing a CRLF pair; the line was already emitted on the CR.
@@ -168,7 +190,10 @@ private void flushEndOfStream() {
168190
type = null;
169191
}
170192

171-
private void appendByte(final byte b) {
193+
private void appendByte(final byte b) throws MessageConstraintException {
194+
if (lineLen >= maxLineLength) {
195+
throw new MessageConstraintException("Maximum line length exceeded: " + maxLineLength);
196+
}
172197
ensureCapacity(lineLen + 1);
173198
lineBuf[lineLen++] = b;
174199
}
@@ -189,13 +214,14 @@ public void releaseResources() {
189214
lineLen = 0;
190215
pendingCr = false;
191216
data.setLength(0);
217+
data.trimToSize();
192218
id = null;
193219
type = null;
194220
bomMatched = 0;
195221
bomDone = false;
196222
}
197223

198-
private void handleLine(final byte[] buf, final int len) {
224+
private void handleLine(final byte[] buf, final int len) throws IOException {
199225
if (len == 0) {
200226
dispatch();
201227
return;
@@ -222,6 +248,9 @@ private void handleLine(final byte[] buf, final int len) {
222248
// Compare ASCII field name without allocations
223249
if (fLen == 4 && buf[0] == 'd' && buf[1] == 'a' && buf[2] == 't' && buf[3] == 'a') {
224250
final String v = new String(buf, vStart, len - vStart, StandardCharsets.UTF_8);
251+
if ((long) data.length() + v.length() + 1 > maxEventSize) {
252+
throw new MessageConstraintException("Maximum event size exceeded: " + maxEventSize);
253+
}
225254
data.append(v).append('\n');
226255
} else if (fLen == 5 && buf[0] == 'e' && buf[1] == 'v' && buf[2] == 'e' && buf[3] == 'n' && buf[4] == 't') {
227256
type = new String(buf, vStart, len - vStart, StandardCharsets.UTF_8);

‎httpclient5-sse/src/main/java/org/apache/hc/client5/http/sse/impl/DefaultEventSource.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public final class DefaultEventSource implements EventSource {
119119
private final Executor callbackExecutor;
120120
private final BackoffStrategy backoff;
121121
private final int maxReconnects;
122+
private final int maxLineLength;
123+
private final int maxEventSize;
122124
private final SseParser parser;
123125

124126
private final AtomicBoolean started = new AtomicBoolean(false);
@@ -195,6 +197,8 @@ public DefaultEventSource(final CloseableHttpAsyncClient client,
195197
final EventSourceConfig cfg = (config != null) ? config : EventSourceConfig.DEFAULT;
196198
this.backoff = cfg.backoff;
197199
this.maxReconnects = cfg.maxReconnects;
200+
this.maxLineLength = cfg.maxLineLength;
201+
this.maxEventSize = cfg.maxEventSize;
198202
this.parser = parser != null ? parser : SseParser.CHAR;
199203
}
200204

@@ -414,8 +418,8 @@ public void onRetry(final long retryMs) {
414418
};
415419

416420
final AsyncEntityConsumer<Void> entity =
417-
(parser == SseParser.BYTE) ? new ByteSseEntityConsumer(cbs)
418-
: new SseEntityConsumer(cbs);
421+
(parser == SseParser.BYTE) ? new ByteSseEntityConsumer(cbs, maxLineLength, maxEventSize)
422+
: new SseEntityConsumer(cbs, maxLineLength, maxEventSize);
419423

420424
return new SseResponseConsumer(entity, ms -> retryAfterHintMs = Math.max(0L, ms));
421425
}

‎httpclient5-sse/src/main/java/org/apache/hc/client5/http/sse/impl/ServerSentEventReader.java‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
*/
2727
package org.apache.hc.client5.http.sse.impl;
2828

29+
import java.io.IOException;
30+
31+
import org.apache.hc.client5.http.sse.EventSourceConfig;
2932
import org.apache.hc.core5.annotation.Internal;
33+
import org.apache.hc.core5.http.MessageConstraintException;
34+
import org.apache.hc.core5.util.Args;
3035

3136
/**
3237
* Minimal-allocation SSE line parser.
@@ -54,15 +59,27 @@ default void onLastEventId(final String id) {
5459
}
5560

5661
private final Callback cb;
62+
private final int maxEventSize;
5763
private final StringBuilder data = new StringBuilder(128);
5864
private String type; // defaults to "message"
5965
private String id;
6066

6167
public ServerSentEventReader(final Callback cb) {
68+
this(cb, EventSourceConfig.DEFAULT_MAX_EVENT_SIZE);
69+
}
70+
71+
public ServerSentEventReader(final Callback cb, final int maxEventSize) {
6272
this.cb = cb;
73+
this.maxEventSize = Args.positive(maxEventSize, "Max event size");
6374
}
6475

65-
public void line(final String line) {
76+
/**
77+
* Processes one line without its terminator.
78+
*
79+
* @throws MessageConstraintException if the data accumulated for the current event
80+
* exceeds the maximum event size
81+
*/
82+
public void line(final String line) throws IOException {
6683
// Trim possible trailing CR without reallocating
6784
final int L0 = line.length();
6885
int end = L0;
@@ -114,6 +131,11 @@ public void line(final String line) {
114131

115132
// data: <value> (append newline; removed on dispatch)
116133
if (vStart <= end) {
134+
if ((long) data.length() + (end - vStart) + 1 > maxEventSize) {
135+
data.setLength(0);
136+
data.trimToSize();
137+
throw new MessageConstraintException("Maximum event size exceeded: " + maxEventSize);
138+
}
117139
data.append(line, vStart, end).append('\n');
118140
}
119141

0 commit comments

Comments
 (0)