Skip to content
Merged
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
6 changes: 4 additions & 2 deletions .github/workflows/desktop-commander-worker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@ jobs:
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
GITHUB_SHA: ${{ github.sha }}
run: node upload-version.mjs --dry-run
- name: Stage bridge-door secrets file
- name: Stage Worker secrets file
env:
BRIDGE_ACCESS_CLIENT_ID: ${{ secrets.BRIDGE_ACCESS_CLIENT_ID }}
BRIDGE_ACCESS_CLIENT_SECRET: ${{ secrets.BRIDGE_ACCESS_CLIENT_SECRET }}
MACHINE_BEARER: ${{ secrets.MACHINE_BEARER }}
run: |
umask 077
python3 - "$RUNNER_TEMP/desktop-commander-secrets.json" <<'PY'
import json, os, sys
data = {
"ACCESS_CLIENT_ID": os.environ["BRIDGE_ACCESS_CLIENT_ID"],
"ACCESS_CLIENT_SECRET": os.environ["BRIDGE_ACCESS_CLIENT_SECRET"],
"MACHINE_BEARER": os.environ["MACHINE_BEARER"],
}
if not all(data.values()):
raise SystemExit("bridge Access secrets are empty")
raise SystemExit("Worker secrets are empty")
with open(sys.argv[1], "w") as f:
json.dump(data, f)
PY
Expand Down
70 changes: 53 additions & 17 deletions pkgs/desktop-commander/worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,67 @@ whole path inside the fleet's own Cloudflare account and hardware.
```
AI client
-> POST https://commander.security.cab/mcp
Access app "desktop-commander": owner Google identity (the IdP
wired under #12412 with the client saved under #12417), or the
"desktop-commander" service token for machine clients.
-> this Worker (src/index.ts): refuses requests without an Access
JWT assertion, then proxies the streamable-HTTP MCP request,
swapping the caller's Access material for its own egress token.
Guarded by this Worker's OAuth 2.1 authorization server
(src/oauth.ts). Hosted clients (Claude, ChatGPT) connect from
their own servers and cannot pass a Cloudflare Access login, so
/mcp takes a Bearer access token instead — or the machine bearer
for the owner's own tooling.
-> this Worker (src/index.ts): checks the Bearer, then proxies the
streamable-HTTP MCP request, swapping the caller's credentials
for its own bridge egress token.
-> https://commander-link.security.cab/mcp
Access app "desktop-commander link (bridge)": service-token-only
policy (the atlas-search pattern).
policy (the atlas-search pattern). This door did not change.
-> sandbox-link Cloudflare Tunnel -> http://localhost:8931 on amd
-> supergateway (systemd user unit desktop-commander-bridge,
pkgs/supergateway) in streamable-HTTP mode
-> the packaged Desktop Commander (pkgs/desktop-commander, stdio)
```

`workers_dev` is off; `commander.security.cab` (a Worker custom
domain) is the only public door.
`workers_dev` is off; `commander.security.cab` (a Worker route) is
the only public door.

## OAuth front door (src/oauth.ts)

Standard MCP authorization shape, so Claude and ChatGPT can connect:

- `GET /.well-known/oauth-protected-resource` (and `.../mcp`) and
`GET /.well-known/oauth-authorization-server` — public discovery.
- `POST /register` — dynamic client registration (RFC 7591); public
clients only (`token_endpoint_auth_method: "none"`), redirect URIs
must be https or loopback http. Clients live in the `OAUTH_KV`
namespace under `client:<id>`.
- `GET /authorize` — the approval page, and the ONE path still behind
Cloudflare Access: the "desktop-commander" Access app is scoped to
`commander.security.cab/authorize` with the owner-email policy only.
The Worker also verifies the Access JWT itself (RS256 against the
team JWKS, audience, owner email) before rendering. Approving
issues a single-use code (10 min, PKCE S256 bound) via the form's
POST back to `/authorize`.
- `POST /token` — authorization-code exchange (PKCE verified, code
consumed on any attempt) issuing an opaque access token (1 h) and
a rotating refresh token (30 d). Codes and tokens are stored in KV
under their SHA-256 (`code:<hash>`, `at:<hash>`, `rt:<hash>`),
never in the clear. Revoke a client by deleting its `at:`/`rt:`
entries (and its `client:` record) from the namespace.
- `/mcp` without a valid Bearer answers 401 with
`WWW-Authenticate: Bearer resource_metadata="https://commander.security.cab/.well-known/oauth-protected-resource"`,
which is what sends hosted clients into the OAuth flow.

The public Access service token that used to guard the whole hostname
is retired at this door (the token still exists; nothing accepts it
here anymore). The bridge door's service token is unchanged.

## Secrets — by name only, never values

| Where | Name |
| --- | --- |
| fleet secrets (Bitwarden) | `cloudflare.access.desktop-commander-link-client-id` / `-client-secret` — bridge-door token the Worker presents |
| fleet secrets (Bitwarden) | `cloudflare.access.desktop-commander-client-id` / `-client-secret` — public-door token for machine MCP clients |
| fleet secrets (Bitwarden) | `cloudflare.access.desktop-commander-client-id` / `-client-secret` — the retired public-door token (kept, unused here) |
| fleet secrets (Bitwarden) | `cloudflare.api-token-desktop-commander-worker` — scoped deploy token |
| GitHub environment `desktop-commander-production` | `DESKTOP_COMMANDER_WORKER_CF_TOKEN` (same deploy token), `BRIDGE_ACCESS_CLIENT_ID`, `BRIDGE_ACCESS_CLIENT_SECRET` |
| Worker version secrets | `ACCESS_CLIENT_ID`, `ACCESS_CLIENT_SECRET` — uploaded per version by the publish workflow from the environment secrets |
| fleet secrets (Bitwarden) | `desktop-commander.machine-bearer` — the /mcp machine bearer |
| GitHub environment `desktop-commander-production` | `DESKTOP_COMMANDER_WORKER_CF_TOKEN` (same deploy token), `BRIDGE_ACCESS_CLIENT_ID`, `BRIDGE_ACCESS_CLIENT_SECRET`, `MACHINE_BEARER` |
| Worker version secrets | `ACCESS_CLIENT_ID`, `ACCESS_CLIENT_SECRET`, `MACHINE_BEARER` — uploaded per version by the publish workflow from the environment secrets |

## Deploys — cf version deployments via GitHub Actions

Expand All @@ -45,7 +79,7 @@ Modeled on the id-mail publisher/promoter pair:
1. **Publish** (`desktop-commander-worker-publish.yml`, manual dispatch
on main, `confirm_upload`): tests, a credential-free dry run, then
`upload-version.mjs` runs `cf workers versions create` with the
bridge secrets file. Uploading changes no traffic. The run emits a
Worker secrets file. Uploading changes no traffic. The run emits a
provenance artifact naming the exact version, tag, and baseline.
2. **Promote** (`desktop-commander-worker-promote.yml`, manual dispatch
with the publish run id + attempt, `confirm_promotion`):
Expand All @@ -70,8 +104,10 @@ touching this directory.

## Client setup

- Hosted clients (Claude, ChatGPT): add the custom connector
`https://commander.security.cab/mcp`; the client registers itself
and sends the owner through the Access sign-in + approval page on
first connection.
- Header-capable clients (Claude Code, Cursor, mcpx): send
`CF-Access-Client-Id` / `CF-Access-Client-Secret` with the
public-door token values.
- Browser clients: sign in with the owner's Google identity at the
Access prompt.
`Authorization: Bearer <machine bearer>` (fleet secrets:
`desktop-commander.machine-bearer`).
12 changes: 8 additions & 4 deletions pkgs/desktop-commander/worker/cloudflare.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import * as entrypoint from "./src/index.ts" with { type: "cf-worker" };
// desktop-commander Worker — deployment source of truth for the cf
// versions flow (see README.md). Mirrors wrangler.toml, kept in step
// as the human-readable config record. The Worker's secrets
// (ACCESS_CLIENT_ID, ACCESS_CLIENT_SECRET) are never here: fleet
// secrets hold them by name, and a publish uploads them with the new
// version (cf workers versions create --secrets-file) so no version
// ever deploys without its bridge credentials.
// (ACCESS_CLIENT_ID, ACCESS_CLIENT_SECRET, MACHINE_BEARER) are never
// here: fleet secrets hold them by name, and a publish uploads them
// with the new version (cf workers versions create --secrets-file) so
// no version ever deploys without its credentials.
export default defineConfig({
worker: {
name: "desktop-commander",
compatibilityDate: "2025-09-15",
entrypoint,
env: {
BRIDGE_BASE: bindings.text("https://commander-link.security.cab"),
ACCESS_TEAM_DOMAIN: bindings.text("snowy-sun-58a9.cloudflareaccess.com"),
// Audience of the Access app scoped to /authorize.
ACCESS_AUD: bindings.text("c6ddf0a5c0ba8742c0a3c74d12495bf5c4fe9bf6f01f604d62583cb2ed353879"),
OAUTH_KV: bindings.kv({ id: "ea675d3c7d5b4f3b9f1cecae82e57d1b" }),
},
},
});
89 changes: 73 additions & 16 deletions pkgs/desktop-commander/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,45 @@
//
// Shape:
// AI client -> https://commander.security.cab/mcp
// Cloudflare Access app "desktop-commander" authenticates the caller
// first (owner Google identity, or the desktop-commander service
// token for machine clients). Unauthenticated requests never reach
// this Worker.
// Hosted clients (Claude, ChatGPT) connect from their own servers
// and cannot pass a Cloudflare Access login here, so /mcp is
// guarded by this Worker's OAuth 2.1 authorization server
// (src/oauth.ts): a Bearer access token issued through /authorize,
// or the machine bearer for the owner's own tooling. The one path
// still behind Access is /authorize — only the owner's Google
// identity reaches the approval page.
// this Worker -> https://commander-link.security.cab/mcp
// A second Access app guards the bridge door with a service-token-
// only policy; the Worker presents that token (ACCESS_CLIENT_ID /
// ACCESS_CLIENT_SECRET, uploaded as per-version secrets).
// bridge door -> Cloudflare Tunnel -> supergateway on the laptop
// -> the packaged Desktop Commander (stdio) on 127.0.0.1:8931.
//
// The Worker is a transparent streamable-HTTP proxy: method, path,
// The /mcp proxy is a transparent streamable-HTTP proxy: method, path,
// query, headers and body pass through, and the response (including
// the Mcp-Session-Id header and any SSE stream) passes back untouched.

export interface Env {
import {
authorizationServerMetadata,
authorizeGet,
authorizePost,
isValidAccessToken,
protectedResourceMetadata,
registerClient,
timingSafeEqual,
tokenEndpoint,
type OAuthEnv,
} from "./oauth";

export interface Env extends OAuthEnv {
BRIDGE_BASE: string;
ACCESS_CLIENT_ID: string;
ACCESS_CLIENT_SECRET: string;
// Bearer for the owner's machine clients (mcpx, curl): a Worker
// secret staged by the publish workflow from the GitHub environment
// secret of the same name; canonical value in fleet secrets as
// desktop-commander.machine-bearer.
MACHINE_BEARER: string;
}

function json(body: unknown, status = 200): Response {
Expand All @@ -31,23 +51,59 @@ function json(body: unknown, status = 200): Response {
});
}

async function isAuthorizedMcpCaller(request: Request, env: Env): Promise<boolean> {
const authorization = request.headers.get("authorization");
if (!authorization?.startsWith("Bearer ")) return false;
const token = authorization.slice("Bearer ".length).trim();
if (!token) return false;
if (await isValidAccessToken(env, token)) return true;
const machineBearer = (env.MACHINE_BEARER ?? "").trim();
return machineBearer.length > 0 && timingSafeEqual(token, machineBearer);
}

export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
const path = url.pathname;

if (url.pathname === "/healthz") {
if (path === "/healthz") {
return json({ ok: true, service: "desktop-commander" });
}
if (url.pathname !== "/mcp") {

// OAuth authorization-server surface (src/oauth.ts).
if (path === "/.well-known/oauth-protected-resource" || path === "/.well-known/oauth-protected-resource/mcp") {
return protectedResourceMetadata();
}
if (path === "/.well-known/oauth-authorization-server") {
return authorizationServerMetadata();
}
if (path === "/register") {
if (request.method !== "POST") return json({ error: "method not allowed" }, 405);
return registerClient(request, env);
}
if (path === "/authorize") {
if (request.method === "GET") return authorizeGet(request, env);
if (request.method === "POST") return authorizePost(request, env);
return json({ error: "method not allowed" }, 405);
}
if (path === "/token") {
if (request.method !== "POST") return json({ error: "method not allowed" }, 405);
return tokenEndpoint(request, env);
}

if (path !== "/mcp") {
return json({ error: "not found" }, 404);
}

// Defense in depth: Access injects this header only after a caller
// passes one of the app's policies. Its absence means the request
// did not come through the gate (a stray route or a future
// workers.dev re-enable), so refuse before touching the bridge.
if (!request.headers.get("Cf-Access-Jwt-Assertion")) {
return json({ error: "missing Cloudflare Access assertion" }, 401);
if (!(await isAuthorizedMcpCaller(request, env))) {
return new Response(JSON.stringify({ error: "unauthorized" }), {
status: 401,
headers: {
"content-type": "application/json",
"WWW-Authenticate":
'Bearer resource_metadata="https://commander.security.cab/.well-known/oauth-protected-resource"',
},
});
}
if (!env.ACCESS_CLIENT_ID || !env.ACCESS_CLIENT_SECRET) {
return json({ error: "bridge credentials are not configured" }, 500);
Expand All @@ -56,8 +112,9 @@ export default {
const target = new URL(url.pathname + url.search, env.BRIDGE_BASE);
const headers = new Headers(request.headers);
headers.delete("host");
// The caller's own Access material is for the public door only;
// the bridge door gets the Worker's egress token instead.
// The caller's own credentials are for the public door only; the
// bridge door gets the Worker's egress token instead.
headers.delete("authorization");
headers.delete("cf-access-jwt-assertion");
headers.delete("cf-access-client-id");
headers.delete("cf-access-client-secret");
Expand Down
Loading