Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mta-sts support #211

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# ************************************ platform ********************************************
############################################################################################
PLATFORM_URL=http://localhost:3300
# Generate by running in a terminal: openssl rand -hex 32
PLATFORM_SECRET=secretsecretsecret



Expand Down
70 changes: 69 additions & 1 deletion apps/mail-bridge/postal-db/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ export type GetDomainDNSRecordsOutput =
optimal: string;
acceptable: string;
};
mtaSts: {
dns: {
valid: boolean;
name: string;
value: string;
};
tls: {
valid: boolean;
name: string;
value: string;
};
policy: {
valid: boolean;
name: string;
value: string;
};
};
}
| { error: string };

Expand Down Expand Up @@ -168,6 +185,23 @@ export async function getDomainDNSRecords(
name: '',
optimal: '',
acceptable: ''
},
mtaSts: {
dns: {
valid: false,
name: '',
value: ''
},
tls: {
valid: false,
name: '',
value: ''
},
policy: {
valid: false,
name: '',
value: ''
}
}
};

Expand Down Expand Up @@ -313,7 +347,7 @@ export async function getDomainDNSRecords(
}
records.mx.name = domainInfo.name;
records.mx.priority = 1;
records.mx.value = `mx.${postalServerUrl}`;
records.mx.value = `${postalServerUrl}`;
records.mx.valid = true;

if (domainInfo.mxStatus !== 'OK' || forceReverify) {
Expand Down Expand Up @@ -356,6 +390,40 @@ export async function getDomainDNSRecords(
records.dmarc.name = '_dmarc';
records.dmarc.optimal = buildDmarcRecord({ p: 'reject' });
records.dmarc.acceptable = buildDmarcRecord({ p: 'quarantine' });

const mtaStsDnsRecord = await lookupTXT(`_mta-sts.${domainInfo.name}`);
records.mtaSts.dns.name = '_mta-sts';
records.mtaSts.dns.value = `v=STSv1; id=${Date.now()}`;
if (mtaStsDnsRecord.success && mtaStsDnsRecord.data.length > 0) {
records.mtaSts.dns.valid =
mtaStsDnsRecord.data.filter(
(_) => _.startsWith('v=STSv1;') && _.includes('id=')
).length === 1;
}

const mtaStsTlsRecord = await lookupTXT(`_smtp._tls.${domainInfo.name}`);
records.mtaSts.tls.name = '_smtp._tls';
records.mtaSts.tls.value = `v=TLSRPTv1; rua=mailto:[email protected]`;
if (mtaStsTlsRecord.success && mtaStsTlsRecord.data.length > 0) {
records.mtaSts.tls.valid =
mtaStsTlsRecord.data.filter(
(_) =>
_.startsWith('v=TLSRPTv1;') &&
_.includes('rua=') &&
_.includes('mailto:[email protected]')
).length === 1;
}

const mtaStsPolicyRecord = await lookupCNAME(`mta-sts.${domainInfo.name}`);
records.mtaSts.policy.name = 'mta-sts';
records.mtaSts.policy.value = `mta-sts.${postalServerUrl}`;
if (
mtaStsPolicyRecord.success &&
mtaStsPolicyRecord.data.includes(records.mtaSts.policy.value)
) {
records.mtaSts.policy.valid = true;
}

return records;
}

Expand Down
17 changes: 17 additions & 0 deletions apps/mail-bridge/trpc/routers/domainRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ export const domainRouter = router({
name: 'localhost',
acceptable: 'v=DMARC1; p=quarantine;',
optimal: 'v=DMARC1; p=reject;'
},
mtaSts: {
dns: {
valid: true,
name: 'localhost',
value: 'v=STSv1; id=123456789'
},
tls: {
valid: true,
name: 'localhost',
value: 'v=TLSRPTv1; rua=mailto:tlsrpt@localhost'
},
policy: {
valid: true,
name: 'localhost',
value: 'mta-sts.localhost'
}
}
} satisfies GetDomainDNSRecordsOutput;
}
Expand Down
2 changes: 2 additions & 0 deletions apps/platform/routes/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ servicesApi.post(
return c.json({ results });
}
);

servicesApi.post();
13 changes: 13 additions & 0 deletions packages/caddy-mta-sts/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
on_demand_tls {
ask {env.PLATFORM_URL}/caddy-check/{env.PLATFORM_SECRET}
}
}

https:// {
tls {
on_demand
}
root * ./files
file_server
}
4 changes: 4 additions & 0 deletions packages/caddy-mta-sts/files/.well-known/mta-sts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: STSv1
mode: enforce
mx: *.e.uninbox.com
max_age: 86400
3 changes: 3 additions & 0 deletions packages/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ export const domains = mysqlTable(
returnPathDnsValid: boolean('return_path_dns_valid')
.notNull()
.default(false),
mtaStsDnsValid: boolean('mta_sts_dns_valid').notNull().default(false),
mtaStsTlsValid: boolean('mta_sts_tls_valid').notNull().default(false),
mtaStsPolicyValid: boolean('mta_sts_policy_valid').notNull().default(false),
lastDnsCheckAt: timestamp('last_dns_check_at'),
disabledAt: timestamp('disabled_at'),
verifiedAt: timestamp('verified_at'),
Expand Down