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
15 changes: 15 additions & 0 deletions .github/workflows/SECRETS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ This document lists all secrets required to deploy MCPs via GitHub Actions.
> the caller and cannot be rotated per tenant. Per-tenant identity requires
> verifying the `x-mesh-token` signature, tracked separately.

### `OAUTH_STATE_SECRET` (per MCP, not a repository secret)
- **Used by**: every MCP whose `withRuntime` has an `oauth` config (the
runtime's `/authorize` → `/oauth/callback` → `/token` broker), via
`@decocms/mcps-shared/oauth-broker` (included by `createGoogleOAuth`).
- **Description**: AES-GCM key material that seals the OAuth `state` and the
`code` handed to Studio. The same helper pins the downstream `redirect_uri`
to `decocms.com` (plus `EXTRA_ALLOWED_REDIRECT_HOSTS`, comma-separated, for
self-hosted Studio). An MCP without it refuses to build its runtime.
- **How to obtain**: `openssl rand -hex 32` (minimum 32 characters), one
distinct value per MCP. Rotating it only invalidates in-flight logins.
- **Where to provision**:
- `kubernetes-bun` MCPs: add `OAUTH_STATE_SECRET` to the site's state secret
- Cloudflare Workers MCPs: `cd <mcp> && bunx wrangler secret put OAUTH_STATE_SECRET`

## Optional Secrets (per MCP)

### MCP: `sora`
Expand Down Expand Up @@ -121,6 +135,7 @@ bunx wrangler secret bulk .secrets.json
Required keys in `.secrets.json`:
- `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET` — Google OAuth credentials
for the project (https://console.cloud.google.com/apis/credentials)
- `OAUTH_STATE_SECRET` — seals OAuth state/code (see above)
- `GMAIL_PUBSUB_TOPIC` — fully-qualified Pub/Sub topic, e.g.
`projects/<project>/topics/gmail-push`. The Gmail API publishes
mailbox-change events here. Grant the system service account
Expand Down
4 changes: 2 additions & 2 deletions airtable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"check": "tsc --noEmit"
},
"dependencies": {
"@decocms/runtime": "1.2.8",
"zod": "^4.0.0"
"@decocms/runtime": "^3.1.0",
"zod": "4.3.6"
},
"devDependencies": {
"@decocms/mcps-shared": "1.0.0",
Expand Down
2 changes: 2 additions & 0 deletions airtable/server/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type DefaultEnv, withRuntime } from "@decocms/runtime";
import type { Registry } from "@decocms/mcps-shared/registry";
import { serve } from "@decocms/mcps-shared/serve";
import { oauthBrokerHardening } from "@decocms/mcps-shared/oauth-broker";
import { z } from "zod";
import { tools } from "./tools/index.ts";
import { AIRTABLE_SCOPES } from "./constants.ts";
Expand All @@ -11,6 +12,7 @@ export type Env = DefaultEnv<typeof StateSchema, Registry>;

const runtime = withRuntime<Env, typeof StateSchema>({
oauth: {
...oauthBrokerHardening(),
mode: "PKCE",
authorizationServer: "https://airtable.com",
authorizationUrl: (callbackUrl) => {
Expand Down
356 changes: 211 additions & 145 deletions bun.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dropbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Set via `bunx wrangler secret put`:
```bash
DROPBOX_CLIENT_ID=<dropbox-app-key>
DROPBOX_CLIENT_SECRET=<dropbox-app-secret>
OAUTH_STATE_SECRET=<openssl rand -hex 32> # seals OAuth state/code; required
# EXTRA_ALLOWED_REDIRECT_HOSTS=studio.example.com # optional, self-hosted Studio
```

### Running locally
Expand Down
4 changes: 2 additions & 2 deletions dropbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
},
"dependencies": {
"@decocms/bindings": "^1.4.0",
"@decocms/runtime": "^1.6.0",
"@decocms/runtime": "^3.1.0",
"@modelcontextprotocol/sdk": "^1.27.1",
"zod": "^4.0.0"
"zod": "4.3.6"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20251014.0",
Expand Down
167 changes: 94 additions & 73 deletions dropbox/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
*
* Secrets come from wrangler (exposed via process.env under nodejs_compat)
* and are read lazily per-request because they aren't populated at module
* init time on Workers.
* init time on Workers. That includes OAUTH_STATE_SECRET, so the runtime is
* built on the first request: a missing secret answers 500 with a clear
* message instead of serving an unhardened OAuth broker.
*/

import { oauthBrokerHardening } from "@decocms/mcps-shared/oauth-broker";
import type { Registry } from "@decocms/mcps-shared/registry";
import { withRuntime } from "@decocms/runtime";
import {
Expand All @@ -32,84 +35,102 @@ function getOAuthCredentials(): { clientId: string; clientSecret: string } {
return { clientId, clientSecret };
}

const runtime = withRuntime<Env, typeof StateSchema, Registry>({
oauth: {
mode: "PKCE",
authorizationServer: "https://www.dropbox.com",

authorizationUrl: (callbackUrl) => {
const clientId = process.env.DROPBOX_CLIENT_ID || "";
const callbackUrlObj = new URL(callbackUrl);
const state = callbackUrlObj.searchParams.get("state");

// Match the airtable/google pattern: keep the redirect_uri free of state
// and pass state as a separate OAuth param.
callbackUrlObj.searchParams.delete("state");
const redirectUri = callbackUrlObj.toString();

const url = new URL("https://www.dropbox.com/oauth2/authorize");
url.searchParams.set("client_id", clientId);
url.searchParams.set("redirect_uri", redirectUri);
url.searchParams.set("response_type", "code");
url.searchParams.set("token_access_type", "offline");
url.searchParams.set("scope", REQUESTED_SCOPES.join(" "));

if (state) {
url.searchParams.set("state", state);
}

return url.toString();
function buildRuntime() {
return withRuntime<Env, typeof StateSchema, Registry>({
oauth: {
...oauthBrokerHardening(),
mode: "PKCE",
authorizationServer: "https://www.dropbox.com",

authorizationUrl: (callbackUrl) => {
const clientId = process.env.DROPBOX_CLIENT_ID || "";
const callbackUrlObj = new URL(callbackUrl);
const state = callbackUrlObj.searchParams.get("state");

// Match the airtable/google pattern: keep the redirect_uri free of state
// and pass state as a separate OAuth param.
callbackUrlObj.searchParams.delete("state");
const redirectUri = callbackUrlObj.toString();

const url = new URL("https://www.dropbox.com/oauth2/authorize");
url.searchParams.set("client_id", clientId);
url.searchParams.set("redirect_uri", redirectUri);
url.searchParams.set("response_type", "code");
url.searchParams.set("token_access_type", "offline");
url.searchParams.set("scope", REQUESTED_SCOPES.join(" "));

if (state) {
url.searchParams.set("state", state);
}

return url.toString();
},

exchangeCode: async ({ code, code_verifier, redirect_uri }) => {
const { clientId, clientSecret } = getOAuthCredentials();

const tokenResponse = await exchangeCodeForToken({
code,
clientId,
clientSecret,
redirectUri: redirect_uri,
codeVerifier: code_verifier,
});

return {
access_token: tokenResponse.access_token,
token_type: tokenResponse.token_type,
expires_in: tokenResponse.expires_in,
refresh_token: tokenResponse.refresh_token,
scope: tokenResponse.scope || REQUESTED_SCOPES.join(" "),
};
},

refreshToken: async (refreshToken) => {
const { clientId, clientSecret } = getOAuthCredentials();

const tokenResponse = await refreshAccessToken({
refreshToken,
clientId,
clientSecret,
});

return {
access_token: tokenResponse.access_token,
token_type: tokenResponse.token_type,
expires_in: tokenResponse.expires_in,
// Dropbox does not rotate refresh tokens — echo back the original so
// the runtime always has one to return to the client.
refresh_token: tokenResponse.refresh_token ?? refreshToken,
scope: tokenResponse.scope || REQUESTED_SCOPES.join(" "),
};
},
},

exchangeCode: async ({ code, code_verifier, redirect_uri }) => {
const { clientId, clientSecret } = getOAuthCredentials();

const tokenResponse = await exchangeCodeForToken({
code,
clientId,
clientSecret,
redirectUri: redirect_uri,
codeVerifier: code_verifier,
});

return {
access_token: tokenResponse.access_token,
token_type: tokenResponse.token_type,
expires_in: tokenResponse.expires_in,
refresh_token: tokenResponse.refresh_token,
scope: tokenResponse.scope || REQUESTED_SCOPES.join(" "),
};
configuration: {
state: StateSchema,
},

refreshToken: async (refreshToken) => {
const { clientId, clientSecret } = getOAuthCredentials();

const tokenResponse = await refreshAccessToken({
refreshToken,
clientId,
clientSecret,
});

return {
access_token: tokenResponse.access_token,
token_type: tokenResponse.token_type,
expires_in: tokenResponse.expires_in,
// Dropbox does not rotate refresh tokens — echo back the original so
// the runtime always has one to return to the client.
refresh_token: tokenResponse.refresh_token ?? refreshToken,
scope: tokenResponse.scope || REQUESTED_SCOPES.join(" "),
};
},
},
tools,
prompts: [],
});
}

configuration: {
state: StateSchema,
},
let runtime: ReturnType<typeof buildRuntime> | undefined;

tools,
prompts: [],
});
async function handle(req: Request, env: Env, ctx: unknown): Promise<Response> {
try {
runtime ??= buildRuntime();
} catch (error) {
return new Response((error as Error).message, { status: 500 });
}
return runtime.fetch(
req,
env,
ctx as unknown as Parameters<typeof runtime.fetch>[2],
);
}

export default {
fetch: runtime.fetch,
fetch: handle,
};
83 changes: 83 additions & 0 deletions dropbox/server/oauth-broker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Regression: the OAuth broker this worker serves (/authorize,
* /oauth/callback, /token from @decocms/runtime) used to take any
* redirect_uri, carry it in unsigned base64 state, and send the code (which
* holds the Dropbox token) wherever that state pointed.
*/
import { beforeAll, describe, expect, it } from "bun:test";

const ORIGIN = "https://dropbox-mcp.decocms.com";
const STUDIO_CB = "https://studio.decocms.com/oauth/callback";

let fetchWorker: (req: Request) => Promise<Response>;

beforeAll(async () => {
process.env.OAUTH_STATE_SECRET = "t".repeat(64);
process.env.DROPBOX_CLIENT_ID = "test-client";
process.env.DROPBOX_CLIENT_SECRET = "test-secret";
const worker = (await import("./main.ts")).default;
fetchWorker = (req) => worker.fetch(req, {} as never, {} as never);
});

const authorize = (redirectUri: string) =>
fetchWorker(
new Request(
`${ORIGIN}/authorize?${new URLSearchParams({
response_type: "code",
client_id: "mesh",
state: "client-state",
redirect_uri: redirectUri,
})}`,
),
);

describe("dropbox OAuth broker", () => {
it("rejects an attacker redirect_uri without redirecting", async () => {
const res = await authorize("https://attacker.example/cb");
expect(res.status).toBe(400);
expect(res.headers.get("location")).toBeNull();
});

it("rejects a look-alike host", async () => {
const res = await authorize("https://decocms.com.attacker.example/cb");
expect(res.status).toBe(400);
});

it("accepts the Studio callback and seals the state", async () => {
const res = await authorize(STUDIO_CB);
expect(res.status).toBe(302);
const upstream = new URL(res.headers.get("location")!);
expect(upstream.host).toBe("www.dropbox.com");
const state = upstream.searchParams.get("state")!;
expect(state.startsWith("v1.")).toBe(true);
expect(state).not.toContain(STUDIO_CB);
});

it("does not follow a forged unsigned state on /oauth/callback", async () => {
const forged = btoa(
JSON.stringify({ redirectUri: "https://attacker.example/cb" }),
);
const res = await fetchWorker(
new Request(
`${ORIGIN}/oauth/callback?error=access_denied&state=${forged}`,
),
);
expect(res.status).toBe(400);
expect(res.headers.get("location")).toBeNull();
});

it("rejects an unsealed code at /token", async () => {
const forged = btoa(JSON.stringify({ accessToken: "x", tokenType: "B" }));
const res = await fetchWorker(
new Request(`${ORIGIN}/token`, {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
grant_type: "authorization_code",
code: forged,
}),
}),
);
expect(res.status).toBe(400);
});
});
4 changes: 2 additions & 2 deletions github-repo-reports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"build": "bun run build:server"
},
"dependencies": {
"@decocms/runtime": "^1.6.0",
"@decocms/runtime": "^3.1.0",
"@octokit/rest": "^22.0.1",
"yaml": "^2.8.2",
"zod": "^4.0.0"
"zod": "4.3.6"
},
"devDependencies": {
"@decocms/mcps-shared": "1.0.0",
Expand Down
2 changes: 2 additions & 0 deletions github-repo-reports/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import { withRuntime } from "@decocms/runtime";
import { serve } from "@decocms/mcps-shared/serve";
import { oauthBrokerHardening } from "@decocms/mcps-shared/oauth-broker";

import { exchangeCodeForToken } from "./lib/github-client.ts";
import { tools } from "./tools/index.ts";
Expand All @@ -32,6 +33,7 @@ const GITHUB_APP_NAME = process.env.GITHUB_APP_NAME || "decocms-bot";

const runtime = withRuntime<Env, typeof StateSchema>({
oauth: {
...oauthBrokerHardening(),
mode: "PKCE",
authorizationServer: "https://github.com",

Expand Down
4 changes: 2 additions & 2 deletions google-analytics-sa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"publish": "cat app.json | deco registry publish -w /shared/deco -y"
},
"dependencies": {
"@decocms/runtime": "1.2.5",
"@decocms/runtime": "^3.1.0",
"google-analytics": "workspace:*",
"zod": "^4.0.0"
"zod": "4.3.6"
},
"devDependencies": {
"@decocms/mcps-shared": "workspace:*",
Expand Down
Loading
Loading