118 lines
3.4 KiB
TypeScript
118 lines
3.4 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import {
|
|
createAdminBccCapability,
|
|
verifyAdminBccCapability,
|
|
verifyAdminSendingDomainDns,
|
|
} from "./admin-email-domain.js";
|
|
|
|
const secret = "admin-bcc-secret".padEnd(40, "x");
|
|
|
|
describe("admin BCC capability", () => {
|
|
it("creates a bounded signed address and verifies it without a database scan", () => {
|
|
const capability = createAdminBccCapability({
|
|
adminUserId: "admin_user_123",
|
|
inboundDomain: "bcc.hivegrid.de",
|
|
secret,
|
|
randomBytes: () => new Uint8Array(8).fill(7),
|
|
});
|
|
|
|
expect(capability.normalizedAddress).toMatch(
|
|
/^b1\.admin_user_123\.[A-Za-z0-9_-]{11}\.[A-Za-z0-9_-]{22}@bcc\.hivegrid\.de$/u
|
|
);
|
|
expect(capability.normalizedAddress.split("@")[0]?.length).toBeLessThanOrEqual(
|
|
64
|
|
);
|
|
expect(
|
|
verifyAdminBccCapability({
|
|
address: capability.normalizedAddress.replace(
|
|
"@bcc.hivegrid.de",
|
|
"@BCC.HIVEGRID.DE"
|
|
),
|
|
inboundDomain: "BCC.HIVEGRID.DE.",
|
|
secret,
|
|
})
|
|
).toEqual(capability);
|
|
});
|
|
|
|
it("rejects tampering, wrong domains and weak secrets", () => {
|
|
const capability = createAdminBccCapability({
|
|
adminUserId: "admin_user_123",
|
|
inboundDomain: "bcc.hivegrid.de",
|
|
secret,
|
|
randomBytes: () => new Uint8Array(8).fill(9),
|
|
});
|
|
|
|
expect(
|
|
verifyAdminBccCapability({
|
|
address: capability.normalizedAddress.replace("admin_user_123", "admin_user_124"),
|
|
inboundDomain: "bcc.hivegrid.de",
|
|
secret,
|
|
})
|
|
).toBeNull();
|
|
expect(
|
|
verifyAdminBccCapability({
|
|
address: capability.normalizedAddress,
|
|
inboundDomain: "other.hivegrid.de",
|
|
secret,
|
|
})
|
|
).toBeNull();
|
|
expect(
|
|
verifyAdminBccCapability({
|
|
address: capability.normalizedAddress,
|
|
inboundDomain: "bcc.hivegrid.de",
|
|
secret: "weak",
|
|
})
|
|
).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("admin sending-domain DNS verification", () => {
|
|
it("requires exact SPF and DKIM records while joining split TXT chunks", async () => {
|
|
const resolveTxt = vi.fn(async (hostname: string) => {
|
|
if (hostname === "hivegrid.de") {
|
|
return [["v=spf1 include:spf.mailjet.com ~all"]];
|
|
}
|
|
return [
|
|
["k=rsa; p=MIIB", "IjANBgkqh"],
|
|
["unrelated"],
|
|
];
|
|
});
|
|
|
|
await expect(
|
|
verifyAdminSendingDomainDns({
|
|
spfRecordName: "HiveGrid.de.",
|
|
spfExpectedValue: "v=spf1 include:spf.mailjet.com ~all",
|
|
dkimRecordName: "mailjet._domainkey.hivegrid.de",
|
|
dkimExpectedValue: "k=rsa; p=MIIBIjANBgkqh",
|
|
resolveTxt,
|
|
})
|
|
).resolves.toEqual({
|
|
spfStatus: "verified",
|
|
dkimStatus: "verified",
|
|
status: "verified",
|
|
});
|
|
});
|
|
|
|
it("distinguishes missing, mismatched and resolver errors", async () => {
|
|
const missing = Object.assign(new Error("not found"), { code: "ENOTFOUND" });
|
|
const resolveTxt = vi.fn(async (hostname: string) => {
|
|
if (hostname === "missing.example") throw missing;
|
|
return [["different"]];
|
|
});
|
|
|
|
await expect(
|
|
verifyAdminSendingDomainDns({
|
|
spfRecordName: "missing.example",
|
|
spfExpectedValue: "spf",
|
|
dkimRecordName: "dkim.example",
|
|
dkimExpectedValue: "dkim",
|
|
resolveTxt,
|
|
})
|
|
).resolves.toEqual({
|
|
spfStatus: "missing",
|
|
dkimStatus: "mismatch",
|
|
status: "degraded",
|
|
});
|
|
});
|
|
});
|