From c3e18dad33d0f934be8d92c35f8b5aea60ce5586 Mon Sep 17 00:00:00 2001 From: Michael Matloka Date: Wed, 19 May 2021 01:27:20 +0200 Subject: [PATCH] Ensure correct event team ID instead of throwing error --- src/worker/plugins/run.ts | 17 +++++++++++------ tests/plugins.test.ts | 38 ++++++++++++++++---------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/worker/plugins/run.ts b/src/worker/plugins/run.ts index ede3a0f0..9b5a72f2 100644 --- a/src/worker/plugins/run.ts +++ b/src/worker/plugins/run.ts @@ -58,10 +58,6 @@ export async function runProcessEvent(server: PluginsServer, event: PluginEvent) try { returnedEvent = (await processEvent(returnedEvent)) || null - if (returnedEvent.team_id != teamId) { - returnedEvent = null // don't try to ingest events with modified teamIDs - throw new Error('Illegal Operation: Plugin tried to change teamID') - } pluginsSucceeded.push(`${pluginConfig.plugin?.name} (${pluginConfig.id})`) } catch (error) { await processError(server, pluginConfig, error, returnedEvent) @@ -79,9 +75,14 @@ export async function runProcessEvent(server: PluginsServer, event: PluginEvent) } } + // Ensure that team ID stays unmodified + if (returnedEvent && typeof returnedEvent == 'object') { + returnedEvent.team_id = teamId + } + if (pluginsSucceeded.length > 0 || pluginsFailed.length > 0) { - event.properties = { - ...event.properties, + returnedEvent.properties = { + ...returnedEvent.properties, $plugins_succeeded: pluginsSucceeded, $plugins_failed: pluginsFailed, } @@ -130,6 +131,10 @@ export async function runProcessEventBatch(server: PluginsServer, batch: PluginE } for (const event of returnedEvents) { + // Ensure that team ID stays unmodified + if (event && typeof event == 'object') { + event.team_id = teamId + } if (event && (pluginsSucceeded.length > 0 || pluginsFailed.length > 0)) { event.properties = { ...event.properties, diff --git a/tests/plugins.test.ts b/tests/plugins.test.ts index 4bd28694..54eaeb1a 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -5,7 +5,7 @@ import { LogLevel, PluginsServer, PluginTaskType } from '../src/types' import { clearError, processError } from '../src/utils/db/error' import { createServer } from '../src/utils/db/server' import { loadPlugin } from '../src/worker/plugins/loadPlugin' -import { runProcessEvent } from '../src/worker/plugins/run' +import { runProcessEvent, runProcessEventBatch } from '../src/worker/plugins/run' import { loadSchedule, setupPlugins } from '../src/worker/plugins/setup' import { commonOrganizationId, @@ -196,12 +196,13 @@ test('local plugin with broken index.js does not do much', async () => { unlink() }) -test('plugin changing teamID throws error', async () => { +test('plugin cannot change team ID (single)', async () => { getPluginRows.mockReturnValueOnce([ mockPluginWithArchive(` - function processEvent (event, meta) { + function processEvent (event, meta) { event.team_id = 400 - return event } + return event + } `), ]) @@ -209,27 +210,20 @@ test('plugin changing teamID throws error', async () => { getPluginAttachmentRows.mockReturnValueOnce([]) await setupPlugins(mockServer) - const { pluginConfigs } = mockServer - const event = { event: '$test', properties: {}, team_id: 2 } as PluginEvent - const returnedEvent = await runProcessEvent(mockServer, event) + const originalEvent = { event: '$test', properties: {}, team_id: 2 } as PluginEvent + const returnedEvent = await runProcessEvent(mockServer, originalEvent) - expect(returnedEvent).toEqual(null) - - expect(processError).toHaveBeenCalledWith( - mockServer, - pluginConfigs.get(39)!, - Error('Illegal Operation: Plugin tried to change teamID'), - null - ) + expect(returnedEvent!.team_id).toEqual(2) }) -test('plugin changing teamID prevents ingestion', async () => { +test('plugin cannot change team ID (batch)', async () => { getPluginRows.mockReturnValueOnce([ mockPluginWithArchive(` - function processEvent (event, meta) { - event.team_id = 400 - return event } + function processEventBatch (events, meta) { + events[0].team_id = 400 + return event + } `), ]) @@ -238,10 +232,10 @@ test('plugin changing teamID prevents ingestion', async () => { await setupPlugins(mockServer) - const event = { event: '$test', properties: {}, team_id: 2 } as PluginEvent - const returnedEvent = await runProcessEvent(mockServer, event) + const originalEvent = { event: '$test', properties: {}, team_id: 2 } as PluginEvent + const returnedEvents = await runProcessEventBatch(mockServer, [originalEvent]) - expect(returnedEvent).toEqual(null) + expect(returnedEvents[0]!.team_id).toEqual(2) }) test('plugin throwing error does not prevent ingestion and failure is noted in event', async () => {