|
| 1 | +import { isIPv4, isIPv6 } from 'node:net' |
| 2 | +import { createLogger } from '@sim/logger' |
| 3 | +import { env } from '@/lib/core/config/env' |
| 4 | +import { getEmailDomain } from '@/lib/core/utils/urls' |
| 5 | + |
| 6 | +const logger = createLogger('SmtpEhloName') |
| 7 | + |
| 8 | +/** |
| 9 | + * A dotted FQDN built from RFC 1035 labels. A single dotless label is |
| 10 | + * deliberately rejected: strict relays treat it the same way they treat the |
| 11 | + * loopback literal this module exists to avoid. |
| 12 | + */ |
| 13 | +const FQDN_PATTERN = |
| 14 | + /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$/ |
| 15 | + |
| 16 | +/** RFC 5321 §4.1.3 tags the IPv6 form, case-insensitively per RFC 5234 §2.3. */ |
| 17 | +const IPV6_TAG_PATTERN = /^IPv6:/i |
| 18 | + |
| 19 | +/** |
| 20 | + * An RFC 5321 §4.1.3 address literal — `[192.0.2.1]` or `[IPv6:2001:db8::1]`. |
| 21 | + * The address itself is parsed rather than pattern-matched, so a bracketed |
| 22 | + * value that merely looks like one (`[::::]`, `[13]`) is refused here instead |
| 23 | + * of at the relay. |
| 24 | + */ |
| 25 | +function isAddressLiteral(value: string): boolean { |
| 26 | + if (!value.startsWith('[') || !value.endsWith(']')) return false |
| 27 | + const inner = value.slice(1, -1) |
| 28 | + return IPV6_TAG_PATTERN.test(inner) ? isIPv6(inner.slice(5)) : isIPv4(inner) |
| 29 | +} |
| 30 | + |
| 31 | +function isValidEhloName(value: string): boolean { |
| 32 | + return value.length <= 255 && (FQDN_PATTERN.test(value) || isAddressLiteral(value)) |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Drops a trailing `:port`, leaving an IPv6 literal such as `[::1]` intact. |
| 37 | + * `getEmailDomain` reports a URL's `host`, so a deployment served on a |
| 38 | + * non-default port would otherwise carry one into the greeting, where it is |
| 39 | + * not a legal domain. |
| 40 | + */ |
| 41 | +function stripPort(host: string): string { |
| 42 | + const lastColon = host.lastIndexOf(':') |
| 43 | + return lastColon === -1 || host.indexOf(']') > lastColon ? host : host.slice(0, lastColon) |
| 44 | +} |
| 45 | + |
| 46 | +let warnedInvalidName = false |
| 47 | + |
| 48 | +/** |
| 49 | + * Resolves the hostname to send in the SMTP `EHLO` greeting, or `undefined` to |
| 50 | + * leave nodemailer's own default in place. |
| 51 | + * |
| 52 | + * Nodemailer derives its default from `os.hostname()` and substitutes the |
| 53 | + * address literal `[127.0.0.1]` whenever that name contains no dot. Kubernetes |
| 54 | + * pod hostnames never contain one, so on every k8s deployment Sim introduces |
| 55 | + * itself to the relay as loopback. Strict relays read that as a misconfigured |
| 56 | + * client and refuse the session before any mail moves — Google Workspace's |
| 57 | + * `smtp-relay.gmail.com` answers `421-4.7.0 Try again later, closing |
| 58 | + * connection`, which surfaces as "All email providers failed" on invitations |
| 59 | + * and verification mail. |
| 60 | + * |
| 61 | + * RFC 5321 §4.1.4 asks the client to greet with its own fully-qualified domain |
| 62 | + * name, so the deployment's own domain is the correct answer when the host |
| 63 | + * cannot supply one. `SMTP_EHLO_NAME` overrides it for relays that expect a |
| 64 | + * different identity than the app is served from. |
| 65 | + */ |
| 66 | +export function getSmtpEhloName(): string | undefined { |
| 67 | + const configured = env.SMTP_EHLO_NAME?.trim() |
| 68 | + if (configured) { |
| 69 | + if (isValidEhloName(configured)) return configured |
| 70 | + if (!warnedInvalidName) { |
| 71 | + warnedInvalidName = true |
| 72 | + logger.warn( |
| 73 | + 'SMTP_EHLO_NAME is not a fully-qualified domain name or address literal; ignoring it. Set it to a dotted hostname such as mail.yourdomain.com.' |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const appDomain = stripPort(getEmailDomain()) |
| 79 | + return isValidEhloName(appDomain) ? appDomain : undefined |
| 80 | +} |
0 commit comments