|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { simpleParser } from "mailparser"; |
| 3 | +import { extractForwardedHeaders } from "./email-headers"; |
| 4 | + |
| 5 | +describe("extractForwardedHeaders", () => { |
| 6 | + it("forwards end-to-end and custom headers", async () => { |
| 7 | + const parsed = await simpleParser( |
| 8 | + [ |
| 9 | + "From: sender@example.com", |
| 10 | + "To: recipient@example.com", |
| 11 | + "Subject: Header forwarding", |
| 12 | + "List-Unsubscribe: <mailto:unsubscribe@example.com>,", |
| 13 | + " <https://example.com/unsubscribe/recipient-token>", |
| 14 | + "List-Unsubscribe-Post: List-Unsubscribe=One-Click", |
| 15 | + "List-Help: <https://example.com/help>", |
| 16 | + "In-Reply-To: <previous@example.com>", |
| 17 | + "References: <first@example.com> <previous@example.com>", |
| 18 | + "Precedence: bulk", |
| 19 | + "Auto-Submitted: auto-generated", |
| 20 | + "Feedback-ID: campaign:customer:usesend", |
| 21 | + "X-Custom-Trace: trace-123", |
| 22 | + "", |
| 23 | + "Hello", |
| 24 | + ].join("\r\n"), |
| 25 | + ); |
| 26 | + |
| 27 | + expect(extractForwardedHeaders(parsed.headerLines)).toEqual({ |
| 28 | + "List-Unsubscribe": |
| 29 | + "<mailto:unsubscribe@example.com>, <https://example.com/unsubscribe/recipient-token>", |
| 30 | + "List-Unsubscribe-Post": "List-Unsubscribe=One-Click", |
| 31 | + "List-Help": "<https://example.com/help>", |
| 32 | + "In-Reply-To": "<previous@example.com>", |
| 33 | + References: "<first@example.com> <previous@example.com>", |
| 34 | + Precedence: "bulk", |
| 35 | + "Auto-Submitted": "auto-generated", |
| 36 | + "Feedback-ID": "campaign:customer:usesend", |
| 37 | + "X-Custom-Trace": "trace-123", |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("does not forward headers that are rebuilt or transport-controlled", async () => { |
| 42 | + const parsed = await simpleParser( |
| 43 | + [ |
| 44 | + "Return-Path: <bounce@example.com>", |
| 45 | + "Received: from untrusted.example.com", |
| 46 | + "Authentication-Results: mx.example.com; dkim=pass", |
| 47 | + "ARC-Seal: i=1; a=rsa-sha256; d=example.com; b=stale", |
| 48 | + "DKIM-Signature: v=1; d=example.com; b=stale", |
| 49 | + "X-SES-CONFIGURATION-SET: untrusted", |
| 50 | + "X-Usesend-Email-ID: spoofed", |
| 51 | + "From: sender@example.com", |
| 52 | + "To: recipient@example.com", |
| 53 | + "Cc: copy@example.com", |
| 54 | + "Bcc: hidden@example.com", |
| 55 | + "Subject: Header forwarding", |
| 56 | + "Message-ID: <old@example.com>", |
| 57 | + "MIME-Version: 1.0", |
| 58 | + "Content-Type: text/plain; charset=utf-8", |
| 59 | + "X-Safe: forwarded", |
| 60 | + "", |
| 61 | + "Hello", |
| 62 | + ].join("\r\n"), |
| 63 | + ); |
| 64 | + |
| 65 | + expect(extractForwardedHeaders(parsed.headerLines)).toEqual({ |
| 66 | + "X-Safe": "forwarded", |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns undefined when there is nothing safe to forward", async () => { |
| 71 | + const parsed = await simpleParser( |
| 72 | + [ |
| 73 | + "From: sender@example.com", |
| 74 | + "To: recipient@example.com", |
| 75 | + "Subject: Header forwarding", |
| 76 | + "", |
| 77 | + "Hello", |
| 78 | + ].join("\r\n"), |
| 79 | + ); |
| 80 | + |
| 81 | + expect(extractForwardedHeaders(parsed.headerLines)).toBeUndefined(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments