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
+44
-6
@@ -21,11 +21,11 @@ import { bareAddresses, buildRelayMessage, cleanSubject } from '../src/modules/e
|
||||
import { isPublicHost, pluginDevelopmentHosts, relayHost, resolveRelayOptions } from '../src/modules/email/relay/options.js'
|
||||
import { mailPanelState } from '../src/modules/email/relay/panel.js'
|
||||
import { clearUnconfirmedRelayMessages, sendThroughRelay } from '../src/modules/email/relay/send.js'
|
||||
import { disconnectRelay, enrolRelay, forgetOtherHosts, getRelayStatus, provesToken, refreshRelayStatus, rotateRelayToken, setRelayToken, siteIsNonPublic, siteNonPublicSignal } from '../src/modules/email/relay/session.js'
|
||||
import { disconnectRelay, enrolRelay, getRelayStatus, provesToken, refreshRelayStatus, rotateRelayToken, setRelayToken, siteIsNonPublic, siteNonPublicSignal } from '../src/modules/email/relay/session.js'
|
||||
import { holdNonce, otherRelayHosts, readRelayCandidate, readRelayStore, updateRelayStore } from '../src/modules/email/relay/store.js'
|
||||
import { NOTIFICATION_FALLBACK, NOTIFICATION_FALLBACK_PL } from '../src/modules/notifications/defaults.js'
|
||||
import { resolveFormMessage } from '../src/modules/notifications/resolveFormMessage.js'
|
||||
import { DEFAULT_DEVELOPMENT_HOSTS, isDevelopmentHost, matchDevelopmentHost } from '../src/modules/shared/developmentHosts.js'
|
||||
import { assertDevelopmentHosts, DEFAULT_DEVELOPMENT_HOSTS, isDevelopmentHost, matchDevelopmentHost } from '../src/modules/shared/developmentHosts.js'
|
||||
import { setRateLimitStore } from '../src/modules/shared/rateLimit.js'
|
||||
import { callConfirmRoute, relayPayload, relaySite } from './mailRelay/helpers.js'
|
||||
|
||||
@@ -754,7 +754,6 @@ describe('the token over time', () => {
|
||||
await sendThroughRelay(apart.payload, apart.options, MESSAGE)
|
||||
expect((await sendThroughRelay(apart.payload, apart.options, { ...MESSAGE, subject: 'Dev, later' })).sent).toBe(true)
|
||||
// …but a development site that still runs ON the launched database now holds the live site's data: it stops by itself
|
||||
forgetOtherHosts(dev.payload)
|
||||
const before = dev.fake.requests.length
|
||||
expect(await sendThroughRelay(dev.payload, dev.options, { ...MESSAGE, to: '[email protected]' })).toMatchObject({ cause: 'client-other-host', code: 'relay-refused', sent: false })
|
||||
expect(dev.fake.requests).toHaveLength(before)
|
||||
@@ -784,7 +783,6 @@ describe('the token over time', () => {
|
||||
|
||||
// months later: production's data is copied back onto the development address, to work on a bug
|
||||
copy(live.store, dev.store)
|
||||
forgetOtherHosts(dev.payload)
|
||||
expect((await readRelayStore(dev.payload)).token).toMatch(/^ims_/) // its own old entry came back with the copy
|
||||
const requests = service.requests.length
|
||||
const held = await sendThroughRelay(dev.payload, dev.options, { ...MESSAGE, to: '[email protected]' })
|
||||
@@ -798,7 +796,6 @@ describe('the token over time', () => {
|
||||
expect((await mailPanelState({ payload: dev.payload }, dev.options)).relay?.refusal).toBeUndefined()
|
||||
// the next fresh copy is held again: the consent was this database's, and the copy replaced it
|
||||
copy(live.store, dev.store)
|
||||
forgetOtherHosts(dev.payload)
|
||||
expect(await sendThroughRelay(dev.payload, dev.options, MESSAGE)).toMatchObject({ cause: 'client-other-host', sent: false })
|
||||
// production never noticed any of it
|
||||
expect((await readRelayStore(live.payload)).token).toBe(liveToken)
|
||||
@@ -897,6 +894,48 @@ describe('the token over time', () => {
|
||||
expect((await sendThroughRelay(copy.payload, { ...copy.options, developmentHosts: [] }, MESSAGE)).sent).toBe(true)
|
||||
})
|
||||
|
||||
test('V-3: production\'s dump restored into the RUNNING development site right after it sent a mail — the very next mail is refused', async () => {
|
||||
const live = shared({ serverURL: 'https://client.pl' })
|
||||
await sendThroughRelay(live.payload, live.options, MESSAGE)
|
||||
const dev = shared({ serverURL: `https://${DEV}` })
|
||||
const on = { ...dev.options, fetchImpl: live.fake.fetch, url: live.fake.url }
|
||||
// the development site is connected and has just sent (its own database, its own data)
|
||||
await setRelayToken(dev.payload, live.fake.issueToken(DEV), on)
|
||||
expect((await sendThroughRelay(dev.payload, on, MESSAGE)).sent).toBe(true)
|
||||
// the restore: production's entries land in the database of the running process
|
||||
for (const [key, value] of live.store) {dev.store.set(key, structuredClone(value))}
|
||||
const requests = live.fake.requests.length
|
||||
expect(await sendThroughRelay(dev.payload, on, { ...MESSAGE, to: '[email protected]' })).toMatchObject({ cause: 'client-other-host', sent: false })
|
||||
expect(live.fake.requests).toHaveLength(requests)
|
||||
expect((await mailPanelState({ payload: dev.payload }, on)).relay?.refusal).toMatchObject({ code: 'client-other-host', reason: 'client.pl' })
|
||||
})
|
||||
|
||||
test('V-1, V-2: a malformed list stops the start; a trailing dot and a Unicode pattern still match', async () => {
|
||||
// what is refused — by the checker, by the options in effect, by a value left in the config by hand
|
||||
for (const bad of ['*.dev.netscale.pl', null, {}, [1, 2], [''], [' '], ['*.dev.netscale.pl', 7], ['*'], ['*.*'], [' * '], ['https://x.dev.example.com'], ['x.example.com/path'], ['a b.example.com'], ['*ż.example.com']]) {
|
||||
expect(() => assertDevelopmentHosts(bad), JSON.stringify(bad)).toThrow(/must be an array of host patterns/)
|
||||
const site = relayPayload({ custom: { iapl: { developmentHosts: bad } } })
|
||||
expect(() => resolveRelayOptions(site.payload, {}), JSON.stringify(bad)).toThrow(/developmentHosts/)
|
||||
expect(() => resolveRelayOptions(relayPayload().payload, { developmentHosts: bad as never }), JSON.stringify(bad)).toThrow(/mail\.relay\.developmentHosts/)
|
||||
}
|
||||
expect(() => assertDevelopmentHosts(['*'])).toThrow(/matches every host/)
|
||||
expect(() => assertDevelopmentHosts('*.dev.netscale.pl', 'iapl({ developmentHosts })')).toThrow('[iapl] iapl({ developmentHosts }) must be an array')
|
||||
// what is taken
|
||||
for (const good of [[], ['*.dev.netscale.pl'], ['staging.client.pl', '*.Preview.Agency-Example.com ', 'acme.*', '*.dev.zażółć.pl', '*.dev.netscale.pl.']]) {expect(assertDevelopmentHosts(good)).toBe(good)}
|
||||
// a fully qualified name with the dot, in the address and in the pattern
|
||||
expect(matchDevelopmentHost('https://acme.dev.netscale.pl.')).toBe('*.dev.netscale.pl')
|
||||
expect(matchDevelopmentHost('https://acme.dev.netscale.pl.:8443/pl')).toBe('*.dev.netscale.pl')
|
||||
expect(matchDevelopmentHost('acme.dev.netscale.pl', ['*.dev.netscale.pl.'])).toBe('*.dev.netscale.pl.')
|
||||
const dotted = relayPayload({ serverURL: 'https://acme.dev.netscale.pl.' })
|
||||
expect(siteNonPublicSignal(dotted.payload, resolveRelayOptions(dotted.payload, {})!)).toEqual({ by: 'development-host', pattern: '*.dev.netscale.pl' })
|
||||
// a pattern written in Unicode matches the punycode host, and the other way round
|
||||
expect(matchDevelopmentHost('https://acme.dev.zażółć.pl', ['*.dev.zażółć.pl'])).toBe('*.dev.zażółć.pl')
|
||||
expect(new URL('https://zażółć.pl').hostname).toMatch(/^xn--/)
|
||||
expect(matchDevelopmentHost(`acme.dev.${new URL('https://zażółć.pl').hostname}`, ['*.dev.ZAŻÓŁĆ.pl'])).toBe('*.dev.ZAŻÓŁĆ.pl')
|
||||
expect(matchDevelopmentHost('https://acme.dev.zażółć.pl', [`*.dev.${new URL('https://zażółć.pl').hostname}`])).not.toBeNull()
|
||||
expect(matchDevelopmentHost('acme.dev.zazolc.pl', ['*.dev.zażółć.pl'])).toBeNull()
|
||||
})
|
||||
|
||||
test('a developer\'s `localhost` on the production database, no env: refused before any call, nothing written', async () => {
|
||||
const production = shared({ serverURL: 'https://client.pl' })
|
||||
await sendThroughRelay(production.payload, production.options, MESSAGE)
|
||||
@@ -933,7 +972,6 @@ describe('the token over time', () => {
|
||||
expect((await mailPanelState({ payload: staging.payload }, staging.options)).nonPublic).toBeUndefined()
|
||||
// the same site once it says so: held from the next e-mail on, although it has a token by now
|
||||
vi.stubEnv('IAPL_NOINDEX', '1')
|
||||
forgetOtherHosts(staging.payload)
|
||||
expect(await sendThroughRelay(staging.payload, staging.options, MESSAGE)).toMatchObject({ cause: 'client-other-host', sent: false })
|
||||
})
|
||||
|
||||
|
||||
+9
-2
@@ -4,6 +4,7 @@ import { describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import type { I18nConfig } from '../src/modules/i18n/types.js'
|
||||
|
||||
import { mailAdapter } from '../src/modules/email/mailAdapter.js'
|
||||
import { pluginDevelopmentHosts } from '../src/modules/email/relay/options.js'
|
||||
import { iapl } from '../src/plugin.js'
|
||||
import { withTotpParts } from './helpers.js'
|
||||
@@ -24,13 +25,19 @@ const base = (): Config =>
|
||||
|
||||
describe('iapl — config transform', () => {
|
||||
test('developmentHosts: nothing in the config by default (the built-in list applies), the project\'s list when given, an empty list too', async () => {
|
||||
const hosts = async (own?: string[]) => {
|
||||
const config = await iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false, ...(own ? { developmentHosts: own } : {}) })(base())
|
||||
const hosts = async (...own: [string[]?]) => {
|
||||
const config = await iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false, ...(own.length ? { developmentHosts: own[0] } : {}) })(base())
|
||||
return pluginDevelopmentHosts({ config } as never)
|
||||
}
|
||||
expect(await hosts()).toEqual(['*.dev.netscale.pl'])
|
||||
expect(await hosts(['*.preview.agency-example.com'])).toEqual(['*.preview.agency-example.com'])
|
||||
expect(await hosts([])).toEqual([])
|
||||
// V-1: a malformed list stops the start — the plugin option, the mail option, the adapter's own
|
||||
for (const bad of ['*.dev.netscale.pl', null, [1, 2], [''], ['*']]) {
|
||||
await expect(hosts(bad as never), JSON.stringify(bad)).rejects.toThrow('[iapl] iapl({ developmentHosts }) must be an array of host patterns')
|
||||
await expect(Promise.resolve().then(() => iapl({ access: { authCollection: 'users' }, i18n, mail: { relay: { developmentHosts: bad as never } }, twoFactor: false })(base()))).rejects.toThrow('iapl({ mail: { relay: { developmentHosts } } })')
|
||||
expect(() => mailAdapter({ relay: { developmentHosts: bad as never } })({ payload: {} as never }), JSON.stringify(bad)).toThrow('mailAdapter({ relay: { developmentHosts } })')
|
||||
}
|
||||
})
|
||||
|
||||
test('adds globals, endpoint, localization and roles', async () => {
|
||||
|
||||
Reference in New Issue
Block a user