Skip to content
Draft
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
- Require authentication for the streaming and blocking Ask APIs in Public SaaS deployments. [#1679](https://github.com/sourcebot-dev/sourcebot/pull/1679)

## [5.1.14] - 2026-09-17

### Added
Expand Down
6 changes: 6 additions & 0 deletions packages/web/src/app/api/(server)/ee/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getAISDKLanguageModelAndOptions } from "@/features/chat/llm.server";
import { resolveContextWindow } from "@/features/chat/modelContextWindow.server";
import { materializeCommandMessageTexts } from "@/ee/features/chat/skills/commandResolution";
import { getAskSkillAvailabilityAnalytics, getAskSkillTurnCompletedAnalytics } from "@/ee/features/chat/skills/skillAnalytics.server";
import { checkAskAuthentication } from "@/features/chat/askAuth";
import { apiHandler } from "@/lib/apiHandler";
import { ErrorCode } from "@/lib/errorCodes";
import { captureEvent } from "@/lib/posthog";
Expand Down Expand Up @@ -50,6 +51,11 @@ export const POST = apiHandler(async (req: NextRequest) => {

const response = await sew(() =>
withOptionalAuth(async ({ org, user, prisma }) => {
const authError = checkAskAuthentication(user);
if (authError) {
return authError;
}

// 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
13 changes: 8 additions & 5 deletions packages/web/src/app/api/(server)/ee/mcp/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { createMcpServer } from '@/ee/features/mcp/server';
import { MCP_PAID_PLAN_REQUIRED_MESSAGE } from '@/ee/features/mcp/constants';
import { checkAskAuthentication } from '@/features/chat/askAuth';
import { withOptionalAuth } from '@/middleware/withAuth';
import { isServiceError } from '@/lib/utils';
import { notAuthenticated, serviceErrorResponse, ServiceError } from '@/lib/serviceError';
import { serviceErrorResponse, ServiceError } from '@/lib/serviceError';
import { ErrorCode } from '@/lib/errorCodes';
import { StatusCodes } from 'http-status-codes';
import { NextRequest } from 'next/server';
Expand Down Expand Up @@ -85,8 +86,9 @@ export const POST = apiHandler(async (request: NextRequest) => {

const response = await sew(() =>
withOptionalAuth(async ({ user, principal }) => {
if (env.EXPERIMENT_ASK_GH_ENABLED === 'true' && !user) {
return notAuthenticated();
const authError = checkAskAuthentication(user);
if (authError) {
return authError;
}
const ownerId = user?.id ?? null;
const sessionId = request.headers.get(MCP_SESSION_ID_HEADER);
Expand Down Expand Up @@ -151,8 +153,9 @@ export const DELETE = apiHandler(async (request: NextRequest) => {

const result = await sew(() =>
withOptionalAuth(async ({ user }) => {
if (env.EXPERIMENT_ASK_GH_ENABLED === 'true' && !user) {
return notAuthenticated();
const authError = checkAskAuthentication(user);
if (authError) {
return authError;
}
const ownerId = user?.id ?? null;
const sessionId = request.headers.get(MCP_SESSION_ID_HEADER);
Expand Down
6 changes: 6 additions & 0 deletions packages/web/src/ee/features/mcp/askCodebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generateChatNameFromMessage } from "@/ee/features/chat/llm.server";
import { getAISDKLanguageModelAndOptions } from "@/features/chat/llm.server";
import { resolveContextWindow } from "@/features/chat/modelContextWindow.server";
import { LanguageModelInfo, SBChatMessage, SearchScope } from "@/features/chat/types";
import { checkAskAuthentication } from "@/features/chat/askAuth";
import { convertLLMOutputToPortableMarkdown, getAnswerPartFromAssistantMessage, getLanguageModelKey } from "@/features/chat/utils";
import { resolveModelCapabilities } from "@/features/chat/modelCapabilities.server";
import { ErrorCode } from "@/lib/errorCodes";
Expand Down Expand Up @@ -49,6 +50,11 @@ const blockStreamUntilFinish = async <T extends UIMessage<unknown, UIDataTypes,
export const askCodebase = (params: AskCodebaseParams): Promise<AskCodebaseResult | ServiceError> =>
sew(() =>
withOptionalAuth(async ({ org, user, prisma }) => {
const authError = checkAskAuthentication(user);
if (authError) {
return authError;
}

// 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
39 changes: 39 additions & 0 deletions packages/web/src/features/chat/askAuth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { beforeEach, describe, expect, test, vi } from "vitest";

const mocks = vi.hoisted(() => ({
env: {
EXPERIMENT_ASK_GH_ENABLED: "false",
},
}));

vi.mock("@sourcebot/shared", () => ({
env: mocks.env,
}));

const { checkAskAuthentication } = await import("./askAuth");

beforeEach(() => {
mocks.env.EXPERIMENT_ASK_GH_ENABLED = "false";
});

describe("checkAskAuthentication", () => {
test("rejects anonymous Ask requests when Public SaaS is enabled", () => {
mocks.env.EXPERIMENT_ASK_GH_ENABLED = "true";

expect(checkAskAuthentication(undefined)).toEqual({
statusCode: 401,
errorCode: "NOT_AUTHENTICATED",
message: "Not authenticated",
});
});

test("allows authenticated Ask requests when Public SaaS is enabled", () => {
mocks.env.EXPERIMENT_ASK_GH_ENABLED = "true";

expect(checkAskAuthentication({ id: "user-1" })).toBeNull();
});

test("allows anonymous Ask requests when Public SaaS is disabled", () => {
expect(checkAskAuthentication(undefined)).toBeNull();
});
});
14 changes: 14 additions & 0 deletions packages/web/src/features/chat/askAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { notAuthenticated, type ServiceError } from "@/lib/serviceError";
import { env } from "@sourcebot/shared";

/**
* Public SaaS requires an authenticated user for Ask requests. Self-hosted
* deployments retain their existing anonymous-access behavior.
*/
export const checkAskAuthentication = (user: object | undefined): ServiceError | null => {
if (env.EXPERIMENT_ASK_GH_ENABLED === "true" && !user) {
return notAuthenticated();
}

return null;
};
Loading