Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: router navigation and security issues (#17195)
boojackd3e221553415
diff --git a/frontend/src/AuthContext.vue b/frontend/src/AuthContext.vueindex 0d01ce301e..b9c5eb3c93 100644--- a/frontend/src/AuthContext.vue+++ b/frontend/src/AuthContext.vue@@ -9,7 +9,6 @@ import { useRouter } from "vue-router";import { isAuthRelatedRoute } from "@/utils/auth";import SigninModal from "@/views/auth/SigninModal.vue";import { t } from "./plugins/i18n";-import { AUTH_PASSWORD_RESET_MODULE } from "./router/auth";import { WORKSPACE_ROOT_MODULE } from "./router/dashboard/workspaceRoutes";import {useAuthStore,@@ -96,14 +95,6 @@ watch(// we only care about the groups for the current user.groupStore.batchFetchGroups(currentUser.value.groups),]);-- // If the user is required to reset their password, redirect them to the reset password page.- if (authStore.requireResetPassword) {- router.replace({- name: AUTH_PASSWORD_RESET_MODULE,- });- return;- }},{immediate: true,diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vueindex 7428eef6aa..a4386a68a9 100644--- a/frontend/src/components/ProvideDashboardContext.vue+++ b/frontend/src/components/ProvideDashboardContext.vue@@ -5,16 +5,7 @@<script lang="ts" setup>import { ref, onMounted } from "vue";-import { onUnmounted } from "vue";import { useRouter } from "vue-router";-import {- AUTH_MFA_MODULE,- AUTH_PASSWORD_FORGOT_MODULE,- AUTH_PASSWORD_RESET_MODULE,- AUTH_SIGNIN_ADMIN_MODULE,- AUTH_SIGNIN_MODULE,- AUTH_SIGNUP_MODULE,-} from "@/router/auth";import {useEnvironmentV1Store,usePolicyV1Store,@@ -29,7 +20,6 @@ const isInitializing = ref<boolean>(true);const policyStore = usePolicyV1Store();-let unregisterBeforeEachHook: (() => void) | undefined;onMounted(async () => {await router.isReady();@@ -47,37 +37,5 @@ onMounted(async () => {useUIStateStore().restoreState();isInitializing.value = false;-- unregisterBeforeEachHook = router.beforeEach(async (to, from, next) => {- if (- to.name === AUTH_SIGNIN_MODULE ||- to.name === AUTH_SIGNIN_ADMIN_MODULE ||- to.name === AUTH_SIGNUP_MODULE ||- to.name === AUTH_MFA_MODULE ||- to.name === AUTH_PASSWORD_FORGOT_MODULE ||- to.name === AUTH_PASSWORD_RESET_MODULE- ) {- next();- return;- }-- const fromProject = from.params.projectId as string;- const toProject = to.params.projectId as string;- if (fromProject !== toProject) {- console.debug(- `[ProvideDashboardContext] project switched ${fromProject} -> ${toProject}`- );- next();- return;- }-- next();- });-});--onUnmounted(() => {- if (unregisterBeforeEachHook) {- unregisterBeforeEachHook();- }});</script>diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.tsindex 111f7d713b..3ee30959b9 100644--- a/frontend/src/router/index.ts+++ b/frontend/src/router/index.ts@@ -19,17 +19,13 @@ import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";import { isAuthRelatedRoute } from "@/utils/auth";import authRoutes, {AUTH_2FA_SETUP_MODULE,- AUTH_MFA_MODULE,AUTH_OAUTH_CALLBACK_MODULE,AUTH_OIDC_CALLBACK_MODULE,- AUTH_PASSWORD_FORGOT_MODULE,AUTH_PASSWORD_RESET_MODULE,- AUTH_SIGNIN_ADMIN_MODULE,AUTH_SIGNIN_MODULE,AUTH_SIGNUP_MODULE,} from "./auth";import dashboardRoutes from "./dashboard";-import { INSTANCE_ROUTE_DETAIL } from "./dashboard/instance";import {DATABASE_ROUTE_DASHBOARD,ENVIRONMENT_V1_ROUTE_DASHBOARD,@@ -38,7 +34,7 @@ import {WORKSPACE_ROOT_MODULE,} from "./dashboard/workspaceRoutes";import { SETTING_ROUTE } from "./dashboard/workspaceSetting";-import setupRoutes from "./setup";+import setupRoutes, { SETUP_MODULE } from "./setup";import sqlEditorRoutes from "./sqlEditor";export const router = createRouter({@@ -70,32 +66,15 @@ export const router = createRouter({router.beforeEach((to, from, next) => {console.debug("Router %s -> %s", from.name, to.name);- const actuatorStore = useActuatorV1Store();- const authStore = useAuthStore();- const routerStore = useRouterStore();+ // === SPECIAL CASES - Allow direct access ===- if (- isAuthRelatedRoute(to.name as string) &&- authStore.isLoggedIn &&- !authStore.unauthenticatedOccurred &&- !authStore.requireResetPassword- ) {- const redirect = (to.query["redirect"] as string) || "/";- next(redirect);+ // Prevent infinite loops only for identical routes without parameter changes+ if (from.name === to.name && from.fullPath === to.fullPath) {+ next(false);return;}- const fromModule = from.name- ? from.name.toString().split(".")[0]- : WORKSPACE_ROOT_MODULE;- const toModule = to.name- ? to.name.toString().split(".")[0]- : WORKSPACE_ROOT_MODULE;-- if (toModule != fromModule) {- routerStore.setBackPath(from.fullPath);- }-+ // Error pages can be accessed directlyif (to.name === "error.403" ||to.name === "error.404" ||@@ -105,9 +84,7 @@ router.beforeEach((to, from, next) => {return;}- // SSO callback routes are relayes to handle the IdP callback and dispatch the subsequent events.- // They are called in the following scenarios:- // - Login via OAuth / OIDC+ // Auth callbacks can be accessed directlyif (to.name === AUTH_OAUTH_CALLBACK_MODULE ||to.name === AUTH_OIDC_CALLBACK_MODULE@@ -116,14 +93,45 @@ router.beforeEach((to, from, next) => {return;}+ const actuatorStore = useActuatorV1Store();+ const authStore = useAuthStore();+ const routerStore = useRouterStore();++ // Allow access to 2FA setup and password reset for logged-in usersif (- to.name === AUTH_SIGNIN_MODULE ||- to.name === AUTH_SIGNIN_ADMIN_MODULE ||- to.name === AUTH_SIGNUP_MODULE ||- to.name === AUTH_MFA_MODULE ||- to.name === AUTH_PASSWORD_RESET_MODULE ||- to.name === AUTH_PASSWORD_FORGOT_MODULE+ authStore.isLoggedIn &&+ (to.name === AUTH_2FA_SETUP_MODULE ||+ to.name === AUTH_PASSWORD_RESET_MODULE)) {+ next();+ return;+ }++ // === MODULE NAVIGATION TRACKING ===++ const fromModule =+ from.name?.toString().split(".")[0] || WORKSPACE_ROOT_MODULE;+ const toModule = to.name?.toString().split(".")[0] || WORKSPACE_ROOT_MODULE;++ if (toModule !== fromModule) {+ routerStore.setBackPath(from.fullPath);+ }++ // === AUTHENTICATION LOGIC ===++ // If user is logged in and tries to access auth pages, redirect to main app+ if (+ isAuthRelatedRoute(to.name as string) &&+ authStore.isLoggedIn &&+ !authStore.unauthenticatedOccurred+ ) {+ const redirect = (to.query["redirect"] as string) || "/";+ next(redirect);+ return;+ }++ // Auth pages: Reset stores and allow access+ if (isAuthRelatedRoute(to.name as string)) {useDatabaseV1Store().reset();useProjectV1Store().reset();useInstanceV1Store().reset();@@ -132,110 +140,136 @@ router.beforeEach((to, from, next) => {});next();return;- } else {- if (!authStore.isLoggedIn) {- const query: LocationQueryRaw = {- ...(to.query || {}),- };- if (to.fullPath !== "/") {- if (to.query["idp"]) {- // TODO: remove query param `idp` from fullPath.- query["idp"] = to.query["idp"];- }- if (!to.query["redirect"]) {- query["redirect"] = to.fullPath;- }+ }++ // Require authentication for all other pages+ if (!authStore.isLoggedIn) {+ const query: LocationQueryRaw = {};++ // Preserve important query parameters+ if (to.query) {+ // Preserve IDP parameter for OAuth flows+ if (to.query["idp"]) {+ query["idp"] = to.query["idp"];}+ // Preserve other auth-related parameters+ if (to.query["token"]) {+ query["token"] = to.query["token"];+ }+ if (to.query["invitation"]) {+ query["invitation"] = to.query["invitation"];+ }+ }- next({- name: AUTH_SIGNIN_MODULE,- query: query,- replace: true,- });- return;+ // Set redirect URL if not root path and not already set+ if (to.fullPath !== "/" && !to.query["redirect"]) {+ query["redirect"] = to.fullPath;}++ next({+ name: AUTH_SIGNIN_MODULE,+ query,+ replace: true,+ });+ return;}- // If there is a `redirect` in query param and prev page is signin or signup, redirect to the target route+ // === POST-AUTH LOGIC ===++ // Handle redirect after successful authenticationif ((from.name === AUTH_SIGNIN_MODULE || from.name === AUTH_SIGNUP_MODULE) &&typeof from.query.redirect === "string") {- window.location.href = from.query.redirect;- return;- }-- if (to.name === AUTH_2FA_SETUP_MODULE) {- next();+ const redirectUrl = from.query.redirect;+ // Validate redirect URL to prevent external redirects and XSS+ if (redirectUrl.startsWith("/") && !redirectUrl.startsWith("//")) {+ next(redirectUrl);+ } else {+ next("/");+ }return;}+ // Enforce 2FA setup if requiredconst currentUserV1 = useCurrentUserV1();-- // If 2FA is required, redirect to MFA setup page if the user has not enabled 2FA.if (hasFeature(PlanFeature.FEATURE_TWO_FA) &&- actuatorStore.serverInfo?.require2fa+ actuatorStore.serverInfo?.require2fa &&+ currentUserV1.value &&+ !currentUserV1.value.mfaEnabled &&+ to.name !== AUTH_2FA_SETUP_MODULE // Prevent redirect loop) {- const user = currentUserV1.value;- if (user && !user.mfaEnabled) {- next({- name: AUTH_2FA_SETUP_MODULE,- replace: true,- });- return;- }+ next({+ name: AUTH_2FA_SETUP_MODULE,+ replace: true,+ });+ return;}+ // Enforce password reset if requiredif (- to.name?.toString().startsWith(ENVIRONMENT_V1_ROUTE_DASHBOARD) ||- to.name?.toString().startsWith(INSTANCE_ROUTE_DASHBOARD) ||- to.name?.toString().startsWith(PROJECT_V1_ROUTE_DASHBOARD) ||- to.name?.toString().startsWith(DATABASE_ROUTE_DASHBOARD) ||- to.name === INSTANCE_ROUTE_DETAIL ||- to.name?.toString().startsWith("sql-editor") ||- to.name?.toString().startsWith(SETTING_ROUTE) ||- to.name?.toString().startsWith("workspace") ||- to.name?.toString().startsWith("setup") ||- to.name === AUTH_PASSWORD_RESET_MODULE+ authStore.requireResetPassword &&+ to.name !== AUTH_PASSWORD_RESET_MODULE // Prevent redirect loop) {- next();+ next({+ name: AUTH_PASSWORD_RESET_MODULE,+ replace: true,+ });return;}- // We may just change the anchor (e.g. in Issue Detail view), thus we don't need- // to fetch the data to verify its existence since we have already verified before.- if (to.path == from.path) {- next();- return;- }+ // === ROUTE ACCESS CONTROL ===- const routerSlug = routerStore.routeSlug(to);- const issueSlug = routerSlug.issueSlug;- const connectionSlug = routerSlug.connectionSlug;- const sheetSlug = routerSlug.sheetSlug;+ // Allow access to main application routes+ const allowedRoutePatterns = [+ ENVIRONMENT_V1_ROUTE_DASHBOARD,+ INSTANCE_ROUTE_DASHBOARD,+ PROJECT_V1_ROUTE_DASHBOARD,+ DATABASE_ROUTE_DASHBOARD,+ SETTING_ROUTE,+ SETUP_MODULE,+ "workspace",+ "sql-editor",+ ];- if (issueSlug) {- // We've moved the preparation data fetch jobs into IssueDetail page- // so just next() here.+ if (+ allowedRoutePatterns.some((pattern) =>+ to.name?.toString().startsWith(pattern)+ )+ ) {next();return;}- if (sheetSlug) {- // We've moved the preparation data fetch jobs into ProvideSQLEditorContext.- // so just next() here.+ // Allow same-path navigation (e.g. anchor changes)+ if (to.path === from.path) {next();return;}- if (connectionSlug) {- // We've moved the preparation data fetch jobs into ProvideSQLEditorContext.- // so just next() here.+ // === DYNAMIC ROUTE HANDLING ===++ const routerSlug = routerStore.routeSlug(to);+ if (+ routerSlug.issueSlug ||+ routerSlug.sheetSlug ||+ routerSlug.connectionSlug+ ) {+ // Data fetching is handled by respective componentsnext();return;}+ // === FALLBACK ===++ // Log unexpected route for debugging+ console.warn("Router: Unexpected route", {+ to: to.name,+ path: to.path,+ fullPath: to.fullPath,+ });+next({name: "error.404",replace: false,