From 773b675ef502c7aee4fe6527c89c690720562091 Mon Sep 17 00:00:00 2001 From: "HomePC\\Kevin" Date: Fri, 4 Dec 2026 07:36:14 +0100 Subject: [PATCH 1/3] fix(apps): align page links and route access --- .../Components/InstalledAppCard.tsx | 3 +- .../AppCatalog/Hooks/useAppCatalogLogic.ts | 11 +--- .../AppCatalog/Types/app-catalog.types.ts | 2 - web/src/features/AppUi/AppUiFrame.tsx | 8 ++- .../AppUi/Components/AppFrameView.tsx | 5 +- .../AppUi/Components/PooledAppFrameView.tsx | 10 ++-- .../Hooks/useAppHostActionDialogLogic.ts | 6 ++- .../AppUi/Hooks/usePrewarmAppHostAction.ts | 9 +++- .../QuickSearch/Hooks/useQuickSearchLogic.ts | 12 ++--- .../Hooks/useApplicationNavigationLogic.ts | 8 +-- web/src/lib/AppUi/bridge.ts | 4 +- web/src/lib/AppUi/contract.ts | 3 +- web/src/lib/AppUi/extensions.ts | 27 ++++++---- web/src/lib/AppUi/page-access.ts | 39 +++++++++++---- .../apps.$installationId.{-$routeId}.tsx | 4 +- .../AppCatalog/catalog-install-ui.test.tsx | 4 +- .../features/AppUi/app-frame-pool.test.tsx | 14 ++++-- .../features/AppUi/app-ui-frame.test.tsx | 50 ++++++++++++++++++- .../QuickSearch/quick-search-ui.test.tsx | 26 +++++++++- .../application-shell.test.tsx | 13 ++++- web/src/tests/lib/AppUi/extensions.test.ts | 17 ++++++- web/src/tests/lib/AppUi/page-access.test.ts | 43 +++++++++++++++- 22 files changed, 249 insertions(+), 69 deletions(-) diff --git a/web/src/features/AppCatalog/Components/InstalledAppCard.tsx b/web/src/features/AppCatalog/Components/InstalledAppCard.tsx index 178a3f18c..11af19b90 100644 --- a/web/src/features/AppCatalog/Components/InstalledAppCard.tsx +++ b/web/src/features/AppCatalog/Components/InstalledAppCard.tsx @@ -36,7 +36,6 @@ export default function InstalledAppCard({ name, publisher, canOpen, - openRouteId, actions, blockedRelease, updateRequiresSource, @@ -113,7 +112,7 @@ export default function InstalledAppCard({ to="/apps/$installationId/{-$routeId}" params={{ installationId: installation.installationId, - routeId: openRouteId, + routeId: undefined, }} search={{}} className={buttonClassName('secondary')} diff --git a/web/src/features/AppCatalog/Hooks/useAppCatalogLogic.ts b/web/src/features/AppCatalog/Hooks/useAppCatalogLogic.ts index d15b75761..d715ea2b6 100644 --- a/web/src/features/AppCatalog/Hooks/useAppCatalogLogic.ts +++ b/web/src/features/AppCatalog/Hooks/useAppCatalogLogic.ts @@ -2,7 +2,7 @@ import { toast } from '@rentnerkev/toasts' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { useEffect, useRef, useState } from 'react' import { useAppUi } from '@/features/AppUi/AppUiProvider.tsx' -import { appUiDefaultRoute } from '@/lib/AppUi/bridge.ts' +import { appPageDefaultRoute } from '@/lib/AppUi/page-access.ts' import { isPackageSourceOutage, sameCatalogLifecycle, @@ -164,17 +164,10 @@ export default function useAppCatalogLogic() { installation, name: installation.name ?? declaredApp?.name ?? app?.name ?? installation.appId, publisher: declaredApp?.publisher, - // An app that declares extensions opens only through a page it declares. canOpen: installation.state === 'running' && app?.appId === installation.appId && - (app.extensionCatalog === undefined || app.pages.length > 0), - openRouteId: - app?.extensionCatalog !== undefined && - app.pages[0] && - app.pages[0].route !== appUiDefaultRoute(app) - ? app.pages[0].route - : undefined, + appPageDefaultRoute(app) !== undefined, actions: catalogInstallationActions( installation, status === 'ready', diff --git a/web/src/features/AppCatalog/Types/app-catalog.types.ts b/web/src/features/AppCatalog/Types/app-catalog.types.ts index 15ca3ae3f..4f97d61e3 100644 --- a/web/src/features/AppCatalog/Types/app-catalog.types.ts +++ b/web/src/features/AppCatalog/Types/app-catalog.types.ts @@ -12,8 +12,6 @@ export type CatalogInstallationView = { name: string publisher: string | undefined canOpen: boolean - /** The route of the app's first page, or undefined for its default route. */ - openRouteId: string | undefined actions: CatalogLifecycleInput[] /** The newest listed release the appliance cannot offer, with the reason. */ blockedRelease: CatalogBlockedRelease | undefined diff --git a/web/src/features/AppUi/AppUiFrame.tsx b/web/src/features/AppUi/AppUiFrame.tsx index a8e2a4718..23afccccd 100644 --- a/web/src/features/AppUi/AppUiFrame.tsx +++ b/web/src/features/AppUi/AppUiFrame.tsx @@ -1,6 +1,7 @@ import { useCallback, useEffect, useEffectEvent, useMemo, useState } from 'react' import { appUiDefaultRoute } from '@/lib/AppUi/bridge.ts' +import { isAppUiRouteAllowed } from '@/lib/AppUi/page-access.ts' import type { AppUiSlot, AppUiSummary } from '@/lib/AppUi/contract.ts' import { APP_UI_PREFIX } from '@/lib/AppUi/paths.ts' import { supportsRemoteRendering, type AppUiRevocation } from '@/lib/AppUi/session.ts' @@ -31,9 +32,12 @@ export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill const sessionId = useAppUi()?.sessionId const [localRoute, setLocalRoute] = useState() const [attempt, setAttempt] = useState(0) + const defaultRoute = appUiDefaultRoute(app) const current = route ?? - (localRoute && app.routes.includes(localRoute) ? localRoute : appUiDefaultRoute(app)) + [localRoute, defaultRoute, ...app.routes].find( + (candidate) => candidate !== undefined && isAppUiRouteAllowed(app, candidate), + ) // A new generation, a reconnect or a retry remounts the view below; drafts the user did not // save pass to the next view of the same session, app, slot and host, never to another. const identity = JSON.stringify([ @@ -46,7 +50,7 @@ export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill const [kept] = useState(() => ({ current: null })) const handoff = useMemo(() => draftHandoff(identity, kept), [identity, kept]) - if (!sessionId) return null + if (!sessionId || current === undefined || !isAppUiRouteAllowed(app, current)) return null // Routing updates the open document through the bridge. return ( diff --git a/web/src/features/AppUi/Components/AppFrameView.tsx b/web/src/features/AppUi/Components/AppFrameView.tsx index 3646aec77..3947a411e 100644 --- a/web/src/features/AppUi/Components/AppFrameView.tsx +++ b/web/src/features/AppUi/Components/AppFrameView.tsx @@ -1,6 +1,7 @@ import { useCallback, useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from 'react' import type { AppUiRendering } from '@/lib/AppUi/bridge.ts' +import { isAppUiRouteAllowed } from '@/lib/AppUi/page-access.ts' import { createRemoteUiStore, type RemoteUiEvent } from '@/lib/AppUi/remote-tree.ts' import { bindAppUiFrame, @@ -47,7 +48,9 @@ export default function AppFrameView({ })) // Keys match the manifest pattern, so the joined list changes exactly when the keys do. const permissionScope = app.permissionKeys.join(' ') - const navigate = useEffectEvent(onNavigate) + const navigate = useEffectEvent((target: string) => { + if (isAppUiRouteAllowed(app, target)) onNavigate(target) + }) const dialog = useEffectEvent(onDialog) const request = useEffectEvent(onApiRequest) const revoke = useEffectEvent((reason: AppUiRevocation) => { diff --git a/web/src/features/AppUi/Components/PooledAppFrameView.tsx b/web/src/features/AppUi/Components/PooledAppFrameView.tsx index 74381c92c..ab87b7472 100644 --- a/web/src/features/AppUi/Components/PooledAppFrameView.tsx +++ b/web/src/features/AppUi/Components/PooledAppFrameView.tsx @@ -9,6 +9,7 @@ import { import type { RemoteUiEvent } from '@/lib/AppUi/remote-tree.ts' import type { AppUiRemoteEventDetail } from '@/lib/AppUi/session.ts' +import { isAppUiRouteAllowed } from '@/lib/AppUi/page-access.ts' import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts' import { ModalActionButton } from '@/shared/Modal/Components/ModalActions.tsx' import type { @@ -47,12 +48,15 @@ export default function PooledAppFrameView({ readonly onRetry: () => void }) { const { t, locale } = useTranslationStore() - const view = useRef({ route: request.route, onNavigate }) + const { app } = request + const handleNavigate = (target: string) => { + if (isAppUiRouteAllowed(app, target)) onNavigate(target) + } + const view = useRef({ route: request.route, onNavigate: handleNavigate }) useLayoutEffect(() => { - view.current = { route: request.route, onNavigate } + view.current = { route: request.route, onNavigate: handleNavigate } }) const [frame, setFrame] = useState() - const { app } = request const opened = useRef(request) // The view takes the frame opened for its request, or opens one, and closes it when it goes. diff --git a/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts b/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts index e73b9a6bd..462ff12c4 100644 --- a/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts +++ b/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts @@ -1,6 +1,7 @@ import { useState } from 'react' import { nativeText } from '@/lib/AppUi/native-presentation.ts' +import { isAppUiRouteAllowed } from '@/lib/AppUi/page-access.ts' import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts' import { useAppUi } from '../AppUiProvider.tsx' import type { AppHostActionDialogProps } from '../Types/app-host-table.types.ts' @@ -18,7 +19,10 @@ export default function useAppHostActionDialogLogic({ const action = app?.hostActions.find((entry) => entry.id === actionId) const [navigation, setNavigation] = useState<{ action: string; route: string }>() // The app may navigate inside the dialog; a new action starts at its own route again. - const route = navigation?.action === actionId ? navigation.route : action?.route + const route = + app && navigation?.action === actionId && isAppUiRouteAllowed(app, navigation.route) + ? navigation.route + : action?.route return { state: { diff --git a/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts b/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts index c4d5ede95..f1c16469c 100644 --- a/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts +++ b/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts @@ -1,5 +1,6 @@ import { useCallback } from 'react' +import { isAppUiRouteAllowed } from '@/lib/AppUi/page-access.ts' import { supportsRemoteRendering } from '@/lib/AppUi/session.ts' import { useAppUi } from '../AppUiProvider.tsx' import type { AppHostTableAction } from '../Types/app-host-table.types.ts' @@ -13,7 +14,13 @@ export default function usePrewarmAppHostAction() { return useCallback( (hostId: string, action: Pick) => { const app = ui?.apps.find((entry) => entry.installationId === action.installationId) - if (!ui?.sessionId || !app || !supportsRemoteRendering(app)) return + if ( + !ui?.sessionId || + !app || + !supportsRemoteRendering(app) || + !isAppUiRouteAllowed(app, action.route) + ) + return ui.framePool.prewarm({ app, sessionId: ui.sessionId, diff --git a/web/src/features/QuickSearch/Hooks/useQuickSearchLogic.ts b/web/src/features/QuickSearch/Hooks/useQuickSearchLogic.ts index 49c998448..3ea20b36d 100644 --- a/web/src/features/QuickSearch/Hooks/useQuickSearchLogic.ts +++ b/web/src/features/QuickSearch/Hooks/useQuickSearchLogic.ts @@ -21,7 +21,7 @@ import { } from '@/lib/QuickSearch/quickSearchActions.ts' import { useQuickActions } from '@/shared/QuickActions/Hooks/useQuickActions.ts' import { useAppUi } from '@/features/AppUi/AppUiProvider.tsx' -import { appUiDefaultRoute } from '@/lib/AppUi/bridge.ts' +import { appPageDefaultRoute, appPageRoute } from '@/lib/AppUi/page-access.ts' import { nativeText } from '@/lib/AppUi/native-presentation.ts' import { searchProxyHostsHandler } from '@/features/Admin/ProxyHostManagement/middleware.ts' import { searchRedirectHostsHandler } from '@/features/Admin/RedirectHostManagement/middleware.ts' @@ -216,11 +216,11 @@ export default function useQuickSearchLogic({ ) // Re-check that the page, or the slot-only search entry, is still offered. const page = app?.pages.find((candidate) => candidate.id === result.pageId) + const slot = result.pageId === undefined ? 'search' : undefined if ( !app || - (result.pageId === undefined - ? app.extensionCatalog !== undefined || !app.slots.includes('search') - : !page) + (result.pageId !== undefined && !page) || + !appPageRoute(app, page?.route, slot) ) throw new Error('unavailable') if (attempt.current !== selection || selectionScope.current !== scope) return @@ -229,9 +229,9 @@ export default function useQuickSearchLogic({ params: { installationId: app.installationId, routeId: - !page || page.route === appUiDefaultRoute(app) ? undefined : page.route, + page?.route === appPageDefaultRoute(app) ? undefined : page?.route, }, - search: page ? {} : { slot: 'search' }, + search: slot ? { slot } : {}, }) } else { await navigate({ to: result.to }) diff --git a/web/src/layouts/AuthenticatedLayout/Components/ApplicationShell/Components/ApplicationNavigation/Hooks/useApplicationNavigationLogic.ts b/web/src/layouts/AuthenticatedLayout/Components/ApplicationShell/Components/ApplicationNavigation/Hooks/useApplicationNavigationLogic.ts index eb6ecf88b..05eca34fe 100644 --- a/web/src/layouts/AuthenticatedLayout/Components/ApplicationShell/Components/ApplicationNavigation/Hooks/useApplicationNavigationLogic.ts +++ b/web/src/layouts/AuthenticatedLayout/Components/ApplicationShell/Components/ApplicationNavigation/Hooks/useApplicationNavigationLogic.ts @@ -7,7 +7,7 @@ import { useId, useState } from 'react' import { NAVIGATION_GROUP_IDS } from '@/config/navigation.config.ts' import { useAppUi } from '@/features/AppUi/AppUiProvider.tsx' -import { appUiDefaultRoute } from '@/lib/AppUi/bridge.ts' +import { appPageDefaultRoute } from '@/lib/AppUi/page-access.ts' import { parseStoredNavigationGroupPreferences } from '@/lib/Navigation/navigationPreferences.ts' import type { NavigationGroupId } from '@/config/Types/navigation-config.types.ts' @@ -62,12 +62,12 @@ export default function useApplicationNavigationLogic( const appPages = apps.flatMap((app) => { const basePath = `/apps/${encodeURIComponent(app.installationId)}` const inApp = pathname === basePath || pathname.startsWith(`${basePath}/`) - const defaultRoute = appUiDefaultRoute(app) + const defaultRoute = appPageDefaultRoute(app) const currentRoute = pathname.slice(basePath.length + 1).split('/')[0] || defaultRoute - // A route the app declares but offers no page for keeps its first page highlighted. + // Internal routes keep the app's default page highlighted. const activeRoute = app.pages.some((page) => page.route === currentRoute) ? currentRoute - : app.pages[0]?.route + : defaultRoute return app.pages.map( (page): { group: NavigationGroupId; page: ApplicationNavigationAppPage } => ({ group: page.group, diff --git a/web/src/lib/AppUi/bridge.ts b/web/src/lib/AppUi/bridge.ts index 5b8b6e885..382db4cc8 100644 --- a/web/src/lib/AppUi/bridge.ts +++ b/web/src/lib/AppUi/bridge.ts @@ -1,7 +1,7 @@ import { uiKitLimits } from '@rentnerproxy/app-contracts' import { z } from 'zod' -import type { AppUiPayload, AppUiSummary } from './contract.ts' +import type { AppUiPayload } from './contract.ts' const recordId = z.string().regex(/^[a-z0-9][a-z0-9_-]{0,63}$/u) const generation = z.number().int().min(1).max(Number.MAX_SAFE_INTEGER) @@ -117,6 +117,6 @@ export type AppUiToast = Extract, { type: 'toast' } export type AppUiApiRequest = Extract, { type: 'apiRequest' }> export type AppUiDialogRequest = Extract, { type: 'dialog' }> -export function appUiDefaultRoute(app: Pick) { +export function appUiDefaultRoute(app: { readonly routes: readonly string[] }) { return app.routes.includes('overview') ? 'overview' : (app.routes[0] ?? 'overview') } diff --git a/web/src/lib/AppUi/contract.ts b/web/src/lib/AppUi/contract.ts index f6f173b48..d32499652 100644 --- a/web/src/lib/AppUi/contract.ts +++ b/web/src/lib/AppUi/contract.ts @@ -79,8 +79,7 @@ export type AppUiSummary = { */ pages: AppUiPage[] /** - * Entry routes of the app's pages this user may not open; the page route refuses them. Absent - * for a summary without extensions. + * Entry routes blocked by a page or host action permission. */ deniedRoutes?: string[] /** The proxy host table columns this user may see. */ diff --git a/web/src/lib/AppUi/extensions.ts b/web/src/lib/AppUi/extensions.ts index 6804aceba..9405b1c74 100644 --- a/web/src/lib/AppUi/extensions.ts +++ b/web/src/lib/AppUi/extensions.ts @@ -8,6 +8,7 @@ import { APP_HOST_TABLE, } from '@/config/app-extensions.config.ts' import { NAVIGATION_GROUP_IDS } from '@/config/navigation.config.ts' +import { appUiDefaultRoute } from './bridge.ts' import type { AppUiHostAction, AppUiHostCell, @@ -154,7 +155,7 @@ function allowed( export interface AppUiExtensions { extensionCatalog?: string pages: AppUiPage[] - /** Entry routes of pages this user may not open, which no page they may open shares. */ + /** Entry routes blocked by a page or host action permission. */ deniedRoutes: string[] hostColumns: AppUiHostColumn[] hostActions: AppUiHostAction[] @@ -173,7 +174,7 @@ export function appUiExtensions( if (!extensions) { if (!app.slots.includes('navigation')) return { pages: [], deniedRoutes: [], hostColumns: [], hostActions: [] } - const route = app.routes.includes('overview') ? 'overview' : (app.routes[0] ?? 'overview') + const route = appUiDefaultRoute(app) return { pages: [{ id: 'navigation', route, title: app.name, group: 'apps', icon: 'puzzle' }], deniedRoutes: [], @@ -181,17 +182,21 @@ export function appUiExtensions( hostActions: [], } } - const hostActions = allowed(extensions.hostActions, heldKeys) - const pages = allowed(extensions.pages, heldKeys) - // A page's own route stays closed by URL to users who may not open that page. - const open = new Set(pages.map(({ route }) => route)) - const deniedRoutes = [ - ...new Set(extensions.pages.map(({ route }) => route).filter((route) => !open.has(route))), - ].toSorted() + const allowedPages = allowed(extensions.pages, heldKeys) + const allowedActions = allowed(extensions.hostActions, heldKeys) + // Alternative entries of the same kind can share a route. A different surface cannot + // override a refusal, since the route can also be opened directly or within a frame. + const openPages = new Set(allowedPages.map(({ route }) => route)) + const openActions = new Set(allowedActions.map(({ route }) => route)) + const deniedRoutes = new Set([ + ...extensions.pages.flatMap(({ route }) => (openPages.has(route) ? [] : [route])), + ...extensions.hostActions.flatMap(({ route }) => (openActions.has(route) ? [] : [route])), + ]) + const hostActions = allowedActions.filter(({ route }) => !deniedRoutes.has(route)) return { extensionCatalog: extensions.catalog, - pages, - deniedRoutes, + pages: allowedPages.filter(({ route }) => !deniedRoutes.has(route)), + deniedRoutes: [...deniedRoutes].toSorted(), hostColumns: allowed(extensions.hostColumns, heldKeys).map( ({ operation: _operation, action, ...column }): AppUiHostColumn => action !== undefined && hostActions.some(({ id }) => id === action) diff --git a/web/src/lib/AppUi/page-access.ts b/web/src/lib/AppUi/page-access.ts index d2c300957..81a90bfc5 100644 --- a/web/src/lib/AppUi/page-access.ts +++ b/web/src/lib/AppUi/page-access.ts @@ -1,26 +1,47 @@ import { appUiDefaultRoute } from './bridge.ts' -import type { AppUiSlot, AppUiSummary } from './contract.ts' +import type { AppUiSummary } from './contract.ts' type PageApp = Pick< AppUiSummary, 'routes' | 'slots' | 'pages' | 'deniedRoutes' | 'extensionCatalog' > +export function isAppUiRouteAllowed( + app: Pick, + route: string, +): boolean { + return app.routes.includes(route) && !app.deniedRoutes?.includes(route) +} + +export function appPageDefaultRoute(app: PageApp): string | undefined { + if (app.extensionCatalog !== undefined) { + return app.pages.find(({ route }) => isAppUiRouteAllowed(app, route))?.route + } + if (!app.slots.includes('navigation')) return undefined + const route = appUiDefaultRoute(app) + return isAppUiRouteAllowed(app, route) ? route : undefined +} + /** * The route and slot an app page opens for a URL, or undefined when the user may not open it. An * app that declares extensions has a page only through `app.page`: it opens at the first page the * user may open, never as a search slot, and a page route the user may not open stays closed even - * when typed into the URL. Other routes of the app stay reachable from inside its pages. + * when typed into the URL. Slot-only apps need the slot requested by the URL. */ export function appPageRoute( app: PageApp, routeId: string | undefined, searchSlot: 'search' | undefined, -): { readonly route: string; readonly slot: AppUiSlot | undefined } | undefined { - const extended = app.extensionCatalog !== undefined - if (extended && (app.pages.length === 0 || searchSlot !== undefined)) return undefined - if (searchSlot === 'search' && !app.slots.includes('search')) return undefined - const route = routeId ?? (extended ? app.pages[0]?.route : undefined) ?? appUiDefaultRoute(app) - if (!app.routes.includes(route) || app.deniedRoutes?.includes(route)) return undefined - return { route, slot: searchSlot ?? (app.pages.length > 0 ? 'navigation' : undefined) } +): { readonly route: string; readonly slot: 'navigation' | 'search' } | undefined { + if (searchSlot === 'search') { + if (app.extensionCatalog !== undefined || !app.slots.includes('search')) return undefined + const route = routeId ?? appUiDefaultRoute(app) + return isAppUiRouteAllowed(app, route) ? { route, slot: 'search' } : undefined + } + + const defaultRoute = appPageDefaultRoute(app) + if (defaultRoute === undefined) return undefined + const route = routeId ?? defaultRoute + if (!isAppUiRouteAllowed(app, route)) return undefined + return { route, slot: 'navigation' } } diff --git a/web/src/routes/_authenticated/apps.$installationId.{-$routeId}.tsx b/web/src/routes/_authenticated/apps.$installationId.{-$routeId}.tsx index ad3506bd4..0abbf4ab9 100644 --- a/web/src/routes/_authenticated/apps.$installationId.{-$routeId}.tsx +++ b/web/src/routes/_authenticated/apps.$installationId.{-$routeId}.tsx @@ -30,8 +30,8 @@ function ApplicationRoute() { const page = appPageRoute(app, routeId, search.slot) if (!page) return {t('appUi.unavailable')} const { route: activeRoute, slot } = page - function openRoute(route: string) { - if (route === activeRoute) return + const openRoute = (route: string) => { + if (route === activeRoute || !appPageRoute(app, route, search.slot)) return void navigate({ to: '/apps/$installationId/{-$routeId}', params: { installationId, routeId: route }, diff --git a/web/src/tests/features/AppCatalog/catalog-install-ui.test.tsx b/web/src/tests/features/AppCatalog/catalog-install-ui.test.tsx index 1a18ae4cc..39e95038b 100644 --- a/web/src/tests/features/AppCatalog/catalog-install-ui.test.tsx +++ b/web/src/tests/features/AppCatalog/catalog-install-ui.test.tsx @@ -225,8 +225,8 @@ test('installs the signed selection and opens it after authorized app discovery' generation: current.generation, uiBridgeVersion: '1.0.0', name: 'Greeting', - slots: [], - routes: [], + slots: ['navigation'], + routes: ['overview'], dialogs: [], events: [], permissionKeys: [], diff --git a/web/src/tests/features/AppUi/app-frame-pool.test.tsx b/web/src/tests/features/AppUi/app-frame-pool.test.tsx index 0fedf36d1..657faf991 100644 --- a/web/src/tests/features/AppUi/app-frame-pool.test.tsx +++ b/web/src/tests/features/AppUi/app-frame-pool.test.tsx @@ -157,10 +157,11 @@ describe('pooled app frames in the page', () => { const toastSpy = spyOn(toast, 'info').mockImplementation(() => '') const pool = createAppFramePool() const openDialog = mock(() => {}) + const onNavigate = mock((_route: string) => {}) // A detached container: happy-dom does not load frame documents, and need not try. const container = document.createElement('div') root = createRoot(container) - const render = async (shown: boolean) => { + const render = async (shown: boolean, currentApp: AppUiSummary = app) => { await act(async () => { root!.render( withTestLanguage( @@ -177,10 +178,10 @@ describe('pooled app frames in the page', () => { {shown ? ( {}} + onNavigate={onNavigate} onRetry={() => {}} onFrameRendering={() => {}} /> @@ -244,6 +245,13 @@ describe('pooled app frames in the page', () => { callbacks.onToast(saved) expect(toastSpy).toHaveBeenCalledTimes(1) + callbacks.onNavigate('overview') + expect(onNavigate).toHaveBeenCalledWith('overview') + await render(true, { ...app, deniedRoutes: ['overview'] }) + callbacks.onNavigate('overview') + expect(onNavigate).toHaveBeenCalledTimes(1) + expect(bindFrame).toHaveBeenCalledTimes(1) + // The dialog closes: the frame and its bridge go with it. await render(false) await act(async () => tick()) diff --git a/web/src/tests/features/AppUi/app-ui-frame.test.tsx b/web/src/tests/features/AppUi/app-ui-frame.test.tsx index 781fcf68c..282799391 100644 --- a/web/src/tests/features/AppUi/app-ui-frame.test.tsx +++ b/web/src/tests/features/AppUi/app-ui-frame.test.tsx @@ -28,6 +28,7 @@ const app: AppUiSummary = { } let sessionId: string | null = 'test-session' let theme: UserThemeMode = 'light' +let apps: AppUiSummary[] = [] const openFrame = mock(async () => ({ ok: true as const, @@ -44,7 +45,7 @@ const bindFrame = mock((..._binding: Parameters) => ({ })) await mock.module('@/features/AppUi/AppUiProvider.tsx', () => ({ - useAppUi: () => ({ sessionId, theme }), + useAppUi: () => ({ sessionId, theme, apps }), })) await mock.module('@/features/AppUi/middleware.ts', () => ({ openAppUiHandler: openFrame })) await mock.module('@/lib/AppUi/session.ts', () => ({ @@ -55,6 +56,8 @@ await mock.module('@/lib/AppUi/session.ts', () => ({ const { act } = await import('react') const { createRoot } = await import('react-dom/client') const { default: AppUiFrame } = await import('@/features/AppUi/AppUiFrame.tsx') +const { default: AppHostActionDialog } = + await import('@/features/AppUi/Components/AppHostActionDialog.tsx') const { toast } = await import('@rentnerkev/toasts') test('updates app context in place and retires the frame on generation or session changes', async () => { @@ -156,6 +159,51 @@ test('updates app context in place and retires the frame on generation or sessio } }) +test('a host action dialog ignores navigation to a refused route', async () => { + sessionId = 'test-session' + const dialogApp: AppUiSummary = { + ...app, + routes: ['overview', 'details', 'settings'], + hostActions: [{ id: 'inspect', route: 'overview', title: 'Inspect host' }], + deniedRoutes: ['settings'], + } + apps = [dialogApp] + const container = document.createElement('div') + document.body.append(container) + const root = createRoot(container) + + try { + await act(async () => { + root.render( + withTestLanguage( + {}} + />, + ), + ) + }) + const callbacks = bindFrame.mock.calls.at(-1)?.[3] + if (!callbacks) throw new Error('The dialog frame was not bound.') + expect(callbacks.onConnected('frame').route).toBe('overview') + + await act(async () => callbacks.onNavigate('details')) + expect(context).toHaveBeenLastCalledWith(expect.objectContaining({ route: 'details' })) + + const contextCount = context.mock.calls.length + await act(async () => callbacks.onNavigate('settings')) + expect(context).toHaveBeenCalledTimes(contextCount) + } finally { + await act(async () => root.unmount()) + container.remove() + apps = [] + } +}) + test('keeps a remotely rendered app in its hidden frame and draws its tree', async () => { sessionId = 'test-session' const container = document.createElement('div') diff --git a/web/src/tests/features/QuickSearch/quick-search-ui.test.tsx b/web/src/tests/features/QuickSearch/quick-search-ui.test.tsx index 4c2ceff01..66ebdecf2 100644 --- a/web/src/tests/features/QuickSearch/quick-search-ui.test.tsx +++ b/web/src/tests/features/QuickSearch/quick-search-ui.test.tsx @@ -583,7 +583,31 @@ describe('global quick search', () => { expect(selectedText()).toContain('Health checks') expect(selectedText()).toContain('Watcher') await key(input(), 'Enter') - expect(router.state.location.pathname).toBe('/apps/test-extensions/checks') + expect(router.state.location.pathname).toBe('/apps/test-extensions') + expect(router.state.location.search).toEqual({}) + }) + + test('opens the overview page explicitly when an earlier page owns the short URL', async () => { + const appWithOverview: AppUiSummary = { + ...pagesApp, + pages: [ + ...pagesApp.pages, + { + id: 'overview', + route: 'overview', + title: 'Watcher overview', + group: 'apps', + icon: 'puzzle', + }, + ], + } + appInventory = { apps: [appWithOverview], refresh: async () => [appWithOverview] } + const { trigger, router } = await mount([PERMISSIONS.APP_ACCESS]) + await click(trigger) + await query('watcher overview') + expect(selectedText()).toContain('Watcher overview') + await key(input(), 'Enter') + expect(router.state.location.pathname).toBe('/apps/test-extensions/overview') expect(router.state.location.search).toEqual({}) }) diff --git a/web/src/tests/layouts/AuthenticatedLayout/Components/ApplicationShell/application-shell.test.tsx b/web/src/tests/layouts/AuthenticatedLayout/Components/ApplicationShell/application-shell.test.tsx index 38452ed58..90f0c1d47 100644 --- a/web/src/tests/layouts/AuthenticatedLayout/Components/ApplicationShell/application-shell.test.tsx +++ b/web/src/tests/layouts/AuthenticatedLayout/Components/ApplicationShell/application-shell.test.tsx @@ -408,14 +408,23 @@ describe('application navigation groups', () => { const [checks, watch] = ['Health checks', 'Watcher'].map((label) => [...container.querySelectorAll('a')].find((link) => link.textContent === label)!, ) - expect(checks?.getAttribute('href')).toBe('/apps/test-extensions/checks') - expect(watch?.getAttribute('href')).toBe('/apps/test-extensions') + expect(checks?.getAttribute('href')).toBe('/apps/test-extensions') + expect(watch?.getAttribute('href')).toBe('/apps/test-extensions/overview') expect(checks?.querySelector('svg')?.getAttribute('class')).toContain('lucide-activity') // A declared route without its own page keeps the app's first page highlighted. expect(checks?.className).toContain('bg-brand-500/12') expect(watch?.className).not.toContain('bg-brand-500/12') const operations = sections[0]!.querySelector('button')! expect(operations.getAttribute('aria-expanded')).toBe('true') + + await act(async () => { + await router.navigate({ + to: '/apps/$installationId/{-$routeId}', + params: { installationId: extensionApp.installationId, routeId: undefined }, + }) + }) + expect(checks?.className).toContain('bg-brand-500/12') + expect(watch?.className).not.toContain('bg-brand-500/12') } finally { await act(async () => activeRoot?.unmount()) activeRoot = null diff --git a/web/src/tests/lib/AppUi/extensions.test.ts b/web/src/tests/lib/AppUi/extensions.test.ts index a3c628000..6c8cf29fe 100644 --- a/web/src/tests/lib/AppUi/extensions.test.ts +++ b/web/src/tests/lib/AppUi/extensions.test.ts @@ -203,8 +203,7 @@ describe('appUiExtensions', () => { icon: 'activity', }, ], - // The checks page's own route stays closed by URL without its key. - deniedRoutes: ['checks'], + deniedRoutes: ['checks', 'details'], hostColumns: [{ id: 'open-column', title: 'Upstream availability' }], hostActions: [], }) @@ -225,6 +224,20 @@ describe('appUiExtensions', () => { }) }) + test('keeps a shared page and host action route closed without both grants', () => { + const app = source({ + version: '1.0.0', + extensions: [page, { ...action, entry: page.entry, permission: 'write' }], + }) + expect(appUiExtensions(app, ['read', 'write']).deniedRoutes).toEqual([]) + for (const key of ['read', 'write']) { + const result = appUiExtensions(app, [key]) + expect(result.deniedRoutes).toEqual(['checks']) + expect(result.pages).toEqual([]) + expect(result.hostActions).toEqual([]) + } + }) + test('keeps one apps entry for a slot-only navigation app and no host extensions', () => { expect(appUiExtensions(source(undefined, ['navigation', 'proxyHostTab']), [])).toEqual({ pages: [ diff --git a/web/src/tests/lib/AppUi/page-access.test.ts b/web/src/tests/lib/AppUi/page-access.test.ts index 2a940199c..13f197aea 100644 --- a/web/src/tests/lib/AppUi/page-access.test.ts +++ b/web/src/tests/lib/AppUi/page-access.test.ts @@ -1,4 +1,5 @@ import { describe, expect, test } from 'bun:test' +import { appUiExtensions, type AppExtensionSource } from '@/lib/AppUi/extensions.ts' import { appPageRoute } from '@/lib/AppUi/page-access.ts' const page = (route: string) => ({ @@ -19,14 +20,47 @@ describe('app page routes', () => { } test('an app with extensions opens at the first page the user may open', () => { - expect(appPageRoute(extended, undefined, undefined)).toEqual({ + const app = { ...extended, pages: [page('checks'), page('overview')], deniedRoutes: [] } + expect(appPageRoute(app, undefined, undefined)).toEqual({ route: 'checks', slot: 'navigation', }) + expect(appPageRoute(app, 'overview', undefined)?.route).toBe('overview') // Other routes of the app stay reachable from inside its pages. expect(appPageRoute(extended, 'details', undefined)?.route).toBe('details') }) + test('a refused host action stays closed when its route is requested as an app page', () => { + const source = { + name: 'Watcher', + routes: ['checks', 'host-settings'], + slots: [], + permissionKeys: ['configure'], + extensions: { + version: '1.0.0', + extensions: [ + { + target: 'app.page', + id: 'checks', + title: 'Checks', + entry: 'checks', + navigation: { group: 'operations', icon: 'activity' }, + }, + { + target: 'proxyHosts.table.rowAction', + id: 'configure', + title: 'Configure', + entry: 'host-settings', + permission: 'configure', + }, + ], + }, + } satisfies AppExtensionSource + const app = { ...source, ...appUiExtensions(source, []) } + expect(appPageRoute(app, undefined, undefined)?.route).toBe('checks') + expect(appPageRoute(app, 'host-settings', undefined)).toBeUndefined() + }) + test('a page the user may not open stays closed when typed into the URL', () => { expect(appPageRoute(extended, 'overview', undefined)).toBeUndefined() expect(appPageRoute(extended, 'unknown', undefined)).toBeUndefined() @@ -54,5 +88,12 @@ describe('app page routes', () => { expect( appPageRoute({ ...slotOnly, slots: ['navigation'] }, 'search', 'search'), ).toBeUndefined() + const searchOnly = { ...slotOnly, slots: ['search' as const], pages: [] } + expect(appPageRoute(searchOnly, undefined, undefined)).toBeUndefined() + expect(appPageRoute(searchOnly, 'overview', undefined)).toBeUndefined() + expect(appPageRoute(searchOnly, undefined, 'search')).toEqual({ + route: 'overview', + slot: 'search', + }) }) }) From 0ce30e4ed81df69fb8f010acd2bef40089e62261 Mon Sep 17 00:00:00 2001 From: "HomePC\\Kevin" Date: Fri, 4 Dec 2026 07:42:05 +0100 Subject: [PATCH 2/3] fix(apps): respect entry grants on shared routes --- web/src/features/AppUi/AppUiFrame.tsx | 31 ++++++++++++--- .../AppUi/Components/AppFrameView.tsx | 3 +- .../AppUi/Components/AppHostActionDialog.tsx | 1 + .../AppUi/Components/PooledAppFrameView.tsx | 2 +- .../Hooks/useAppHostActionDialogLogic.ts | 4 +- .../AppUi/Hooks/useNativeAppUiLogic.ts | 6 ++- .../AppUi/Hooks/usePrewarmAppHostAction.ts | 9 +++-- web/src/features/AppUi/NativeAppUi.tsx | 8 ++-- .../AppUi/Types/app-frame-view.types.ts | 1 + web/src/features/AppUi/appFramePool.ts | 6 ++- web/src/lib/AppUi/contract.ts | 6 +-- web/src/lib/AppUi/extensions.ts | 39 ++++++++++++------- web/src/lib/AppUi/page-access.ts | 9 ++++- .../features/AppUi/app-frame-pool.test.tsx | 13 ++++++- .../features/AppUi/app-ui-frame.test.tsx | 3 +- .../features/AppUi/native-app-ui.test.tsx | 25 +++++++++++- web/src/tests/lib/AppUi/extensions.test.ts | 28 +++++++++---- 17 files changed, 143 insertions(+), 51 deletions(-) diff --git a/web/src/features/AppUi/AppUiFrame.tsx b/web/src/features/AppUi/AppUiFrame.tsx index 23afccccd..66d530d0c 100644 --- a/web/src/features/AppUi/AppUiFrame.tsx +++ b/web/src/features/AppUi/AppUiFrame.tsx @@ -20,6 +20,7 @@ type FrameProps = { app: AppUiSummary slot?: AppUiSlot | undefined hostId?: string | undefined + hostActionId?: string | undefined route?: string | undefined onNavigate?: ((route: string) => void) | undefined /** Fill the height of a fixed-height container, such as a dialog body. */ @@ -28,7 +29,15 @@ type FrameProps = { type AppFrameSource = OpenedSource & { frameToken: string } -export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill }: FrameProps) { +export default function AppUiFrame({ + app, + slot, + hostId, + hostActionId, + route, + onNavigate, + fill, +}: FrameProps) { const sessionId = useAppUi()?.sessionId const [localRoute, setLocalRoute] = useState() const [attempt, setAttempt] = useState(0) @@ -36,21 +45,24 @@ export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill const current = route ?? [localRoute, defaultRoute, ...app.routes].find( - (candidate) => candidate !== undefined && isAppUiRouteAllowed(app, candidate), + (candidate) => + candidate !== undefined && isAppUiRouteAllowed(app, candidate, hostActionId), ) // A new generation, a reconnect or a retry remounts the view below; drafts the user did not - // save pass to the next view of the same session, app, slot and host, never to another. + // save pass to the next view for the same session, app, slot, host and action. const identity = JSON.stringify([ sessionId ?? null, app.installationId, slot ?? null, hostId ?? null, + hostActionId ?? null, ]) // A holder the views write after render, from their layout effects; never read while rendering. const [kept] = useState(() => ({ current: null })) const handoff = useMemo(() => draftHandoff(identity, kept), [identity, kept]) - if (!sessionId || current === undefined || !isAppUiRouteAllowed(app, current)) return null + if (!sessionId || current === undefined || !isAppUiRouteAllowed(app, current, hostActionId)) + return null // Routing updates the open document through the bridge. return ( @@ -62,12 +74,14 @@ export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill app.generation, slot, hostId, + hostActionId, attempt, ])} sessionId={sessionId} app={app} slot={slot} hostId={hostId} + hostActionId={hostActionId} route={current} fill={fill} onNavigate={onNavigate ?? setLocalRoute} @@ -100,11 +114,11 @@ function AppUiMount(props: MountProps) { ) const showOwnFrame = useCallback(() => setPooled(false), []) if (!pooled || !pool) return - const { app, sessionId, slot, hostId, route, fill, onNavigate, onRetry } = props + const { app, sessionId, slot, hostId, hostActionId, route, fill, onNavigate, onRetry } = props return ( { @@ -203,6 +219,7 @@ function OpenedAppFrame({ app, source, slot, + hostActionId, route, fill, onNavigate, @@ -211,6 +228,7 @@ function OpenedAppFrame({ app: AppUiSummary source: AppFrameSource slot: AppUiSlot | undefined + hostActionId: string | undefined route: string fill: boolean | undefined onNavigate: (route: string) => void @@ -223,6 +241,7 @@ function OpenedAppFrame({ app={app} source={source} slot={slot} + hostActionId={hostActionId} route={route} fill={fill} theme={ui?.theme ?? 'light'} diff --git a/web/src/features/AppUi/Components/AppFrameView.tsx b/web/src/features/AppUi/Components/AppFrameView.tsx index 3947a411e..92d32a2e3 100644 --- a/web/src/features/AppUi/Components/AppFrameView.tsx +++ b/web/src/features/AppUi/Components/AppFrameView.tsx @@ -23,6 +23,7 @@ export default function AppFrameView({ app, source, slot, + hostActionId, route, fill, theme, @@ -49,7 +50,7 @@ export default function AppFrameView({ // Keys match the manifest pattern, so the joined list changes exactly when the keys do. const permissionScope = app.permissionKeys.join(' ') const navigate = useEffectEvent((target: string) => { - if (isAppUiRouteAllowed(app, target)) onNavigate(target) + if (isAppUiRouteAllowed(app, target, hostActionId)) onNavigate(target) }) const dialog = useEffectEvent(onDialog) const request = useEffectEvent(onApiRequest) diff --git a/web/src/features/AppUi/Components/AppHostActionDialog.tsx b/web/src/features/AppUi/Components/AppHostActionDialog.tsx index 20b760a4a..b6c82a40c 100644 --- a/web/src/features/AppUi/Components/AppHostActionDialog.tsx +++ b/web/src/features/AppUi/Components/AppHostActionDialog.tsx @@ -24,6 +24,7 @@ export default function AppHostActionDialog(props: AppHostActionDialogProps) { app={state.app} slot="proxyHostTab" hostId={props.hostId} + hostActionId={props.actionId} route={state.route} onNavigate={handler.handleNavigate} fill diff --git a/web/src/features/AppUi/Components/PooledAppFrameView.tsx b/web/src/features/AppUi/Components/PooledAppFrameView.tsx index ab87b7472..8728bee26 100644 --- a/web/src/features/AppUi/Components/PooledAppFrameView.tsx +++ b/web/src/features/AppUi/Components/PooledAppFrameView.tsx @@ -50,7 +50,7 @@ export default function PooledAppFrameView({ const { t, locale } = useTranslationStore() const { app } = request const handleNavigate = (target: string) => { - if (isAppUiRouteAllowed(app, target)) onNavigate(target) + if (isAppUiRouteAllowed(app, target, request.hostActionId)) onNavigate(target) } const view = useRef({ route: request.route, onNavigate: handleNavigate }) useLayoutEffect(() => { diff --git a/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts b/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts index 462ff12c4..7d197b063 100644 --- a/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts +++ b/web/src/features/AppUi/Hooks/useAppHostActionDialogLogic.ts @@ -20,7 +20,9 @@ export default function useAppHostActionDialogLogic({ const [navigation, setNavigation] = useState<{ action: string; route: string }>() // The app may navigate inside the dialog; a new action starts at its own route again. const route = - app && navigation?.action === actionId && isAppUiRouteAllowed(app, navigation.route) + app && + navigation?.action === actionId && + isAppUiRouteAllowed(app, navigation.route, actionId) ? navigation.route : action?.route diff --git a/web/src/features/AppUi/Hooks/useNativeAppUiLogic.ts b/web/src/features/AppUi/Hooks/useNativeAppUiLogic.ts index 63a1edc32..a1dfdbe17 100644 --- a/web/src/features/AppUi/Hooks/useNativeAppUiLogic.ts +++ b/web/src/features/AppUi/Hooks/useNativeAppUiLogic.ts @@ -1,6 +1,7 @@ import { useQuery } from '@tanstack/react-query' import { callAppUi } from '@/lib/AppUi/client.ts' import type { AppUiPayload, AppUiSummary } from '@/lib/AppUi/contract.ts' +import { isAppUiRouteAllowed } from '@/lib/AppUi/page-access.ts' import { useAppUi } from '../AppUiProvider.tsx' import { openAppUiHandler } from '../middleware.ts' @@ -94,7 +95,10 @@ export function useNativeAppUiLogic(app: AppUiSummary) { return result.payload } - return { state: { source, scope }, handler: { call } } + const pages = + source.data?.presentation.pages.filter(({ route }) => isAppUiRouteAllowed(app, route)) ?? + [] + return { state: { source, scope, pages }, handler: { call } } } export type NativeAppSession = Pick, 'handler'> & { diff --git a/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts b/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts index f1c16469c..548644a9f 100644 --- a/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts +++ b/web/src/features/AppUi/Hooks/usePrewarmAppHostAction.ts @@ -12,13 +12,15 @@ import type { AppHostTableAction } from '../Types/app-host-table.types.ts' export default function usePrewarmAppHostAction() { const ui = useAppUi() return useCallback( - (hostId: string, action: Pick) => { + (hostId: string, action: Pick) => { const app = ui?.apps.find((entry) => entry.installationId === action.installationId) + const entry = app?.hostActions.find(({ id }) => id === action.actionId) if ( !ui?.sessionId || !app || + !entry || !supportsRemoteRendering(app) || - !isAppUiRouteAllowed(app, action.route) + !isAppUiRouteAllowed(app, entry.route, entry.id) ) return ui.framePool.prewarm({ @@ -26,7 +28,8 @@ export default function usePrewarmAppHostAction() { sessionId: ui.sessionId, slot: 'proxyHostTab', hostId, - route: action.route, + hostActionId: entry.id, + route: entry.route, }) }, [ui], diff --git a/web/src/features/AppUi/NativeAppUi.tsx b/web/src/features/AppUi/NativeAppUi.tsx index 7f544250b..b4ac34ee7 100644 --- a/web/src/features/AppUi/NativeAppUi.tsx +++ b/web/src/features/AppUi/NativeAppUi.tsx @@ -19,8 +19,8 @@ export default function NativeAppUi({ }) { const { t, locale } = useTranslationStore() const { state, handler } = useNativeAppUiLogic(app) - const { source, scope } = state - const activePage = source.data?.presentation.pages.find((page) => page.route === route) + const { source, scope, pages } = state + const activePage = pages.find((page) => page.route === route) const session: NativeAppSession | undefined = source.data && { scope, handler, @@ -52,7 +52,7 @@ export default function NativeAppUi({ ) : ( <>