Mail relay: contract 1.7.0 — X-Intecion-Site on every request with a token, host-mismatch

contracts/mail-relay refreshed from IWDL 6cd99f7 (64 files). A token the service knows as another
site's is forgotten without revoking and the site enrols as itself; a hand-issued one is refused.
The fake requires the header and answers host-mismatch.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 22:13:58 +01:00
co-authored by Claude Fable 5.2
parent a2b07b1dc1
commit fef2c51bae
60 changed files with 326 additions and 118 deletions
+30 -8
View File
@@ -252,7 +252,7 @@ describe('when the service asks the site to enrol again', () => {
const { fake, options, payload } = relaySite({ fake: { allow: [HOST] } })
await sendThroughRelay(payload, options, MESSAGE)
const old = (await readRelayStore(payload)).token!
await fake.handle(new Request(`${fake.url}/v1/revoke`, { headers: { authorization: `Bearer ${old}` }, method: 'POST' }))
await fake.handle(new Request(`${fake.url}/v1/revoke`, { headers: { authorization: `Bearer ${old}`, 'x-intecion-site': HOST }, method: 'POST' }))
expect((await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Again' })).sent).toBe(true)
expect((await readRelayStore(payload)).token).not.toBe(old)
})
@@ -585,7 +585,7 @@ describe('the token over time', () => {
const fresh = (await readRelayStore(b.payload)).token
expect(fresh).not.toBe(old)
// a container that still held the old token asks too: already-rotated — the store has the newer one, nothing is lost
const late = await a.fake.handle(new Request(`${a.fake.url}/v1/rotate`, { headers: { authorization: `Bearer ${old}` }, method: 'POST' }))
const late = await a.fake.handle(new Request(`${a.fake.url}/v1/rotate`, { headers: { authorization: `Bearer ${old}`, 'x-intecion-site': HOST }, method: 'POST' }))
expect(((await late.json()) as { code: string }).code).toBe('already-rotated')
expect((await sendThroughRelay(b.payload, a.options, { ...MESSAGE, subject: 'From B' })).sent).toBe(true)
expect((await readRelayStore(a.payload)).token).toBe(fresh)
@@ -596,7 +596,7 @@ describe('the token over time', () => {
await sendThroughRelay(payload, options, MESSAGE)
const old = (await readRelayStore(payload)).token!
// another process rotated and its answer was lost: the store still has the old token
await fake.handle(new Request(`${fake.url}/v1/rotate`, { headers: { authorization: `Bearer ${old}` }, method: 'POST' }))
await fake.handle(new Request(`${fake.url}/v1/rotate`, { headers: { authorization: `Bearer ${old}`, 'x-intecion-site': HOST }, method: 'POST' }))
expect(await rotateRelayToken(payload, options, { force: true })).toBe('failed')
expect((await readRelayStore(payload)).token).toBeUndefined()
expect((await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Healed' })).sent).toBe(true)
@@ -609,7 +609,7 @@ describe('the token over time', () => {
const old = (await readRelayStore(payload)).token!
await disconnectRelay(payload, options)
expect(await readRelayStore(payload)).toEqual({ disconnected: true, v: 1 })
const refusedByService = await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${old}` } }))
const refusedByService = await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${old}`, 'x-intecion-site': HOST } }))
expect(refusedByService.status).toBe(401)
const before = fake.requests.length
expect(await sendThroughRelay(payload, options, MESSAGE)).toMatchObject({ code: 'relay-disconnected', sent: false })
@@ -636,7 +636,7 @@ describe('the token over time', () => {
expect(await mailPanelState({ payload }, options)).toMatchObject({ relay: { connected: true, refusal: { code: 'reverify' }, tokenSource: 'manual' } })
fake.clientAddress = '203.0.113.10'
await fake.handle(new Request(`${fake.url}/v1/revoke`, { headers: { authorization: `Bearer ${token}` }, method: 'POST' }))
await fake.handle(new Request(`${fake.url}/v1/revoke`, { headers: { authorization: `Bearer ${token}`, 'x-intecion-site': HOST }, method: 'POST' }))
const gone = await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Gone' })
expect(gone).toMatchObject({ cause: 'invalid-token', code: 'relay-refused', sent: false })
expect(gone.sent === false && gone.detail).toMatch(/ask Intecion for a new token/)
@@ -790,7 +790,7 @@ describe('review of 7d766b6: R-1 to R-7, R-11, and four mutations it found uncau
expect((await readRelayStore(payload)).token).toBeUndefined()
expect((await getRelayStatus(payload, options)).refusal).toMatchObject({ code: 'client-foreign-token' })
// the other site's token still works there
expect((await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${foreign}` } }))).status).toBe(200)
expect((await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${foreign}`, 'x-intecion-site': 'other-client.example.org' } }))).status).toBe(200)
// the site's own token, issued by hand for its host, passes the same check
await setRelayToken(payload, fake.issueToken(HOST))
expect((await sendThroughRelay(payload, options, MESSAGE)).sent).toBe(true)
@@ -801,6 +801,28 @@ describe('review of 7d766b6: R-1 to R-7, R-11, and four mutations it found uncau
expect(await sendThroughRelay(payload, { ...options, totalTimeoutMs: 300 }, MESSAGE)).toMatchObject({ code: 'relay-unreachable', confirmed: true, sent: false })
})
test('contract 1.7.0: every request with a token names the site, and 403 host-mismatch means "not yours: enrol as yourself"', async () => {
const { fake, options, payload } = relaySite({ fake: { allow: [HOST, 'other-client.example.org'] } })
const headers: (null | string)[] = []
fake.intercept = (request) => void (request.headers.has('authorization') && headers.push(request.headers.get('x-intecion-site')))
await sendThroughRelay(payload, options, MESSAGE)
await refreshRelayStatus(payload, options)
await rotateRelayToken(payload, options, { force: true })
expect(headers.length).toBeGreaterThanOrEqual(3)
expect(new Set(headers)).toEqual(new Set([HOST]))
// a token of another site sits in the store under this site's host (the comparison of hosts cannot see it)
const foreign = fake.issueToken('other-client.example.org')
await updateRelayStore(payload, (current) => ({ ...current, token: foreign }))
const before = fake.requests.length
expect((await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'As myself' })).sent).toBe(true)
expect(fake.requests.slice(before).map((r) => r.path)).toEqual(['/v1/send', '/v1/enrol', '/v1/send'])
expect(fake.outbox.at(-1)).toMatchObject({ host: HOST, subject: 'As myself' })
expect((await readRelayStore(payload)).token).not.toBe(foreign)
// forgotten here, not revoked there
expect(fake.requests.map((r) => r.path)).not.toContain('/v1/revoke')
expect((await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${foreign}`, 'x-intecion-site': 'other-client.example.org' } }))).status).toBe(200)
})
test('R-3: junk calls to the confirm route do not keep the site\'s own enrolment out', async () => {
const { fake, options, payload } = relaySite({ fake: { allow: [HOST] } })
const confirm = callConfirmRoute(payload) // every call comes from one address, as the service's does
@@ -872,12 +894,12 @@ describe('review of 7d766b6: R-1 to R-7, R-11, and four mutations it found uncau
expect(await getRelayStatus(payload, options)).toMatchObject({ connected: false, disconnected: true, revokePending: true })
fake.intercept = undefined
// the token still works at the service — that is why the panel must not say "done"
expect((await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${token}` } }))).status).toBe(200)
expect((await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${token}`, 'x-intecion-site': HOST } }))).status).toBe(200)
expect(await sendThroughRelay(payload, options, MESSAGE)).toMatchObject({ code: 'relay-disconnected', sent: false })
// pressed again with the service back: revoked and forgotten
expect(await disconnectRelay(payload, options)).toEqual({ revoked: true })
expect(await readRelayStore(payload)).toEqual({ disconnected: true, v: 1 })
expect((await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${token}` } }))).status).toBe(401)
expect((await fake.handle(new Request(`${fake.url}/v1/status`, { headers: { authorization: `Bearer ${token}`, 'x-intecion-site': HOST } }))).status).toBe(401)
})
test('R-6: without the confirm route in the config the site does not ask to be enrolled', async () => {