-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(email): greet SMTP relays with a qualified hostname instead of [127.0.0.1] #6799
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
78a54fc
fix(email): greet SMTP relays with a qualified hostname instead of [1…
waleedlatif1 43b2c70
fix(email): parse EHLO address literals and drop a port from the app …
waleedlatif1 3334de8
fix(email): accept any casing of the IPv6 literal tag, and stop ownin…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { resetEnvMock, resetUrlsMock, setEnv, urlsMockFns } from '@sim/testing' | ||
| import { afterAll, beforeEach, describe, expect, it } from 'vitest' | ||
| import { getSmtpEhloName } from '@/lib/messaging/email/ehlo' | ||
|
|
||
| afterAll(() => { | ||
| resetEnvMock() | ||
| resetUrlsMock() | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| resetEnvMock() | ||
| setEnv({ SMTP_EHLO_NAME: undefined }) | ||
| urlsMockFns.mockGetEmailDomain.mockReturnValue('sim.example.com') | ||
| }) | ||
|
|
||
| describe('getSmtpEhloName', () => { | ||
| it("falls back to the app's own domain so k8s pods never greet as [127.0.0.1]", () => { | ||
| expect(getSmtpEhloName()).toBe('sim.example.com') | ||
| }) | ||
|
|
||
| it('prefers an explicitly configured SMTP_EHLO_NAME', () => { | ||
| setEnv({ SMTP_EHLO_NAME: 'mail.yourdomain.com' }) | ||
| expect(getSmtpEhloName()).toBe('mail.yourdomain.com') | ||
| }) | ||
|
|
||
| it('trims surrounding whitespace', () => { | ||
| setEnv({ SMTP_EHLO_NAME: ' mail.yourdomain.com ' }) | ||
| expect(getSmtpEhloName()).toBe('mail.yourdomain.com') | ||
| }) | ||
|
|
||
| it('accepts an RFC 5321 address literal', () => { | ||
| setEnv({ SMTP_EHLO_NAME: '[203.0.113.5]' }) | ||
| expect(getSmtpEhloName()).toBe('[203.0.113.5]') | ||
| }) | ||
|
|
||
| it.each(['[IPv6:2001:db8::1]', '[ipv6:2001:db8::1]', '[IPV6:2001:db8::1]'])( | ||
| 'accepts the RFC 5321 IPv6 address literal %s, whose tag is case-insensitive', | ||
| (literal) => { | ||
| setEnv({ SMTP_EHLO_NAME: literal }) | ||
| expect(getSmtpEhloName()).toBe(literal) | ||
| } | ||
| ) | ||
|
|
||
| it.each(['[::::]', '[13]', '[999.1.1.1]', '[2001:db8::1]', '[IPv6:203.0.113.5]'])( | ||
| 'ignores the malformed address literal %s rather than letting the relay refuse it', | ||
| (literal) => { | ||
| setEnv({ SMTP_EHLO_NAME: literal }) | ||
| expect(getSmtpEhloName()).toBe('sim.example.com') | ||
| } | ||
| ) | ||
|
|
||
| it('ignores a dotless name, which strict relays reject just like the literal', () => { | ||
| setEnv({ SMTP_EHLO_NAME: 'sim-app' }) | ||
| expect(getSmtpEhloName()).toBe('sim.example.com') | ||
| }) | ||
|
|
||
| it('ignores a name carrying CRLF rather than passing it into the EHLO command', () => { | ||
| setEnv({ SMTP_EHLO_NAME: 'evil.com\r\nMAIL FROM:<attacker@evil.com>' }) | ||
| expect(getSmtpEhloName()).toBe('sim.example.com') | ||
| }) | ||
|
|
||
| it('strips a port from the app domain instead of falling back over it', () => { | ||
| urlsMockFns.mockGetEmailDomain.mockReturnValue('sim.example.com:8443') | ||
| expect(getSmtpEhloName()).toBe('sim.example.com') | ||
| }) | ||
|
|
||
| it("returns undefined for a dev app domain, leaving nodemailer's default", () => { | ||
| urlsMockFns.mockGetEmailDomain.mockReturnValue('localhost:3000') | ||
| expect(getSmtpEhloName()).toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns undefined when neither source yields a qualified name', () => { | ||
| setEnv({ SMTP_EHLO_NAME: 'localhost' }) | ||
| urlsMockFns.mockGetEmailDomain.mockReturnValue('localhost') | ||
| expect(getSmtpEhloName()).toBeUndefined() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { isIPv4, isIPv6 } from 'node:net' | ||
| import { createLogger } from '@sim/logger' | ||
| import { env } from '@/lib/core/config/env' | ||
| import { getEmailDomain } from '@/lib/core/utils/urls' | ||
|
|
||
| const logger = createLogger('SmtpEhloName') | ||
|
|
||
| /** | ||
| * A dotted FQDN built from RFC 1035 labels. A single dotless label is | ||
| * deliberately rejected: strict relays treat it the same way they treat the | ||
| * loopback literal this module exists to avoid. | ||
| */ | ||
| const FQDN_PATTERN = | ||
| /^[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])?)+$/ | ||
|
|
||
| /** RFC 5321 §4.1.3 tags the IPv6 form, case-insensitively per RFC 5234 §2.3. */ | ||
| const IPV6_TAG_PATTERN = /^IPv6:/i | ||
|
|
||
| /** | ||
| * An RFC 5321 §4.1.3 address literal — `[192.0.2.1]` or `[IPv6:2001:db8::1]`. | ||
| * The address itself is parsed rather than pattern-matched, so a bracketed | ||
| * value that merely looks like one (`[::::]`, `[13]`) is refused here instead | ||
| * of at the relay. | ||
| */ | ||
| function isAddressLiteral(value: string): boolean { | ||
| if (!value.startsWith('[') || !value.endsWith(']')) return false | ||
| const inner = value.slice(1, -1) | ||
| return IPV6_TAG_PATTERN.test(inner) ? isIPv6(inner.slice(5)) : isIPv4(inner) | ||
| } | ||
|
|
||
| function isValidEhloName(value: string): boolean { | ||
| return value.length <= 255 && (FQDN_PATTERN.test(value) || isAddressLiteral(value)) | ||
| } | ||
|
|
||
| /** | ||
| * Drops a trailing `:port`, leaving an IPv6 literal such as `[::1]` intact. | ||
| * `getEmailDomain` reports a URL's `host`, so a deployment served on a | ||
| * non-default port would otherwise carry one into the greeting, where it is | ||
| * not a legal domain. | ||
| */ | ||
| function stripPort(host: string): string { | ||
| const lastColon = host.lastIndexOf(':') | ||
| return lastColon === -1 || host.indexOf(']') > lastColon ? host : host.slice(0, lastColon) | ||
| } | ||
|
|
||
| let warnedInvalidName = false | ||
|
|
||
| /** | ||
| * Resolves the hostname to send in the SMTP `EHLO` greeting, or `undefined` to | ||
| * leave nodemailer's own default in place. | ||
| * | ||
| * Nodemailer derives its default from `os.hostname()` and substitutes the | ||
| * address literal `[127.0.0.1]` whenever that name contains no dot. Kubernetes | ||
| * pod hostnames never contain one, so on every k8s deployment Sim introduces | ||
| * itself to the relay as loopback. Strict relays read that as a misconfigured | ||
| * client and refuse the session before any mail moves — Google Workspace's | ||
| * `smtp-relay.gmail.com` answers `421-4.7.0 Try again later, closing | ||
| * connection`, which surfaces as "All email providers failed" on invitations | ||
| * and verification mail. | ||
| * | ||
| * RFC 5321 §4.1.4 asks the client to greet with its own fully-qualified domain | ||
| * name, so the deployment's own domain is the correct answer when the host | ||
| * cannot supply one. `SMTP_EHLO_NAME` overrides it for relays that expect a | ||
| * different identity than the app is served from. | ||
| */ | ||
| export function getSmtpEhloName(): string | undefined { | ||
| const configured = env.SMTP_EHLO_NAME?.trim() | ||
| if (configured) { | ||
| if (isValidEhloName(configured)) return configured | ||
| if (!warnedInvalidName) { | ||
| warnedInvalidName = true | ||
| logger.warn( | ||
| '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.' | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const appDomain = stripPort(getEmailDomain()) | ||
| return isValidEhloName(appDomain) ? appDomain : undefined | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.