Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: not redirect to setup page for workspace member (#21333)

ecmadao5398130c6ac7
diff --git a/.github/workflows/frontend-tests.yml b/.github/workflows/frontend-tests.yml
index 3aefd8d9ba..9cd4071961 100644
--- a/.github/workflows/frontend-tests.yml
+++ b/.github/workflows/frontend-tests.yml
@@ -27,15 +27,9 @@ jobs:
# The runner's pnpm store persists between runs, so the Actions
# cache adds nothing and downloading it is slow.
cache: ''
- - uses: pnpm/action-setup@v6
+ - uses: pnpm/setup@v2
with:
version: '11.17.0'
- # Fetch the pnpm binary instead of running the npm-based
- # self-installer: the self-hosted runner's bundled npm
- # (externals/node24) is missing lib/cli.js, so the installer dies
- # before pnpm exists. Ordering setup-node first also keeps this off
- # the runner's bundled toolchain.
- standalone: true
- name: Install dependencies
run: pnpm install --frozen-lockfile
working-directory: frontend
@@ -57,15 +51,9 @@ jobs:
# The runner's pnpm store persists between runs, so the Actions
# cache adds nothing and downloading it is slow.
cache: ''
- - uses: pnpm/action-setup@v6
+ - uses: pnpm/setup@v2
with:
version: '11.17.0'
- # Fetch the pnpm binary instead of running the npm-based
- # self-installer: the self-hosted runner's bundled npm
- # (externals/node24) is missing lib/cli.js, so the installer dies
- # before pnpm exists. Ordering setup-node first also keeps this off
- # the runner's bundled toolchain.
- standalone: true
- name: Install dependencies
run: pnpm install --frozen-lockfile
working-directory: frontend
diff --git a/frontend/src/app/router/guard.test.ts b/frontend/src/app/router/guard.test.ts
index fb1964797f..fc14d6cad6 100644
--- a/frontend/src/app/router/guard.test.ts
+++ b/frontend/src/app/router/guard.test.ts
@@ -11,6 +11,7 @@ const session = {
hasTwoFa: false,
isSaaSMode: false,
disallowSignup: false,
+ enableOnboarding: false,
currentUser: undefined as { mfaEnabled: boolean } | undefined,
// Mirrors the store default: PIPELINE until the workspace profile loads.
databaseChangeMode: DatabaseChangeMode.PIPELINE,
@@ -22,6 +23,11 @@ const resets = {
resetProjects: vi.fn(),
};
+const workspaceSetup = {
+ fetchServerInfo: vi.fn<() => Promise<unknown>>(),
+ fetchWorkspaceIamPolicy: vi.fn<() => Promise<unknown>>(),
+};
+
vi.mock("@/stores/app", () => ({
useAppStore: {
getState: () => ({
@@ -31,6 +37,9 @@ vi.mock("@/stores/app", () => ({
getWorkspaceProfile: () => ({ requireMfa: session.requireMfa }),
hasFeature: () => session.hasTwoFa,
isSaaSMode: () => session.isSaaSMode,
+ enableOnboarding: () => session.enableOnboarding,
+ fetchServerInfo: workspaceSetup.fetchServerInfo,
+ fetchWorkspaceIamPolicy: workspaceSetup.fetchWorkspaceIamPolicy,
authenticationInfo: {
restriction: { disallowSignup: session.disallowSignup },
},
@@ -74,9 +83,12 @@ beforeEach(() => {
session.hasTwoFa = false;
session.isSaaSMode = false;
session.disallowSignup = false;
+ session.enableOnboarding = false;
session.currentUser = undefined;
session.databaseChangeMode = DatabaseChangeMode.PIPELINE;
vi.clearAllMocks();
+ workspaceSetup.fetchServerInfo.mockResolvedValue({});
+ workspaceSetup.fetchWorkspaceIamPolicy.mockResolvedValue({});
setRouteNameIndex(
new Map<string, string>([
[AUTH_SIGNIN_MODULE, "/auth"],
@@ -116,6 +128,24 @@ async function runCatchAllLoader(path: string): Promise<Response> {
}) as Response | Promise<Response>;
}
+async function runWorkspaceSetupLoader(
+ path = "/auth/setup"
+): Promise<Response | null> {
+ const matched = matchRoutes(routes, path);
+ const leafRoute = matched?.at(-1)?.route;
+ if (typeof leafRoute?.loader !== "function") {
+ return null;
+ }
+ const url = new URL(`https://app.example.com${path}`);
+ return leafRoute.loader({
+ request: new Request(url),
+ url,
+ pattern: "/auth/setup",
+ params: {},
+ context: new RouterContextProvider(),
+ }) as Response | null | Promise<Response | null>;
+}
+
describe("rootGuard", () => {
test("error page is allowed directly", () => {
expect(run(WORKSPACE_ROUTE_404, "/404")).toBeNull();
@@ -178,6 +208,64 @@ describe("rootGuard", () => {
expect(matched?.at(-1)?.route.handle).toEqual({ name: "auth.setup" });
});
+ test("allows the sole workspace admin to enter workspace setup", async () => {
+ session.isLoggedIn = true;
+ session.enableOnboarding = true;
+
+ expect(await runWorkspaceSetupLoader()).toBeNull();
+ });
+
+ test("redirects an ineligible user away from workspace setup", async () => {
+ session.isLoggedIn = true;
+
+ expect(location(await runWorkspaceSetupLoader())).toBe("/landing");
+ });
+
+ test("refreshes the member count before checking setup eligibility", async () => {
+ session.isLoggedIn = true;
+ session.enableOnboarding = true;
+ workspaceSetup.fetchServerInfo.mockImplementation(async () => {
+ session.enableOnboarding = false;
+ return {};
+ });
+
+ expect(location(await runWorkspaceSetupLoader())).toBe("/landing");
+ });
+
+ test("preserves a valid setup redirect for an ineligible user", async () => {
+ session.isLoggedIn = true;
+
+ expect(
+ location(
+ await runWorkspaceSetupLoader(
+ "/auth/setup?redirect=%2Fprojects%2Fexample"
+ )
+ )
+ ).toBe("/projects/example");
+ });
+
+ test("rejects an external setup redirect for an ineligible user", async () => {
+ session.isLoggedIn = true;
+
+ expect(
+ location(
+ await runWorkspaceSetupLoader(
+ "/auth/setup?redirect=https%3A%2F%2Fexample.com"
+ )
+ )
+ ).toBe("/landing");
+ });
+
+ test("redirects away from workspace setup when IAM loading fails", async () => {
+ session.isLoggedIn = true;
+ session.enableOnboarding = true;
+ workspaceSetup.fetchWorkspaceIamPolicy.mockRejectedValue(
+ new Error("policy unavailable")
+ );
+
+ expect(location(await runWorkspaceSetupLoader())).toBe("/landing");
+ });
+
test("oauth callback is allowed directly", () => {
expect(run(AUTH_OAUTH_CALLBACK_MODULE, "/auth/oauth/callback")).toBeNull();
});
diff --git a/frontend/src/app/router/guard.ts b/frontend/src/app/router/guard.ts
index 5a3ccbfe81..97fdc6f821 100644
--- a/frontend/src/app/router/guard.ts
+++ b/frontend/src/app/router/guard.ts
@@ -76,6 +76,32 @@ export function buildSigninRedirectQuery(url: URL): Record<string, string> {
return query;
}
+export async function workspaceSetupGuard(url: URL): Promise<Response | null> {
+ const store = useAppStore.getState();
+ if (!store.isLoggedIn()) {
+ return null;
+ }
+
+ try {
+ await Promise.all([
+ store.fetchServerInfo(),
+ store.fetchWorkspaceIamPolicy(true),
+ ]);
+ } catch {
+ return redirect(resolvePath(WORKSPACE_ROUTE_LANDING));
+ }
+ if (store.enableOnboarding()) {
+ return null;
+ }
+
+ const redirectParam = url.searchParams.get("redirect");
+ const target =
+ redirectParam?.startsWith("/") && !redirectParam.startsWith("//")
+ ? redirectParam
+ : resolvePath(WORKSPACE_ROUTE_LANDING);
+ return redirect(target);
+}
+
// Route-name prefixes that an authenticated user may always access.
const ALLOWED_ROUTE_PATTERNS = [
ACCOUNT_ROUTE,
diff --git a/frontend/src/app/router/routes/auth.tsx b/frontend/src/app/router/routes/auth.tsx
index 3bd873580e..f4329ce507 100644
--- a/frontend/src/app/router/routes/auth.tsx
+++ b/frontend/src/app/router/routes/auth.tsx
@@ -1,5 +1,6 @@
import type { RouteObject } from "react-router";
import { SplashLayout } from "@/app/layouts/SplashLayout";
+import { workspaceSetupGuard } from "@/app/router/guard";
import {
AUTH_2FA_SETUP_MODULE,
AUTH_MFA_MODULE,
@@ -89,6 +90,7 @@ export const authRoutes: RouteObject[] = [
{
path: "setup",
handle: { name: AUTH_SETUP_MODULE },
+ loader: ({ request }) => workspaceSetupGuard(new URL(request.url)),
lazy: lazyPage(
() => import("@/routes/auth/WorkspaceSetupPage"),
(m) => m.WorkspaceSetupPage
diff --git a/frontend/src/stores/app/auth.ts b/frontend/src/stores/app/auth.ts
index d86f3b464d..9248ed91e8 100644
--- a/frontend/src/stores/app/auth.ts
+++ b/frontend/src/stores/app/auth.ts
@@ -8,6 +8,7 @@ import {
AUTH_PASSWORD_RESET_MODULE,
AUTH_SETUP_MODULE,
AUTH_SIGNIN_MODULE,
+ WORKSPACE_ROUTE_LANDING,
} from "@/app/router/handles";
import {
navigateByName,
@@ -173,7 +174,8 @@ export const createAuthSlice: AppSliceCreator<AuthSlice> = (set, get) => ({
const redirectQuery = new URLSearchParams(window.location.search).get(
"redirect"
);
- const nextPage = redirectUrl ?? (redirectQuery || "/");
+ const explicitRedirect = redirectUrl ?? redirectQuery;
+ const nextPage = explicitRedirect || "/";
if (resp.mfaTempToken) {
set({ unauthenticatedOccurred: false });
navigateByName(AUTH_MFA_MODULE, {
@@ -209,9 +211,19 @@ export const createAuthSlice: AppSliceCreator<AuthSlice> = (set, get) => ({
return;
}
if (resp.user && needsProfileSetup(resp.user)) {
- navigateByName(AUTH_SETUP_MODULE, {
- query: { redirect: nextPage },
- });
+ set({ workspacePolicy: undefined });
+ await get()
+ .fetchWorkspaceIamPolicy(true)
+ .catch(() => undefined);
+ if (get().enableOnboarding()) {
+ navigateByName(AUTH_SETUP_MODULE, {
+ query: { redirect: nextPage },
+ });
+ } else if (explicitRedirect) {
+ navigateToPath(nextPage, { replace: true });
+ } else {
+ navigateByName(WORKSPACE_ROUTE_LANDING, { replace: true });
+ }
return;
}
if (redirect) {
@@ -237,9 +249,13 @@ export const createAuthSlice: AppSliceCreator<AuthSlice> = (set, get) => ({
}
await get().fetchServerInfo();
- // See `login()`. The profile must be available before the next route is
- // selected because the root redirect reads the database change mode.
- await get().loadWorkspaceProfile(true);
+ set({ workspacePolicy: undefined });
+ await Promise.all([
+ get().loadWorkspaceProfile(true),
+ get()
+ .fetchWorkspaceIamPolicy(true)
+ .catch(() => undefined),
+ ]);
set({ authSessionKey: uniqueId() });
if (get().enableOnboarding()) {
@@ -250,7 +266,11 @@ export const createAuthSlice: AppSliceCreator<AuthSlice> = (set, get) => ({
const redirectQuery = new URLSearchParams(window.location.search).get(
"redirect"
);
- navigateToPath(redirectQuery || "/", { replace: true });
+ if (redirectQuery) {
+ navigateToPath(redirectQuery, { replace: true });
+ return;
+ }
+ navigateByName(WORKSPACE_ROUTE_LANDING, { replace: true });
},
logout: async () => {
diff --git a/frontend/src/stores/app/index.test.ts b/frontend/src/stores/app/index.test.ts
index adb068a130..6c0ba60979 100644
--- a/frontend/src/stores/app/index.test.ts
+++ b/frontend/src/stores/app/index.test.ts
@@ -341,6 +341,16 @@ const user = createProto(UserSchema, {
workspace: "workspaces/default",
});
+const workspacePolicyForUser = (role: string) =>
+ createProto(IamPolicySchema, {
+ bindings: [
+ createProto(BindingSchema, {
+ role,
+ members: [`user:${user.email}`],
+ }),
+ ],
+ });
+
const projectA = createProto(ProjectSchema, {
name: "projects/a",
title: "A",
@@ -647,9 +657,13 @@ describe("useAppStore", () => {
mocks.getCurrentUser.mockResolvedValue(firstLoginUser);
mocks.getActuatorInfo.mockResolvedValue({
workspace: user.workspace,
+ userCountInIam: 1,
saas: false,
});
mocks.getWorkspace.mockResolvedValue({ name: user.workspace });
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceAdmin")
+ );
const store = createAppStore();
await store.getState().login({
@@ -661,6 +675,81 @@ describe("useAppStore", () => {
});
});
+ test("invited SaaS first login skips workspace setup and opens the workspace landing page", async () => {
+ vi.stubGlobal("location", {
+ search: "?workspace=default&email=alice%40example.com",
+ });
+ const firstLoginUser = createProto(UserSchema, {
+ ...user,
+ title: user.email,
+ });
+ mocks.login.mockResolvedValue({
+ requireResetPassword: false,
+ user: firstLoginUser,
+ });
+ mocks.getCurrentUser.mockResolvedValue(firstLoginUser);
+ mocks.getActuatorInfo.mockResolvedValue({
+ workspace: user.workspace,
+ userCountInIam: 2,
+ saas: true,
+ });
+ mocks.getWorkspace.mockResolvedValue({ name: user.workspace });
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceMember")
+ );
+ const store = createAppStore();
+
+ await store.getState().login({
+ request: { email: user.email, password: "secret" } as never,
+ });
+
+ expect(mocks.navigateByName).toHaveBeenCalledWith("workspace.landing", {
+ replace: true,
+ });
+ expect(mocks.navigateByName).not.toHaveBeenCalledWith(
+ "auth.setup",
+ expect.anything()
+ );
+ });
+
+ test("invited SaaS first login preserves an explicit redirect", async () => {
+ vi.stubGlobal("location", {
+ search:
+ "?workspace=default&email=alice%40example.com&redirect=%2Fprojects%2Ffoo",
+ });
+ const firstLoginUser = createProto(UserSchema, {
+ ...user,
+ title: user.email,
+ });
+ mocks.login.mockResolvedValue({
+ requireResetPassword: false,
+ user: firstLoginUser,
+ });
+ mocks.getCurrentUser.mockResolvedValue(firstLoginUser);
+ mocks.getActuatorInfo.mockResolvedValue({
+ workspace: user.workspace,
+ userCountInIam: 2,
+ saas: true,
+ });
+ mocks.getWorkspace.mockResolvedValue({ name: user.workspace });
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceMember")
+ );
+ const store = createAppStore();
+
+ await store.getState().login({
+ request: { email: user.email, password: "secret" } as never,
+ });
+
+ expect(mocks.navigateToPath).toHaveBeenCalledWith("/projects/foo", {
+ replace: true,
+ });
+ expect(mocks.navigateByName).not.toHaveBeenCalledWith(
+ "auth.setup",
+ expect.anything()
+ );
+ });
+
// Regression guard: `signup()` used to override the destination with the SQL
// Editor whenever the mode read EDITOR, with no check for an explicit
// redirect. That branch was dead (signup always boots signed out, so the mode
@@ -675,6 +764,9 @@ describe("useAppStore", () => {
workspace: user.workspace,
userCountInIam: 2,
});
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceMember")
+ );
mocks.getSetting.mockResolvedValue(
createProto(SettingSchema, {
value: createProto(SettingValueSchema, {
@@ -708,6 +800,9 @@ describe("useAppStore", () => {
userCountInIam: 1,
saas: false,
});
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceAdmin")
+ );
const store = createAppStore();
await store.getState().signup({
@@ -729,6 +824,9 @@ describe("useAppStore", () => {
userCountInIam: 1,
saas: true,
});
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceAdmin")
+ );
const store = createAppStore();
await store.getState().signup({
@@ -742,17 +840,101 @@ describe("useAppStore", () => {
});
});
- // Guards signup's own `loadWorkspaceProfile` call. The explicit-redirect test
- // above returns before `rootGuard` is consulted, so it passes with or without
- // the load; this one pins that a no-redirect signup hands the guard a loaded
- // profile, which is what sends an EDITOR workspace to the SQL Editor.
- test("signup loads the workspace profile before choosing the next page", async () => {
+ test("invited SaaS signup skips workspace setup and opens the workspace landing page", async () => {
mocks.signup.mockResolvedValue({});
mocks.getCurrentUser.mockResolvedValue(user);
mocks.getActuatorInfo.mockResolvedValue({
workspace: user.workspace,
userCountInIam: 2,
+ saas: true,
});
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceMember")
+ );
+ const store = createAppStore();
+
+ await store.getState().signup({
+ email: user.email,
+ name: "Test",
+ password: "secret",
+ } as never);
+
+ expect(mocks.navigateByName).toHaveBeenCalledWith("workspace.landing", {
+ replace: true,
+ });
+ expect(mocks.navigateByName).not.toHaveBeenCalledWith(
+ "auth.setup",
+ expect.anything()
+ );
+ });
+
+ test("non-admin signup skips workspace setup even with one IAM user", async () => {
+ mocks.signup.mockResolvedValue({});
+ mocks.getCurrentUser.mockResolvedValue(user);
+ mocks.getActuatorInfo.mockResolvedValue({
+ workspace: user.workspace,
+ userCountInIam: 1,
+ saas: true,
+ });
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceMember")
+ );
+ const store = createAppStore();
+
+ await store.getState().signup({
+ email: user.email,
+ name: "Test",
+ password: "secret",
+ } as never);
+
+ expect(mocks.navigateByName).toHaveBeenCalledWith("workspace.landing", {
+ replace: true,
+ });
+ expect(mocks.navigateByName).not.toHaveBeenCalledWith(
+ "auth.setup",
+ expect.anything()
+ );
+ });
+
+ test("signup skips workspace setup when the workspace policy cannot be loaded", async () => {
+ mocks.signup.mockResolvedValue({});
+ mocks.getCurrentUser.mockResolvedValue(user);
+ mocks.getActuatorInfo.mockResolvedValue({
+ workspace: user.workspace,
+ userCountInIam: 1,
+ saas: true,
+ });
+ mocks.getIamPolicy.mockRejectedValue(new Error("policy unavailable"));
+ const store = createAppStore();
+ store.setState({
+ workspacePolicy: workspacePolicyForUser("roles/workspaceAdmin"),
+ });
+
+ await store.getState().signup({
+ email: user.email,
+ name: "Test",
+ password: "secret",
+ } as never);
+
+ expect(mocks.navigateByName).toHaveBeenCalledWith("workspace.landing", {
+ replace: true,
+ });
+ expect(mocks.navigateByName).not.toHaveBeenCalledWith(
+ "auth.setup",
+ expect.anything()
+ );
+ });
+
+ test("signup loads the workspace profile before opening the landing page", async () => {
+ mocks.signup.mockResolvedValue({});
+ mocks.getCurrentUser.mockResolvedValue(user);
+ mocks.getActuatorInfo.mockResolvedValue({
+ workspace: user.workspace,
+ userCountInIam: 2,
+ });
+ mocks.getIamPolicy.mockResolvedValue(
+ workspacePolicyForUser("roles/workspaceMember")
+ );
mocks.getSetting.mockResolvedValue(
createProto(SettingSchema, {
value: createProto(SettingValueSchema, {
@@ -776,7 +958,9 @@ describe("useAppStore", () => {
expect(
store.getState().appFeatures["bb.feature.database-change-mode"]
).toBe(DatabaseChangeMode.EDITOR);
- expect(mocks.navigateToPath).toHaveBeenCalledWith("/", { replace: true });
+ expect(mocks.navigateByName).toHaveBeenCalledWith("workspace.landing", {
+ replace: true,
+ });
});
test("lists groups and populates the group cache", async () => {
diff --git a/frontend/src/stores/app/workspace.ts b/frontend/src/stores/app/workspace.ts
index a75d5fbc63..639356a80a 100644
--- a/frontend/src/stores/app/workspace.ts
+++ b/frontend/src/stores/app/workspace.ts
@@ -687,7 +687,17 @@ export const createWorkspaceSlice: AppSliceCreator<WorkspaceSlice> = (
activeVcsUserCount: () => get().serverInfo?.activeVcsUserCount ?? 0,
- enableOnboarding: () => get().userCountInIam() === 1,
+ enableOnboarding: () => {
+ const currentUserName = get().currentUser?.name;
+ return (
+ get().userCountInIam() === 1 &&
+ get().workspacePolicy !== undefined &&
+ !!currentUserName &&
+ get()
+ .getWorkspaceRolesByName(currentUserName)
+ .has(PresetRoleType.WORKSPACE_ADMIN)
+ );
+ },
workspaceSetupGuideEnabled: (allowMultipleMembers = false) => {
const currentUserName = get().currentUser?.name;