Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/parser/src/schemas/ses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const SesReceipt = z.object({
spfVerdict: SesReceiptVerdict,
dmarcVerdict: SesReceiptVerdict,
dkimVerdict: SesReceiptVerdict,
dmarcPolicy: z.enum(['none', 'quarantine', 'reject']),
// Present only when the message fails DMARC authentication
dmarcPolicy: z.enum(['none', 'quarantine', 'reject']).optional(),
action: z.object({
type: z.enum(['Lambda']),
invocationType: z.literal('Event'),
invocationType: z.enum(['Event', 'RequestResponse']).optional(),
functionArn: z.string(),
}),
});
Expand All @@ -34,17 +35,18 @@ const SesMail = z.object({
value: z.string(),
})
),
// Each field is present only if the incoming email has the matching header
commonHeaders: z.object({
from: z.array(z.string()),
to: z.array(z.string()),
from: z.array(z.string()).optional(),
to: z.array(z.string()).optional(),
cc: z.array(z.string()).optional(),
bcc: z.array(z.string()).optional(),
sender: z.array(z.string()).optional(),
'reply-to': z.array(z.string()).optional(),
returnPath: z.string(),
messageId: z.string(),
date: z.string(),
subject: z.string(),
replyTo: z.array(z.string()).optional(),
returnPath: z.string().optional(),
messageId: z.string().optional(),
date: z.string().optional(),
subject: z.string().optional(),
}),
});

Expand Down Expand Up @@ -146,7 +148,6 @@ const SesRecordSchema = z.object({
* "dkimVerdict": {
* "status": "PASS"
* },
* "dmarcPolicy": "reject",
* "processingTimeMillis": 574,
* "action": {
* "type": "Lambda",
Expand Down
6 changes: 5 additions & 1 deletion packages/parser/tests/events/ses/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mail": {
"commonHeaders": {
"from": ["Jane Doe <janedoe@example.com>"],
"replyTo": ["janedoe+replies@example.com"],
"to": ["johndoe@example.com"],
"returnPath": "janedoe@example.com",
"messageId": "<0123456789example.com>",
Expand Down Expand Up @@ -36,6 +37,10 @@
"name": "From",
"value": "Jane Doe <janedoe@example.com>"
},
{
"name": "Reply-To",
"value": "janedoe+replies@example.com"
},
{
"name": "Date",
"value": "Wed, 7 Oct 2015 12:34:56 -0700"
Expand Down Expand Up @@ -69,7 +74,6 @@
"dkimVerdict": {
"status": "PASS"
},
"dmarcPolicy": "reject",
"processingTimeMillis": 574,
"action": {
"type": "Lambda",
Expand Down
54 changes: 54 additions & 0 deletions packages/parser/tests/unit/schema/ses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,60 @@ describe('Schema: SES', () => {
expect(result).toStrictEqual(event);
});

it('keeps the DMARC policy when the message fails DMARC authentication', () => {
// Prepare
const event = structuredClone(baseEvent);
Object.assign(event.Records[0].ses.receipt, {
dmarcVerdict: { status: 'FAIL' },
dmarcPolicy: 'reject',
});

// Act
const result = SesSchema.parse(event);

// Assess
expect(result).toStrictEqual(event);
});

it.each(['Event', 'RequestResponse'] as const)(
'parses a Lambda action with the %s invocation type',
(invocationType) => {
// Prepare
const event = structuredClone(baseEvent);
event.Records[0].ses.receipt.action.invocationType = invocationType;

// Act
const result = SesSchema.parse(event);

// Assess
expect(result).toStrictEqual(event);
}
);

it('parses a Lambda action without an invocation type', () => {
// Prepare
const event = structuredClone(baseEvent);
delete event.Records[0].ses.receipt.action.invocationType;

// Act
const result = SesSchema.parse(event);

// Assess
expect(result).toStrictEqual(event);
});

it('parses an email without any of the common headers', () => {
// Prepare
const event = structuredClone(baseEvent);
event.Records[0].ses.mail.commonHeaders = {};

// Act
const result = SesSchema.parse(event);

// Assess
expect(result).toStrictEqual(event);
});

it('throws if the event is not a SES event', () => {
// Prepare
const event = {
Expand Down
Loading