Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions webview/shared/src/chat/lib/messageHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ describe('extractEventMessageId', () => {
});
});

describe('malformed stream events', () => {
it('ignores null, undefined, primitive, and array event payloads', () => {
let state = { ...initialState, currentSessionId: 'ses-malformed' } as AppState;
const dispatch = (action: Parameters<typeof appReducer>[1]) => {
state = appReducer(state, action);
};
const handler = createMessageHandler(dispatch, () => state);

for (const event of [null, undefined, 'invalid', []]) {
handler({
data: { type: 'streamEvent', sessionId: 'ses-malformed', event },
} as MessageEvent);
}

assert.equal(state.isProcessing, false);
assert.equal(state.streaming, null);
});
});

describe('normalizeMessage - responseType handling', () => {
it('ignores undefined interactive events from an untrusted payload', () => {
const result = normalizeMessage({
Expand Down
8 changes: 7 additions & 1 deletion webview/shared/src/chat/lib/messageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9251,6 +9251,8 @@ function handleStreamEvent(
knownReasoningPartIDs?: Set<string>,
pendingRenderableTextPart?: { partID?: string; messageID?: string },
): void {
if (!payload || typeof payload !== "object") return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reject malformed events before substituting the envelope

When the host sends { type: "streamEvent", event: null, processing: true }, the stream case sets payload to the outer data object via asRecord(data.event) ?? data, so this guard never returns; moreover, the shared pre-switch logic has already dispatched SET_PROCESSING, which can leave a phantom loading state. Reject a missing/non-record data.event at the message boundary before processing bootstrap, and exercise that path through the handler rather than matching source text.

AGENTS.md reference: AGENTS.md:L9-L14

Useful? React with 👍 / 👎.


const dispatchProcessingTrue = () => {
if (!shouldSuppressProcessingBootstrap) {
dispatch({ type: "SET_IS_PROCESSING", payload: true });
Expand Down Expand Up @@ -14303,7 +14305,11 @@ export function createMessageHandler(dispatch: Dispatch<AppAction>, getState: ()
}
case "streamEvent": {
const stateBeforeStreamEvent = getState();
const payload = asRecord(data.event) ?? data;
const payload = asRecord(data.event);
if (!payload || Array.isArray(payload)) {
logger.warn("Ignoring stream event with an invalid payload");
break;
}
const streamEventType = asString(payload.type) || "unknown";
const envelopeSessionId =
asString(data.sessionId) ||
Expand Down
Loading