SPB Git

Repository Public

harness.ts 14.8 KB 276 lines typescript

1// Harnais : vrai processus serveur, vraie base PostgreSQL, vrai Stalwart (domaine de recette stg.spboucher.ai, joint par tunnel),2// authentificateur WebAuthn logiciel. Rien n'est bouchonné dans le serveur.3import { spawn, type ChildProcess } from "node:child_process"4import crypto from "node:crypto"5import http from "node:http"6import fs from "node:fs"7import os from "node:os"8import path from "node:path"9import postgres from "postgres"1011export const PORT = Number(process.env.SPMAIL_TEST_PORT ?? 8741)12export const ORIGIN = `http://localhost:${PORT}`13export const DB = process.env.SPMAIL_TEST_DB ?? "postgres://127.0.0.1/sp_mail_test"14if (!/_test$/.test(DB)) throw new Error("tests refuse to run outside a *_test database")15const S = process.env.SPMAIL_SECRETS ?? path.join(os.homedir(), "Desktop/Cluster/secrets/sp-mail")16export const STALWART = process.env.SPMAIL_STALWART_URL ?? "http://127.0.0.1:18080"17export const sql = postgres(DB, { onnotice: () => {} })18/** Identifiant de la passe : toutes les adresses d'essai le portent (…-<RUN>@stg.spboucher.ai). */19export const RUN = crypto.randomBytes(3).toString("hex")20export const STG = "stg.spboucher.ai"21/** Adresse hébergée traitée comme « externe » par le crochet de test SPMAIL_TREAT_AS_EXTERNAL (flux d'approbation). */22export const EXTERNAL_ADDR = `api-ext-${RUN}@${STG}`23const env: Record<string, string | undefined> = {24  ...process.env, DATABASE_URL: DB, PORT: String(PORT), SPMAIL_ORIGIN: ORIGIN, SPMAIL_RP_ID: "localhost", SPMAIL_LOG_LEVEL: "warn", SPMAIL_WEB_DIR: "/nonexistent",25  SPMAIL_RATE_SCALE: "20", SPMAIL_STALWART_URL: STALWART, SPMAIL_STALWART_ADMIN_PASSWORD: fs.readFileSync(path.join(S, "stalwart-admin.pw"), "utf8").trim(),26  SPMAIL_MASTER_KEY: crypto.randomBytes(32).toString("base64"),27  // API /api/v1 : réponses vérifiées contre leur schéma, travailleurs rapides, recul des webhooks raccourci, crochet « externe ».28  SPMAIL_VALIDATE_RESPONSES: "1", SPMAIL_WATCH_INTERVAL_MS: "1000", SPMAIL_WORKER_TICK_MS: "300", SPMAIL_WEBHOOK_BACKOFF: "2,2", SPMAIL_EXPIRY_INTERVAL_MS: "3000",29  SPMAIL_TREAT_AS_EXTERNAL: EXTERNAL_ADDR,30}3132/** Le tunnel SSH vers Stalwart tombe parfois : il est rouvert au besoin (commande du README). */33export async function ensureTunnel() {34  const up = async () => fetch(`${STALWART}/jmap/session`, { signal: AbortSignal.timeout(4000) }).then(() => true, () => false)35  if (await up()) return36  if (!/127\.0\.0\.1:18080/.test(STALWART)) throw new Error("mail server unreachable")37  await new Promise<void>((resolve) => spawn("ssh", ["-f", "-N", "-o", "ExitOnForwardFailure=yes", "-o", "ServerAliveInterval=20", "-L", "18080:127.0.0.1:8080", "BHS64"], { stdio: "ignore" }).on("close", () => resolve()))38  for (let i = 0; i < 20; i++) { if (await up()) return; await sleep(300) }39  throw new Error("could not reopen the tunnel to the mail server")40}41let server: ChildProcess | undefined42async function boot(extra: Record<string, string> = {}) {43  await ensureTunnel()44  server = spawn("node_modules/.bin/tsx", ["server/src/index.ts"], { env: { ...env, ...extra }, stdio: ["ignore", "inherit", "inherit"] })45  for (let i = 0; i < 80; i++) {46    try { if ((await fetch(`${ORIGIN}/api/health/ready`)).ok) return } catch {}47    await sleep(150)48  }49  throw new Error("server did not start")50}51export async function startServer() {52  await sql.unsafe("drop schema public cascade; create schema public;")53  await boot()54}55export async function startServerWith(extra: Record<string, string>) {56  await sql.unsafe("drop schema public cascade; create schema public;")57  await boot(extra)58}59/** Arrête le processus serveur et attend sa sortie (arrêt propre des travailleurs). */60export async function haltServer() {61  const p = server62  if (!p) return63  server = undefined64  await new Promise<void>((resolve) => { p.once("exit", () => resolve()); p.kill("SIGTERM"); setTimeout(() => { p.kill("SIGKILL"); resolve() }, 20_000).unref() })65}66/** Redémarre le serveur SANS toucher à la base : sert à prouver que les files persistantes survivent à un arrêt. */67export async function restartServer(extra: Record<string, string> = {}) {68  await haltServer()69  await boot(extra)70}71export async function stopServer() {72  await haltServer()73  for (const r of receivers) await r.stop().catch(() => {}) // un récepteur resté ouvert empêcherait le processus de test de finir74  await sql.end()75}76export function cli(...args: string[]): Promise<string> {77  return new Promise((resolve, reject) => {78    const p = spawn("node_modules/.bin/tsx", ["server/src/cli.ts", ...args], { env })79    let out = ""80    p.stdout.on("data", (d) => (out += d))81    p.stderr.on("data", (d) => (out += d))82    p.on("close", (code) => (code === 0 ? resolve(out) : reject(new Error(out))))83  })84}85export const tokenOf = (out: string) => out.match(/#([A-Za-z0-9_-]{20,})/)![1]86export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))87export async function until<T>(fn: () => Promise<T | undefined | false | null>, what: string, timeout = 30000): Promise<T> {88  const t0 = Date.now()89  while (Date.now() - t0 < timeout) {90    const v = await fn()91    if (v) return v92    await sleep(400)93  }94  throw new Error(`timed out waiting for: ${what}`)95}96// ---------------------------------------------------------------- software authenticator (ES256, "none" attestation)97const b64u = (b: Buffer | Uint8Array) => Buffer.from(b).toString("base64url")98function cborBytes(b: Buffer) {99  return b.length < 24 ? Buffer.concat([Buffer.from([0x40 + b.length]), b]) : b.length < 256 ? Buffer.concat([Buffer.from([0x58, b.length]), b]) : Buffer.concat([Buffer.from([0x59, b.length >> 8, b.length & 255]), b])100}101const cborText = (s: string) => Buffer.concat([Buffer.from([0x60 + s.length]), Buffer.from(s)])102export class Authenticator {103  creds = new Map<string, { key: crypto.KeyObject; userHandle: string; counter: number }>()104  async create(options: any) {105    const { privateKey, publicKey } = crypto.generateKeyPairSync("ec", { namedCurve: "P-256" })106    const jwk = publicKey.export({ format: "jwk" })107    const id = crypto.randomBytes(16)108    const cose = Buffer.concat([109      Buffer.from([0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21]), cborBytes(Buffer.from(jwk.x!, "base64url")),110      Buffer.from([0x22]), cborBytes(Buffer.from(jwk.y!, "base64url")),111    ])112    const authData = Buffer.concat([113      crypto.createHash("sha256").update(options.rp.id).digest(), Buffer.from([0x5d]), Buffer.alloc(4), Buffer.alloc(16),114      Buffer.from([0, id.length]), id, cose,115    ])116    const attestationObject = Buffer.concat([117      Buffer.from([0xa3]), cborText("fmt"), cborText("none"), cborText("attStmt"), Buffer.from([0xa0]), cborText("authData"), cborBytes(authData),118    ])119    const clientDataJSON = Buffer.from(JSON.stringify({ type: "webauthn.create", challenge: options.challenge, origin: ORIGIN, crossOrigin: false }))120    this.creds.set(b64u(id), { key: privateKey, userHandle: options.user.id, counter: 0 })121    return {122      id: b64u(id), rawId: b64u(id), type: "public-key", clientExtensionResults: {}, authenticatorAttachment: "platform",123      response: { clientDataJSON: b64u(clientDataJSON), attestationObject: b64u(attestationObject), transports: ["internal"] },124    }125  }126  async get(options: any, credId?: string) {127    const id = credId ?? [...this.creds.keys()][0]128    const c = this.creds.get(id)!129    c.counter++130    const counter = Buffer.alloc(4)131    counter.writeUInt32BE(c.counter)132    const authData = Buffer.concat([crypto.createHash("sha256").update(options.rpId).digest(), Buffer.from([0x1d]), counter])133    const clientDataJSON = Buffer.from(JSON.stringify({ type: "webauthn.get", challenge: options.challenge, origin: ORIGIN, crossOrigin: false }))134    const signature = crypto.sign("sha256", Buffer.concat([authData, crypto.createHash("sha256").update(clientDataJSON).digest()]), c.key)135    return {136      id, rawId: id, type: "public-key", clientExtensionResults: {}, authenticatorAttachment: "platform",137      response: { clientDataJSON: b64u(clientDataJSON), authenticatorData: b64u(authData), signature: b64u(signature), userHandle: c.userHandle },138    }139  }140}141142143// ---------------------------------------------------------------- clients144export class ApiError extends Error {145  constructor(public status: number, public code: string) { super(`${status} ${code}`) }146}147/** Client HTTP : `web` (cookie + en-têtes anti-CSRF) ou `native` (jeton porteur, aucune en-tête Origin). */148export class Client {149  cookie = ""150  bearer = ""151  auth = new Authenticator()152  constructor(public kind: "web" | "native" = "web") {}153  headers(extra: Record<string, string> = {}) {154    const h: Record<string, string> = { ...extra }155    if (this.kind === "web") { h.origin = ORIGIN; h["x-spmail"] = "1"; if (this.cookie) h.cookie = this.cookie }156    else { h["x-spmail-client"] = "native"; if (this.bearer) h.authorization = `Bearer ${this.bearer}` }157    return h158  }159  async raw(method: string, url: string, body?: unknown, extra: Record<string, string> = {}) {160    const isBuf = body instanceof Uint8Array161    const r = await fetch(ORIGIN + url, {162      method, headers: this.headers(body === undefined ? extra : { "content-type": isBuf ? "application/octet-stream" : "application/json", ...extra }),163      body: body === undefined ? undefined : isBuf ? (body as unknown as BodyInit) : JSON.stringify(body),164    })165    const set = r.headers.getSetCookie?.() ?? []166    for (const c of set) this.cookie = c.split(";")[0].endsWith("=") ? "" : c.split(";")[0]167    return r168  }169  async call<T = any>(method: string, url: string, body?: unknown): Promise<T> {170    const r = await this.raw(method, url, body)171    const j = await r.json().catch(() => ({}))172    if (!r.ok) throw new ApiError(r.status, (j as any).error ?? "error")173    return j as T174  }175  async enroll(token: string, handle?: string, displayName?: string) {176    const { challenge_id, options } = await this.call("POST", "/api/enroll/options", { token, handle, display_name: displayName })177    const res = await this.call("POST", "/api/enroll/verify", { challenge_id, response: await this.auth.create(options), client: { kind: this.kind, deviceName: "test", platform: "test" } })178    if (res.token) this.bearer = res.token179  }180  async login(from: Client = this) {181    const { challenge_id, options } = await this.call("POST", "/api/login/options", {})182    const res = await this.call("POST", "/api/login/verify", { challenge_id, response: await from.auth.get(options), client: { kind: this.kind, deviceName: "test", platform: "test" } })183    if (res.token) this.bearer = res.token184  }185  async jmap(mb: string, calls: unknown[], using = ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail", "urn:ietf:params:jmap:submission"]) {186    const r = await this.call("POST", `/api/jmap/${mb}/api`, { using, methodCalls: calls })187    return r.methodResponses as [string, any, string][]188  }189}190191// ---------------------------------------------------------------- réauthentification (clé d'accès logicielle)192export async function reauth(c: Client) {193  const { challenge_id, options } = await c.call("POST", "/api/reauth/options", {})194  await c.call("POST", "/api/reauth/verify", { challenge_id, response: await c.auth.get(options) })195}196197// ---------------------------------------------------------------- client /api/v1 (clé d'API : porteur, aucune en-tête Origin)198export class V1Error extends Error {199  constructor(public status: number, public code: string, public body: any) { super(`${status} ${code}`) }200}201export type V1Response<T = any> = { status: number; body: T; headers: Headers }202export class ApiKey {203  constructor(public token: string, public id = "") {}204  async raw(method: string, path: string, body?: unknown, headers: Record<string, string> = {}): Promise<Response> {205    const isBuf = body instanceof Uint8Array206    return fetch(`${ORIGIN}/api/v1${path}`, {207      method, headers: { authorization: `Bearer ${this.token}`, ...(body === undefined ? {} : { "content-type": isBuf ? "application/octet-stream" : "application/json" }), ...headers },208      body: body === undefined ? undefined : isBuf ? (body as unknown as BodyInit) : JSON.stringify(body),209    })210  }211  async res<T = any>(method: string, path: string, body?: unknown, headers: Record<string, string> = {}): Promise<V1Response<T>> {212    const r = await this.raw(method, path, body, headers)213    const j = await r.json().catch(() => ({}))214    return { status: r.status, body: j as T, headers: r.headers }215  }216  async call<T = any>(method: string, path: string, body?: unknown, headers: Record<string, string> = {}): Promise<T> {217    const r = await this.res<T>(method, path, body, headers)218    if (r.status >= 400) throw new V1Error(r.status, (r.body as any)?.error?.code ?? "error", r.body)219    return r.body220  }221}222/** Session web sur /api/v1 (enveloppe d'erreur v1). */223export async function v1Session<T = any>(c: Client, method: string, path: string, body?: unknown): Promise<T> {224  const r = await c.raw(method, `/api/v1${path}`, body)225  const j = await r.json().catch(() => ({}))226  if (!r.ok) throw new V1Error(r.status, (j as any)?.error?.code ?? "error", j)227  return j as T228}229export async function expectV1(p: Promise<unknown>, status: number, code?: string) {230  try { await p } catch (e) {231    if (e instanceof V1Error && e.status === status && (!code || e.code === code)) return e232    throw new Error(`expected ${status} ${code ?? ""}, got ${e instanceof V1Error ? `${e.status} ${e.code} ${JSON.stringify(e.body).slice(0, 300)}` : String(e)}`)233  }234  throw new Error(`expected ${status} ${code ?? ""}, but the call succeeded`)235}236237// ---------------------------------------------------------------- récepteur de webhooks local (127.0.0.1)238export type Hook = { headers: http.IncomingHttpHeaders; body: string; json: any }239const receivers = new Set<Receiver>()240export class Receiver {241  hits: Hook[] = []242  /** Statuts à répondre aux prochaines requêtes (ensuite 200). */243  script: number[] = []244  port = 0245  private srv?: http.Server246  async start(port = 0) {247    this.srv = http.createServer((req, res) => {248      let body = ""249      req.on("data", (d) => (body += d))250      req.on("end", () => {251        let json: any = null252        try { json = JSON.parse(body) } catch {}253        this.hits.push({ headers: req.headers, body, json })254        const status = this.script.shift() ?? 200255        if (status >= 300 && status < 400) res.writeHead(status, { location: "http://127.0.0.1:1/elsewhere" })256        else res.writeHead(status, { "content-type": "text/plain" })257        res.end("ok")258      })259    })260    await new Promise<void>((r) => this.srv!.listen(port, "127.0.0.1", () => r()))261    this.port = (this.srv.address() as { port: number }).port262    receivers.add(this)263    return this264  }265  get url() { return `http://127.0.0.1:${this.port}/hook` }266  of(type: string) { return this.hits.filter((h) => h.json?.type === type) }267  async stop() {268    const srv = this.srv269    this.srv = undefined270    receivers.delete(this)271    if (!srv) return272    srv.closeAllConnections()273    await new Promise<void>((r) => srv.close(() => r()))274  }275}276
Files main
Menu

Theme