chore: Add spam checking for workflow bodies (#18822)
* Add akismet package to tasker * Create scanWorkflowBody task * Schedule workflow body scan * Add AKISMET_API_KEY .env * Auto lock user if spam is detected * Uncommit key * Add safe param to workflow step * Migration for safe field * Do not process workflow steps is `safe` is false * Update migration to set previous records to true * Address comments * Refactor `scheduleWorkflowNotifications` to accept an object * If new steps or editing old ones send to tasker * Call `scheduleWorkflowNotifications` in task * Fix `IS_SELF_HOSTED` * Remove unused function * Make `safe` optional in schema * Type fix * Revert "Make `safe` optional in schema" This reverts commit d0964702affa87c35562300301473d25635c565b. * Revert "Type fix" This reverts commit d9a031303269a2994ae46f576ab2a3d31e4d977b. * Type fixes * Type fixes * Address comments * Fix tests * Add tests * Update tests * Typo fix * Update `safe` to `verifiedAt` * feat: Compare workflow reminder bodies to default template (#19060) * Add `getTemplateForAction` function * Use `getTemplateForAction` when creating a new step * Use `getTemplateForAction` when action changes * Have `emailReminderTemplate` accept an object as a param * Rename `getTemplateForAction` to `getTemplateBodyForAction` * Simplify changing body when changing templates * Create `compareReminderBodyToTemplate` * In task, compare if reminderBody is a template * Linting * Add tests * refactor: `emailReminderTemplate` to accept object as param (#19288) * Add `getTemplateForAction` function * Use `getTemplateForAction` when creating a new step * Use `getTemplateForAction` when action changes * Have `emailReminderTemplate` accept an object as a param * Rename `getTemplateForAction` to `getTemplateBodyForAction` * Simplify changing body when changing templates * Create `compareReminderBodyToTemplate` * In task, compare if reminderBody is a template * Linting * Add tests * Refactor `scheduleEmailReminders` * Refactor `create.handler` for new workflows * Refactor `emailReminderManager` * Refactor `getEmailTemplateText` * Fix typo * Type fix - whatsapp plain text template imports * Type fix - no template found * Type fix - add `isBrandingDisabled` to `emailReminderTemplate` * Add workflow and user to prisma mock * Fix imports for akismet dependencies * Record user lock reason * Undo linting changes * Fix tests * New workflow, at verify created step * Handle if `SCANNING_WORKFLOW_STEPS` is toggled * Move `verifiedAt` checks to specific schedule functions - `scheduleWhatsappReminder` - `scheduleEmailReminder` - `scheduleSMSReminder` * Update logic * Do not fallback verifiedAt * Add comment to next.config.js
This commit is contained in:
1 parent
0965fad72a
commit
8c9eb18463
43 files changed
+876
-262
No files matched your search
@@ -18,6 +18,7 @@ type TaskPayloads = {
|
||||
typeof import("./tasks/translateEventTypeData").ZTranslateEventDataPayloadSchema
|
||||
>;
|
||||
createCRMEvent: z.infer<typeof import("./tasks/crm/schema").createCRMEventSchema>;
|
||||
scanWorkflowBody: z.infer<typeof import("./tasks/scanWorkflowBody").scanWorkflowBodySchema>;
|
||||
};
|
||||
export type TaskTypes = keyof TaskPayloads;
|
||||
export type TaskHandler = (payload: string) => Promise<void>;
|
||||
|
||||
@@ -22,6 +22,7 @@ const tasks: Record<TaskTypes, () => Promise<TaskHandler>> = {
|
||||
translateEventTypeData: () =>
|
||||
import("./translateEventTypeData").then((module) => module.translateEventTypeData),
|
||||
createCRMEvent: () => import("./crm/createCRMEvent").then((module) => module.createCRMEvent),
|
||||
scanWorkflowBody: () => import("./scanWorkflowBody").then((module) => module.scanWorkflowBody),
|
||||
};
|
||||
|
||||
export const tasksConfig = {
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
import prismaMock from "../../../../tests/libs/__mocks__/prismaMock";
|
||||
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
|
||||
import { lockUser, LockReason } from "@calcom/lib/autoLock";
|
||||
import { scheduleWorkflowNotifications } from "@calcom/trpc/server/routers/viewer/workflows/util";
|
||||
|
||||
import { scanWorkflowBody } from "./scanWorkflowBody";
|
||||
|
||||
const mockAkismetCheckSpam = vi.fn();
|
||||
|
||||
// Mock the entire module
|
||||
vi.mock("akismet-api", () => {
|
||||
return {
|
||||
AkismetClient: class {
|
||||
constructor() {
|
||||
return {
|
||||
checkSpam: mockAkismetCheckSpam,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@calcom/lib/autoLock", async (importActual) => {
|
||||
const actual = await importActual<typeof import("@calcom/lib/autoLock")>();
|
||||
return {
|
||||
...actual, // Keep all original exports
|
||||
lockUser: vi.fn(), // Override just the lockUser function
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@calcom/trpc/server/routers/viewer/workflows/util", () => ({
|
||||
scheduleWorkflowNotifications: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockWorkflowStep = {
|
||||
id: 1,
|
||||
reminderBody: "Test reminder body",
|
||||
workflow: {
|
||||
user: {
|
||||
timeFormat: 24,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const mockWorkflow = {
|
||||
id: 1,
|
||||
time: 24,
|
||||
timeUnit: "hour",
|
||||
trigger: "BEFORE",
|
||||
activeOn: [{ eventTypeId: 1 }],
|
||||
team: null,
|
||||
};
|
||||
|
||||
describe("scanWorkflowBody", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
process.env.AKISMET_API_KEY = "test-key";
|
||||
prismaMock.workflowStep.findMany.mockResolvedValue([mockWorkflowStep]);
|
||||
prismaMock.workflow.findFirst.mockResolvedValue(mockWorkflow);
|
||||
});
|
||||
|
||||
it("should skip scan if AKISMET_API_KEY is not set", async () => {
|
||||
process.env.AKISMET_API_KEY = "";
|
||||
const payload = JSON.stringify({
|
||||
userId: 1,
|
||||
workflowStepIds: [1],
|
||||
});
|
||||
|
||||
await scanWorkflowBody(payload);
|
||||
|
||||
expect(prismaMock.workflowStep.findMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should mark workflow step as safe if no reminder body", async () => {
|
||||
const payload = JSON.stringify({
|
||||
userId: 1,
|
||||
workflowStepIds: [1],
|
||||
});
|
||||
|
||||
prismaMock.workflowStep.findMany.mockResolvedValue([{ ...mockWorkflowStep, reminderBody: null }]);
|
||||
prismaMock.workflow.findFirst.mockResolvedValue(mockWorkflow);
|
||||
|
||||
await scanWorkflowBody(payload);
|
||||
|
||||
expect(prismaMock.workflowStep.update).toHaveBeenCalledWith({
|
||||
where: { id: 1 },
|
||||
data: { verifiedAt: expect.any(Date) },
|
||||
});
|
||||
});
|
||||
|
||||
it("should mark workflow step as safe if content is not spam", async () => {
|
||||
const payload = JSON.stringify({
|
||||
userId: 1,
|
||||
workflowStepIds: [1],
|
||||
});
|
||||
|
||||
prismaMock.workflowStep.findMany.mockResolvedValue([mockWorkflowStep]);
|
||||
prismaMock.workflow.findFirst.mockResolvedValue(mockWorkflow);
|
||||
mockAkismetCheckSpam.mockResolvedValue(false);
|
||||
|
||||
await scanWorkflowBody(payload);
|
||||
|
||||
expect(mockAkismetCheckSpam).toHaveBeenCalledWith({
|
||||
user_ip: "127.0.0.1",
|
||||
content: mockWorkflowStep.reminderBody,
|
||||
});
|
||||
expect(prismaMock.workflowStep.update).toHaveBeenCalledWith({
|
||||
where: { id: 1 },
|
||||
data: { verifiedAt: expect.any(Date) },
|
||||
});
|
||||
});
|
||||
|
||||
it("should lock user and not update step if content is spam", async () => {
|
||||
const payload = JSON.stringify({
|
||||
userId: 1,
|
||||
workflowStepIds: [1],
|
||||
});
|
||||
|
||||
prismaMock.workflowStep.findMany.mockResolvedValue([mockWorkflowStep]);
|
||||
mockAkismetCheckSpam.mockResolvedValue(true);
|
||||
|
||||
await scanWorkflowBody(payload);
|
||||
|
||||
expect(mockAkismetCheckSpam).toHaveBeenCalled();
|
||||
expect(prismaMock.workflowStep.update).not.toHaveBeenCalled();
|
||||
expect(lockUser).toHaveBeenCalledWith("userId", "1", LockReason.SPAM_WORKFLOW_BODY);
|
||||
});
|
||||
|
||||
it("should schedule workflow notifications after successful scan", async () => {
|
||||
const payload = JSON.stringify({
|
||||
userId: 1,
|
||||
workflowStepIds: [1],
|
||||
});
|
||||
|
||||
prismaMock.workflowStep.findMany.mockResolvedValue([mockWorkflowStep]);
|
||||
prismaMock.workflow.findFirst.mockResolvedValue(mockWorkflow);
|
||||
mockAkismetCheckSpam.mockResolvedValue(false);
|
||||
|
||||
await scanWorkflowBody(payload);
|
||||
|
||||
expect(scheduleWorkflowNotifications).toHaveBeenCalledWith({
|
||||
activeOn: [1],
|
||||
isOrg: false,
|
||||
workflowSteps: [mockWorkflowStep],
|
||||
time: mockWorkflow.time,
|
||||
timeUnit: mockWorkflow.timeUnit,
|
||||
trigger: mockWorkflow.trigger,
|
||||
userId: 1,
|
||||
teamId: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle invalid payload", async () => {
|
||||
const payload = "invalid-json";
|
||||
|
||||
await expect(scanWorkflowBody(payload)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("should handle workflow not found", async () => {
|
||||
const payload = JSON.stringify({
|
||||
userId: 1,
|
||||
workflowStepIds: [1],
|
||||
});
|
||||
|
||||
prismaMock.workflowStep.findMany.mockResolvedValue([mockWorkflowStep]);
|
||||
prismaMock.workflow.findFirst.mockResolvedValue(null);
|
||||
mockAkismetCheckSpam.mockResolvedValue(false);
|
||||
|
||||
await scanWorkflowBody(payload);
|
||||
|
||||
expect(scheduleWorkflowNotifications).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
import { AkismetClient } from "akismet-api";
|
||||
import type { Comment } from "akismet-api";
|
||||
import z from "zod";
|
||||
|
||||
import { getTemplateBodyForAction } from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
||||
import compareReminderBodyToTemplate from "@calcom/features/ee/workflows/lib/compareReminderBodyToTemplate";
|
||||
import { lockUser, LockReason } from "@calcom/lib/autoLock";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { scheduleWorkflowNotifications } from "@calcom/trpc/server/routers/viewer/workflows/util";
|
||||
|
||||
export const scanWorkflowBodySchema = z.object({
|
||||
userId: z.number(),
|
||||
workflowStepIds: z.array(z.number()),
|
||||
});
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["[tasker] scanWorkflowBody"] });
|
||||
|
||||
export async function scanWorkflowBody(payload: string) {
|
||||
if (!process.env.AKISMET_API_KEY) {
|
||||
log.info("AKISMET_API_KEY not set, skipping scan");
|
||||
return;
|
||||
}
|
||||
|
||||
const { workflowStepIds, userId } = scanWorkflowBodySchema.parse(JSON.parse(payload));
|
||||
|
||||
const workflowSteps = await prisma.workflowStep.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: workflowStepIds,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
workflow: {
|
||||
select: {
|
||||
user: {
|
||||
select: {
|
||||
locale: true,
|
||||
timeFormat: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const client = new AkismetClient({ key: process.env.AKISMET_API_KEY, blog: WEBAPP_URL });
|
||||
|
||||
for (const workflowStep of workflowSteps) {
|
||||
if (!workflowStep.reminderBody) {
|
||||
await prisma.workflowStep.update({
|
||||
where: {
|
||||
id: workflowStep.id,
|
||||
},
|
||||
data: {
|
||||
verifiedAt: new Date(),
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const timeFormat = getTimeFormatStringFromUserTimeFormat(workflowStep.workflow.user?.timeFormat);
|
||||
|
||||
// Determine if body is a template
|
||||
const defaultTemplate = getTemplateBodyForAction({
|
||||
action: workflowStep.action,
|
||||
locale: workflowStep.workflow.user?.locale ?? "en",
|
||||
template: workflowStep.template,
|
||||
timeFormat,
|
||||
});
|
||||
|
||||
if (!defaultTemplate) {
|
||||
log.error(`Template not found for action ${workflowStep.action}, template ${workflowStep.template}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
compareReminderBodyToTemplate({ reminderBody: workflowStep.reminderBody, template: defaultTemplate })
|
||||
) {
|
||||
await prisma.workflowStep.update({
|
||||
where: {
|
||||
id: workflowStep.id,
|
||||
},
|
||||
data: {
|
||||
verifiedAt: new Date(),
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const comment: Comment = {
|
||||
user_ip: "127.0.0.1",
|
||||
content: workflowStep.reminderBody,
|
||||
};
|
||||
|
||||
const isSpam = await client.checkSpam(comment);
|
||||
|
||||
if (isSpam) {
|
||||
// We won't delete the workflow step incase it is flagged as a false positive
|
||||
log.warn(`Workflow step ${workflowStep.id} is spam with body ${workflowStep.reminderBody}`);
|
||||
await lockUser("userId", userId.toString(), LockReason.SPAM_WORKFLOW_BODY);
|
||||
|
||||
// Return early if spam is detected
|
||||
return;
|
||||
} else {
|
||||
await prisma.workflowStep.update({
|
||||
where: {
|
||||
id: workflowStep.id,
|
||||
},
|
||||
data: {
|
||||
verifiedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const workflow = await prisma.workflow.findFirst({
|
||||
where: {
|
||||
steps: {
|
||||
some: {
|
||||
id: {
|
||||
in: workflowStepIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
activeOn: true,
|
||||
team: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!workflow) {
|
||||
log.warn(`Workflow with steps ${workflowStepIds} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
const isOrg = !!workflow?.team?.isOrganization;
|
||||
|
||||
await scheduleWorkflowNotifications({
|
||||
activeOn: workflow.activeOn.map((activeOn) => activeOn.eventTypeId) ?? [],
|
||||
isOrg,
|
||||
workflowSteps,
|
||||
time: workflow.time,
|
||||
timeUnit: workflow.timeUnit,
|
||||
trigger: workflow.trigger,
|
||||
userId,
|
||||
teamId: workflow.team?.id || null,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user