/** * Serves the MTA-STS policy for stacksjs.com. * * MTA-STS is how a domain tells other mail servers that TLS is not optional. * Without it SMTP's STARTTLS is opportunistic — a sender that cannot negotiate * TLS delivers in plaintext instead, which is exactly what an attacker * stripping the STARTTLS banner is counting on. * * The zone has advertised `_mta-sts.stacksjs.com` for as long as the record has * existed, with nothing serving the policy it points at. That is worse than not * advertising: every sender honouring MTA-STS fetched a 404 and fell back to * opportunistic TLS, so mail kept flowing and nothing reported a problem. * * Deliberately its own process on its own vhost. RFC 8461 §3.3 requires the * policy to be fetched over HTTPS with a certificate valid for * `mta-sts.` and forbids following redirects, so it cannot be served * from the main site or pointed anywhere more convenient. * * The policy body is generated by ts-cloud's `buildMtaStsPolicy`; it is inlined * here rather than imported so this process starts with no dependency on the * framework build. Keep the `id` in the `_mta-sts` TXT record equal to the one * below — senders cache by id and only refetch when it changes. */ const PORT = Number(process.env.PORT ?? 8461) /** * `mode: testing` on purpose. * * `enforce` is the mode that refuses delivery — and therefore bounces real * mail — when TLS to a listed MX cannot be authenticated. Move to it once * reports show the MX set and its certificate are right, not before. * * CRLF because RFC 8461 §3.2 defines the format that way; a strict parser reads * an LF-only file as a single unparseable line. */ const POLICY = [ 'version: STSv1', 'mode: testing', 'mx: mail.stacksjs.com', 'max_age: 604800', ].join('\r\n') + '\r\n' /** * The overlap ts-cloud's cutover needs. * * Every release gets its own templated systemd unit, and the new one has to * bind the port while the old one is still serving - the old is stopped only * once the new is up. Without `reusePort` (SO_REUSEPORT) that bind is * `EADDRINUSE`, the new unit crash-loops, the old one keeps the port, and the * deploy fails with the policy still served by the previous release. Which is * exactly what it did: restart counter 34 against a healthy old process. The * page and API servers already set this; this one was the last that did not. * * `INVOCATION_ID` is set by systemd for every unit it starts, and answers the * case `APP_ENV` cannot: the allowlist below is the idiom the other two servers * use, and it silently does nothing if this unit's environment never carries * `APP_ENV`. Off for a local run, where two servers fighting over one port * should still fail loudly rather than both half-serving. */ const isDeployed = ['production', 'staging', 'development'].includes((process.env.APP_ENV || '').toLowerCase()) || Boolean(process.env.INVOCATION_ID) Bun.serve({ port: PORT, hostname: '127.0.0.1', reusePort: isDeployed, fetch(req: Request): Response { // One legitimate URL, and this vhost faces the whole internet. Anything // else it could be persuaded to serve is surface for no benefit. if (new URL(req.url).pathname !== '/.well-known/mta-sts.txt') return new Response('Not Found', { status: 404 }) return new Response(POLICY, { headers: { 'content-type': 'text/plain; charset=utf-8' }, }) }, }) // eslint-disable-next-line no-console console.log(`MTA-STS policy on 127.0.0.1:${PORT}/.well-known/mta-sts.txt`)