-
-
Notifications
You must be signed in to change notification settings - Fork 372
feat: MAIL FROM label override and aggregate domain verification #392
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,2 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "Domain" ADD COLUMN "mailFromLabel" TEXT; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| import { env } from "~/env"; | ||
| import { db } from "~/server/db"; | ||
| import { logger } from "~/server/logger/log"; | ||
| import { parseSesHook, SesHookParser } from "~/server/service/ses-hook-parser"; | ||
| import { SesHookParser } from "~/server/service/ses-hook-parser"; | ||
| import { SesSettingsService } from "~/server/service/ses-settings-service"; | ||
| import { SnsNotificationMessage } from "~/types/aws-types"; | ||
| import { SesEvent, SnsNotificationMessage } from "~/types/aws-types"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
|
|
@@ -14,11 +14,11 @@ export async function GET() { | |
| export async function POST(req: Request) { | ||
| const data = await req.json(); | ||
|
|
||
| console.log(data, data.Message); | ||
| logger.info({ type: data?.Type, messageId: data?.MessageId }, "Received SNS callback"); | ||
|
|
||
| const isEventValid = await checkEventValidity(data); | ||
|
|
||
| console.log("Is event valid: ", isEventValid); | ||
| logger.info({ isEventValid, topicArn: data?.TopicArn }, "SNS callback validation result"); | ||
|
|
||
| if (!isEventValid) { | ||
| return Response.json({ data: "Event is not valid" }); | ||
|
|
@@ -28,10 +28,32 @@ export async function POST(req: Request) { | |
| return handleSubscription(data); | ||
| } | ||
|
|
||
| let message = null; | ||
|
|
||
| try { | ||
| message = JSON.parse(data.Message || "{}"); | ||
| const rawMessage = data?.Message; | ||
| if (typeof rawMessage !== "string" || rawMessage.trim() === "") { | ||
| logger.warn({ messageId: data?.MessageId }, "SNS callback without message payload"); | ||
| return Response.json({ data: "Ignored non-SES callback message" }); | ||
| } | ||
|
|
||
| let message: unknown; | ||
| try { | ||
| message = JSON.parse(rawMessage); | ||
| } catch { | ||
| logger.info( | ||
| { messageId: data?.MessageId, rawMessage }, | ||
| "Ignoring SNS notification with non-JSON payload", | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ); | ||
| return Response.json({ data: "Ignored non-SES callback message" }); | ||
| } | ||
|
|
||
| if (!isSesEventPayload(message)) { | ||
| logger.info( | ||
| { messageId: data?.MessageId }, | ||
| "Ignoring SNS notification that is not an SES event payload", | ||
| ); | ||
| return Response.json({ data: "Ignored non-SES callback message" }); | ||
| } | ||
|
|
||
| const status = await SesHookParser.queue({ | ||
| event: message, | ||
| messageId: data.MessageId, | ||
|
|
@@ -42,11 +64,19 @@ export async function POST(req: Request) { | |
|
|
||
| return Response.json({ data: "Success" }); | ||
| } catch (e) { | ||
| console.error(e); | ||
| logger.error({ err: e, messageId: data?.MessageId }, "Failed to process SES callback"); | ||
| return Response.json({ data: "Error is parsing hook" }); | ||
| } | ||
| } | ||
|
|
||
| function isSesEventPayload(value: unknown): value is SesEvent { | ||
| if (!value || typeof value !== "object") { | ||
| return false; | ||
| } | ||
| const event = value as Partial<SesEvent>; | ||
| return typeof event.eventType === "string" && !!event.mail; | ||
|
Comment on lines
+72
to
+77
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. Harden SES event type guard before queueing The current guard accepts any truthy Proposed fix function isSesEventPayload(value: unknown): value is SesEvent {
if (!value || typeof value !== "object") {
return false;
}
- const event = value as Partial<SesEvent>;
- return typeof event.eventType === "string" && !!event.mail;
+ const event = value as Partial<SesEvent> & { mail?: unknown };
+ if (typeof event.eventType !== "string") {
+ return false;
+ }
+ if (!event.mail || typeof event.mail !== "object") {
+ return false;
+ }
+ const mail = event.mail as { messageId?: unknown; timestamp?: unknown; source?: unknown };
+ return (
+ typeof mail.messageId === "string" &&
+ typeof mail.timestamp === "string" &&
+ typeof mail.source === "string"
+ );
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| /** | ||
| * Handles the subscription confirmation event. called only once for a webhook | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { DomainStatus } from "@prisma/client"; | ||
|
|
||
| /** | ||
| * Severity order: worst first. Used to combine identity, DKIM, and MAIL FROM (SPF) checks. | ||
| */ | ||
| const STATUS_WORST_FIRST: DomainStatus[] = [ | ||
| DomainStatus.FAILED, | ||
| DomainStatus.TEMPORARY_FAILURE, | ||
| DomainStatus.PENDING, | ||
| DomainStatus.NOT_STARTED, | ||
| DomainStatus.SUCCESS, | ||
| ]; | ||
|
|
||
| function parseLooseStatus(value?: string | null): DomainStatus { | ||
| if (!value) { | ||
| return DomainStatus.NOT_STARTED; | ||
| } | ||
| const normalized = value.toUpperCase(); | ||
| if ((Object.values(DomainStatus) as string[]).includes(normalized)) { | ||
| return normalized as DomainStatus; | ||
| } | ||
| return DomainStatus.NOT_STARTED; | ||
| } | ||
|
|
||
| /** | ||
| * Single status for UX: all of SES identity verification, DKIM, and MAIL FROM (SPF) must be SUCCESS | ||
| * for the aggregate to be SUCCESS. | ||
| */ | ||
| export function aggregateDomainStatus(domain: { | ||
| status: DomainStatus; | ||
| dkimStatus?: string | null; | ||
| spfDetails?: string | null; | ||
| }): DomainStatus { | ||
| const parts: DomainStatus[] = [ | ||
| domain.status, | ||
| parseLooseStatus(domain.dkimStatus), | ||
| parseLooseStatus(domain.spfDetails), | ||
| ]; | ||
|
|
||
| let minIdx = STATUS_WORST_FIRST.length - 1; | ||
| for (const p of parts) { | ||
| const idx = STATUS_WORST_FIRST.indexOf(p); | ||
| if (idx !== -1 && idx < minIdx) { | ||
| minIdx = idx; | ||
| } | ||
| } | ||
| return STATUS_WORST_FIRST[minIdx]!; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aggregateDomainStatus(domain)here is computed fromteam.domains, but the admin API selection for domains (seeteamAdminSelectioninapps/web/src/server/api/routers/admin.ts) only includesstatus/isVerifyingand does not includedkimStatus/spfDetails. With missing fields,aggregateDomainStatustreats them asNOT_STARTED, so even aSUCCESSdomain will display asnot_started. Fix by includingdkimStatusandspfDetails(and any other required fields) in the admin query selection, or by usingdomain.statusdirectly when those fields aren’t present.