Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces logic to handle JSONL separators by tracking whether the last processed event was a JSON message. While this correctly handles whitespace between JSON objects, a potential Denial of Service vulnerability was identified where the buffer could grow indefinitely if followed by a continuous stream of whitespace. A limit on the buffer size for whitespace-only content is recommended to prevent memory exhaustion.
| if (_lastWasJson && _buffer.trim().isEmpty) { | ||
| // Whitespace-only after a JSON message: treat as JSONL separator. | ||
| // Hold in buffer until more data arrives or stream ends. | ||
| break; | ||
| } |
There was a problem hiding this comment.
The newly introduced logic for handling JSONL separators in the _processBuffer method allows the _buffer to grow indefinitely if a JSON message is followed by a continuous stream of whitespace. When _lastWasJson is true and the buffer contains only whitespace, the code breaks out of the processing loop without clearing or emitting the buffer (lines 136-140). An attacker providing a stream of data can exploit this by sending a valid JSON message followed by an infinite or extremely large amount of whitespace, leading to memory exhaustion and a Denial of Service (DoS) crash of the application.
To remediate this, implement a limit on the maximum amount of whitespace that can be held in the buffer as a separator. If the whitespace-only buffer exceeds a reasonable threshold (e.g., 4KB), it should be cleared or emitted as a TextEvent to prevent unbounded memory growth.
if (_lastWasJson && _buffer.trim().isEmpty) {
if (_buffer.length > 4096) {
_emitText(_buffer);
_buffer = '';
} else {
break;
}
}The current implementation is treating newline character as a TextEvent, as opposed to a JSONL newline separator. See transport decoupling for more information: https://github.com/google/A2UI/blob/main/specification/v0_9/docs/a2ui_protocol.md#transport-decoupling
Description
Fixes #867.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-devrel channel on Discord.