Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- [EE] Required authentication for streaming and blocking Ask API requests when AskGH is enabled. [#1678](https://github.com/sourcebot-dev/sourcebot/pull/1678)

## [5.1.14] - 2026-09-17

### Added
Expand Down
107 changes: 107 additions & 0 deletions packages/web/src/app/api/(server)/ee/chat/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { NextRequest } from 'next/server';
import { notAuthenticated, serviceErrorSchema } from '@/lib/serviceError';
import { ErrorCode } from '@/lib/errorCodes';

const mocks = vi.hoisted(() => ({
env: { EXPERIMENT_ASK_GH_ENABLED: 'false' },
withOptionalAuth: vi.fn(),
checkAskEntitlement: vi.fn(),
createMessageStream: vi.fn(),
findChat: vi.fn(),
createChat: vi.fn(),
}));

vi.mock('@/lib/apiHandler', () => ({
apiHandler: (handler: unknown) => handler,
}));
vi.mock('@/middleware/withAuth', () => ({
withOptionalAuth: mocks.withOptionalAuth,
}));
vi.mock('@sourcebot/shared', () => ({
env: mocks.env,
createLogger: () => ({ error: vi.fn() }),
}));
vi.mock('@/lib/utils', () => ({
isServiceError: (value: unknown) => serviceErrorSchema.safeParse(value).success,
}));
vi.mock('@/features/chat/utils.server', () => ({
checkAskEntitlement: mocks.checkAskEntitlement,
}));
vi.mock('@/ee/features/chat/agent', () => ({
createMessageStream: mocks.createMessageStream,
}));
vi.mock('@/features/chat/utils', () => ({}));
vi.mock('@/features/chat/llm.server', () => ({}));
vi.mock('@/features/chat/modelCapabilities.server', () => ({}));
vi.mock('@/features/chat/modelContextWindow.server', () => ({}));
vi.mock('@/ee/features/chat/llm.server', () => ({}));
vi.mock('@/ee/features/chat/askMcpAnalytics.server', () => ({}));
vi.mock('@/ee/features/chat/skills/commandResolution', () => ({}));
vi.mock('@/ee/features/chat/skills/skillAnalytics.server', () => ({}));
vi.mock('@/ee/features/audit/audit', () => ({}));
vi.mock('@/lib/posthog', () => ({}));
vi.mock('@sentry/nextjs', () => ({ captureException: vi.fn() }));

const { POST: streamingPost } = await import('./route');
const { POST: blockingPost } = await import('../../chat/blocking/route');

// Stop requests that pass authentication at the next gate, before model or DB work.
const entitlementError = {
statusCode: 403,
errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS,
message: 'Ask entitlement required',
};

beforeEach(() => {
vi.clearAllMocks();
mocks.checkAskEntitlement.mockResolvedValue(entitlementError);
});

describe.each([
{
name: 'streaming',
post: streamingPost,
path: '/api/ee/chat',
body: {
id: 'chat-id',
messages: [{ id: 'message-id', role: 'user', parts: [{ type: 'text', text: 'Explain this code' }] }],
selectedSearchScopes: [],
languageModel: { provider: 'openai', model: 'gpt-4o' },
},
},
{
name: 'blocking',
post: blockingPost,
path: '/api/chat/blocking',
body: { query: 'Explain this code' },
},
])('$name Ask authentication', ({ post, path, body }) => {
test.each([
{ askGhEnabled: 'true', authenticated: false, denied: true },
{ askGhEnabled: 'true', authenticated: true, denied: false },
{ askGhEnabled: 'false', authenticated: false, denied: false },
{ askGhEnabled: 'false', authenticated: true, denied: false },
])('AskGH=$askGhEnabled, authenticated=$authenticated', async ({ askGhEnabled, authenticated, denied }) => {
mocks.env.EXPERIMENT_ASK_GH_ENABLED = askGhEnabled;
// Simulate an org with anonymous access enabled by allowing either context.
mocks.withOptionalAuth.mockImplementation((callback) => callback({
org: { id: 1 },
user: authenticated ? { id: 'user-id' } : undefined,
prisma: { chat: { findUnique: mocks.findChat, create: mocks.createChat } },
}));

const response = await post(new NextRequest(`https://sourcebot.example.com${path}`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body),
}));

expect(response.status).toBe(denied ? 401 : entitlementError.statusCode);
expect(await response.json()).toEqual(denied ? notAuthenticated() : entitlementError);
expect(mocks.checkAskEntitlement).toHaveBeenCalledTimes(denied ? 0 : 1);
expect(mocks.findChat).not.toHaveBeenCalled();
expect(mocks.createChat).not.toHaveBeenCalled();
expect(mocks.createMessageStream).not.toHaveBeenCalled();
});
});
6 changes: 5 additions & 1 deletion packages/web/src/app/api/(server)/ee/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getAskSkillAvailabilityAnalytics, getAskSkillTurnCompletedAnalytics } f
import { apiHandler } from "@/lib/apiHandler";
import { ErrorCode } from "@/lib/errorCodes";
import { captureEvent } from "@/lib/posthog";
import { notFound, requestBodySchemaValidationError, ServiceError, serviceErrorResponse } from "@/lib/serviceError";
import { notAuthenticated, notFound, requestBodySchemaValidationError, ServiceError, serviceErrorResponse } from "@/lib/serviceError";
import { isServiceError } from "@/lib/utils";
import { withOptionalAuth } from "@/middleware/withAuth";
import * as Sentry from "@sentry/nextjs";
Expand Down Expand Up @@ -50,6 +50,10 @@ export const POST = apiHandler(async (req: NextRequest) => {

const response = await sew(() =>
withOptionalAuth(async ({ org, user, prisma }) => {
if (env.EXPERIMENT_ASK_GH_ENABLED === 'true' && !user) {
return notAuthenticated();
}

// Gate the generative path behind the `ask` entitlement. The client
// also gates this, but server-side enforcement can't be bypassed.
const askError = await checkAskEntitlement();
Expand Down
6 changes: 5 additions & 1 deletion packages/web/src/ee/features/mcp/askCodebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LanguageModelInfo, SBChatMessage, SearchScope } from "@/features/chat/t
import { convertLLMOutputToPortableMarkdown, getAnswerPartFromAssistantMessage, getLanguageModelKey } from "@/features/chat/utils";
import { resolveModelCapabilities } from "@/features/chat/modelCapabilities.server";
import { ErrorCode } from "@/lib/errorCodes";
import { ServiceError, ServiceErrorException } from "@/lib/serviceError";
import { notAuthenticated, ServiceError, ServiceErrorException } from "@/lib/serviceError";
import { withOptionalAuth } from "@/middleware/withAuth";
import { ChatVisibility, Prisma } from "@sourcebot/db";
import { createLogger, env } from "@sourcebot/shared";
Expand Down Expand Up @@ -49,6 +49,10 @@ const blockStreamUntilFinish = async <T extends UIMessage<unknown, UIDataTypes,
export const askCodebase = (params: AskCodebaseParams): Promise<AskCodebaseResult | ServiceError> =>
sew(() =>
withOptionalAuth(async ({ org, user, prisma }) => {
if (env.EXPERIMENT_ASK_GH_ENABLED === 'true' && !user) {
return notAuthenticated();
}

// Ask Sourcebot is a paid feature. askCodebase() is the single choke point
// for the programmatic ask path (the MCP `ask_codebase` tool and the
// /api/chat/blocking route both wrap it), so gating here covers both without
Expand Down
Loading