Skip to content

Commit 21f8e51

Browse files
committed
feat(sse): make parser limits configurable via SSEParserConfig
- Default max line length raised from 64 KB to 1 MB to fix interop with servers emitting large single data: lines - Limits configurable via a2a.sse.* properties (MicroProfile Config) or programmatically through SSEParserConfig builder Fixes a2aproject#1123 Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
1 parent 808756e commit 21f8e51

12 files changed

Lines changed: 633 additions & 25 deletions

File tree

‎docs/content/dev/configuration.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,45 @@ The `DefaultPushNotificationUrlValidator` enforces these policies. Custom implem
113113

114114
> **Known limitation — DNS rebinding:** hostname resolution happens at validation time, not at connection time. An attacker controlling a domain could register a URL that initially resolves to a public IP, then change the DNS record to an internal address before the next push event fires. The DNS cache reduces this window but does not eliminate it. Deployments with strict SSRF requirements should use network-level firewall rules to block outbound traffic to internal ranges.
115115
116+
### SSE Parser Limits
117+
118+
```properties
119+
# Maximum characters per raw SSE line (default: 1048576, 0 to disable)
120+
a2a.sse.max-line-length=1048576
121+
122+
# Maximum data: lines per event block (default: 1000)
123+
a2a.sse.max-buffer-lines=1000
124+
125+
# Maximum total characters across all data: values per event (default: 1048576)
126+
a2a.sse.max-buffer-chars=1048576
127+
```
128+
129+
These limits control the client-side `ServerSentEventParser` used when consuming SSE streams. They provide defense-in-depth against maliciously large events without rejecting legitimate payloads.
130+
131+
> **Scope:** The `a2a.sse.*` properties are read by the Quarkus CDI producer (`SSEParserConfigProducer`) and applied to the CDI-managed `A2AHttpClient` bean. Clients created via `A2AHttpClientFactory` (ServiceLoader) or the no-arg constructors use the built-in defaults. To customize limits outside Quarkus, construct the client programmatically (see example below).
132+
133+
**`a2a.sse.max-line-length`** caps the number of characters in a single raw SSE line (`data:`, `event:`, etc.). Set to `0` to disable the per-line check entirely (the per-event `max-buffer-chars` limit still applies). Increase this if your agents return large single-line JSON payloads.
134+
135+
**`a2a.sse.max-buffer-lines`** limits how many `data:` lines a single event block can contain.
136+
137+
**`a2a.sse.max-buffer-chars`** limits the total character count across all `data:` values within a single event. This is the primary guard against oversized events.
138+
139+
> **Note:** These limits only apply to clients backed by `ServerSentEventParser` (`JdkA2AHttpClient` and `AndroidA2AHttpClient`). The Vert.x HTTP client uses Vert.x-native SSE parsing and is not affected by these settings.
140+
141+
**Programmatic configuration** (without MicroProfile Config):
142+
143+
```java
144+
SSEParserConfig sseConfig = SSEParserConfig.builder()
145+
.maxBufferChars(4 * 1024 * 1024) // 4 MB per event
146+
.build();
147+
A2AHttpClient httpClient = new JdkA2AHttpClient(
148+
HttpClient.newBuilder()
149+
.version(HttpClient.Version.HTTP_2)
150+
.followRedirects(HttpClient.Redirect.NEVER)
151+
.build(),
152+
sseConfig);
153+
```
154+
116155
### Tuning Guidelines
117156

118157
- **Streaming Performance**: The executor handles streaming subscriptions. Too few threads can cause timeouts under concurrent load.

‎extras/http-client-android/src/main/java/org/a2aproject/sdk/client/http/android/AndroidA2AHttpClient.java‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.a2aproject.sdk.client.http.android;
22

3+
import static org.a2aproject.sdk.util.Assert.checkNotNullParam;
4+
35
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
46
import static java.net.HttpURLConnection.HTTP_MULT_CHOICE;
57
import static java.net.HttpURLConnection.HTTP_OK;
@@ -28,6 +30,7 @@
2830
import org.a2aproject.sdk.client.http.A2AHttpHeaders;
2931
import org.a2aproject.sdk.client.http.A2AHttpResponse;
3032
import org.a2aproject.sdk.client.http.ServerSentEvent;
33+
import org.a2aproject.sdk.client.http.SSEParserConfig;
3134
import org.a2aproject.sdk.client.http.ServerSentEventParser;
3235
import org.a2aproject.sdk.common.A2AErrorMessages;
3336
import org.a2aproject.sdk.spec.A2AClientHTTPError;
@@ -48,24 +51,44 @@ public class AndroidA2AHttpClient implements A2AHttpClient {
4851
return t;
4952
});
5053

54+
private final SSEParserConfig sseParserConfig;
55+
56+
public AndroidA2AHttpClient() {
57+
this(SSEParserConfig.DEFAULT);
58+
}
59+
60+
/**
61+
* Creates a new Android HTTP client with custom SSE parser limits.
62+
*
63+
* @param sseParserConfig the SSE parser configuration to use for streaming responses
64+
*/
65+
public AndroidA2AHttpClient(SSEParserConfig sseParserConfig) {
66+
this.sseParserConfig = checkNotNullParam("sseParserConfig", sseParserConfig);
67+
}
68+
5169
@Override
5270
public GetBuilder createGet() {
53-
return new AndroidGetBuilder();
71+
return new AndroidGetBuilder(sseParserConfig);
5472
}
5573

5674
@Override
5775
public PostBuilder createPost() {
58-
return new AndroidPostBuilder();
76+
return new AndroidPostBuilder(sseParserConfig);
5977
}
6078

6179
@Override
6280
public DeleteBuilder createDelete() {
63-
return new AndroidDeleteBuilder();
81+
return new AndroidDeleteBuilder(sseParserConfig);
6482
}
6583

6684
private abstract static class AndroidBuilder<T extends Builder<T>> implements Builder<T> {
6785
protected String url = "";
6886
protected Map<String, String> headers = new HashMap<>();
87+
protected final SSEParserConfig sseParserConfig;
88+
89+
AndroidBuilder(SSEParserConfig sseParserConfig) {
90+
this.sseParserConfig = sseParserConfig;
91+
}
6992

7093
@Override
7194
public T url(String url) {
@@ -203,7 +226,7 @@ protected void processSSEResponse(
203226
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
204227
String line;
205228
if (isSse) {
206-
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer);
229+
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer, sseParserConfig);
207230
while ((line = reader.readLine()) != null) {
208231
sseParser.processLine(line);
209232
}
@@ -244,6 +267,10 @@ protected CompletableFuture<Void> executeAsyncSSE(
244267
}
245268

246269
private static class AndroidGetBuilder extends AndroidBuilder<GetBuilder> implements GetBuilder {
270+
AndroidGetBuilder(SSEParserConfig sseParserConfig) {
271+
super(sseParserConfig);
272+
}
273+
247274
@Override
248275
public A2AHttpResponse get() throws IOException {
249276
HttpURLConnection connection = createConnection("GET", false);
@@ -271,6 +298,10 @@ private static class AndroidPostBuilder extends AndroidBuilder<PostBuilder>
271298
private String body = "";
272299
private boolean followRedirects = false;
273300

301+
AndroidPostBuilder(SSEParserConfig sseParserConfig) {
302+
super(sseParserConfig);
303+
}
304+
274305
@Override
275306
public PostBuilder body(String body) {
276307
this.body = body;
@@ -325,6 +356,10 @@ public CompletableFuture<Void> postAsyncSSE(
325356

326357
private static class AndroidDeleteBuilder extends AndroidBuilder<DeleteBuilder>
327358
implements DeleteBuilder {
359+
AndroidDeleteBuilder(SSEParserConfig sseParserConfig) {
360+
super(sseParserConfig);
361+
}
362+
328363
@Override
329364
public A2AHttpResponse delete() throws IOException {
330365
HttpURLConnection connection = createConnection("DELETE", false);

‎extras/http-client-android/src/test/java/org/a2aproject/sdk/client/http/android/OkHttpA2AHttpClient.java‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.a2aproject.sdk.client.http.A2AHttpHeaders;
2323
import org.a2aproject.sdk.client.http.A2AHttpResponse;
2424
import org.a2aproject.sdk.client.http.ServerSentEvent;
25+
import org.a2aproject.sdk.client.http.SSEParserConfig;
2526
import org.a2aproject.sdk.client.http.ServerSentEventParser;
2627
import org.a2aproject.sdk.common.A2AErrorMessages;
2728
import org.a2aproject.sdk.spec.A2AClientHTTPError;
@@ -48,24 +49,39 @@ class OkHttpA2AHttpClient implements A2AHttpClient {
4849
return t;
4950
});
5051

52+
private final SSEParserConfig sseParserConfig;
53+
54+
OkHttpA2AHttpClient() {
55+
this(SSEParserConfig.DEFAULT);
56+
}
57+
58+
OkHttpA2AHttpClient(SSEParserConfig sseParserConfig) {
59+
this.sseParserConfig = sseParserConfig;
60+
}
61+
5162
@Override
5263
public GetBuilder createGet() {
53-
return new OkHttpGetBuilder();
64+
return new OkHttpGetBuilder(sseParserConfig);
5465
}
5566

5667
@Override
5768
public PostBuilder createPost() {
58-
return new OkHttpPostBuilder();
69+
return new OkHttpPostBuilder(sseParserConfig);
5970
}
6071

6172
@Override
6273
public DeleteBuilder createDelete() {
63-
return new OkHttpDeleteBuilder();
74+
return new OkHttpDeleteBuilder(sseParserConfig);
6475
}
6576

6677
private abstract static class OkHttpBuilder<T extends Builder<T>> implements Builder<T> {
6778
protected String url = "";
6879
protected final Map<String, String> headers = new HashMap<>();
80+
protected final SSEParserConfig sseParserConfig;
81+
82+
OkHttpBuilder(SSEParserConfig sseParserConfig) {
83+
this.sseParserConfig = sseParserConfig;
84+
}
6985

7086
@Override
7187
public T url(String url) {
@@ -189,7 +205,7 @@ private void parseResponseBody(
189205
new InputStreamReader(body.byteStream(), StandardCharsets.UTF_8))) {
190206
String line;
191207
if (isSse) {
192-
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer);
208+
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer, sseParserConfig);
193209
while ((line = reader.readLine()) != null) {
194210
sseParser.processLine(line);
195211
}
@@ -214,6 +230,10 @@ private void parseResponseBody(
214230
}
215231

216232
private static class OkHttpGetBuilder extends OkHttpBuilder<GetBuilder> implements GetBuilder {
233+
OkHttpGetBuilder(SSEParserConfig sseParserConfig) {
234+
super(sseParserConfig);
235+
}
236+
217237
@Override
218238
public A2AHttpResponse get() throws IOException {
219239
OkHttpClient client = buildClient(false);
@@ -248,6 +268,10 @@ private static class OkHttpPostBuilder extends OkHttpBuilder<PostBuilder> implem
248268
private String body = "";
249269
private boolean followRedirects = false;
250270

271+
OkHttpPostBuilder(SSEParserConfig sseParserConfig) {
272+
super(sseParserConfig);
273+
}
274+
251275
@Override
252276
public PostBuilder body(String body) {
253277
this.body = body;
@@ -295,6 +319,10 @@ public CompletableFuture<Void> postAsyncSSE(
295319
}
296320

297321
private static class OkHttpDeleteBuilder extends OkHttpBuilder<DeleteBuilder> implements DeleteBuilder {
322+
OkHttpDeleteBuilder(SSEParserConfig sseParserConfig) {
323+
super(sseParserConfig);
324+
}
325+
298326
@Override
299327
public A2AHttpResponse delete() throws IOException {
300328
OkHttpClient client = buildClient(false);

‎http-client/src/main/java/org/a2aproject/sdk/client/http/JdkA2AHttpClient.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
public class JdkA2AHttpClient implements A2AHttpClient {
6060

6161
private final HttpClient httpClient;
62+
private final SSEParserConfig sseParserConfig;
6263
private volatile @Nullable HttpClient noRedirectClient;
6364

6465
/**
@@ -81,7 +82,7 @@ public JdkA2AHttpClient() {
8182
this(HttpClient.newBuilder()
8283
.version(HttpClient.Version.HTTP_2)
8384
.followRedirects(HttpClient.Redirect.NEVER)
84-
.build());
85+
.build(), SSEParserConfig.DEFAULT);
8586
}
8687

8788
/**
@@ -100,7 +101,20 @@ public JdkA2AHttpClient() {
100101
* @throws IllegalArgumentException if {@code httpClient} is {@code null}
101102
*/
102103
public JdkA2AHttpClient(HttpClient httpClient) {
104+
this(httpClient, SSEParserConfig.DEFAULT);
105+
}
106+
107+
/**
108+
* Creates a new JDK-based HTTP client using a caller-provided JDK {@link HttpClient}
109+
* and custom SSE parser limits.
110+
*
111+
* @param httpClient the JDK HTTP client to delegate requests to
112+
* @param sseParserConfig the SSE parser configuration to use for streaming responses
113+
* @throws IllegalArgumentException if {@code httpClient} or {@code sseParserConfig} is {@code null}
114+
*/
115+
public JdkA2AHttpClient(HttpClient httpClient, SSEParserConfig sseParserConfig) {
103116
this.httpClient = checkNotNullParam("httpClient", httpClient);
117+
this.sseParserConfig = checkNotNullParam("sseParserConfig", sseParserConfig);
104118
}
105119

106120
@Override
@@ -182,7 +196,7 @@ protected CompletableFuture<Void> asyncRequest(
182196
Consumer<Throwable> errorConsumer,
183197
Runnable completeRunnable
184198
) {
185-
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer);
199+
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer, sseParserConfig);
186200
AtomicBoolean useSseParser = new AtomicBoolean(false);
187201
AtomicBoolean errorNotified = new AtomicBoolean(false);
188202
StringBuilder nonSseBodyBuffer = new StringBuilder();
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.a2aproject.sdk.client.http;
2+
3+
/**
4+
* Configuration for {@link ServerSentEventParser} limits.
5+
*
6+
* <p>All limits have safe defaults suitable for most deployments. Use the {@link Builder}
7+
* to override individual values -- for example, to raise the per-event character budget
8+
* for agents that return large artifacts.
9+
*
10+
* <p>Setting {@code maxLineLength} to {@code 0} disables the per-line length check entirely,
11+
* relying on {@code maxBufferChars} alone to bound memory.
12+
*
13+
* @param maxLineLength max characters per raw SSE line (0 = disabled)
14+
* @param maxBufferLines max {@code data:} lines per event block
15+
* @param maxBufferChars max total characters across all {@code data:} values per event
16+
*/
17+
public record SSEParserConfig(int maxLineLength, int maxBufferLines, int maxBufferChars) {
18+
19+
/**
20+
* Default configuration: 1 MB per-line and per-event character limit, 1 000 data lines per event.
21+
*/
22+
public static final SSEParserConfig DEFAULT = new SSEParserConfig(1024 * 1024, 1000, 1024 * 1024);
23+
24+
public SSEParserConfig {
25+
if (maxLineLength < 0) {
26+
throw new IllegalArgumentException("maxLineLength must be >= 0, got " + maxLineLength);
27+
}
28+
if (maxBufferLines <= 0) {
29+
throw new IllegalArgumentException("maxBufferLines must be > 0, got " + maxBufferLines);
30+
}
31+
if (maxBufferChars <= 0) {
32+
throw new IllegalArgumentException("maxBufferChars must be > 0, got " + maxBufferChars);
33+
}
34+
}
35+
36+
/**
37+
* Returns a new {@link Builder} initialized with the {@link #DEFAULT} values.
38+
*/
39+
public static Builder builder() {
40+
return new Builder();
41+
}
42+
43+
public static final class Builder {
44+
private int maxLineLength = DEFAULT.maxLineLength;
45+
private int maxBufferLines = DEFAULT.maxBufferLines;
46+
private int maxBufferChars = DEFAULT.maxBufferChars;
47+
48+
Builder() {
49+
}
50+
51+
/**
52+
* Sets the maximum number of characters allowed in a single raw SSE line.
53+
* Set to {@code 0} to disable the per-line check.
54+
*/
55+
public Builder maxLineLength(int maxLineLength) {
56+
this.maxLineLength = maxLineLength;
57+
return this;
58+
}
59+
60+
/**
61+
* Sets the maximum number of {@code data:} lines allowed in a single event block.
62+
*/
63+
public Builder maxBufferLines(int maxBufferLines) {
64+
this.maxBufferLines = maxBufferLines;
65+
return this;
66+
}
67+
68+
/**
69+
* Sets the maximum total characters across all {@code data:} values in a single event.
70+
*/
71+
public Builder maxBufferChars(int maxBufferChars) {
72+
this.maxBufferChars = maxBufferChars;
73+
return this;
74+
}
75+
76+
public SSEParserConfig build() {
77+
return new SSEParserConfig(maxLineLength, maxBufferLines, maxBufferChars);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)