Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ›‚ Applies header auth checks to the alias load route

Alicia Sykes28f5cce1c6b0
diff --git a/src/router.js b/src/router.js
index 70246ae8..5fab261e 100644
--- a/src/router.js
+++ b/src/router.js
@@ -18,7 +18,7 @@ import Keys from '@/utils/StoreMutations';
import { isAuthEnabled, isLoggedIn, isGuestAccessEnabled } from '@/utils/auth/Auth';
import { isOidcEnabled } from '@/utils/auth/OidcAuth';
import { isKeycloakEnabled } from '@/utils/auth/KeycloakAuth';
-import { isHeaderAuthEnabled } from '@/utils/auth/HeaderAuth';
+import { initHeaderAuth, isHeaderAuthEnabled } from '@/utils/auth/HeaderAuth';
import { startingView as defaultStartingView, routePaths } from '@/utils/config/defaults';
import { VIEW_META, getUrlForAlias } from '@/utils/config/ConfigHelpers';
import ErrorHandler from '@/utils/logging/ErrorHandler';
@@ -61,6 +61,12 @@ const isSelfReferential = (url) => {
} catch { return false; }
};
+/* Checks wheaather header auth is enabled, before the alias is allowed to access items */
+const identityResolved = async () => {
+ if (!isHeaderAuthEnabled() || isLoggedIn()) return true;
+ return initHeaderAuth().then(() => true, () => false);
+};
+
/* Build the canonical /<view>/:page?/:section? routes for a given view + component.
* withSection=false for workspace (no single-section view yet). Page meta is
* owned by App.vue's watcher via PageMeta.js β€” routes don't carry titles. */
@@ -137,8 +143,9 @@ const router = createRouter({
path: '/:alias',
name: 'alias',
component: () => import('./views/404.vue'),
- beforeEnter: (to, from, next) => {
- const url = getUrlForAlias(store.state.rootConfig?.sections, to.params.alias);
+ beforeEnter: async (to, from, next) => {
+ const url = (await identityResolved())
+ ? getUrlForAlias(store.state.rootConfig?.sections, to.params.alias) : undefined;
const loops = !!url && isSelfReferential(url);
if (loops) ErrorHandler(`Alias '${to.params.alias}' points back at itself, not redirecting`);
if (!url || loops) { next('/404'); return; }
diff --git a/tests/unit/alias-route-guard.test.js b/tests/unit/alias-route-guard.test.js
new file mode 100644
index 00000000..01b12c03
--- /dev/null
+++ b/tests/unit/alias-route-guard.test.js
@@ -0,0 +1,74 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+let currentUser = false;
+let headerAuthOn = true;
+const sections = [{
+ items: [
+ { title: 'Bobs App', url: 'https://bob.local', alias: 'bobapp', displayData: { showForUsers: ['bob'] } },
+ ],
+}];
+
+/* Header auth only names the user once /get-user answers, well after the router starts */
+const resolveAs = (user) => () => Promise.resolve().then(() => { currentUser = { user }; });
+const initHeaderAuth = vi.fn(resolveAs('alice'));
+
+vi.mock('@/utils/auth/HeaderAuth', () => ({
+ isHeaderAuthEnabled: () => headerAuthOn,
+ initHeaderAuth: (...args) => initHeaderAuth(...args),
+}));
+vi.mock('@/utils/auth/Auth', () => ({
+ getCurrentUser: () => currentUser,
+ isLoggedIn: () => !!currentUser,
+ isLoggedInAsGuest: () => false,
+ isAuthEnabled: () => headerAuthOn || !!currentUser,
+ isGuestAccessEnabled: () => false,
+}));
+vi.mock('@/views/Home.vue', () => ({ default: { template: '<div />' } }));
+vi.mock('@/store', () => ({
+ default: {
+ state: { rootConfig: { sections }, config: { appConfig: {} } },
+ dispatch: vi.fn(),
+ commit: vi.fn(),
+ },
+}));
+
+const { default: router } = await import('@/router');
+
+/* An alias redirect deliberately aborts the navigation, so a rejection here is expected */
+const visit = (path) => router.push(path).catch(() => {});
+
+describe('alias route guard', () => {
+ beforeEach(() => {
+ currentUser = false;
+ headerAuthOn = true;
+ window.location.replace = vi.fn();
+ initHeaderAuth.mockClear();
+ });
+
+ it.each([
+ ['a user the item is hidden from', resolveAs('alice')],
+ ['nobody at all', () => Promise.reject(Error('User from upstream proxy was not found'))],
+ ])('does not redirect when header auth resolves %s', async (_label, init) => {
+ initHeaderAuth.mockImplementationOnce(init);
+ await visit('/bobapp');
+ expect(window.location.replace).not.toHaveBeenCalled();
+ expect(router.currentRoute.value.path).toBe('/404');
+ });
+
+ it('redirects once the resolved user is allowed to see the item', async () => {
+ initHeaderAuth.mockImplementationOnce(resolveAs('bob'));
+ await visit('/bobapp');
+ expect(window.location.replace).toHaveBeenCalledWith('https://bob.local');
+ });
+
+ it.each([
+ ['header auth is not in use', false, false],
+ ['the user is already known', true, { user: 'bob' }],
+ ])('redirects without waiting when %s', async (_label, headerAuth, user) => {
+ headerAuthOn = headerAuth;
+ currentUser = user;
+ await visit('/bobapp');
+ expect(initHeaderAuth).not.toHaveBeenCalled();
+ expect(window.location.replace).toHaveBeenCalledWith('https://bob.local');
+ });
+});