From 06adc9a93fdfceaf044e3050e917a864c746fbe9 Mon Sep 17 00:00:00 2001 From: Codex co-lead Date: Wed, 2 Dec 2026 14:53:03 +0100 Subject: [PATCH 1/2] Isoler le connecteur MCP de staging --- workers/mcp/src/index.ts | 5 +++ workers/mcp/src/staging-health.ts | 27 +++++++++++++ workers/mcp/test/staging-health.test.ts | 51 +++++++++++++++++++++++++ workers/mcp/wrangler.jsonc | 24 +++++++++++- 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 workers/mcp/src/staging-health.ts create mode 100644 workers/mcp/test/staging-health.test.ts diff --git a/workers/mcp/src/index.ts b/workers/mcp/src/index.ts index d88e6de..dfa557b 100644 --- a/workers/mcp/src/index.ts +++ b/workers/mcp/src/index.ts @@ -9,11 +9,13 @@ import { agentConnectConfig, type AgentConnectEnv } from "./agent-connect.ts"; import { handleAuthorize } from "./consent.ts"; import { handleMcp } from "./mcp.ts"; import { registrationRedirectsAllowed } from "./redirects.ts"; +import { attestStagingApi } from "./staging-health.ts"; export interface Env extends AgentConnectEnv { OAUTH_KV: KVNamespace; FACTURX_BASE_URL: string; PUBLIC_BASE_URL?: string; + STAGING_API_ATTESTATION?: string; OAUTH_PROVIDER: OAuthHelpers; } @@ -86,6 +88,9 @@ function providerFor(request: Request, env: Env): OAuthProvider { export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { + if (env.STAGING_API_ATTESTATION === "enabled" && request.method === "GET" && new URL(request.url).pathname === "/health") { + return attestStagingApi(env); + } return providerFor(request, env).fetch(request, env, ctx); }, }; diff --git a/workers/mcp/src/staging-health.ts b/workers/mcp/src/staging-health.ts new file mode 100644 index 0000000..d59b7ed --- /dev/null +++ b/workers/mcp/src/staging-health.ts @@ -0,0 +1,27 @@ +const STAGING_API_ORIGIN = "https://api-staging.facturxapi.com"; +const STAGING_WEB_ORIGIN = "https://staging.facturxapi.com"; +const RELEASE_HEADER = "x-canonical-api-release-id"; + +export async function attestStagingApi( + env: { FACTURX_BASE_URL: string; WEB_ORIGIN?: string }, + fetchImpl: typeof fetch = fetch, +): Promise { + const headers = new Headers({ "Cache-Control": "no-store" }); + const unavailable = () => new Response(null, { status: 503, headers }); + if (env.FACTURX_BASE_URL !== STAGING_API_ORIGIN || env.WEB_ORIGIN !== STAGING_WEB_ORIGIN) { + return unavailable(); + } + + try { + const response = await fetchImpl(new URL("/meta", STAGING_API_ORIGIN), { + redirect: "error", + signal: AbortSignal.timeout(5000), + }); + const release = response.headers.get(RELEASE_HEADER); + if (!response.ok || !release || !/^[0-9a-f]{40}$/.test(release)) return unavailable(); + headers.set(RELEASE_HEADER, release); + return new Response(null, { status: 204, headers }); + } catch { + return unavailable(); + } +} diff --git a/workers/mcp/test/staging-health.test.ts b/workers/mcp/test/staging-health.test.ts new file mode 100644 index 0000000..5fd7766 --- /dev/null +++ b/workers/mcp/test/staging-health.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { attestStagingApi } from "../src/staging-health.ts"; + +const staging = { + FACTURX_BASE_URL: "https://api-staging.facturxapi.com", + WEB_ORIGIN: "https://staging.facturxapi.com", +}; +const releaseHeader = "x-canonical-api-release-id"; +const revision = "e95e2be4d01f3ab2a7861de35de518bb0a9760b1"; + +test("staging attestation relays the exact release from the staging API without credentials", async () => { + const respond: typeof fetch = async (target, options) => { + assert.equal(String(target), "https://api-staging.facturxapi.com/meta"); + assert.equal(options?.redirect, "error"); + assert.equal(options?.headers, undefined); + return new Response(null, { headers: { [releaseHeader]: revision } }); + }; + const response = await attestStagingApi(staging, respond); + assert.equal(response.status, 204); + assert.equal(response.headers.get(releaseHeader), revision); + assert.equal(response.headers.get("Cache-Control"), "no-store"); +}); + +test("staging attestation refuses a mismatched origin without contacting it", async () => { + let fetched = false; + const respond: typeof fetch = async () => { + fetched = true; + return new Response(); + }; + for (const override of [ + { FACTURX_BASE_URL: "https://api.facturxapi.com" }, + { WEB_ORIGIN: "https://facturxapi.com" }, + ]) { + const response = await attestStagingApi({ ...staging, ...override }, respond); + assert.equal(response.status, 503); + assert.equal(response.headers.has(releaseHeader), false); + } + assert.equal(fetched, false); +}); + +test("staging attestation refuses a missing or invalid upstream release", async () => { + for (const value of [null, "unknown", "e95e2be4"]) { + const respond: typeof fetch = async () => new Response(null, { + headers: value ? { [releaseHeader]: value } : {}, + }); + const response = await attestStagingApi(staging, respond); + assert.equal(response.status, 503); + assert.equal(response.headers.has(releaseHeader), false); + } +}); diff --git a/workers/mcp/wrangler.jsonc b/workers/mcp/wrangler.jsonc index fdbc240..92db97d 100644 --- a/workers/mcp/wrangler.jsonc +++ b/workers/mcp/wrangler.jsonc @@ -1,6 +1,6 @@ { - // DO NOT DEPLOY from a branch or a pull request. The Worker is deployed only at a FacturX - // release, after review, with two PROD GO. + // Production is deployed only at a FacturX release, after review and two PROD GO. + // Deploy the reviewed staging configuration explicitly with --env staging. // Never put a secret here. Secrets are set with `wrangler secret put`: // AGENT_CONNECT_SIGNING_SECRET, AGENT_CONNECT_EXCHANGE_SECRET (account connect, both ≥ 32 chars, // same values as the API's AGENT_CONNECT_SIGNING_SECRET and current AGENT_CONNECT_EXCHANGE_SECRETS). @@ -19,5 +19,25 @@ "vars": { "FACTURX_BASE_URL": "https://api.facturxapi.com", "WEB_ORIGIN": "https://facturxapi.com" + }, + "env": { + "staging": { + "name": "facturx-mcp-staging", + "workers_dev": false, + "routes": [{ "pattern": "mcp-staging.facturxapi.com", "custom_domain": true }], + "kv_namespaces": [ + { + "binding": "OAUTH_KV", + // Namespace reserved for staging OAuth. + "id": "d9f6eed43672497d9c249fd41621899f" + } + ], + "vars": { + "PUBLIC_BASE_URL": "https://mcp-staging.facturxapi.com", + "FACTURX_BASE_URL": "https://api-staging.facturxapi.com", + "WEB_ORIGIN": "https://staging.facturxapi.com", + "STAGING_API_ATTESTATION": "enabled" + } + } } } From aca518fea092ac96cafc88e67301fa09be7d083e Mon Sep 17 00:00:00 2001 From: Niass Date: Wed, 2 Dec 2026 15:11:22 +0100 Subject: [PATCH 2/2] Fix staging MCP health redirect handling on Workers --- workers/mcp/src/staging-health.ts | 2 +- workers/mcp/test/staging-health.test.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/workers/mcp/src/staging-health.ts b/workers/mcp/src/staging-health.ts index d59b7ed..153bec4 100644 --- a/workers/mcp/src/staging-health.ts +++ b/workers/mcp/src/staging-health.ts @@ -14,7 +14,7 @@ export async function attestStagingApi( try { const response = await fetchImpl(new URL("/meta", STAGING_API_ORIGIN), { - redirect: "error", + redirect: "manual", signal: AbortSignal.timeout(5000), }); const release = response.headers.get(RELEASE_HEADER); diff --git a/workers/mcp/test/staging-health.test.ts b/workers/mcp/test/staging-health.test.ts index 5fd7766..379d93f 100644 --- a/workers/mcp/test/staging-health.test.ts +++ b/workers/mcp/test/staging-health.test.ts @@ -12,7 +12,7 @@ const revision = "e95e2be4d01f3ab2a7861de35de518bb0a9760b1"; test("staging attestation relays the exact release from the staging API without credentials", async () => { const respond: typeof fetch = async (target, options) => { assert.equal(String(target), "https://api-staging.facturxapi.com/meta"); - assert.equal(options?.redirect, "error"); + assert.equal(options?.redirect, "manual"); assert.equal(options?.headers, undefined); return new Response(null, { headers: { [releaseHeader]: revision } }); }; @@ -49,3 +49,16 @@ test("staging attestation refuses a missing or invalid upstream release", async assert.equal(response.headers.has(releaseHeader), false); } }); + +test("staging attestation refuses a redirect even if it carries a valid release", async () => { + const respond: typeof fetch = async () => new Response(null, { + status: 302, + headers: { + Location: "https://api.facturxapi.com/meta", + [releaseHeader]: revision, + }, + }); + const response = await attestStagingApi(staging, respond); + assert.equal(response.status, 503); + assert.equal(response.headers.has(releaseHeader), false); +});