diff --git a/changelog/unreleased/bugfix-vault-permalinks.md b/changelog/unreleased/bugfix-vault-permalinks.md new file mode 100644 index 00000000000..5a930d1452d --- /dev/null +++ b/changelog/unreleased/bugfix-vault-permalinks.md @@ -0,0 +1,6 @@ +Bugfix: Resolve Safe/Vault permalinks + +A permanent link to a file inside the Safe failed for the recipient. Such links now +open the shared file. + +https://github.com/owncloud/ocis/pull/12881 diff --git a/web/packages/web-runtime/src/pages/resolvePrivateLink.vue b/web/packages/web-runtime/src/pages/resolvePrivateLink.vue index 9cb2bd88468..6983187ad31 100644 --- a/web/packages/web-runtime/src/pages/resolvePrivateLink.vue +++ b/web/packages/web-runtime/src/pages/resolvePrivateLink.vue @@ -46,7 +46,8 @@ import { useRouteQuery, createLocationSpaces, createLocationShares, - useClientService + useClientService, + useCapabilityStore } from '@ownclouders/web-pkg' import { unref, defineComponent, computed, onMounted, ref, Ref } from 'vue' import { dirname } from 'path' @@ -61,8 +62,10 @@ export default defineComponent({ setup() { const router = useRouter() const id = useRouteParam('fileId') + const scope = useRouteParam('scope') const { $gettext } = useGettext() const clientService = useClientService() + const capabilityStore = useCapabilityStore() const resource: Ref = ref() const sharedParentResource: Ref = ref() @@ -83,13 +86,34 @@ export default defineComponent({ }) const resolvePrivateLinkTask = useTask(function* (signal, id) { + // vault file but non-vault /f/ URL: hard-reload into /vault so the vault client + // base URLs are picked up (they are derived from the URL at boot). The MFA + // step-up is enforced by the authService guard on the vault scope, which also + // fires on a router.push, so it is not the reason for the full reload. + if ( + capabilityStore.vaultEnabled && + id?.split('$')[0] === capabilityStore.vaultStorageProvider && + unref(scope) !== 'vault' + ) { + window.location.replace( + router.resolve({ + name: 'resolvePrivateLink', + params: { scope: 'vault', fileId: id }, + query: router.currentRoute.value.query + }).href + ) + return + } + if ( [ `${SHARE_JAIL_ID}$${SHARE_JAIL_ID}!${SHARE_JAIL_ID}`, `${SHARE_JAIL_ID}$${SHARE_JAIL_ID}` ].includes(id) ) { - return router.push(createLocationShares('files-shares-with-me')) + return router.push( + createLocationShares('files-shares-with-me', { params: { scope: unref(scope) } }) + ) } let result: Awaited> @@ -140,7 +164,7 @@ export default defineComponent({ const { params, query } = createFileRouteOptions(space, { fileId, path }) const openWithDefault = unref(openWithDefaultApp) !== 'false' && !unref(details) - targetLocation.params = params + targetLocation.params = { ...params, scope: unref(scope) } targetLocation.query = { ...query, scrollTo: unref(resource).fileId, @@ -156,9 +180,10 @@ export default defineComponent({ return !resolvePrivateLinkTask.last || resolvePrivateLinkTask.isRunning }) - const sharedWithMeRoute = computed(() => { - return { name: 'files-shares-with-me' } - }) + const sharedWithMeRoute = computed(() => ({ + name: 'files-shares-with-me', + params: { scope: unref(scope) } + })) const openSharedWithMeLabel = computed(() => { return $gettext('Open "Shared with me"') diff --git a/web/packages/web-runtime/src/router/index.ts b/web/packages/web-runtime/src/router/index.ts index 43e180d21bb..ef02d65011c 100644 --- a/web/packages/web-runtime/src/router/index.ts +++ b/web/packages/web-runtime/src/router/index.ts @@ -54,7 +54,7 @@ const routes = [ meta: { title: $gettext('Oidc redirect'), authContext: 'anonymous' } }, { - path: '/f/:fileId', + path: '/:scope(vault)?/f/:fileId', name: 'resolvePrivateLink', component: ResolvePrivateLinkPage, meta: { title: $gettext('Private link'), authContext: 'user' } diff --git a/web/packages/web-runtime/tests/unit/pages/resolvePrivateLink.spec.ts b/web/packages/web-runtime/tests/unit/pages/resolvePrivateLink.spec.ts index 6ca0e9f1837..81aec4b536b 100644 --- a/web/packages/web-runtime/tests/unit/pages/resolvePrivateLink.spec.ts +++ b/web/packages/web-runtime/tests/unit/pages/resolvePrivateLink.spec.ts @@ -1,7 +1,7 @@ import resolvePrivateLink from '../../../src/pages/resolvePrivateLink.vue' import { defaultPlugins, defaultComponentMocks, shallowMount } from '@ownclouders/web-test-helpers' import { mock } from 'vitest-mock-extended' -import { queryItemAsString, useGetResourceContext } from '@ownclouders/web-pkg' +import { queryItemAsString, useGetResourceContext, useRouteParam } from '@ownclouders/web-pkg' import { Resource, SHARE_JAIL_ID, SpaceResource } from '@ownclouders/web-client' vi.mock('@ownclouders/web-pkg', async (importOriginal) => ({ @@ -84,6 +84,83 @@ describe('resolvePrivateLink', () => { ) }) }) + describe('vault permalinks', () => { + const vaultCapabilityState = { + capabilities: { vault: { enabled: true, vault_storage_provider: 'vault-provider' } } + } + + const originalLocation = window.location + afterEach(() => { + Object.defineProperty(window, 'location', { + configurable: true, + value: originalLocation + }) + }) + + it('hard-redirects a vault permalink into vault scope instead of resolving it', async () => { + const replace = vi.fn() + Object.defineProperty(window, 'location', { + configurable: true, + value: { replace, href: '' } + }) + const { wrapper, mocks } = getWrapper({ + fileId: 'vault-provider$space!item', + capabilityState: vaultCapabilityState + }) + await wrapper.vm.resolvePrivateLinkTask.last + expect(replace).toHaveBeenCalledTimes(1) + expect(mocks.$router.resolve).toHaveBeenCalledWith( + expect.objectContaining({ + name: 'resolvePrivateLink', + params: { scope: 'vault', fileId: 'vault-provider$space!item' } + }) + ) + expect(mocks.$router.push).not.toHaveBeenCalled() + }) + + it('resolves normally when the permalink is not a vault resource', async () => { + const replace = vi.fn() + Object.defineProperty(window, 'location', { + configurable: true, + value: { replace, href: '' } + }) + const fileId = 'other-provider$space!item' + const space = mock({ getDriveAliasAndItem: () => 'personal/home' }) + const resource = mock({ fileId }) + const { wrapper, mocks } = getWrapper({ + space, + resource, + fileId, + path: '/', + capabilityState: vaultCapabilityState + }) + await wrapper.vm.resolvePrivateLinkTask.last + expect(replace).not.toHaveBeenCalled() + expect(mocks.$router.push).toHaveBeenCalled() + }) + + it('does not reload when already in vault scope', async () => { + const replace = vi.fn() + Object.defineProperty(window, 'location', { + configurable: true, + value: { replace, href: '' } + }) + const fileId = 'vault-provider$space!item' + const space = mock({ getDriveAliasAndItem: () => 'personal/home' }) + const resource = mock({ fileId }) + const { wrapper, mocks } = getWrapper({ + space, + resource, + fileId, + path: '/', + scope: 'vault', + capabilityState: vaultCapabilityState + }) + await wrapper.vm.resolvePrivateLinkTask.last + expect(replace).not.toHaveBeenCalled() + expect(mocks.$router.push).toHaveBeenCalled() + }) + }) it('passes the details query param if given via query', async () => { const details = 'sharing' const { wrapper, mocks } = getWrapper({ details, path: '/' }) @@ -138,7 +215,9 @@ function getWrapper({ fileId = '', details = '', hiddenShare = false, - openWithDefaultAppQuery = 'true' + openWithDefaultAppQuery = 'true', + scope = '', + capabilityState = {} }: { space?: SpaceResource resource?: Resource @@ -147,7 +226,13 @@ function getWrapper({ details?: string hiddenShare?: boolean openWithDefaultAppQuery?: string + scope?: string + capabilityState?: Record } = {}) { + vi.mocked(useRouteParam).mockImplementation( + (str) => (str === 'scope' && scope ? scope : str) as any + ) + vi.mocked(queryItemAsString).mockImplementation((str) => { if (str === 'fileId') { return fileId @@ -174,7 +259,7 @@ function getWrapper({ mocks, wrapper: shallowMount(resolvePrivateLink, { global: { - plugins: [...defaultPlugins()], + plugins: [...defaultPlugins({ piniaOptions: { capabilityState } })], mocks, provide: mocks }