caelum0x 173b60097d feat(mail): OCI relay pool, direct MTA (parked), inbound HTTPS ingest, Cloudflare inbound forwarder
- mail-oci: OCI Email Delivery client (signed REST), per-customer domain/DKIM/sender onboarding, suppression mirror, sending limits
- mail-mta: RelayPool transport with OCI primary, optional secondary, health/back-off/quota failover; direct MTA kept but off
- mail-inbound: DSN/ARF/DMARC/TLS-RPT parsing + HMAC-signed POST /inbound/raw ingest (no port 25)
- workers/inbound-forwarder: Cloudflare Email Routing worker forwarding raw mail over HTTPS

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 01:14:52 +03:00

74 lines
3.2 KiB
JavaScript

// Runs the Worker's email() handler logic in Node against a local mail-inbound
// HTTPS ingest, with a fake ForwardableEmailMessage. No Cloudflare account
// needed. Usage:
// INGEST_URL=http://127.0.0.1:8585 INGEST_SECRET=... node scripts/local-check.mjs
import { forwardToIngest } from "../src/index.ts";
const env = {
INGEST_URL: process.env.INGEST_URL ?? "http://127.0.0.1:8585",
INGEST_SECRET: process.env.INGEST_SECRET ?? "",
ALLOWED_RCPT_DOMAINS: process.env.ALLOWED_RCPT_DOMAINS ?? "dev.test,reports.dev.test,gmail.com",
};
if (!env.INGEST_SECRET) {
console.error("INGEST_SECRET is required");
process.exit(2);
}
const domain = process.env.FEED_DOMAIN ?? "dev.test";
function fakeMessage(from, to, raw) {
const bytes = new TextEncoder().encode(raw);
let rejected = null;
return {
msg: {
from,
to,
headers: new Headers({ "message-id": "<local-check@example>" }),
raw: new Blob([bytes]).stream(),
rawSize: bytes.length,
setReject(reason) {
rejected = reason;
},
},
rejected: () => rejected,
};
}
const dsn = `From: MAILER-DAEMON@mx.example.net\r\nTo: <bounces+wk1@${domain}>\r\nSubject: Delivery Status Notification (Failure)\r\nMessage-ID: <wk-dsn-1@mx.example.net>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/report; report-type=delivery-status; boundary="b1"\r\n\r\n--b1\r\nContent-Type: text/plain\r\n\r\nfailed\r\n--b1\r\nContent-Type: message/delivery-status\r\n\r\nReporting-MTA: dns; mx.example.net\r\n\r\nFinal-Recipient: rfc822; user@example.net\r\nAction: failed\r\nStatus: 5.1.1\r\nDiagnostic-Code: smtp; 550 5.1.1 user unknown\r\n\r\n--b1--\r\n`;
const cases = [
{ label: "DSN to bounces+wk1 (accepted)", from: "", to: `bounces+wk1@${domain}`, raw: dsn, expectStatus: 202, expectReject: null },
{ label: "unknown user (rejected 550 5.1.1)", from: "x@evil.example", to: `nobody@reports.${domain}`, raw: "Subject: x\r\n\r\nx", expectStatus: 404, expectReject: /550 5\.1\.1/ },
{ label: "foreign domain (relaying denied, local check)", from: "x@evil.example", to: "victim@gmail.com", raw: "Subject: x\r\n\r\nx", expectStatus: 403, expectReject: /550 5\.7\.1/ },
];
let failed = 0;
for (const c of cases) {
const { msg, rejected } = fakeMessage(c.from, c.to, c.raw);
let status;
try {
status = (await forwardToIngest(msg, env)).status;
} catch (e) {
status = `threw: ${e.message}`;
}
const rej = rejected();
const ok = status === c.expectStatus && (c.expectReject ? c.expectReject.test(rej ?? "") : rej === null);
if (!ok) failed++;
console.log(`[${ok ? "PASS" : "FAIL"}] ${c.label}: status=${status} reject=${rej}`);
}
// Wrong secret must make the handler throw (sender retries), never reject permanently.
{
const { msg, rejected } = fakeMessage("", `bounces+wk2@${domain}`, dsn);
let threw = false;
try {
await forwardToIngest(msg, { ...env, INGEST_SECRET: "wrong-secret-wrong-secret-wrong-secret" });
} catch {
threw = true;
}
const ok = threw && rejected() === null;
if (!ok) failed++;
console.log(`[${ok ? "PASS" : "FAIL"}] wrong secret -> 401 -> handler throws (temporary failure), no permanent reject`);
}
console.log(failed === 0 ? "ALL PASS" : `${failed} FAILED`);
process.exit(failed === 0 ? 0 : 1);