Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.
Closed
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
17 changes: 11 additions & 6 deletions src/worker/plugins/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 16 additions & 22 deletions tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -196,40 +196,34 @@ 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
}
`),
])

getPluginConfigRows.mockReturnValueOnce([pluginConfig39])
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
}
`),
])

Expand All @@ -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 () => {
Expand Down