Development hosts, review V-1 to V-3: a malformed list stops the start, a trailing dot and Unicode patterns match, every send reads the store
- V-1: assertDevelopmentHosts - iapl({ developmentHosts }), mail.relay.developmentHosts and
mailAdapter({ relay }) refuse anything but an array of non-empty host patterns, and a pattern of `*` alone
(it would make the public site a development address). Nothing is ignored silently any more.
- V-2: one trailing dot of the host is stripped before matching; patterns are punycoded, so one written in
Unicode matches; a `*` inside a non-ASCII label is refused.
- V-3: the 30-second memory of other hosts is gone. A dump restored into a running development site stops
its very next e-mail.
- docs, AGENT.md, changelog draft, skills
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
99c73ed0ff
commit
6c4e244094
@@ -19,7 +19,8 @@ export function hostOf(value: null | string | undefined): string {
|
||||
const raw = (value ?? '').trim()
|
||||
if (!raw) {return ''}
|
||||
try {
|
||||
return new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`).host.toLowerCase()
|
||||
// (a fully qualified name may end in a dot — `acme.dev.example.pl.` is the same host)
|
||||
return new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`).host.toLowerCase().replace(/\.(?=:\d+$|$)/, '')
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
@@ -33,12 +34,60 @@ export function matchDevelopmentHost(host: null | string | undefined, patterns:
|
||||
const name = hostOf(host).replace(/:\d+$/, '')
|
||||
if (!name) {return null}
|
||||
for (const pattern of patterns) {
|
||||
const source = pattern.trim().toLowerCase().split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*')
|
||||
// a pattern is compared the way hosts are written on the wire: lowercase, punycode, no trailing dot
|
||||
const written = typeof pattern === 'string' ? (asciiPattern(pattern) ?? pattern.trim().toLowerCase()) : ''
|
||||
const source = written.split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*')
|
||||
if (source && new RegExp(`^${source}$`).test(name)) {return pattern}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const STAR = 'zq9star9qz'
|
||||
|
||||
/**
|
||||
* A pattern as hosts are written: lowercase, punycode (`*.dev.zażółć.pl` → `*.dev.xn--za-….pl`), no trailing
|
||||
* dot, no port. null when it is not a host pattern at all (empty, a space inside, a `*` in a label with
|
||||
* non-ASCII letters, …).
|
||||
*/
|
||||
function asciiPattern(pattern: string): null | string {
|
||||
const raw = pattern.trim().toLowerCase().replace(/\.$/, '')
|
||||
if (!raw || /[\s/@?#\\]/.test(raw)) {return null}
|
||||
const stars = raw.split('*').length - 1
|
||||
try {
|
||||
const host = new URL(`https://${raw.replaceAll('*', STAR)}`).hostname
|
||||
// (a `*` inside a punycoded label would stand for nothing a person could mean)
|
||||
if (host.split(STAR).length - 1 !== stars || host.split('.').some((label) => label.startsWith('xn--') && label.includes(STAR))) {return null}
|
||||
return host.replaceAll(STAR, '*')
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a `developmentHosts` value when the project's config is built, and returns the list as given.
|
||||
* Throws — the site does not start — on anything but an array of host patterns, because a list that is
|
||||
* silently ignored or silently empty means a copy of production's data mails real people:
|
||||
* - not an array (a single string, `null`, an object);
|
||||
* - an entry that is not a non-empty string, or not a host pattern;
|
||||
* - a pattern made of `*` alone (with or without dots): it would make EVERY host a development address,
|
||||
* the public site included. To switch the mail rule's waiting off or on for all hosts use
|
||||
* `mail.relay.otherHosts` instead.
|
||||
* `[]` is valid: no host is a development address. `undefined` is not checked here (the default applies).
|
||||
*/
|
||||
export function assertDevelopmentHosts(value: unknown, option = 'developmentHosts'): string[] {
|
||||
const fail = (why: string): never => {
|
||||
throw new Error(`[iapl] ${option} must be an array of host patterns such as ['*.dev.netscale.pl'] ([] = none): ${why}. Got ${JSON.stringify(value)}.`)
|
||||
}
|
||||
if (!Array.isArray(value)) {return fail('it is not an array')}
|
||||
value.forEach((entry, index) => {
|
||||
if (typeof entry !== 'string' || !entry.trim()) {fail(`entry ${index} is not a non-empty string`)}
|
||||
const written = asciiPattern(entry as string)
|
||||
if (!written) {fail(`entry ${index} ("${entry}") is not a host pattern — a host name with \`*\` for any characters, no scheme, no path`)}
|
||||
if (!/[^*.]/.test(written as string)) {fail(`entry ${index} ("${entry}") matches every host, so the public site would count as a development address`)}
|
||||
})
|
||||
return value as string[]
|
||||
}
|
||||
|
||||
/** Is `host` a development address (`matchDevelopmentHost` as a yes or no)? */
|
||||
export const isDevelopmentHost = (host: null | string | undefined, patterns: readonly string[] = DEFAULT_DEVELOPMENT_HOSTS): boolean =>
|
||||
matchDevelopmentHost(host, patterns) !== null
|
||||
|
||||
Reference in New Issue
Block a user