Repository navigation
feat(guard): check every API email, pause any team for clear phishing or scams #530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
50e4cb1
a6bd484
2aa5080
da73f51
3acc06e
af264b5
6b8e920
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { adminApiRoute } from "~/server/admin-api"; | ||
| import { listNewEmails, newEmailsQuery } from "~/server/service/guard-service"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| /** | ||
| * GET /api/admin/guard/emails?cursor=<nextCursor>&limit=<1-200> | ||
| * New API and SMTP emails across all teams, oldest first. Pass `nextCursor` | ||
| * back to continue; without a cursor it starts 10 minutes ago. | ||
| */ | ||
| export const GET = adminApiRoute(async ({ req }) => { | ||
| const url = new URL(req.url); | ||
| return listNewEmails( | ||
| newEmailsQuery.parse(Object.fromEntries(url.searchParams)), | ||
| ); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,15 @@ import { logger } from "~/server/logger/log"; | |
| import IORedis from "ioredis"; | ||
| import { redisKey } from "~/server/redis"; | ||
| import { TeamService } from "~/server/service/team-service"; | ||
| import { emailTeam } from "~/server/service/team-email"; | ||
| import { sendToDiscord } from "~/server/service/notification-service"; | ||
|
|
||
| /** | ||
| * What the Guard (our separate review service) can read and do, through the | ||
| * internal admin API. It reads applications, campaigns, new teams and their | ||
| * emails; it can approve or stop a campaign, log an alert, and pause a team. | ||
| * It can never block or resume a team, or delete anything. | ||
| * It can never block or resume a team, or delete anything. It pauses older | ||
| * teams only for phishing or scams, naming the emails that show it. | ||
| * | ||
| * New teams' campaigns wait for the Guard's check before sending, but only | ||
| * while the Guard is running (it checks in by listing campaigns), and only | ||
|
|
@@ -233,7 +236,8 @@ export async function listActiveCampaigns( | |
| select: { id: true }, | ||
| }); | ||
| const pageIds = rows.slice(0, query.limit).map((row) => row.id); | ||
| const nextCursor = rows.length > query.limit ? (pageIds.at(-1) ?? null) : null; | ||
| const nextCursor = | ||
| rows.length > query.limit ? (pageIds.at(-1) ?? null) : null; | ||
| if (pageIds.length === 0) return { items: [], nextCursor }; | ||
|
|
||
| const campaigns = await db.campaign.findMany({ | ||
|
|
@@ -419,8 +423,44 @@ export const teamActionBody = z.object({ | |
| action: z.enum(["ALERT", "PAUSE"]), | ||
| reason: z.string().trim().min(1).max(2_000), | ||
| details: z.record(z.unknown()).optional(), | ||
| /** Needed to pause a team past its first 14 days. */ | ||
| abuse: z.enum(["phishing", "scam"]).optional(), | ||
| /** The team's emails that show the abuse. Needed with `abuse`. */ | ||
| emailIds: z.array(z.string().min(1)).min(1).max(20).optional(), | ||
| }); | ||
|
|
||
| const ABUSE_LABEL = { | ||
| phishing: | ||
| "phishing (pretending to be another company to get people's details)", | ||
| scam: "a scam", | ||
| } as const; | ||
|
|
||
| /** The email a team gets when the Guard pauses it for phishing or a scam. */ | ||
| export function abusePauseEmail(input: { | ||
| teamName: string; | ||
| abuse: keyof typeof ABUSE_LABEL; | ||
| subjects: string[]; | ||
| }) { | ||
| // Their own subjects, but cleaned: one line, no links or domains, short. | ||
| const subjects = input.subjects | ||
| .map((subject) => | ||
| subject | ||
| .replace(/\b[a-z][a-z0-9+.-]*:\/\/\S+/gi, "") | ||
| .replace(/\b(?:[a-z0-9-]+\.)+[a-z]{2,24}\b(?:\/\S*)?/gi, "") | ||
| .replace(/\s+/g, " ") | ||
| .trim() | ||
| .slice(0, 100), | ||
| ) | ||
| .filter(Boolean) | ||
| .slice(0, 3) | ||
| .map((subject) => `- "${subject}"`) | ||
| .join("\n"); | ||
| return { | ||
| subject: "useSend: we paused sending on your account", | ||
| text: `Hi ${input.teamName} team,\n\nWe paused sending on your useSend account because emails sent from it look like ${ABUSE_LABEL[input.abuse]}, for example:\n${subjects}\n\nIf you didn't send these, one of your API keys has probably leaked, often from a hacked website. Delete that key in useSend, create a new one, and check the site that used it.\n\nReply to this email and we'll turn sending back on once it's sorted. While sending is paused, new emails fail instead of going out.\n\nuseSend`, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * ALERT only logs. PAUSE stops the team's sending and its campaigns, like an | ||
| * admin pause, for new teams only; an admin | ||
|
|
@@ -432,7 +472,7 @@ export async function recordTeamAction( | |
| ) { | ||
| const team = await db.team.findUnique({ | ||
| where: { id: teamId }, | ||
| select: { id: true, createdAt: true }, | ||
| select: { id: true, name: true, createdAt: true }, | ||
| }); | ||
| if (!team) throw new GuardNotFoundError("Team not found"); | ||
|
|
||
|
|
@@ -455,7 +495,24 @@ export async function recordTeamAction( | |
| return { ok: true, changed: false }; | ||
| } | ||
|
|
||
| assertGuardMayAct(team); | ||
| // New teams can be paused for anything clear; older teams only for | ||
| // phishing or scams, with the emails that show it. | ||
| let evidence: Array<{ subject: string }> = []; | ||
| if (body.abuse) { | ||
| const ids = [...new Set(body.emailIds ?? [])]; | ||
| if (ids.length === 0) { | ||
| throw new GuardForbiddenError("Name the emails that show it (emailIds)"); | ||
| } | ||
| evidence = await db.email.findMany({ | ||
| where: { id: { in: ids }, teamId }, | ||
| select: { subject: true }, | ||
| }); | ||
| if (evidence.length !== ids.length) { | ||
| throw new GuardForbiddenError("Some of those emails aren't this team's"); | ||
| } | ||
| } else { | ||
| assertGuardMayAct(team); | ||
| } | ||
|
|
||
| const changed = await db.$transaction(async (tx) => { | ||
| await tx.$queryRaw`SELECT id FROM "Team" WHERE id = ${teamId} FOR UPDATE`; | ||
|
|
@@ -487,6 +544,104 @@ export async function recordTeamAction( | |
| // The cache expires within 2 minutes anyway. | ||
| logger.error({ err: error, teamId }, "[Guard]: Cache refresh failed"); | ||
| } | ||
| // Always tell the founder, whatever the agent posts itself. | ||
| try { | ||
| await sendToDiscord(guardPauseNotice(team, body, evidence.length)); | ||
| } catch (error) { | ||
| logger.error({ err: error, teamId }, "[Guard]: Discord notice failed"); | ||
| } | ||
| if (body.abuse) { | ||
| // Tell them, so they can find a leaked key. emailTeam never throws. | ||
| const message = abusePauseEmail({ | ||
| teamName: team.name, | ||
| abuse: body.abuse, | ||
| subjects: [...new Set(evidence.map((email) => email.subject))], | ||
| }); | ||
| await emailTeam(teamId, message.subject, message.text); | ||
| } | ||
| } | ||
| return { ok: true, changed }; | ||
| } | ||
|
|
||
| /** The Discord message for every Guard pause. */ | ||
| export function guardPauseNotice( | ||
| team: { id: number; name: string; createdAt: Date }, | ||
| body: z.infer<typeof teamActionBody>, | ||
| evidenceCount: number, | ||
| ) { | ||
| const age = isNewTeam(team) | ||
| ? "new team" | ||
| : `customer since ${team.createdAt.toISOString().slice(0, 10)}`; | ||
| const why = body.abuse | ||
| ? `${body.abuse} (${evidenceCount} email${evidenceCount === 1 ? "" : "s"} as evidence). The team has been emailed.` | ||
| : "clear abuse."; | ||
| return `🛑 **Guard paused ${team.name}** (#${team.id}, ${age}) for ${why}\n${body.reason.slice(0, 600)}\nResume on the admin teams page if it's wrong: ${env.NEXTAUTH_URL}/admin/teams`; | ||
| } | ||
|
|
||
| // --- New emails (API and SMTP, not campaigns) --- | ||
|
|
||
| export const newEmailsQuery = z.object({ | ||
| /** From `nextCursor`; without it, starts from the last 10 minutes. */ | ||
| cursor: z | ||
| .string() | ||
| .regex(/^\d{4}-\d\d-\d\dT[\d:.]+Z\|[\w-]*$/) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| .optional(), | ||
| limit: z.coerce.number().int().min(1).max(200).default(100), | ||
| }); | ||
|
|
||
| /** | ||
| * Emails sent through the API or SMTP since the cursor, oldest first, across | ||
| * all teams, for the Guard's content check. Campaign emails are checked | ||
| * through their campaign instead. Recipients are reduced to their domains. | ||
| */ | ||
| export async function listNewEmails(query: z.infer<typeof newEmailsQuery>) { | ||
| const [afterIso, afterId] = query.cursor?.split("|") ?? []; | ||
| const after = afterIso | ||
| ? new Date(afterIso) | ||
| : new Date(Date.now() - 10 * 60_000); | ||
| const rows = await db.email.findMany({ | ||
| where: { | ||
| campaignId: null, | ||
| // A date range first, so the createdAt index does the work; then skip | ||
| // what the last page already returned at that same instant. | ||
| createdAt: { gte: after }, | ||
| ...(afterId ? { NOT: { createdAt: after, id: { lte: afterId } } } : {}), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Late-committed emails can fall behind the feed cursor and never reach Guard.
Make the feed read only rows whose timestamp is safely behind the commit horizon, or otherwise guarantee that no row can commit behind the cursor. Choose the mechanism that fits the feed's polling contract. The feed must not skip a committed non-campaign email.
|
||
| }, | ||
| orderBy: [{ createdAt: "asc" }, { id: "asc" }], | ||
| take: query.limit, | ||
| select: { | ||
| id: true, | ||
| createdAt: true, | ||
| from: true, | ||
| to: true, | ||
| replyTo: true, | ||
| subject: true, | ||
| text: true, | ||
| html: true, | ||
| team: { | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| createdAt: true, | ||
| isBlocked: true, | ||
| sendingPausedAt: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| const last = rows.at(-1); | ||
| return { | ||
| items: rows.map(({ to, text, html, team, ...email }) => ({ | ||
| ...email, | ||
| toDomains: [ | ||
| ...new Set(to.map((address) => address.split("@")[1] ?? "")), | ||
| ].filter(Boolean), | ||
| text: truncate(text, EMAIL_TEXT_MAX), | ||
| html: truncate(html, EMAIL_TEXT_MAX), | ||
| team: { ...team, isNew: isNewTeam(team) }, | ||
| })), | ||
| nextCursor: last | ||
| ? `${last.createdAt.toISOString()}|${last.id}` | ||
| : (query.cursor ?? `${after.toISOString()}|`), | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.