fix(editor): Return early in ws message handler if no 'command' keyword is found (#7946)

## Summary
Avoid processing websocket messages not n8n sent (filter on 'command'
keyword)
This commit is contained in:
Csaba Tuncsik 2023-12-08 13:42:50 +01:00 committed by GitHub
parent dcf12867b3
commit 5b2defc867
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 65 deletions

View file

@ -136,7 +136,9 @@ const onMouseLeave = () => {
};
const receiveMessage = ({ data }: MessageEvent) => {
if (data?.includes('"command"')) {
if (!data?.includes?.('"command"')) {
return;
}
try {
const json = JSON.parse(data);
if (json.command === 'n8nReady') {
@ -151,7 +153,6 @@ const receiveMessage = ({ data }: MessageEvent) => {
} catch (e) {
console.error(e);
}
}
};
const onDocumentScroll = () => {
if (insideIframe.value) {

View file

@ -247,4 +247,18 @@ describe('WorkflowPreview', () => {
expect(emitted()).toEqual({});
});
});
it('should not do anything if no "command" is sent in the message and the `includes` method cannot be applied to the data', async () => {
const { emitted } = renderComponent({
pinia,
props: {},
});
window.postMessage(null, '*');
await waitFor(() => {
expect(console.error).not.toHaveBeenCalled();
expect(emitted()).toEqual({});
});
});
});

View file

@ -4312,7 +4312,9 @@ export default defineComponent({
}
},
async onPostMessageReceived(message: MessageEvent) {
if (message?.data?.includes('"command"')) {
if (!message?.data?.includes?.('"command"')) {
return;
}
try {
const json = JSON.parse(message.data);
if (json && json.command === 'openWorkflow') {
@ -4363,7 +4365,6 @@ export default defineComponent({
this.workflowsStore.activeWorkflowExecution = json.execution;
}
} catch (e) {}
}
},
async onImportWorkflowDataEvent(data: IDataObject) {
await this.importWorkflowData(data.data as IWorkflowDataUpdate, 'file');