Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions workers/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -86,6 +88,9 @@ function providerFor(request: Request, env: Env): OAuthProvider<Env> {

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
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);
},
};
27 changes: 27 additions & 0 deletions workers/mcp/src/staging-health.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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: "manual",
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();
}
}
64 changes: 64 additions & 0 deletions workers/mcp/test/staging-health.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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, "manual");
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);
}
});

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);
});
24 changes: 22 additions & 2 deletions workers/mcp/wrangler.jsonc
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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"
}
}
}
}