diff --git a/frontend/src/components/AppRootConsent.tsx b/frontend/src/components/AppRootConsent.tsx index b18df87db..344761124 100644 --- a/frontend/src/components/AppRootConsent.tsx +++ b/frontend/src/components/AppRootConsent.tsx @@ -4,77 +4,128 @@ import { Box, Typography } from '@mui/material' import type { DeviceApp } from '../services/graphQLDeviceApps' import { AppChange, refused } from '../services/deviceAppsShared' import type { DeviceApps } from '../hooks/useDeviceApps' -import { capabilityWords } from '../helpers/appsHelper' +import { asksConsent, capabilityWords, ConsentAsking } from '../helpers/appsHelper' import { graphQLCatalogApp } from '../services/graphQLAppCatalog' import { Confirm } from './Confirm' -/* Asking for the person's yes to a root app's full control, in words naming the app, its publisher and that it gets - full control of the device (presence-server docs/device-apps-design.md, "Root, shown and consented to"), and what - else it asks of remote.it — its capabilities, part of the consent (presence-contract docs/self-contained-apps.md, - "Manifest"). For a product it says the yes covers every device in the product, now and later, as graphql keeps it - on the product. +/* Asking for the person's yes to what a release asks (helpers/appsHelper asksConsent, graphql's rule): to run as root — + full control of the device (presence-server docs/device-apps-design.md, "Root, shown and consented to") — and the + capabilities it declares (presence-contract docs/self-contained-apps.md, "Manifest"), in words naming the app and its + publisher. A root release is asked as root, its capabilities beside; a restricted one that declares capabilities is + asked for those. For a product it says the yes covers every device in the product, now and later, as graphql keeps + it on the product. - The capabilities named are the release's the yes is given to (release), not the version the device runs: a release - may ask more than the one before it, and graphql takes the yes as covering what that release asks. They are read - from its catalogue entry (useReleaseAsks); until they are, the yes waits. */ + What is asked is the release's the yes is given to (release), not the version the device runs: a release may ask + more than the one before it, and graphql takes the yes as covering what that release asks. Where the change may give + one of several (a channel's candidate or current, by the device's bucket), all they ask together. It is read from its + catalogue entry, under the account the device or product was read for (useReleaseAsks); until it is, the yes waits. + action: the confirm button's words, where they are not turning it on, for a release that runs as root or not. */ export const RootConsent: React.FC<{ - app?: { id?: string; name: string; publisher: string; capabilities?: string[] } // open while there is one + // open while there is one; root and capabilities: the app's own, where the catalogue does not name the release + app?: { id?: string; name: string; publisher: string; root: boolean; capabilities?: string[] } product?: boolean // a product's consent, for every device in it - action?: string // the confirm button's words, where they are not turning it on - // A release of an app already on that newly runs as root: the consent moves it there (a re-consent). + action?: (root: boolean) => string + // A release of an app already on that newly asks for consent: the consent moves it there (a re-consent). version?: string | null - // The release the yes is given to, where it is not version: a pin, the release enabling gives. None: the app's own. - release?: string | null - accountId?: string // whose catalogue names the release: the device's account + // The release the yes is given to, where it is not version: a pin, the release enabling gives — or the releases a + // change may give. None: the app's own. + release?: string | string[] | null + accountId: string | undefined // whose catalogue names the release: the device's or the product's account onConfirm: () => void onDeny: () => void }> = ({ app, product, action, version, release, accountId, onConfirm, onDeny }) => { const { t } = useTranslation() const words = { app: app?.name, publisher: app?.publisher, version } const asks = useReleaseAsks(app, release ?? version, accountId) + // Not known (null): asked as the most it could be, and not given until it is. + const root = asks ? asks.root : true return ( {version && ( - {product + {root + ? product + ? t( + 'productApps.rootUpdate', + '{{app}} {{version}} needs to run as root. Its devices stay on the version they run until you consent.', + words + ) + : t( + 'deviceApps.rootUpdate', + '{{app}} {{version}} needs to run as root. This device stays on the version it runs until you consent.', + words + ) + : product ? t( - 'productApps.rootUpdate', - '{{app}} {{version}} needs to run as root. Its devices stay on the version they run until you consent.', + 'productApps.asksUpdate', + '{{app}} {{version}} asks more of remote.it. Its devices stay on the version they run until you consent.', words ) : t( - 'deviceApps.rootUpdate', - '{{app}} {{version}} needs to run as root. This device stays on the version it runs until you consent.', + 'deviceApps.asksUpdate', + '{{app}} {{version}} asks more of remote.it. This device stays on the version it runs until you consent.', words )} )} - - {product + + {root + ? product + ? t( + 'productApps.rootBody', + '{{app}}, published by {{publisher}}, runs as root: it gets full control of this device — every file, account and program on it — on every device in this product, now and later.', + words + ) + : t( + 'deviceApps.rootBody', + '{{app}}, published by {{publisher}}, runs as root: it gets full control of this device — every file, account and program on it.', + words + ) + : product ? t( - 'productApps.rootBody', - '{{app}}, published by {{publisher}}, runs as root: it gets full control of this device — every file, account and program on it — on every device in this product, now and later.', + 'productApps.asksBody', + '{{app}}, published by {{publisher}}, runs restricted, with an account of its own on every device in this product, now and later.', words ) : t( - 'deviceApps.rootBody', - '{{app}}, published by {{publisher}}, runs as root: it gets full control of this device — every file, account and program on it.', + 'deviceApps.asksBody', + '{{app}}, published by {{publisher}}, runs restricted, with an account of its own on this device.', words )} - + {asks === null && ( + + {t( + 'apps.asksUnknown', + 'What this version asks could not be read, so it cannot be consented to here. Try again later.' + )} + + )} + {product ? t( @@ -88,33 +139,45 @@ export const RootConsent: React.FC<{ ) } -/* What the release a consent is for asks of remote.it (its capabilities): its catalogue entry's for that release; the - app's own where no release is named, or where the catalogue does not answer for it (an API without one, an error, a - release it does not list — graphql still refuses a yes that does not cover what the release asks). undefined while - it is read. */ +/* What the releases a consent is for ask (whether one runs as root, and their capabilities, together): each its + catalogue entry's, read under the account the device or product was read for. The app's own where no release is + named (an app offered to the device: its fields are the release the device would be given). null where a release + named is not one the catalogue answers for (an API without one, an error, one it does not list): what it asks is + not known, and a yes to it is not taken. undefined while they are read. */ export function useReleaseAsks( - app: { id?: string; capabilities?: string[] } | undefined, - release: string | null | undefined, - accountId?: string -): string[] | undefined { - const [read, setRead] = useState<{ key: string; asks: string[] | null }>() - const key = app?.id && release ? `${accountId ?? ''}/${app.id}/${release}` : '' + app: { id?: string; root: boolean; capabilities?: string[] } | undefined, + release: string | string[] | null | undefined, + accountId: string | undefined +): ConsentAsking | null | undefined { + const [read, setRead] = useState<{ key: string; asks: ConsentAsking | null }>() + const releases = (Array.isArray(release) ? release : [release]).filter((r): r is string => !!r) + const key = app?.id && releases.length ? `${accountId ?? ''}/${app.id}/${releases.join(',')}` : '' useEffect(() => { - if (!key || !app?.id || !release) return + if (!key || !app?.id) return let current = true graphQLCatalogApp(app.id, { accountId, releases: 100 }).then(answer => { if (!current) return - const entry = typeof answer === 'object' && answer ? answer.releases.find(r => r.version === release) : undefined - setRead({ key, asks: entry ? entry.capabilities : null }) + const listed = typeof answer === 'object' && answer ? answer.releases : [] + const entries = releases.map(version => listed.find(r => r.version === version)) + const known = entries.every(entry => !!entry) + setRead({ + key, + asks: known + ? { + root: entries.some(entry => entry!.root), + capabilities: [...new Set(entries.flatMap(entry => entry!.capabilities))], + } + : null, + }) }) return () => { current = false } }, [key]) if (!app) return undefined - if (!key) return app.capabilities ?? [] + if (!key) return { root: app.root, capabilities: app.capabilities ?? [] } if (read?.key !== key) return undefined - return read.asks ?? app.capabilities ?? [] + return read.asks } // What an app asks of remote.it beyond running, a line each: in its consent, and where it is offered. @@ -140,16 +203,16 @@ export const AppCapabilities: React.FC<{ capabilities?: string[]; title?: string } /* Enabling or disabling an app, from wherever it is offered (the device's Apps section, the app's page on the device, - an app's service's connect panel). A root app is enabled only once the person says yes (RootConsent); that yes is - consentRoot, which graphql refuses a root app without. Disabling, or a restricted app, at once. A release that newly - runs as root while the app is enabled (awaitingRoot) is asked for the same way (reconsent): the yes is - setDeviceAppRelease's consentRoot alone — Update now —, which moves the device to that release. graphql's refusal, - in its words, goes to onRefused; a change made, to onChanged. */ + an app's service's connect panel, the device's catalogue). An app is enabled only once the person says yes + (RootConsent) where graphql asks for one (needsConsent); that yes is consentRoot. Disabling, or an app that asks for + nothing a consent does not already cover, at once. A release that newly asks while the app is enabled (awaitingRoot) + is asked for the same way (reconsent): the yes is setDeviceAppRelease's consentRoot alone — Update now —, which moves + the device to that release. graphql's refusal, in its words, goes to onRefused; a change made, to onChanged. */ export function useAppSwitch( { turn, setRelease }: Pick & Partial>, - onRefused?: (reason: string) => void, - onChanged?: (app: DeviceApp) => void, - accountId?: string // the device's account, whose catalogue names the release consented to + onRefused: ((reason: string) => void) | undefined, + onChanged: ((app: DeviceApp) => void) | undefined, + accountId: string | undefined // the device's account, whose catalogue names the release consented to ) { const { t } = useTranslation() const [asking, setAsking] = useState() @@ -160,7 +223,7 @@ export function useAppSwitch( } const flip = (app: DeviceApp, enabled: boolean) => { - if (enabled && app.root) setAsking(app) + if (enabled && needsConsent(app)) setAsking(app) else turn(app, enabled).then(done(app)) } @@ -168,8 +231,9 @@ export function useAppSwitch( // An enabled app held back for a release that runs as root: the yes moves it there. const waiting = (app?: DeviceApp) => !!app?.enabled && (!!app.awaitingRoot || app.heldBack?.reason === 'consent') const updating = waiting(asking) ? asking?.awaitingRoot || asking?.latest || '' : null - // The release the yes goes to: the one waiting for it, else the one enabling gives — its pin, else its channel's. - const target = updating || asking?.pinned || asking?.latest || asking?.release || asking?.version || null + // The release the yes goes to: the one waiting for it (graphql's awaitingRoot, enabled or not). None for an app + // offered to the device: its own fields are what its release asks there. + const target = updating || asking?.awaitingRoot || null const consent = ( t('deviceApps.rootUpdateAction', 'Consent and update') : undefined} onConfirm={() => { const app = asking setAsking(undefined) @@ -191,3 +255,9 @@ export function useAppSwitch( return { flip, reconsent, consent } } + +/* Whether turning an app on asks the person's yes, as graphql decides it: a release waiting for one (awaitingRoot — + graphql names it, enabled or not, where what its release asks is not covered by the consent kept with it), or, for + one the device does not have yet (offered: no consent kept), a release that asks for one (asksConsent). */ +export const needsConsent = (app: Pick) => + !!app.awaitingRoot || (asksConsent(app) && !app.rootConsent) diff --git a/frontend/src/components/DeviceApps.test.tsx b/frontend/src/components/DeviceApps.test.tsx index 23640191b..9c536a169 100644 --- a/frontend/src/components/DeviceApps.test.tsx +++ b/frontend/src/components/DeviceApps.test.tsx @@ -288,6 +288,38 @@ describe('the switch enables and disables', () => { expect(turn).toHaveBeenCalledWith(expect.objectContaining({ id: 'remoteit.console' }), true, true) }) + it('a restricted app that declares capabilities asks the yes to them first, in their words', async () => { + apps.value = { ...apps.value, apps: [backup({ capabilities: ['ssh-exec'] })] } + const page = await render() + await click(switchOf(appRow(page, 'acme.backup'))) + expect(turn).not.toHaveBeenCalled() + expect(page.querySelector('[data-confirm-title]')?.textContent).toBe('Allow Backup what it asks?') + expect(page.querySelector('[data-root-consent]')?.getAttribute('data-root-consent')).toBe('capabilities') + expect(page.querySelector('[data-confirm] [data-app-capability]')?.getAttribute('data-app-capability')).toBe('ssh-exec') + expect(page.querySelector('[data-confirm-ok]')?.textContent).toBe('Allow and turn on') + await click(page.querySelector('[data-confirm-ok]')) + expect(turn).toHaveBeenCalledWith(expect.objectContaining({ id: 'acme.backup' }), true, true) + }) + + it('a consent kept that covers its release (none awaited): turned on at once; one awaited asks again', async () => { + const consent = { by: 'U1', email: null, at: '2026-11-10T00:00:00Z', version: '1.4.0', via: 'person' } + apps.value = { ...apps.value, apps: [consoleApp({ enabled: false, state: 'disabled', rootConsent: consent })] } + let page = await render() + await click(switchOf(appRow(page, 'remoteit.console'))) + expect(page.querySelector('[data-confirm]')).toBeNull() + expect(turn).toHaveBeenCalledWith(expect.objectContaining({ id: 'remoteit.console' }), true) + act(() => root.unmount()) + turn.mockClear() + apps.value = { + ...apps.value, + apps: [consoleApp({ enabled: false, state: 'disabled', rootConsent: consent, awaitingRoot: '1.5.0' })], + } + page = await render() + await click(switchOf(appRow(page, 'remoteit.console'))) + expect(turn).not.toHaveBeenCalled() + expect(page.querySelector('[data-confirm-title]')?.textContent).toBe('Run Console as root?') + }) + it('a restricted app at once; any app disabled at once', async () => { const page = await render() await click(switchOf(appRow(page, 'acme.backup'))) diff --git a/frontend/src/components/ProductPolicyApps.test.tsx b/frontend/src/components/ProductPolicyApps.test.tsx index d766ea9df..7ff05beb0 100644 --- a/frontend/src/components/ProductPolicyApps.test.tsx +++ b/frontend/src/components/ProductPolicyApps.test.tsx @@ -2,9 +2,9 @@ import React, { act } from 'react' import { createRoot, Root } from 'react-dom/client' import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' -const { setApp, setAppSetting } = vi.hoisted(() => ({ setApp: vi.fn(), setAppSetting: vi.fn() })) -// The release a consent names is read from its catalogue entry; none here: the app's own capabilities. -vi.mock('../services/graphQLAppCatalog', () => ({ graphQLCatalogApp: () => Promise.resolve('UNSERVED') })) +const { setApp, setAppSetting, catalog } = vi.hoisted(() => ({ setApp: vi.fn(), setAppSetting: vi.fn(), catalog: vi.fn() })) +// The release a consent names is read from its catalogue entry; unserved by default: the app's own. +vi.mock('../services/graphQLAppCatalog', () => ({ graphQLCatalogApp: catalog })) vi.mock('../services/post', () => ({ post: vi.fn() })) vi.mock('../services/graphQL', () => ({ graphQLGetErrors: vi.fn(), @@ -41,12 +41,12 @@ vi.mock('./SelectSetting', () => ({ vi.mock('./Icon', () => ({ Icon: ({ name }: any) => (name ? : null) })) vi.mock('./Notice', () => ({ Notice: ({ children }: any) =>
{children}
})) vi.mock('./Confirm', () => ({ - Confirm: ({ open, title, action, children, onConfirm, onDeny }: any) => + Confirm: ({ open, title, action, children, disabled, onConfirm, onDeny }: any) => open ? (
{title} {children} - {!done && ( - )} {app.enabled && app.heldBack?.reason === 'pinned' && !held && ( - )} @@ -277,7 +303,7 @@ export const DeviceAppPage: React.FC = () => { variant="standard" value={app.channel ?? ''} disabled={runLocked || !!saving} - onChange={e => e.target.value !== (app.channel ?? '') && release('channel', { channel: e.target.value })} + onChange={e => e.target.value !== (app.channel ?? '') && move('channel', { channel: e.target.value })} SelectProps={{ native: true }} inputProps={{ 'data-app-channel': true, 'aria-label': t('apps.channel', 'Channel') }} > @@ -296,7 +322,7 @@ export const DeviceAppPage: React.FC = () => { variant="standard" value={app.pinned ?? ''} disabled={runLocked || !!saving} - onChange={e => e.target.value !== (app.pinned ?? '') && pin(e.target.value)} + onChange={e => e.target.value !== (app.pinned ?? '') && move('pin', { version: e.target.value })} SelectProps={{ native: true }} inputProps={{ 'data-app-pin': true, 'aria-label': t('apps.pin', 'Pin') }} > @@ -508,16 +534,22 @@ export const DeviceAppPage: React.FC = () => { )} {consent} + moving?.values.version + ? root + ? t('apps.pinRootAction', 'Pin with full control') + : t('apps.pinAsksAction', 'Allow and pin') + : t('deviceApps.rootUpdateAction', 'Consent and update') + } onConfirm={() => { - const version = pinning - setPinning(undefined) - if (version) release('pin', { version, consentRoot: true }) + const move = moving + setMoving(undefined) + if (move) release(move.key, { ...move.values, consentRoot: true }) }} - onDeny={() => setPinning(undefined)} + onDeny={() => setMoving(undefined)} /> { const product = products.find(p => p.id === productId) const accountId = useSelector(selectActiveAccountId) const manage = useSelector(selectPermissions).includes('ADMIN') - const { served, policy, reload } = useProductPolicy(productId) + const { served, policy, reload, accountId: readUnder } = useProductPolicy(productId) if (!product) return @@ -76,6 +76,7 @@ export const ProductPolicyPage: React.FC = () => { {policy.apps && (