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(cli): exclude llm_gateway from read-only preset, surface invalid_scope errors (#67252)

okxinte6138bf0cc64
diff --git a/frontend/src/lib/scopes.test.ts b/frontend/src/lib/scopes.test.ts
index 7a775ec1914c..dc5604364e98 100644
--- a/frontend/src/lib/scopes.test.ts
+++ b/frontend/src/lib/scopes.test.ts
@@ -43,10 +43,13 @@ describe('API_KEY_SCOPE_PRESETS', () => {
expect(preset.label).toBe('Read-only access')
})
- it('contains :read for every entry in API_SCOPES', () => {
+ it('contains :read for every entry in API_SCOPES except unprivileged-excluded scopes', () => {
const preset = findPreset('read_only_access')
- const expected = API_SCOPES.map(({ key }) => `${key}:read`).sort()
+ const expected = API_SCOPES.filter(({ unprivilegedExcluded }) => !unprivilegedExcluded)
+ .map(({ key }) => `${key}:read`)
+ .sort()
expect([...preset.scopes].sort()).toEqual(expected)
+ expect(preset.scopes).not.toContain('llm_gateway:read')
})
})
diff --git a/frontend/src/lib/scopes.tsx b/frontend/src/lib/scopes.tsx
index 2fe1b70260dd..21ecc0c7e43a 100644
--- a/frontend/src/lib/scopes.tsx
+++ b/frontend/src/lib/scopes.tsx
@@ -13,6 +13,8 @@ export type APIScope = {
disabledWhenProjectScoped?: boolean
description?: string
warnings?: Partial<Record<'read' | 'write', string | JSX.Element>>
+ /** Mirrors `PRIVILEGED_SCOPES` in posthog/scopes.py — excluded from every unprivileged preset. */
+ unprivilegedExcluded?: boolean
}
// Scopes whose write action also writes a feature flag as a side effect (survey targeting flag,
@@ -98,7 +100,13 @@ export const API_SCOPES: APIScope[] = [
{ key: 'legal_document', objectName: 'Legal document', objectPlural: 'legal documents' },
{ key: 'live_debugger', objectName: 'Live debugger', objectPlural: 'live debugger' },
{ key: 'llm_analytics', objectName: 'AI observability', objectPlural: 'AI observability' },
- { key: 'llm_gateway', objectName: 'LLM gateway', objectPlural: 'LLM gateway', disabledActions: ['write'] },
+ {
+ key: 'llm_gateway',
+ objectName: 'LLM gateway',
+ objectPlural: 'LLM gateway',
+ disabledActions: ['write'],
+ unprivilegedExcluded: true,
+ },
{ key: 'llm_prompt', objectName: 'LLM prompt', objectPlural: 'LLM prompts' },
{ key: 'llm_provider_key', objectName: 'LLM provider key', objectPlural: 'LLM provider keys' },
{ key: 'llm_skill', objectName: 'LLM skill', objectPlural: 'LLM skills' },
@@ -266,9 +274,10 @@ export const API_KEY_SCOPE_PRESETS: {
{
value: 'mcp_server',
label: 'MCP Server',
- scopes: API_SCOPES.filter(({ key }) => !key.includes('llm_gateway') && !key.includes('file_system')).map(
- ({ key }) => `${key}:write`
- ),
+ // file_system is excluded because the MCP server doesn't request it, not because it's privileged.
+ scopes: API_SCOPES.filter(
+ ({ key, unprivilegedExcluded }) => !unprivilegedExcluded && key !== 'file_system'
+ ).map(({ key }) => `${key}:write`),
access_type: 'all',
},
{
@@ -280,7 +289,7 @@ export const API_KEY_SCOPE_PRESETS: {
{
value: 'read_only_access',
label: 'Read-only access',
- scopes: API_SCOPES.map(({ key }) => `${key}:read`),
+ scopes: API_SCOPES.filter(({ unprivilegedExcluded }) => !unprivilegedExcluded).map(({ key }) => `${key}:read`),
},
{ value: 'all_access', label: 'All access', scopes: ['*'] },
]
diff --git a/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.test.ts b/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.test.ts
index 30b3c78d2c4b..e35f3232c263 100644
--- a/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.test.ts
+++ b/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.test.ts
@@ -2,6 +2,7 @@ import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import { AGENT_USE_CASE_SCOPES } from 'lib/agentScopes.generated'
+import api, { ApiError } from 'lib/api'
import { API_SCOPES } from 'lib/scopes'
import { initKeaTests } from '~/test/init'
@@ -121,4 +122,28 @@ describe('cliAuthorizeLogic', () => {
expect(logic.values.filteredScopes.map((scope) => scope.key)).toContain('feature_flag')
expect(logic.values.filteredScopes.every((scope) => scope.key === 'feature_flag')).toBe(true)
})
+
+ it('surfaces the invalid_scope error from the backend on submit', async () => {
+ router.actions.push('/cli/authorize', { code: 'ABCD-1234' })
+ logic.actions.setAuthorizeValues({
+ userCode: 'ABCD-1234',
+ organizationId: 'org-id',
+ projectId: 1,
+ scopes: ['llm_gateway:read'],
+ })
+ jest.spyOn(api, 'create').mockRejectedValueOnce(
+ new ApiError(undefined, 400, undefined, {
+ error: 'invalid_scope',
+ error_description: 'llm_gateway:read is not permitted',
+ })
+ )
+
+ await expectLogic(logic, () => {
+ logic.actions.submitAuthorize()
+ }).toDispatchActions(['submitAuthorizeFailure'])
+
+ expect(logic.values.authorizeErrors.scopes).toBe(
+ 'One or more selected scopes are not permitted. Try choosing a different preset.'
+ )
+ })
})
diff --git a/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.ts b/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.ts
index 643328604e81..029c7c083b42 100644
--- a/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.ts
+++ b/frontend/src/scenes/authentication/cli/cliAuthorizeLogic.ts
@@ -53,10 +53,11 @@ function getDefaultScopesForUseCases(useCases: CLIUseCase[]): string[] {
const DEFAULT_SCOPES = getDefaultScopesForUseCases(DEFAULT_USE_CASES)
// `read` for every scope that supports it (write-disabled scopes like file_system
-// still allow read; only skip scopes whose read action is disabled).
-const READ_ONLY_SCOPES = API_SCOPES.filter(({ disabledActions }) => !disabledActions?.includes('read')).map(
- ({ key }) => `${key}:read`
-)
+// still allow read; only skip scopes whose read action is disabled or that are
+// unprivileged-excluded, like llm_gateway, which the backend rejects for this flow).
+const READ_ONLY_SCOPES = API_SCOPES.filter(
+ ({ disabledActions, unprivilegedExcluded }) => !disabledActions?.includes('read') && !unprivilegedExcluded
+).map(({ key }) => `${key}:read`)
// Presets offered in the consent screen dropdown. Values match the URL `use_cases`
// so a requested use case maps onto the matching preset.
@@ -137,7 +138,7 @@ export const cliAuthorizeLogic = kea<cliAuthorizeLogicType>([
},
],
})),
- forms(() => ({
+ forms(({ actions }) => ({
authorize: {
defaults: {
userCode: '',
@@ -164,18 +165,25 @@ export const cliAuthorizeLogic = kea<cliAuthorizeLogicType>([
})
return response
} catch (error: any) {
- const errorCode = error?.code || error?.error
+ const errorCode = error?.data?.error || error?.code
if (errorCode === 'invalid_code') {
- throw { userCode: 'Invalid or expired code. Please try again.' }
+ actions.setAuthorizeManualErrors({ userCode: 'Invalid or expired code. Please try again.' })
} else if (errorCode === 'expired') {
- throw { userCode: 'This code has expired. Please request a new code in your terminal.' }
+ actions.setAuthorizeManualErrors({
+ userCode: 'This code has expired. Please request a new code in your terminal.',
+ })
} else if (errorCode === 'access_denied') {
- throw { projectId: 'You do not have access to this project.' }
+ actions.setAuthorizeManualErrors({ projectId: 'You do not have access to this project.' })
} else if (errorCode === 'invalid_project') {
- throw { projectId: 'Project not found.' }
+ actions.setAuthorizeManualErrors({ projectId: 'Project not found.' })
+ } else if (errorCode === 'invalid_scope') {
+ actions.setAuthorizeManualErrors({
+ scopes: 'One or more selected scopes are not permitted. Try choosing a different preset.',
+ })
} else {
- throw { userCode: 'An error occurred. Please try again.' }
+ actions.setAuthorizeManualErrors({ userCode: 'An error occurred. Please try again.' })
}
+ throw error
}
},
},