Files
baserow/web-frontend/modules/builder/components/PublicPageContent.vue
Tsering Paljor 6391674129 fix: Revoke a login's access tokens on logout (#6951)
* Reject the access tokens of a login session once the user logs out.

* Reject user source access tokens once the user logs out of the published application.

* Refuse websocket connections made with the access token of a logged-out session.

* Keep a logged-out session revoked for backends running another Baserow version.

* Test that an unreadable revoked-session marker keeps a token off the websocket cached fast path.

* Revoke the session's access tokens when logging out with an expired refresh token.

* Test that logging out with an already rotated refresh token rejects the session's access tokens.

* Make the user source blacklist test show that the access token authenticates before the logout.

* Log a published application visitor out in the tabs where their login ended elsewhere.

* Add the changelog entries.

* Keep a published app login whose cookie the browser never saved, such as in a cross-site iframe.
2026-12-02 08:57:10 +04:00

438 lines
12 KiB
Vue

<template>
<div>
<PreviewSecurityNotice
v-if="showPreviewSecurityNotice"
:untrusted-content="builder.untrusted_content"
/>
<BuilderToasts />
<RecursiveWrapper :components="builderPageDecorators">
<PageContent
v-if="canViewPage"
:path="path"
:params="params"
:elements="elements"
:shared-elements="sharedElements"
/>
</RecursiveWrapper>
</div>
</template>
<script setup>
import { computed, watch, onMounted, onBeforeUnmount, provide } from 'vue'
import { useStore } from 'vuex'
import { useHead, useNuxtApp } from '#app'
import PageContent from '@baserow/modules/builder/components/page/PageContent'
import { DataProviderType } from '@baserow/modules/core/dataProviderTypes'
import { DATA_SCOPES } from '@baserow/modules/builder/constants'
import BuilderToasts from '@baserow/modules/builder/components/BuilderToasts'
import PreviewSecurityNotice from '@baserow/modules/builder/components/page/PreviewSecurityNotice'
import ApplicationBuilderFormulaInput from '@baserow/modules/builder/components/ApplicationBuilderFormulaInput'
import _ from 'lodash'
import { prefixInternalResolvedUrl } from '@baserow/modules/builder/utils/urlResolution'
import { userCanViewPage } from '@baserow/modules/builder/utils/visibility'
import { getCustomFaviconLinks } from '@baserow/modules/builder/utils/favicon'
import { consumeLoginError } from '@baserow/modules/builder/utils/auth'
import { consumeStaleSiteReloadNotice } from '@baserow/modules/builder/utils/error'
import { resolveSafeNextPath } from '@baserow/modules/builder/utils/routing'
import { QUERY_PARAM_TYPE_HANDLER_FUNCTIONS } from '@baserow/modules/builder/enums'
import RecursiveWrapper from '@baserow/modules/core/components/RecursiveWrapper'
import { ThemeConfigBlockType } from '@baserow/modules/builder/themeConfigBlockTypes'
import { useRoute, useRouter } from '#imports'
defineOptions({
name: 'PublicPageContent',
})
const store = useStore()
const route = useRoute()
const router = useRouter()
const nuxtApp = useNuxtApp()
const { $registry, $i18n } = nuxtApp
const props = defineProps({
workspace: {
type: Object,
required: true,
},
builder: {
type: Object,
required: true,
},
page: {
type: Object,
required: true,
},
params: {
type: Object,
required: true,
},
path: {
type: String,
required: true,
},
mode: {
type: String,
required: true,
},
})
provide('workspace', props.workspace)
provide('builder', props.builder)
provide('currentPage', props.page)
provide('mode', props.mode)
provide('formulaComponent', ApplicationBuilderFormulaInput)
provide(
'applicationContext',
computed(() => applicationContext.value)
)
const elements = computed(() => {
return store.getters['element/getRootElements'](props.page)
})
const builderPageDecorators = computed(() => {
// Get available page decorators from registry
return Object.values($registry.getAll('builderPageDecorator') || {})
.filter((decorator) => decorator.isDecorationAllowed(props.workspace))
.map((decorator) => ({
component: decorator.component,
props: decorator.getProps(),
}))
})
const applicationContext = computed(() => ({
workspace: props.workspace,
builder: props.builder,
pageParamsValue: props.params,
mode: props.mode,
dataScope: DATA_SCOPES.EXCLUDE_TARGET_SUBTREE,
}))
const dispatchContext = computed(() =>
DataProviderType.getAllDataSourceDispatchContext(
$registry.getAll('builderDataProvider'),
{ ...applicationContext.value, page: props.page }
)
)
// Separate dispatch context for application level data sources
const applicationDispatchContext = computed(() =>
DataProviderType.getAllDataSourceDispatchContext(
$registry.getAll('builderDataProvider'),
{ builder: props.builder, mode: props.mode }
)
)
const sharedPage = computed(() =>
store.getters['page/getSharedPage'](props.builder)
)
const sharedElements = computed(() =>
store.getters['element/getRootElements'](sharedPage.value)
)
// Preview tells the editor when code they added to this page is restricted.
const showPreviewSecurityNotice = computed(
() =>
props.mode === 'preview' &&
(props.builder.untrusted_content ||
(nuxtApp.$isAuthorCodeRestricted() &&
($registry
.getList('plugin')
.some((plugin) =>
plugin.hasBuilderAuthorCode({ builder: props.builder })
) ||
[props.page, sharedPage.value].some((page) =>
(page?.orderedElements ?? []).some((element) =>
$registry.get('element', element.type).hasAuthorCode(element)
)
))))
)
const isAuthenticated = computed(() =>
store.getters['userSourceUser/isAuthenticated'](props.builder)
)
const faviconLinks = computed(() => getCustomFaviconLinks(props.builder))
const themeConfigBlocks = computed(() =>
$registry.getOrderedList('themeConfigBlock')
)
const themeStyle = computed(() =>
ThemeConfigBlockType.getAllStyles(
themeConfigBlocks.value,
props.builder.theme
)
)
const elementResponsiveStyles = computed(() =>
$registry
.getList('element')
.map((elementType) => elementType.getPublicResponsiveStyles(props.builder))
.filter(Boolean)
.join('\n')
)
const headConfig = computed(() => {
const cssVars = Object.entries(themeStyle.value)
.map(([key, value]) => `\n${key}: ${value};`)
.join(' ')
const header = {
titleTemplate: '',
title: props.page.name,
bodyAttrs: {
class: 'public-page',
},
style: [
{ innerHTML: `:root { ${cssVars} }`, type: 'text/css' },
{
innerHTML: elementResponsiveStyles.value,
type: 'text/css',
},
],
}
if (faviconLinks.value) {
header.link = faviconLinks.value
}
const pluginHeaders = $registry.getList('plugin').map((plugin) =>
plugin.getBuilderApplicationHeaderAddition({
builder: props.builder,
mode: props.mode,
})
)
const result = _.mergeWith(
{},
...pluginHeaders,
header,
(objValue, srcValue, key) => {
switch (key) {
case 'link':
case 'script':
if (_.isArray(objValue)) {
return objValue.concat(srcValue)
}
}
return undefined // Default merge action
}
)
return result
})
useHead(headConfig)
watch(
() => route.query,
(newQuery) => {
// when query string changed due to user action,
// update the page's query parameters in the store
Promise.all(
props.page.query_params.map(({ name, type }) => {
let value
try {
if (newQuery[name]) {
value = QUERY_PARAM_TYPE_HANDLER_FUNCTIONS[type](newQuery[name])
}
} catch {
// Skip setting the parameter if the user-provided value
// doesn't pass our parameter `type` validation.
return null
}
return store.dispatch('pageParameter/setParameter', {
page: props.page,
name,
value,
})
})
)
},
{ immediate: true, deep: true }
)
watch(
() => dispatchContext.value,
(newDispatchContext, oldDispatchContext) => {
/**
* Update data source content on dispatch context changes
*/
if (!_.isEqual(newDispatchContext, oldDispatchContext)) {
store.dispatch('dataSourceContent/debouncedFetchPageDataSourceContent', {
page: props.page,
data: newDispatchContext,
mode: props.mode,
})
}
},
{ deep: true }
)
watch(
() => applicationDispatchContext.value,
(newDispatchContext, oldDispatchContext) => {
/**
* Update data source content on dispatch context changes
*/
if (!_.isEqual(newDispatchContext, oldDispatchContext)) {
store.dispatch('dataSourceContent/debouncedFetchPageDataSourceContent', {
page: sharedPage.value,
data: newDispatchContext,
mode: props.mode,
})
}
},
{ deep: true }
)
watch(
() => isAuthenticated.value,
async (newIsAuthenticated) => {
// When the user login or logout, we need to refetch the elements and actions
// as they might have changed
await Promise.all([
store.dispatch('dataSource/fetchPublished', {
page: sharedPage.value,
}),
store.dispatch('dataSource/fetchPublished', {
page: props.page,
}),
store.dispatch('element/fetchPublished', {
builder: props.builder,
page: sharedPage.value,
}),
store.dispatch('element/fetchPublished', {
builder: props.builder,
page: props.page,
}),
store.dispatch('builderWorkflowAction/fetchPublished', {
page: props.page,
}),
store.dispatch('builderWorkflowAction/fetchPublished', {
page: sharedPage.value,
}),
])
if (newIsAuthenticated) {
// If the user has just logged in, we redirect him to the next page.
await maybeRedirectToNextPage()
} else {
// If the user is on a hidden page, redirect them to the Login page if possible.
await maybeRedirectUserToLoginPage()
}
}
)
/**
* When the visitor comes back to this tab, their login may have ended in another
* tab. It's then forgotten, and the page reacts like after a logout.
*/
const forgetLoginEndedElsewhere = () => {
if (document.visibilityState === 'visible') {
store.dispatch('userSourceUser/forgetLoginEndedElsewhere', {
application: props.builder,
})
}
}
onMounted(async () => {
document.addEventListener('visibilitychange', forgetLoginEndedElsewhere)
checkProviderLoginError()
checkStaleSiteReloadNotice()
await maybeRedirectUserToLoginPage()
})
onBeforeUnmount(() => {
document.removeEventListener('visibilitychange', forgetLoginEndedElsewhere)
})
/**
* Returns true if the current user is allowed to view this page,
* otherwise returns false.
*/
const canViewPage = computed(() =>
userCanViewPage(
store.getters['userSourceUser/getUser'](props.builder),
store.getters['userSourceUser/isAuthenticated'](props.builder),
props.page
)
)
/**
* If the user does not have access to the current page, redirect them to
* the Login page if possible.
*/
const maybeRedirectUserToLoginPage = async () => {
if (!canViewPage.value && props.builder.login_page_id) {
const loginPage = await store.getters['page/getById'](
props.builder,
props.builder.login_page_id
)
const url = prefixInternalResolvedUrl(
loginPage.path,
'page',
props.mode,
props.builder.id
)
const currentPath = route.fullPath
if (url !== currentPath) {
store.dispatch('builderToast/info', {
title: $i18n.t('publicPage.authorizedToastTitle'),
message: $i18n.t('publicPage.authorizedToastMessage'),
})
const nextPath = encodeURIComponent(currentPath)
await router.push({ path: url, query: { next: nextPath } })
}
}
}
const maybeRedirectToNextPage = async () => {
const nextPath = resolveSafeNextPath(route.query.next)
if (nextPath) {
await router.push(nextPath)
}
}
/**
* The page has been reloaded because the site was published again while the
* visitor was doing something, like submitting a form, logging in or loading more
* content. That action didn't happen, so we tell them to try again.
*/
const checkStaleSiteReloadNotice = () => {
if (consumeStaleSiteReloadNotice()) {
store.dispatch('builderToast/info', {
title: $i18n.t('publicPage.siteUpdatedToastTitle'),
message: $i18n.t('publicPage.siteUpdatedToastMessage'),
})
}
}
/**
* When an SSO login fails, the provider redirects back with an error code
* in a query parameter. An auth form element reads and removes that
* parameter when it mounts and displays the message inline like the
* email/password flow.
*
* But an IdP can send the user straight to a deep page that the user
* isn't allowed to view, which isn't rendered at all. The elements of
* the page are mounted before this hook runs, so an error still present
* in the URL here means nothing displayed it, and we report it with a
* notification instead.
*/
const checkProviderLoginError = () => {
const message = consumeLoginError($registry, props.builder.user_sources)
if (message) {
store.dispatch('builderToast/error', {
title: $i18n.t('publicPage.loginErrorToastTitle'),
message,
})
}
}
</script>