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:
co-authored by
Claude Fable 5.2
parent
a2b07b1dc1
commit
fef2c51bae
@@ -170,7 +170,7 @@ async function handIssued(payload: BasePayload, code: keyof typeof HAND_ISSUED):
|
||||
* `rate-limited` is not among them (its per-address gate runs before the token is looked at), nor are
|
||||
* `invalid-request`, `too-large`, `unavailable`, `internal` and the answers that are not the service's own.
|
||||
*/
|
||||
const TOKEN_SEEN = new Set(['already-rotated', 'attachment-refused', 'backend-failed', 'idempotency-conflict', 'in-progress', 'quota-exceeded', 'recipient-suppressed', 'reverify', 'site-pending', 'site-suspended', 'too-many-recipients'])
|
||||
const TOKEN_SEEN = new Set(['already-rotated', 'attachment-refused', 'backend-failed', 'host-mismatch', 'idempotency-conflict', 'in-progress', 'quota-exceeded', 'recipient-suppressed', 'reverify', 'site-pending', 'site-suspended', 'too-many-recipients'])
|
||||
|
||||
/** Whether an answer shows that the service accepted the token it was sent with. */
|
||||
export const provesToken = (answer: RelayAnswer<unknown>): boolean => answer.ok || (!answer.noAnswer && TOKEN_SEEN.has(answer.code))
|
||||
@@ -195,7 +195,8 @@ export type TokenUse = {
|
||||
* - no token yet → enrol first (when `enrol` is `'auto'`), then call;
|
||||
* - a token the call-back delivered but `/v1/enrol` never confirmed is tried first; on
|
||||
* `401 invalid-token` it is dropped and the earlier token is used again;
|
||||
* - `401 invalid-token` or `401 reverify` → enrol again once, then call again.
|
||||
* - `401 invalid-token` or `401 reverify` → enrol again once, then call again;
|
||||
* - `403 host-mismatch` (the token is another site's) → forget it without revoking, enrol as this site.
|
||||
* A candidate becomes the token in use only when the service has SEEN IT ACCEPTED: a `200`, or a refusal
|
||||
* the service gives after it has checked the token (TOKEN_SEEN). No answer, an error page, the
|
||||
* per-address `rate-limited` and `browser-not-allowed` say nothing about a token — the candidate then stays
|
||||
@@ -250,6 +251,19 @@ export async function withRelayToken<T>(
|
||||
if (failed) {return use.enrolment && !use.enrolment.ok ? failed : answer}
|
||||
continue
|
||||
}
|
||||
if (!answer.ok && answer.code === 'host-mismatch') {
|
||||
// The service knows this token as another site's (contract §8): a copied database or a token pasted into
|
||||
// the wrong site that the comparison of the stored host did not catch. It is forgotten here — never
|
||||
// revoked — and the site enrols as itself; a site that cannot be called back is told to ask for its own.
|
||||
const message = `The token belongs to another site, not to "${relayHost(payload, options)}": it is not used. Ask Intecion for a token for this site.`
|
||||
const now = new Date().toISOString()
|
||||
await dropRelayCandidate(payload)
|
||||
await updateRelayStore(payload, (current) => ({ ...current, hostChecked: false, manual: false, service: null, source: undefined, state: undefined, token: undefined, ...(stored.manual ? { refusal: { at: now, code: LOCAL_CODES.foreignToken, message, until: now } } : {}) }))
|
||||
if (stored.manual) {return refusal(LOCAL_CODES.foreignToken, message)}
|
||||
const failed = await enrolNow()
|
||||
if (failed) {return use.enrolment && !use.enrolment.ok ? failed : answer}
|
||||
continue
|
||||
}
|
||||
if (candidate && !provesToken(answer)) {return answer} // says nothing about the token: both stay as they are
|
||||
if (candidate) {
|
||||
// Seen accepted: this is the token now.
|
||||
@@ -287,7 +301,7 @@ export async function refreshRelayStatus(payload: BasePayload, options: Resolved
|
||||
const answer = await withRelayToken<StatusBody>(
|
||||
payload,
|
||||
options,
|
||||
(token) => relayRequest<StatusBody>({ fetchImpl: options.fetchImpl, method: 'GET', path: '/v1/status', timeoutMs, token, url: options.url }),
|
||||
(token) => relayRequest<StatusBody>({ fetchImpl: options.fetchImpl, method: 'GET', path: '/v1/status', site: relayHost(payload, options), timeoutMs, token, url: options.url }),
|
||||
timeoutMs === undefined ? {} : { deadline: Date.now() + timeoutMs },
|
||||
{ first: false },
|
||||
)
|
||||
@@ -332,7 +346,7 @@ export async function rotateRelayToken(payload: BasePayload, options: ResolvedRe
|
||||
const due = stored.service?.rotateAfter ? Date.parse(stored.service.rotateAfter) : Number.NaN
|
||||
if (!force && !(due <= Date.now())) {return 'not-due'}
|
||||
const used = stored.token
|
||||
const answer = await relayRequest<{ token?: string }>({ fetchImpl: options.fetchImpl, method: 'POST', path: '/v1/rotate', timeoutMs, token: used, url: options.url })
|
||||
const answer = await relayRequest<{ token?: string }>({ fetchImpl: options.fetchImpl, method: 'POST', path: '/v1/rotate', site: relayHost(payload, options), timeoutMs, token: used, url: options.url })
|
||||
if (answer.ok && typeof answer.body.token === 'string' && answer.body.token) {
|
||||
const fresh = answer.body.token
|
||||
await updateRelayStore(payload, (current) => ({
|
||||
@@ -364,7 +378,7 @@ export async function disconnectRelay(payload: BasePayload, options: ResolvedRel
|
||||
// (a store copied from another host is dropped by this read: that site's token is never revoked from here)
|
||||
const stored = await readSiteStore(payload, options)
|
||||
if (stored.token && options.token === undefined) {
|
||||
const answer = await relayRequest({ fetchImpl: options.fetchImpl, method: 'POST', path: '/v1/revoke', token: stored.token, url: options.url })
|
||||
const answer = await relayRequest({ fetchImpl: options.fetchImpl, method: 'POST', path: '/v1/revoke', site: relayHost(payload, options), token: stored.token, url: options.url })
|
||||
// Only the service's own word counts: "done", or "I do not know this token" (it is gone already).
|
||||
if (!answer.ok && !(answer.code === 'invalid-token' && !answer.noAnswer)) {
|
||||
// Not confirmed: the site stops using the token now, but it still works at the service. It is kept so
|
||||
|
||||
Reference in New Issue
Block a user