Mail relay: development hosts are known by pattern, with no env - built in: *.dev.netscale.pl
The launch rule called a site non-public only when IAPL_NOINDEX was set (or the project named its hosts).
The owner's development sites set nothing, so production's database copied onto a development address
counted as a launch and would have mailed real people.
- iapl({ developmentHosts }): host patterns of development addresses. Default DEFAULT_DEVELOPMENT_HOSTS =
['*.dev.netscale.pl'], Intecion's own development domain; a project replaces the list or empties it.
mail.relay.developmentHosts overrides it for the mail service.
- a site whose host matches is non-public exactly as with IAPL_NOINDEX=1; either signal is enough
- siteNonPublicSignal, matchDevelopmentHost, isDevelopmentHost, pluginDevelopmentHosts exported
- panel state: nonPublicBy, nonPublicPattern; doctor: mail-non-public (info) names the signal
- every case of the launch table runs with no env on the built-in pattern
- docs, AGENT.md, skills, changelog draft
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
634908eb57
commit
14abf369da
@@ -1,7 +1,7 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import { type RelayOptions, resolveRelayOptions } from './options.js'
|
||||
import { disconnectRelay as disconnect, enrolRelay as enrol, type EnrolResult, siteIsNonPublic as nonPublic, type RelayStatus, rotateRelayToken as rotate, getRelayStatus as status } from './session.js'
|
||||
import { disconnectRelay as disconnect, enrolRelay as enrol, type EnrolResult, siteIsNonPublic as nonPublic, type NonPublicSignal, siteNonPublicSignal as nonPublicSignal, type RelayStatus, rotateRelayToken as rotate, getRelayStatus as status } from './session.js'
|
||||
|
||||
export { RELAY_SENDER_ADDRESS, relayAdapter, relayAttachmentLimit, relaySenderName } from './adapter.js'
|
||||
export type { RelayAdapterArgs } from './adapter.js'
|
||||
@@ -25,12 +25,12 @@ export { expectMailTo, MAIL_KIND, MAIL_KINDS, mailKindOf, runWithMailKind, withM
|
||||
export type { MailKind } from './kinds.js'
|
||||
export { bareAddress, bareAddresses, buildRelayMessage, cleanSubject } from './message.js'
|
||||
export type { BuildRelayMessageArgs, BuiltRelayMessage, RelaySendRequest } from './message.js'
|
||||
export { isPublicHost, relayHost, resolveRelayOptions } from './options.js'
|
||||
export { isPublicHost, pluginDevelopmentHosts, relayHost, resolveRelayOptions } from './options.js'
|
||||
export type { RelayOptions } from './options.js'
|
||||
export { sendThroughRelay } from './send.js'
|
||||
export type { RelaySendContext, RelaySendResult } from './send.js'
|
||||
export { setRelayToken } from './session.js'
|
||||
export type { EnrolResult, RelayStatus } from './session.js'
|
||||
export type { EnrolResult, NonPublicSignal, RelayStatus } from './session.js'
|
||||
|
||||
const DISABLED = 'The mail service is switched off for this project (mail: { relay: false }).'
|
||||
|
||||
@@ -67,8 +67,13 @@ export const disconnectRelay = (payload: BasePayload, options?: RelayOptions): P
|
||||
export const rotateRelayToken = (payload: BasePayload, options?: RelayOptions) => rotate(payload, resolved(payload, options), { force: true })
|
||||
|
||||
/**
|
||||
* Whether this site counts as a non-public copy for `mail.relay.otherHosts`: `IAPL_NOINDEX=1`, or its host is
|
||||
* matched by `mail.relay.developmentHosts`. Such a site neither enrols nor sends by itself while its database
|
||||
* holds another host's connection (docs/email.md "Staging, copies and several hosts").
|
||||
* Whether this site counts as a non-public copy for `mail.relay.otherHosts`: its host matches a development
|
||||
* pattern (`developmentHosts`; built in: `*.dev.netscale.pl`), or `IAPL_NOINDEX=1`. Such a site neither enrols
|
||||
* nor sends by itself while its database holds another host's connection (docs/email.md "Staging, copies and
|
||||
* several hosts").
|
||||
*/
|
||||
export const siteIsNonPublic = (payload: BasePayload, options?: RelayOptions): boolean => nonPublic(payload, resolveRelayOptions(payload, options) || (options ?? {}))
|
||||
|
||||
/** Which signal says so — `{ by: 'development-host', pattern }` or `{ by: 'noindex' }` — or null for a public site. */
|
||||
export const siteNonPublicSignal = (payload: BasePayload, options?: RelayOptions): NonPublicSignal | null =>
|
||||
nonPublicSignal(payload, resolveRelayOptions(payload, options) || (options ?? {}))
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { BasePayload, SendEmailOptions } from 'payload'
|
||||
|
||||
import type { MailKind } from './kinds.js'
|
||||
|
||||
import { DEFAULT_DEVELOPMENT_HOSTS } from '../../shared/developmentHosts.js'
|
||||
import { RELAY_BASE_URL } from './constants.js'
|
||||
|
||||
/**
|
||||
@@ -25,9 +26,11 @@ export type RelayOptions = {
|
||||
/** Calls to the public confirm route per address. Default 10 a minute. */
|
||||
confirmRateLimit?: { max?: number; windowMs?: number }
|
||||
/**
|
||||
* Host patterns of development and preview addresses (`*` = any characters), for example
|
||||
* `['*.dev.example-agency.pl']`. A site on such a host is treated as a non-public copy for `otherHosts`,
|
||||
* exactly like a site with `IAPL_NOINDEX=1`. Nothing is built in: name the agency's own pattern here.
|
||||
* Host patterns of development and preview addresses (`*` = any characters), for the mail service only.
|
||||
* Default: the plugin's list — `iapl({ developmentHosts })`, which itself defaults to
|
||||
* `DEFAULT_DEVELOPMENT_HOSTS` (`['*.dev.netscale.pl']`, Intecion's own development domain). A site on such a
|
||||
* host is treated as a non-public copy for `otherHosts`, exactly like a site with `IAPL_NOINDEX=1`; no
|
||||
* environment variable is needed. `[]` = no host is a development address.
|
||||
*/
|
||||
developmentHosts?: string[]
|
||||
/**
|
||||
@@ -50,7 +53,7 @@ export type RelayOptions = {
|
||||
* address's entry is still there), a moved domain, or a copy of another site's database.
|
||||
*
|
||||
* Default (`'auto'`): by direction. A **public** site enrols by itself — launch day and a domain move need
|
||||
* nobody. A **non-public** site (`IAPL_NOINDEX=1`, or a host in `developmentHosts`) that finds another
|
||||
* nobody. A **non-public** site (a host in `developmentHosts`, or `IAPL_NOINDEX=1`) that finds another
|
||||
* host's connection it was never told about is a copy of that site's data: it does not enrol and does not
|
||||
* send by itself (it would mail real people), until an admin presses "Connect again".
|
||||
*
|
||||
@@ -84,7 +87,7 @@ export type RelayOptions = {
|
||||
url?: string
|
||||
}
|
||||
|
||||
export type ResolvedRelayOptions = { ownUrl: boolean; url: string } & Omit<RelayOptions, 'url'>
|
||||
export type ResolvedRelayOptions = { developmentHosts: string[]; ownUrl: boolean; url: string } & Omit<RelayOptions, 'developmentHosts' | 'url'>
|
||||
|
||||
type PluginMail = { graphDeprecationWarning?: boolean; relay?: false | RelayOptions }
|
||||
|
||||
@@ -93,6 +96,15 @@ export function pluginMailOptions(payload: Pick<BasePayload, 'config'>): PluginM
|
||||
return ((payload.config?.custom as { iapl?: { mail?: PluginMail } } | undefined)?.iapl?.mail ?? {})
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin's development-host patterns: `iapl({ developmentHosts })` as left in the config
|
||||
* (`config.custom.iapl.developmentHosts`), else the built-in list.
|
||||
*/
|
||||
export function pluginDevelopmentHosts(payload: Pick<BasePayload, 'config'>): string[] {
|
||||
const own = (payload.config?.custom as { iapl?: { developmentHosts?: unknown } } | undefined)?.iapl?.developmentHosts
|
||||
return Array.isArray(own) ? own.filter((p): p is string => typeof p === 'string') : [...DEFAULT_DEVELOPMENT_HOSTS]
|
||||
}
|
||||
|
||||
/**
|
||||
* The options in effect: the adapter's own `relay` argument over the plugin option. `null` = the relay
|
||||
* is switched off.
|
||||
@@ -102,7 +114,7 @@ export function resolveRelayOptions(payload: Pick<BasePayload, 'config'>, own?:
|
||||
if (own === false || (own === undefined && fromPlugin === false)) {return null}
|
||||
const merged: RelayOptions = { ...(fromPlugin || {}), ...(own || {}) }
|
||||
const url = (merged.url ?? RELAY_BASE_URL).replace(/\/+$/, '')
|
||||
return { ...merged, ownUrl: url !== RELAY_BASE_URL, url }
|
||||
return { ...merged, developmentHosts: merged.developmentHosts ?? pluginDevelopmentHosts(payload), ownUrl: url !== RELAY_BASE_URL, url }
|
||||
}
|
||||
|
||||
const LABEL = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/
|
||||
|
||||
@@ -11,7 +11,7 @@ import { RELAY_SENDER_ADDRESS } from './adapter.js'
|
||||
import { relayRequest } from './client.js'
|
||||
import { RELAY_CONFIRM_PATH, RELAY_CONNECT_PATH, RELAY_DISCONNECT_PATH, RELAY_STATUS_PATH } from './constants.js'
|
||||
import { isPublicHost, relayHost, resolveRelayOptions } from './options.js'
|
||||
import { disconnectRelay, enrolRelay, type EnrolResult, getRelayStatus, LOCAL_CODES, otherHostsInTheWay, readSiteStore, type RelayStatus, setRelayToken, siteIsNonPublic } from './session.js'
|
||||
import { disconnectRelay, enrolRelay, type EnrolResult, getRelayStatus, LOCAL_CODES, type NonPublicSignal, otherHostsInTheWay, readSiteStore, type RelayStatus, setRelayToken, siteNonPublicSignal } from './session.js'
|
||||
import { otherRelayHosts } from './store.js'
|
||||
|
||||
type Settings = {
|
||||
@@ -29,8 +29,11 @@ export type MailPanelState = {
|
||||
accessKeyField?: boolean
|
||||
/** The address the service calls this site back at. */
|
||||
confirmUrl?: string
|
||||
/** The site says it is not public (`IAPL_NOINDEX`, or a host in `mail.relay.developmentHosts`). */
|
||||
/** The site is not public: its host is a development address (`developmentHosts`), or `IAPL_NOINDEX` is set. */
|
||||
nonPublic?: boolean
|
||||
/** Which signal says so; with `development-host`, `nonPublicPattern` is the pattern that matched. */
|
||||
nonPublicBy?: NonPublicSignal['by']
|
||||
nonPublicPattern?: string
|
||||
/** The database also holds the connection of a host this site did not name as its own, and the site is not held for it. */
|
||||
otherHostEntries?: boolean
|
||||
/** Enabled in the project (`mail.relay` is not `false`). */
|
||||
@@ -67,10 +70,10 @@ export async function mailPanelState(req: Pick<PayloadRequest, 'payload'>, own:
|
||||
}
|
||||
// …and so is a non-public copy of another site's database: it waits for an admin's "Connect again", with or
|
||||
// without a token of its own. Asked now, not read from what a past send left: the rule ends with its reason.
|
||||
let nonPublic = false
|
||||
let signal: NonPublicSignal | null = null
|
||||
let otherHostEntries = false
|
||||
if (status && options && options.token === undefined) {
|
||||
nonPublic = siteIsNonPublic(payload, options)
|
||||
signal = siteNonPublicSignal(payload, options)
|
||||
const stored = await readSiteStore(payload, options)
|
||||
const foreign = status.disconnected ? [] : await otherHostsInTheWay(payload, options, stored)
|
||||
const known = status.refusal?.code === LOCAL_CODES.otherHost
|
||||
@@ -87,7 +90,7 @@ export async function mailPanelState(req: Pick<PayloadRequest, 'payload'>, own:
|
||||
...(status?.host ? { confirmUrl: `https://${status.host}${api}${RELAY_CONFIRM_PATH}` } : {}),
|
||||
relay: status ? { ...status, senderAddress: status.senderAddress ?? RELAY_SENDER_ADDRESS } : null,
|
||||
replyAddress: typeof settings.smtpFromAddress === 'string' && settings.smtpFromAddress.trim() ? settings.smtpFromAddress.trim() : null,
|
||||
...(nonPublic ? { nonPublic: true } : {}),
|
||||
...(signal ? { nonPublic: true, nonPublicBy: signal.by, ...(signal.by === 'development-host' ? { nonPublicPattern: signal.pattern } : {}) } : {}),
|
||||
...(otherHostEntries ? { otherHostEntries: true } : {}),
|
||||
...(smtpIncomplete(settings) ? { smtpIncomplete: true } : {}),
|
||||
transport,
|
||||
|
||||
@@ -2,13 +2,14 @@ import type { BasePayload } from 'payload'
|
||||
|
||||
import { randomBytes } from 'node:crypto'
|
||||
|
||||
import type { ResolvedRelayOptions } from './options.js'
|
||||
import type { RelayOptions, ResolvedRelayOptions } from './options.js'
|
||||
|
||||
import { PLUGIN_VERSION } from '../../../version.js'
|
||||
import { isSiteNoindex } from '../../seo/siteNoindex.js'
|
||||
import { matchDevelopmentHost } from '../../shared/developmentHosts.js'
|
||||
import { type RelayAnswer, type RelayRefusal, relayRequest } from './client.js'
|
||||
import { RELAY_CONFIRM_PATH, RELAY_DEFAULTS, RELAY_PRODUCT, type RelayStatusCode, relayStatusFor } from './constants.js'
|
||||
import { isPublicHost, pluginMailOptions, relayHost } from './options.js'
|
||||
import { isPublicHost, pluginDevelopmentHosts, pluginMailOptions, relayHost } from './options.js'
|
||||
import * as store from './store.js'
|
||||
import { otherRelayHosts, type RelaySiteState, type RelayStored, releaseNonce, sha256Hex } from './store.js'
|
||||
|
||||
@@ -95,17 +96,24 @@ async function otherHostsNow(payload: BasePayload, host: string, fresh: boolean)
|
||||
|
||||
const bare = (host: string) => host.trim().toLowerCase().replace(/^www\./, '')
|
||||
|
||||
/** Why a site counts as non-public: its host matches a development pattern, or `IAPL_NOINDEX` is set. */
|
||||
export type NonPublicSignal = { by: 'development-host'; pattern: string } | { by: 'noindex' }
|
||||
|
||||
/**
|
||||
* Is this site a non-public copy — a development, preview or staging address? `IAPL_NOINDEX=1` (the switch
|
||||
* such a site already has), or a host that matches `mail.relay.developmentHosts`.
|
||||
* Which signal makes this site a non-public copy — a development, preview or staging address — or null for a
|
||||
* public site. Either is enough: its host matches a development pattern (`mail.relay.developmentHosts`, else
|
||||
* `iapl({ developmentHosts })`, else the built-in list — no environment variable needed), or `IAPL_NOINDEX=1`.
|
||||
* The host is named first: it is the signal a site has without anybody setting anything.
|
||||
*/
|
||||
export function siteIsNonPublic(payload: BasePayload, options: Pick<ResolvedRelayOptions, 'developmentHosts' | 'host'>): boolean {
|
||||
if (isSiteNoindex()) {return true}
|
||||
const host = relayHost(payload, options)
|
||||
return (options.developmentHosts ?? []).some((pattern) => {
|
||||
const source = pattern.trim().toLowerCase().split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*')
|
||||
return Boolean(source) && new RegExp(`^${source}$`).test(host)
|
||||
})
|
||||
export function siteNonPublicSignal(payload: BasePayload, options: Pick<RelayOptions, 'developmentHosts' | 'host'>): NonPublicSignal | null {
|
||||
const pattern = matchDevelopmentHost(relayHost(payload, options), options.developmentHosts ?? pluginDevelopmentHosts(payload))
|
||||
if (pattern) {return { by: 'development-host', pattern }}
|
||||
return isSiteNoindex() ? { by: 'noindex' } : null
|
||||
}
|
||||
|
||||
/** Is this site a non-public copy (`siteNonPublicSignal` as a yes or no)? */
|
||||
export function siteIsNonPublic(payload: BasePayload, options: Pick<RelayOptions, 'developmentHosts' | 'host'>): boolean {
|
||||
return siteNonPublicSignal(payload, options) !== null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,7 +143,7 @@ export async function otherHostsInTheWay(
|
||||
}
|
||||
|
||||
const otherHostMessage = (host: string, foreign: string[]) =>
|
||||
`This database holds the mail-service connection of another site (${foreign.join(', ')}), and this site ("${host}") is a non-public copy (IAPL_NOINDEX, or a development host) or set to wait: it does not connect or send by itself. Press "Connect again" in Site integrations → E-mail sending if it should send e-mails (mail.relay.otherHosts).`
|
||||
`This database holds the mail-service connection of another site (${foreign.join(', ')}), and this site ("${host}") is a non-public copy (a development host, or IAPL_NOINDEX) or set to wait: it does not connect or send by itself. Press "Connect again" in Site integrations → E-mail sending if it should send e-mails (mail.relay.otherHosts).`
|
||||
|
||||
async function enrolOnce(payload: BasePayload, options: ResolvedRelayOptions, force: boolean, deadline?: number): Promise<EnrolResult> {
|
||||
const stored = await readSiteStore(payload, options)
|
||||
|
||||
Reference in New Issue
Block a user