This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new d5dc3e202 fix(ai): reject non-object run stream frames (#5179)
d5dc3e202 is described below

commit d5dc3e202228ef0fe9a7c392669885ca6df6bccf
Author: Loyal-Young <[email protected]>
AuthorDate: Fri Nov 27 11:53:53 2026 +0800

    fix(ai): reject non-object run stream frames (#5179)
---
 web/src/api/ai.test.ts | 17 +++++++++++++++++
 web/src/api/ai.ts      |  7 +++++++
 2 files changed, 24 insertions(+)

diff --git a/web/src/api/ai.test.ts b/web/src/api/ai.test.ts
index 207baf7a9..8af0d2b56 100644
--- a/web/src/api/ai.test.ts
+++ b/web/src/api/ai.test.ts
@@ -515,6 +515,23 @@ describe('AI API', () => {
       } satisfies Partial<AiStreamError>);
     });
 
+    it.each(['null', '[]', '42', '"text"'])(
+      'rejects a non-object agent payload as malformed: %s',
+      async (payload) => {
+        vi.stubGlobal(
+          'fetch',
+          vi.fn().mockResolvedValue(eventStreamResponse([`event: agent\ndata: 
${payload}\n\n`])),
+        );
+        const onEvent = vi.fn();
+
+        await expect(openRunStream(7, body, { onEvent 
})).rejects.toMatchObject({
+          name: 'AiStreamError',
+          code: 'llm.stream.malformed_event',
+        } satisfies Partial<AiStreamError>);
+        expect(onEvent).not.toHaveBeenCalled();
+      },
+    );
+
     it('rejectsAResponseThatIsNotAnEventStreamTest', async () => {
       // A buffering gateway answers 200 with HTML and then yields nothing: 
without this guard the
       // UI spins forever with an empty console.
diff --git a/web/src/api/ai.ts b/web/src/api/ai.ts
index 63c00ae19..6af98c1e2 100644
--- a/web/src/api/ai.ts
+++ b/web/src/api/ai.ts
@@ -334,6 +334,13 @@ function emitRunFrame(frame: string, handlers: 
RunStreamHandlers): boolean {
     );
   }
 
+  if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
+    throw new AiStreamError(
+      `Malformed AI stream event: ${describePayload(payload)}`,
+      'llm.stream.malformed_event',
+    );
+  }
+
   const event = parsed as ChatSseEvent;
   const type = (event as { type?: unknown }).type;
   if (typeof type !== 'string' || !LIVE_EVENT_TYPE_SET.has(type)) {

Reply via email to