From 16262e934e26bc71db53ab085250dbb494e54ff7 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Tue, 1 Dec 2026 09:22:31 +0100 Subject: [PATCH] fix: name only what a connection gets, refuse scoped callers Co-Authored-By: Claude Opus 5.5 --- ...1bb22391e9ac7b9c4d11786d59fa9b17fc8f9.json | 28 +++++++++++++++++++ ...7b145f92f1c396817e4c13e50019fc085d6c.json} | 14 +++++++--- backend/tests/mcp_role_access.rs | 26 +++++++++++++++++ docs/workspace-roles.md | 2 ++ .../mcp/McpWorkspaceAccessList.svelte | 2 +- .../components/mcp/workspaceAccess.test.ts | 9 +++--- .../src/lib/components/mcp/workspaceAccess.ts | 15 +++++++--- 7 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 backend/.sqlx/query-44f272788e9dcceef20348675db1bb22391e9ac7b9c4d11786d59fa9b17fc8f9.json rename backend/.sqlx/{query-cc2d27c55c3dac7b0f287b56c66ec1ee8856df25ebe62074bb1628ee7f39415b.json => query-ee761c085738bb22b01a48627e067b145f92f1c396817e4c13e50019fc085d6c.json} (64%) diff --git a/backend/.sqlx/query-44f272788e9dcceef20348675db1bb22391e9ac7b9c4d11786d59fa9b17fc8f9.json b/backend/.sqlx/query-44f272788e9dcceef20348675db1bb22391e9ac7b9c4d11786d59fa9b17fc8f9.json new file mode 100644 index 0000000000..e2a992cd8b --- /dev/null +++ b/backend/.sqlx/query-44f272788e9dcceef20348675db1bb22391e9ac7b9c4d11786d59fa9b17fc8f9.json @@ -0,0 +1,28 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT workspace.id, workspace.name FROM workspace\n JOIN usr ON usr.workspace_id = workspace.id\n WHERE usr.email = $1 AND usr.disabled = false AND workspace.deleted = false\n ORDER BY workspace.name", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Varchar" + }, + { + "ordinal": 1, + "name": "name", + "type_info": "Varchar" + } + ], + "parameters": { + "Left": [ + "Text" + ] + }, + "nullable": [ + false, + false + ] + }, + "hash": "44f272788e9dcceef20348675db1bb22391e9ac7b9c4d11786d59fa9b17fc8f9" +} diff --git a/backend/.sqlx/query-cc2d27c55c3dac7b0f287b56c66ec1ee8856df25ebe62074bb1628ee7f39415b.json b/backend/.sqlx/query-ee761c085738bb22b01a48627e067b145f92f1c396817e4c13e50019fc085d6c.json similarity index 64% rename from backend/.sqlx/query-cc2d27c55c3dac7b0f287b56c66ec1ee8856df25ebe62074bb1628ee7f39415b.json rename to backend/.sqlx/query-ee761c085738bb22b01a48627e067b145f92f1c396817e4c13e50019fc085d6c.json index 8881f24f4d..22f1035e0c 100644 --- a/backend/.sqlx/query-cc2d27c55c3dac7b0f287b56c66ec1ee8856df25ebe62074bb1628ee7f39415b.json +++ b/backend/.sqlx/query-ee761c085738bb22b01a48627e067b145f92f1c396817e4c13e50019fc085d6c.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT workspace_id, is_admin, operator, workspace_role FROM usr\n WHERE email = $1 AND workspace_id = ANY($2) AND disabled = false", + "query": "SELECT workspace_id, username, is_admin, operator, workspace_role FROM usr\n WHERE email = $1 AND workspace_id = ANY($2) AND disabled = false", "describe": { "columns": [ { @@ -10,16 +10,21 @@ }, { "ordinal": 1, + "name": "username", + "type_info": "Varchar" + }, + { + "ordinal": 2, "name": "is_admin", "type_info": "Bool" }, { - "ordinal": 2, + "ordinal": 3, "name": "operator", "type_info": "Bool" }, { - "ordinal": 3, + "ordinal": 4, "name": "workspace_role", "type_info": "Varchar" } @@ -34,8 +39,9 @@ false, false, false, + false, true ] }, - "hash": "cc2d27c55c3dac7b0f287b56c66ec1ee8856df25ebe62074bb1628ee7f39415b" + "hash": "ee761c085738bb22b01a48627e067b145f92f1c396817e4c13e50019fc085d6c" } diff --git a/backend/tests/mcp_role_access.rs b/backend/tests/mcp_role_access.rs index e03719fe82..9154061983 100644 --- a/backend/tests/mcp_role_access.rs +++ b/backend/tests/mcp_role_access.rs @@ -194,6 +194,32 @@ async fn role_setting_caps_every_token(db: Pool) -> anyhow::Result<()> let out = call(DEFAULT, "ROLEUNBOUND_TOKEN", "list_workspaces", json!({})).await; assert!(out["result"].to_string().contains("read only"), "{out}"); + let listed = |bearer: &'static str| async move { + reqwest::Client::new() + .get(format!( + "http://localhost:{port}/api/mcp/gateway/oauth/server/workspace_access" + )) + .bearer_auth(bearer) + .send() + .await + .unwrap() + }; + let access: Value = listed("SECRET_TOKEN").await.json().await?; + let here = access + .as_array() + .unwrap() + .iter() + .find(|w| w["id"] == "test-workspace") + .unwrap(); + assert_eq!( + (&here["access"], &here["limited"]), + (&json!("read_only"), &json!(true)) + ); + assert!( + !listed("ROLEUNBOUND_TOKEN").await.status().is_success(), + "a scoped token must not list its owner's other workspaces" + ); + set_access(&db, json!({"access": "off"})).await; assert!(tools(port, WORKSPACE, "ROLESEL_TOKEN").await.is_empty()); assert_eq!( diff --git a/docs/workspace-roles.md b/docs/workspace-roles.md index 2d5c6984b5..24a625b4f8 100644 --- a/docs/workspace-roles.md +++ b/docs/workspace-roles.md @@ -75,6 +75,8 @@ it must not restrict a role, cost a seat or reach an authoring gate. `runnables` gets the tools server whatever the token, and an `mcp:all` token under another limit the session toolset, since endpoint tools are never served under one. Where a connection covers several workspaces the list is one for all, so the limit refuses at call time. +- The connect page and the MCP tab of the home page read `workspace_access`, the same computation + as `list_workspaces`, so a user is shown what they get rather than what they ask for. - A selection intersects with the token's own (`McpScopeConfig::limited_by`). - A lapsed license freezes it as it does roles: it keeps applying and can be removed, not changed. On Community it is ignored and refused at write. diff --git a/frontend/src/lib/components/mcp/McpWorkspaceAccessList.svelte b/frontend/src/lib/components/mcp/McpWorkspaceAccessList.svelte index e471593965..1340344ac8 100644 --- a/frontend/src/lib/components/mcp/McpWorkspaceAccessList.svelte +++ b/frontend/src/lib/components/mcp/McpWorkspaceAccessList.svelte @@ -22,7 +22,7 @@

What it gets

    {#each listed as workspace (workspace.id)} - {@const separate = connectsSeparately(workspace, all, covered)} + {@const separate = connectsSeparately(workspace, all, covered, access)}
  • {workspace.name} diff --git a/frontend/src/lib/components/mcp/workspaceAccess.test.ts b/frontend/src/lib/components/mcp/workspaceAccess.test.ts index 35205e8fdf..ea3d8e2697 100644 --- a/frontend/src/lib/components/mcp/workspaceAccess.test.ts +++ b/frontend/src/lib/components/mcp/workspaceAccess.test.ts @@ -35,8 +35,9 @@ it('names the lower of the picked access and the workspace one', () => { }) it('tools are part of a connection to their workspace alone', () => { - expect(connectsSeparately(ws('a', 'runnables'), false, ['a'])).toBe(false) - expect(connectsSeparately(ws('a', 'runnables'), false, ['a', 'b'])).toBe(true) - expect(connectsSeparately(ws('a', 'runnables'), true, [])).toBe(true) - expect(connectsSeparately(ws('a', 'read_only'), true, [])).toBe(false) + expect(connectsSeparately(ws('a', 'runnables'), false, ['a'], 'full')).toBe(false) + expect(connectsSeparately(ws('a', 'runnables'), false, ['a', 'b'], 'full')).toBe(true) + expect(connectsSeparately(ws('a', 'runnables'), true, [], 'full')).toBe(true) + expect(connectsSeparately(ws('a', 'runnables'), true, [], 'read')).toBe(false) + expect(connectsSeparately(ws('a', 'read_only'), true, [], 'full')).toBe(false) }) diff --git a/frontend/src/lib/components/mcp/workspaceAccess.ts b/frontend/src/lib/components/mcp/workspaceAccess.ts index b956f4696d..ada829c20d 100644 --- a/frontend/src/lib/components/mcp/workspaceAccess.ts +++ b/frontend/src/lib/components/mcp/workspaceAccess.ts @@ -39,10 +39,17 @@ export function defaultConnection(workspaces: McpWorkspaceAccess[]): ConnectionD const asTools = workspaces.filter((w) => w.access === 'runnables') const session = workspaces.some((w) => w.access !== 'runnables' && w.access !== 'off') if (session || asTools.length === 0) return { all: true, picked: [], oneEach: false } - return { all: false, picked: [asTools[0].id], oneEach: asTools.length > 1 } + const first = asTools.find((w) => w.runnables) ?? asTools[0] + return { all: false, picked: [first.id], oneEach: asTools.length > 1 } } -/** Whether `workspace`, set to tools, needs its own connection next to the `covered` ones. */ -export function connectsSeparately(workspace: McpWorkspaceAccess, all: boolean, covered: string[]) { - return workspace.access === 'runnables' && (all || covered.length > 1) +/** Whether `workspace`, set to tools, needs its own connection next to the `covered` ones. A + * read-only connection runs nothing there either way. */ +export function connectsSeparately( + workspace: McpWorkspaceAccess, + all: boolean, + covered: string[], + picked: McpAccess +) { + return workspace.access === 'runnables' && picked !== 'read' && (all || covered.length > 1) }