Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix: bk api (#107752)

Alex Vc2a0a47fe803
diff --git a/products/business_knowledge/backend/sandbox.py b/products/business_knowledge/backend/sandbox.py
index c19645a0b6f1..21825c731d8f 100644
--- a/products/business_knowledge/backend/sandbox.py
+++ b/products/business_knowledge/backend/sandbox.py
@@ -28,10 +28,43 @@ MAX_OPEN_RUNS_PER_OWNER = 3
FINISHED_ACTIVITY_CACHE_SECONDS = 60 * 60
BK_MCP_SCOPE = "business_knowledge:read"
+# The PostHog MCP server reads /api/users/@me/ to start a session, so without user:read it never connects.
+BK_MCP_SCOPES = [BK_MCP_SCOPE, "user:read"]
BK_SEARCH_TOOL = "business-knowledge-documents-search"
BK_WINDOW_TOOL = "business-knowledge-document-window-retrieve"
DOCS_SEARCH_TOOL = "docs-search"
BK_DISPLAY_TOOLS = frozenset({BK_SEARCH_TOOL, BK_WINDOW_TOOL})
+# Read tools the token's scopes unlock but the answer never needs. Knowledge content can carry
+# injected instructions, so hide anything that reads the asker's own data.
+BK_HIDDEN_TOOLS = [
+ DOCS_SEARCH_TOOL,
+ "user-get",
+ "user-home-settings-get",
+ "llma-personal-spend",
+ "reminder-get",
+ "reminders-list",
+ "tasks-list",
+ "tasks-retrieve",
+ "tasks-runs-list",
+ "tasks-runs-retrieve",
+ "tasks-runs-session-logs-retrieve",
+ "tasks-config-list",
+ "tasks-me-config-list",
+ "tasks-models-retrieve",
+ "channel-list",
+ "channel-retrieve",
+ "channel-instructions-retrieve",
+ "inbox-reports-list",
+ "inbox-reports-retrieve",
+ "inbox-report-artefacts-list",
+ "inbox-report-artefacts-retrieve",
+ "inbox-report-checks-list",
+ "inbox-report-checks-retrieve",
+ "inbox-source-configs-list",
+ "inbox-source-configs-retrieve",
+ "task-context-wiki-channel-resolve",
+ "task-context-wiki-page-retrieve",
+]
_RECOGNIZED_TOOLS = BK_DISPLAY_TOOLS | {DOCS_SEARCH_TOOL}
# Exact single-exec form. A later mention of the tool name inside the command is not a call.
@@ -213,12 +246,12 @@ def start_sandbox_run(
create_pr=False,
internal=True,
sandbox_environment_id=str(env_id),
- posthog_mcp_scopes=[BK_MCP_SCOPE],
+ posthog_mcp_scopes=BK_MCP_SCOPES,
model=SANDBOX_MODEL,
runtime_adapter=SANDBOX_RUNTIME_ADAPTER,
output_schema=SandboxAnswer,
extra_run_state={
- "mcp_exclude_tools": [DOCS_SEARCH_TOOL],
+ "mcp_exclude_tools": BK_HIDDEN_TOOLS,
"config_snapshot": {"connectors": {"mcp_installation_ids": []}},
},
)
diff --git a/products/business_knowledge/backend/tests/test_sandbox.py b/products/business_knowledge/backend/tests/test_sandbox.py
index 51413c17823c..2083f0eb4149 100644
--- a/products/business_knowledge/backend/tests/test_sandbox.py
+++ b/products/business_knowledge/backend/tests/test_sandbox.py
@@ -21,7 +21,6 @@ from products.business_knowledge.backend.api.serializers import SandboxQuestionS
from products.business_knowledge.backend.logic import KnowledgeSearchResult
from products.business_knowledge.backend.models import KnowledgeChunk, KnowledgeDocument, KnowledgeSource, SafetyVerdict
from products.business_knowledge.backend.sandbox import (
- BK_MCP_SCOPE,
BK_SEARCH_TOOL,
BK_WINDOW_TOOL,
DOCS_SEARCH_TOOL,
@@ -204,8 +203,8 @@ class TestSandboxAPI(APIBaseTest):
assert task.internal is True
assert task.repository is None
assert task.created_by_id == self.user.id
- assert run.state["pending_dispatch"]["posthog_mcp_scopes"] == [BK_MCP_SCOPE]
- assert run.state["mcp_exclude_tools"] == [DOCS_SEARCH_TOOL]
+ assert run.state["pending_dispatch"]["posthog_mcp_scopes"] == ["business_knowledge:read", "user:read"]
+ assert {DOCS_SEARCH_TOOL, "user-get", "tasks-runs-session-logs-retrieve"} <= set(run.state["mcp_exclude_tools"])
assert run.state["config_snapshot"]["connectors"]["mcp_installation_ids"] == []
assert run.state["model"] == "claude-sonnet-5"
assert run.state["runtime_adapter"] == "claude"
diff --git a/products/business_knowledge/frontend/scenes/PlaygroundTurn.tsx b/products/business_knowledge/frontend/scenes/PlaygroundTurn.tsx
index 1c3bf774231e..e5a7239bfa45 100644
--- a/products/business_knowledge/frontend/scenes/PlaygroundTurn.tsx
+++ b/products/business_knowledge/frontend/scenes/PlaygroundTurn.tsx
@@ -2,6 +2,8 @@ import { memo } from 'react'
import { IconDocument, IconSearch } from '@posthog/icons'
+import { LemonMarkdown } from 'lib/lemon-ui/LemonMarkdown'
+
import { Thread } from 'products/posthog_ai/frontend/api/primitives'
import { type PlaygroundTurnApi, SandboxToolNameEnumApi } from '../generated/api.schemas'
@@ -56,7 +58,7 @@ export const PlaygroundTurn = memo(function PlaygroundTurn({ id, question, turn
) : null}
{run?.status === 'completed' && run.reply ? (
<Thread.Message type="ai" wrapperClassName="max-w-4/5">
- <Thread.Markdown content={run.reply} id={`${id}-reply`} />
+ <LemonMarkdown disableImages>{run.reply}</LemonMarkdown>
{run.sources.length > 0 ? (
<div className="mt-2 flex flex-col gap-1 border-t pt-2">
<span className="text-xs font-semibold text-secondary">Sources</span>
diff --git a/products/tasks/backend/presentation/views/api.py b/products/tasks/backend/presentation/views/api.py
index e4390e623511..a8d2f28c8803 100644
--- a/products/tasks/backend/presentation/views/api.py
+++ b/products/tasks/backend/presentation/views/api.py
@@ -578,7 +578,7 @@ class TaskViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
description="Retrieve a single task by ID.",
)
def retrieve(self, request, pk=None, **kwargs):
- bypass_visibility = _can_bypass_visibility(request, self.team_id)
+ bypass_visibility = is_sandbox_agent_request(request, pk) or _can_bypass_visibility(request, self.team_id)
task = tasks_facade.get_task_detail(pk, self.team_id, self._user_id(), bypass_visibility=bypass_visibility)
if task is None:
raise NotFound()
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 850371897d21..d40b9f536ee1 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -6543,6 +6543,26 @@ class TestTaskRunAPI(BaseTaskAPITest):
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+ def test_only_the_task_bound_sandbox_reads_a_product_private_task(self):
+ private_task = Task.objects.create(
+ team=self.team,
+ created_by=self.user,
+ title="Question",
+ description="Answer from business knowledge",
+ origin_product=Task.OriginProduct.BUSINESS_KNOWLEDGE,
+ )
+ other_task = self.create_task()
+ own_sandbox = self._sandbox_oauth_client(private_task.id)
+ other_sandbox = self._sandbox_oauth_client(other_task.id, client_id=POSTHOG_AI_APP_CLIENT_ID_DEV)
+ url = f"/api/projects/@current/tasks/{private_task.id}/"
+
+ own = own_sandbox.get(url)
+
+ self.assertEqual(own.status_code, status.HTTP_200_OK)
+ self.assertEqual(own.json()["description"], "Answer from business knowledge")
+ self.assertEqual(other_sandbox.get(url).status_code, status.HTTP_404_NOT_FOUND)
+ self.assertEqual(self.client.get(url).status_code, status.HTTP_404_NOT_FOUND)
+
@parameterized.expand(
[
# Both caller-controlled output writers have to enforce this: the dedicated set_output