43 lines · 1.4 KB
import { describe, expect, mock, test } from "bun:test";

mock.module("./abuse", () => ({
  abuseForwardAddress: () => null,
  guessAbuseCategory: () => "other",
  recordAbuseReport: async () => ({}),
  siteIdForUrl: async () => null,
}));

const { dmarcPassed } = await import("./abuse-email");

const header = (value: string) => ({ key: "authentication-results", value });

describe("dmarcPassed", () => {
  test("accepts an aligned pass in the topmost header", () => {
    expect(
      dmarcPassed(
        [header("mx.cloudflare.net; spf=pass smtp.mailfrom=bank.com; dmarc=pass header.from=bank.com")],
        "bank.com",
      ),
    ).toBe(true);
  });

  test("rejects missing, failing or misaligned results", () => {
    expect(dmarcPassed([], "bank.com")).toBe(false);
    expect(dmarcPassed([header("mx.cloudflare.net; dmarc=fail header.from=bank.com")], "bank.com")).toBe(false);
    expect(dmarcPassed([header("mx.cloudflare.net; dmarc=none header.from=bank.com")], "bank.com")).toBe(false);
    expect(dmarcPassed([header("mx.cloudflare.net; dmarc=pass header.from=evil.com")], "bank.com")).toBe(false);
  });

  test("ignores a forged pass below the receiver's own header", () => {
    expect(
      dmarcPassed(
        [
          header("mx.cloudflare.net; dmarc=fail header.from=bank.com"),
          header("forged; dmarc=pass header.from=bank.com"),
        ],
        "bank.com",
      ),
    ).toBe(false);