forked from penpot/penpot
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(mcp): Cognito OAuth gate for multi-user MCP behind mPass #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Moneta fork — Cognito OAuth gate | ||
|
|
||
| This document covers the Pressingly/Moneta fork additions that put the Penpot | ||
| MCP server behind AWS Cognito / mPass SSO, mirroring `surfsense-mcp-server` | ||
| and `plane-mcp-server`. Everything lives in | ||
| `packages/server/src/moneta/`; upstream files carry three one-line hooks | ||
| (`index.ts`, `PenpotMcpServer.ts`, `PluginBridge.ts`). | ||
|
|
||
| ## How the two legs pair | ||
|
|
||
| ``` | ||
| MCP client (Claude/Cursor) Penpot user's browser | ||
| │ │ | ||
| ▼ Bearer (Cognito access token) ▼ _oauth2_proxy cookie | ||
| https://design-mcp.<domain>/mcp wss://design.<domain>/mcp/ws | ||
| │ Traefik: strip-auth-headers only │ Traefik: mpass-auth ForwardAuth | ||
| ▼ ▼ + X-Auth-Request-Email injected | ||
| Cognito gate (this fork) penpot-frontend nginx → :4402 | ||
| │ identity = id_token email │ identity = header email | ||
| └────────────► pair by equality ◄──────────┘ | ||
| (PluginBridge clientsByToken) | ||
| ``` | ||
|
|
||
| - **MCP leg.** `/mcp` is *not* behind mPass. The server is its own OAuth 2.0 | ||
| authorization server (RFC 8414/9728 discovery, RFC 7591 DCR shim, | ||
| `/authorize` + `/token` proxied to Cognito with a two-hop redirect through | ||
| `{MCP_BASE_URL}/auth/callback`), so MCP clients auto-OAuth — no manual | ||
| token paste. Inbound Bearers are Cognito **access tokens**, JWKS-verified | ||
| per request. | ||
| - **Pairing identity.** A Cognito access token has no `email` and an opaque | ||
| UUID `username` for federated users, so identity comes from the **id_token** | ||
| captured at code/refresh exchange (userInfo endpoint as self-healing | ||
| fallback), precedence `email → cognito:username → access-token username` — | ||
| the same order oauth2-proxy uses for `X-Auth-Request-Email` / | ||
| `X-Auth-Request-User`. No identity → fail closed (401). | ||
| - **Plugin leg.** Unchanged for the user: the Penpot plugin connects to | ||
| `wss://design.<domain>/mcp/ws` (the URL penpot's `cfg/mcp-ws-uri` already | ||
| produces), which traverses mPass. The bridge prefers the injected | ||
| `X-Auth-Request-Email` header over the upstream `?userToken=` JWE. | ||
| - The resolved identity is exposed to the untouched upstream handlers as | ||
| `req.query.userToken`, so session bookkeeping and `PluginBridge` pairing | ||
| work exactly as upstream wrote them. Each `mcp-session-id` is additionally | ||
| pinned to the identity that created it. | ||
|
|
||
| In Cognito mode multi-user mode is forced on (`index.ts` hook), so the | ||
| single-user "any connected plugin" fallback can never cross users. The | ||
| upstream `/mcp/stream?userToken=<JWE>` path effectively retires in this mode | ||
| (requests without a Bearer get 401); MCP clients use | ||
| `https://design-mcp.<domain>/mcp` instead of the URL shown in Penpot's UI. | ||
|
|
||
| ## Environment | ||
|
|
||
| | Variable | Purpose | | ||
| |---|---| | ||
| | `COGNITO_USER_POOL_ID` | Enables Cognito mode (its presence is the switch). | | ||
| | `COGNITO_AWS_REGION` / `AWS_REGION` | Cognito region. | | ||
| | `OIDC_CLIENT_ID` | Pre-registered Cognito app client (shared with the other MCPs). | | ||
| | `OIDC_CLIENT_SECRET` | Optional — omit for public/PKCE clients. | | ||
| | `MCP_BASE_URL` | Public URL of this server, e.g. `https://design-mcp.<domain>`. | | ||
| | `MCP_ALLOWED_ORIGINS` | CSV CORS allow-list, or `*` (dev/Inspector). | | ||
| | `MCP_ALLOWED_CLIENT_REDIRECT_URIS` | CSV allow-list for DCR redirect URIs; unset = allow all. | | ||
| | `MCP_OAUTH_STORAGE_URL` | `redis://valkey:6379/13` — encrypted OAuth state (AES-256-GCM, key HKDF-derived from `OIDC_CLIENT_SECRET` or `MCP_JWT_SIGNING_KEY`). Unset = in-memory. | | ||
| | `MCP_JWT_SIGNING_KEY` | Storage-encryption key material when there is no client secret. | | ||
| | `MCP_ENV` | `production` logs a warning when storage is in-memory. | | ||
|
|
||
| Valkey DB allocation: 11 = surfsense-mcp, 12 = plane-mcp, **13 = penpot-mcp**. | ||
|
|
||
| ## One-time IdP step | ||
|
|
||
| Add `https://design-mcp.<domain>/auth/callback` to the Cognito app client's | ||
| allowed callback URLs (same app client as the sibling MCPs — no new client). | ||
|
|
||
| ## Devstack | ||
|
|
||
| Service `penpot-mcp` in `foss-server-bundle/docker-compose.dev.yml` (profiles | ||
| `penpot-mcp` / `mcp`): Traefik router `design-mcp.<domain>` → | ||
| `penpot-mcp:4401` with `strip-auth-headers` only, healthcheck on `/healthz`. | ||
| Rebuild with `make dev.build.penpot.mcp`. Remember `PENPOT_MCP_URI` / | ||
| `PENPOT_MCP_URI_WS` in `.env` so penpot-frontend's nginx proxies `/mcp/ws` to | ||
| this server (the plugin leg). | ||
|
|
||
| Local smoke test without Cognito reachability: start with a fake pool id and | ||
| probe `GET /healthz`, `GET /.well-known/oauth-authorization-server`, | ||
| `POST /register`, and confirm `/mcp` answers 401 with a `WWW-Authenticate` | ||
| challenge. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * Moneta fork — plugin-side identity for the WebSocket bridge. | ||
| * | ||
| * In the devstack the plugin connects from the user's browser to | ||
| * wss://design.<domain>/mcp/ws, which traverses Traefik's mpass-auth | ||
| * ForwardAuth: oauth2-proxy validates the shared SSO cookie and Traefik | ||
| * injects X-Auth-Request-Email / X-Auth-Request-User into the upgrade request | ||
| * before nginx proxies it to this server. Reading those headers gives the | ||
| * plugin connection the same identity string that the Cognito gate derives | ||
| * for MCP sessions, so the two sides pair by simple equality. | ||
| * | ||
| * Precedence mirrors cognito.pickPairingIdentity exactly: a *usable* email | ||
| * first (see cognito.usablePairingEmail — in the Moneta pool this header | ||
| * carries the bare askii user id, the join key with the MCP leg's native | ||
| * twin identity), then X-Auth-Request-User, which oauth2-proxy fills from | ||
| * the `cognito:username` claim (OAUTH2_PROXY_USER_ID_CLAIM). | ||
| * | ||
| * Trust: external requests can never carry forged headers — Traefik's | ||
| * strip-auth-headers middleware removes inbound X-Auth-Request-* on every | ||
| * router before mpass-auth re-adds them. Inside the docker network the | ||
| * boundary is the network itself, the same model the sibling MCP servers use | ||
| * for their identity-header paths. | ||
| * | ||
| * Returns null when Cognito mode is off or no header is present, in which | ||
| * case the caller falls back to the upstream ?userToken= pairing untouched. | ||
| */ | ||
|
|
||
| import type * as http from "node:http"; | ||
| import { createLogger } from "../logger"; | ||
| import { usablePairingEmail } from "./cognito"; | ||
| import { monetaAuthEnabled } from "./config"; | ||
|
|
||
| const logger = createLogger("moneta.bridge"); | ||
|
|
||
| function headerValue(request: http.IncomingMessage, name: string): string | null { | ||
| const raw = request.headers[name]; | ||
| const value = Array.isArray(raw) ? raw[0] : raw; | ||
| const trimmed = value?.trim(); | ||
| return trimmed ? trimmed : null; | ||
| } | ||
|
|
||
| /** Short non-reversible marker for log correlation — never the full identifier. */ | ||
| function fingerprint(value: string | null): string { | ||
| return value ? `${value.slice(0, 8)}…` : "<unset>"; | ||
| } | ||
|
|
||
| export function monetaIdentityFromUpgrade(request: http.IncomingMessage): string | null { | ||
| if (!monetaAuthEnabled()) { | ||
| return null; | ||
| } | ||
| const emailHeader = headerValue(request, "x-auth-request-email"); | ||
| const userHeader = headerValue(request, "x-auth-request-user"); | ||
| const identity = usablePairingEmail(emailHeader) ?? userHeader; | ||
| // Fingerprints only at info — full identifiers would leak PII into | ||
| // production logs. Raw header values are available at debug for pairing | ||
| // diagnosis (the dev overlay runs with PENPOT_MCP_LOG_LEVEL=debug). | ||
| logger.info( | ||
| "Plugin connection identity: %s (email header=%s, user header=%s)", | ||
| identity ? fingerprint(identity) : "<none — falling back to ?userToken>", | ||
| fingerprint(emailHeader), | ||
| fingerprint(userHeader) | ||
| ); | ||
| logger.debug( | ||
| "Plugin connection identity (full): %s (email header=%s, user header=%s)", | ||
| identity ?? "<none>", | ||
| emailHeader ?? "<unset>", | ||
| userHeader ?? "<unset>" | ||
| ); | ||
| return identity; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.