mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 12:50:37 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work.
> - Apps and Connections let operators connect external providers
through remote MCP servers and OAuth.
> - Slack's curated `mcp-oauth` method pins Slack's bot install
endpoints (`oauth/v2/authorize` and `oauth.v2.access`) while sending
user-only scopes (`search:read.public`, `search:read.private`, etc.) in
`scope`, so Slack rejects sign-in with `"Invalid permissions
requested"`.
> - In addition, when OAuth fails or returns an error for a
customer-owned OAuth app like Slack, `ConnectionSetupFlow`'s `KeyStep`
drops `oauthError` completely, hiding the failure banner, and
`OAuthClientFields` hides the method's `guidanceMd` prerequisites.
> - This pull request switches Slack's `mcp-oauth` method to Slack's
documented MCP user OAuth endpoints (`oauth/v2_user/authorize` and
`oauth.v2.user.access`), propagates allowlisted RFC OAuth
`providerError` codes without reflecting untrusted provider prose,
displays OAuth errors and popup fallback links on `KeyStep`, and renders
setup prerequisites (`guidanceMd`) beside customer OAuth fields.
> - The benefit is that customer-owned Slack MCP sign-in succeeds
(including when resuming pre-fix drafts) and any OAuth error or Slack
MCP prerequisite is clearly shown to the user.
## Linked Issues or Issue Description
Fixes: #13935
Refs #17258
Refs #14037
Builds on #17258 (`fix/slack-mcp-user-oauth-flow`) and adds the missing
customer-OAuth error surface and setup guidance in
`ConnectionSetupFlow`.
## What Changed
- Updated `packages/shared/src/app-definitions/slack.json` and
`scripts/ingest-app-definitions.mjs` so Slack's `mcp-oauth` method uses
`https://slack.com/oauth/v2_user/authorize` and
`https://slack.com/api/oauth.v2.user.access`, and added Slack MCP
prerequisites (`User Token Scopes`, callback URL, and `Model Context
Protocol`) to `guidanceMd`.
- Updated `server/src/routes/tool-access.ts` to propagate allowlisted
RFC `providerError` codes (`normalizeOAuthProviderError`, excluding
`unrecognized`) via `provider_error` on OAuth recovery redirects and
connection-intent postMessages, while continuing to never read or
reflect untrusted `error_description` or `error_uri`.
- Updated `ui/src/features/connections/ConnectionSetupFlow.tsx` and
`ui/src/lib/oauthWindowOutcome.ts` so:
- `KeyStep` renders `oauthError` (not only `connectMutation.error`) and
the `"Open sign-in in a new tab"` fallback link when popups are blocked.
- `oauthCallbackErrorMessage` and `oauthFailureMessage` map allowlisted
`providerError` values (such as `invalid_scope`, `invalid_redirect_uri`,
`invalid_client`) and Paperclip failure codes (including
`slack_mcp_access_disabled`) to actionable Paperclip-authored copy.
- Opener popup polling reads same-origin failure `code` and
`provider_error` details from the returned callback URL.
- `OAuthClientFields` renders `method.guidanceMd` for required
customer-owned OAuth apps whose client secret is optional.
- Updated `tests/runner-e2e/source-sdk-public-inputs.json` for the
changed `@paperclipai/shared` sources.
- Updated Slack OAuth test fixtures in
`server/src/__tests__/tool-access-service.test.ts` and
`server/src/services/tool-access/oauth/client-settings.test.ts`, and
added regressions in `packages/shared/src/app-definitions.test.ts`,
`server/src/__tests__/tool-access-service.test.ts`,
`ui/src/features/connections/ConnectionSetupFlow.oauth.test.ts`, and
`ui/src/features/connections/ConnectionSetupFlow.slack-oauth.test.tsx`.
## Verification
- Shared, UI, and OAuth client-settings unit + DOM tests (95/95 passed):
- `pnpm exec vitest run packages/shared/src/app-definitions.test.ts
ui/src/features/connections/ConnectionSetupFlow.oauth.test.ts
ui/src/features/connections/ConnectionSetupFlow.slack-oauth.test.tsx
ui/src/features/connections/ConnectionSetupFlow.architecture.test.ts
server/src/services/tool-access/oauth/client-settings.test.ts`
- Embedded-Postgres server OAuth start + legacy-draft upgrade + callback
error redirect + redaction tests:
- `pnpm exec vitest run server/src/__tests__/tool-access-service.test.ts
-t "uses Slack user OAuth endpoints even for legacy drafts|starts OAuth
from|refreshes|personal|slack"` (33 passed)
- `pnpm exec vitest run
server/src/__tests__/generic-mcp-connection.test.ts -t "returns browser
denials|denied and cancelled callbacks|redacts"` (9 passed)
- Typechecks & guardrails:
- `pnpm --filter @paperclipai/shared typecheck && pnpm --filter
@paperclipai/server typecheck && pnpm --filter @paperclipai/ui
typecheck` (all passed)
- `node scripts/check-test-budget.mjs --files
packages/shared/src/app-definitions.test.ts
server/src/__tests__/tool-access-service.test.ts
server/src/services/tool-access/oauth/client-settings.test.ts
ui/src/features/connections/ConnectionSetupFlow.oauth.test.ts
ui/src/features/connections/ConnectionSetupFlow.slack-oauth.test.tsx`
(PASS)
- `pnpm verify --base 2d06567795
--explain` (exit 2 due to `scripts/ingest-app-definitions.mjs` being a
global CI input; `tests/runner-e2e/source-sdk-public-inputs.json`
verified in sync; sharded PR CI supplies broad repo coverage).
## Risks
- Low risk. The endpoint change applies only to Slack's `mcp-oauth` tool
method; Slack chat-channel setup and Slack search OAuth retain their
existing bot/user flows.
- Resuming a pre-fix Slack MCP draft automatically re-resolves the
updated `v2_user` endpoints from the catalog definition during
`startOAuth` and overwrites the stored bot URLs.
- Only allowlisted RFC OAuth error codes (`normalizeOAuthProviderError`)
are forwarded in `provider_error`; untrusted provider
`error_description` and `error_uri` values are never read or echoed.
## Model Used
- Claude Opus 4.6 by Anthropic (in Antigravity with tool use, shell
execution, and file editing). Used for root-cause analysis, test-driven
implementation, and local verification.
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
151 lines
7.4 KiB
TypeScript
151 lines
7.4 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import type { toolConnections } from "@paperclipai/db";
|
|
import { APP_DEFINITIONS } from "@paperclipai/shared";
|
|
import { configuredOAuthClientForConnection } from "./client-settings.js";
|
|
|
|
type Connection = typeof toolConnections.$inferSelect;
|
|
|
|
function connection(config: Record<string, unknown>): Connection {
|
|
return {
|
|
id: "connection-1",
|
|
companyId: "company-1",
|
|
transport: "mcp_remote",
|
|
config,
|
|
transportConfig: {},
|
|
credentialSecretRefs: [],
|
|
} as unknown as Connection;
|
|
}
|
|
|
|
const slack = (oauth: Record<string, unknown> = {}) =>
|
|
connection({ sourceTemplateKey: "slack", connectionMethodKey: "mcp-oauth", oauth });
|
|
const generic = (oauth: Record<string, unknown> = {}) =>
|
|
connection({ url: "https://mcp.example.test/mcp", oauth });
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
describe("instance OAuth clients", () => {
|
|
it("uses the instance client only at the token endpoint its app definition fixes", () => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_ID", "instance-slack-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_SECRET", "instance-slack-secret");
|
|
|
|
expect(configuredOAuthClientForConnection(slack(), "slack", "https://slack.com/api/oauth.v2.user.access")).toMatchObject({
|
|
clientId: "instance-slack-id",
|
|
clientSecret: "instance-slack-secret",
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
["a different host", "https://attacker.example/token"],
|
|
["a lookalike path on the vendor host", "https://slack.com/api/oauth.v2.user.access/../../collect"],
|
|
["plain http on the vendor host", "http://slack.com/api/oauth.v2.user.access"],
|
|
["a vendor subdomain", "https://evil.slack.com/api/oauth.v2.user.access"],
|
|
["no token endpoint", null],
|
|
])("never returns the instance secret for %s", (_label, tokenUrl) => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_ID", "instance-slack-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_SECRET", "instance-slack-secret");
|
|
|
|
const client = configuredOAuthClientForConnection(slack({ tokenUrl }), "slack", tokenUrl);
|
|
expect(client.clientId).toBeNull();
|
|
expect(client.clientSecret).toBeNull();
|
|
});
|
|
|
|
it("never gives a custom connection the instance client of a provider it names", () => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_ID", "instance-slack-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_SECRET", "instance-slack-secret");
|
|
|
|
const client = configuredOAuthClientForConnection(
|
|
generic({ provider: "slack", tokenUrl: "https://attacker.example/token" }),
|
|
"slack",
|
|
"https://attacker.example/token",
|
|
);
|
|
expect(client.clientId).toBeNull();
|
|
expect(client.clientSecret).toBeNull();
|
|
});
|
|
|
|
it("never gives a custom connection the unsuffixed instance client", () => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_CLIENT_ID", "instance-generic-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_CLIENT_SECRET", "instance-generic-secret");
|
|
|
|
const client = configuredOAuthClientForConnection(generic(), "mcp_example_test", "https://mcp.example.test/token");
|
|
expect(client.clientId).toBeNull();
|
|
expect(client.clientSecret).toBeNull();
|
|
});
|
|
|
|
it("never gives a custom server a host-named instance client, even at that host", () => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_MCP_EXAMPLE_TEST_CLIENT_ID", "instance-host-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_MCP_EXAMPLE_TEST_CLIENT_SECRET", "instance-host-secret");
|
|
|
|
expect(configuredOAuthClientForConnection(generic(), "mcp_example_test", "https://mcp.example.test/oauth/token").clientId).toBeNull();
|
|
const lookalike = connection({ url: "https://mcp-example.test/mcp", oauth: {} });
|
|
expect(configuredOAuthClientForConnection(lookalike, "mcp_example_test", "https://mcp-example.test/oauth/token").clientId).toBeNull();
|
|
});
|
|
|
|
it.each(["", " ", " slack"])("never treats the gallery marker %j as an app definition", (marker) => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_CLIENT_ID", "instance-generic-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_CLIENT_SECRET", "instance-generic-secret");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_ID", "instance-slack-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_SLACK_CLIENT_SECRET", "instance-slack-secret");
|
|
const marked = connection({ sourceTemplateKey: marker, url: "https://attacker.example/mcp", oauth: {} });
|
|
|
|
for (const provider of [marker, "slack", "attacker_example"]) {
|
|
const client = configuredOAuthClientForConnection(marked, provider, "https://attacker.example/token");
|
|
expect(client.clientId).toBeNull();
|
|
expect(client.clientSecret).toBeNull();
|
|
}
|
|
expect(configuredOAuthClientForConnection(marked, "slack", "https://slack.com/api/oauth.v2.user.access").clientId).toBeNull();
|
|
});
|
|
|
|
it("never uses one app's instance client for a connection that claims another provider", () => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_GMAIL_CLIENT_ID", "instance-gmail-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_GMAIL_CLIENT_SECRET", "instance-gmail-secret");
|
|
|
|
const client = configuredOAuthClientForConnection(
|
|
slack({ provider: "gmail" }),
|
|
"gmail",
|
|
"https://oauth2.googleapis.com/token",
|
|
);
|
|
expect(client.clientId).toBeNull();
|
|
expect(client.clientSecret).toBeNull();
|
|
});
|
|
|
|
it("allows a discovered token endpoint only on a host the definition fixes when it fixes no token endpoint", () => {
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_NOTION_CLIENT_ID", "instance-notion-id");
|
|
vi.stubEnv("PAPERCLIP_TOOL_OAUTH_NOTION_CLIENT_SECRET", "instance-notion-secret");
|
|
const notion = connection({ sourceTemplateKey: "notion", connectionMethodKey: "mcp-oauth" });
|
|
|
|
expect(configuredOAuthClientForConnection(notion, "notion", "https://mcp.notion.com/token").clientId).toBe("instance-notion-id");
|
|
for (const tokenUrl of [
|
|
"https://api.notion.com/v1/oauth/token",
|
|
"https://evil.notion.com/token",
|
|
"https://mcp.notion.com:8443/token",
|
|
"https://attacker.example/token",
|
|
]) {
|
|
expect(configuredOAuthClientForConnection(notion, "notion", tokenUrl).clientId).toBeNull();
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
["asana", "mcp-own-oauth", "https://app.asana.com/-/oauth_token"],
|
|
["railway", "mcp-oauth", "https://backboard.railway.com/oauth/token"],
|
|
])("allows the token endpoint that the %s definition pins", (slug, methodKey, tokenUrl) => {
|
|
vi.stubEnv(`PAPERCLIP_TOOL_OAUTH_${slug.toUpperCase()}_CLIENT_ID`, "instance-id");
|
|
vi.stubEnv(`PAPERCLIP_TOOL_OAUTH_${slug.toUpperCase()}_CLIENT_SECRET`, "instance-secret");
|
|
const pinned = connection({ sourceTemplateKey: slug, connectionMethodKey: methodKey });
|
|
|
|
expect(configuredOAuthClientForConnection(pinned, slug, tokenUrl).clientId).toBe("instance-id");
|
|
expect(configuredOAuthClientForConnection(pinned, slug, "https://attacker.example/token").clientId).toBeNull();
|
|
});
|
|
|
|
it("makes every customer OAuth method pin its token endpoint or name its same-host fallback", () => {
|
|
const sameHostFallback = new Set(["box:mcp-own-oauth", "notion:mcp-oauth", "posthog:mcp-oauth", "sentry:mcp-oauth", "xero:mcp-own-oauth"]);
|
|
const noFixedEndpoint = new Set(["oauth-generic:oauth"]);
|
|
const unpinned = APP_DEFINITIONS.flatMap((app) => app.methods
|
|
.filter((method) => method.auth === "oauth" && !method.oauthStrategy && method.ownershipModes.includes("customer")
|
|
&& method.purpose !== "ai" && method.transport !== "runtime_auth" && !method.defaults?.tokenEndpoint)
|
|
.map((method) => `${app.slug}:${method.key}`));
|
|
expect(unpinned.sort()).toEqual([...sameHostFallback, ...noFixedEndpoint].sort());
|
|
});
|
|
});
|