Files
ipal-kit/src/modules/security/totp/SafeTotpView.tsx
T
Radosław SmolińskiandClaude Fable 5.2 93e75426ab 2FA screens: the back link is checked on the server (twoFactor.safeBack)
@clocklimited/payload-2fa sends the browser to the `back` query value after a
valid code without checking it (GHSA-rffw-fq79-g3ch upstream, not fixed in the
fork): a prepared link ran script in the panel or left the site.

SafeTotpView now stands in front of the package's setup and verify views and
passes on a checked value: a path on the site's origin inside the admin route,
else the admin root. The package's views stay in the import map through
admin.dependencies, so ipal-kit still never imports the package.

twoFactor.safeBack: false turns it off, a function replaces the rule;
safeAdminBack, wrapTotpViews and SafeTotpView are exported. Projects with 2FA
must regenerate the admin import map.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-24 15:29:06 +01:00

55 lines
2.1 KiB
TypeScript

import type { AdminViewServerProps } from 'payload'
import { getFromImportMap } from 'payload/shared'
import React from 'react'
import { safeAdminBack, type SafeBackRule } from './safeBack.js'
export type SafeTotpViewProps = {
/** `twoFactor.safeBack` as a function: replaces the default rule. */
ipalSafeBack?: SafeBackRule
/** Import-map path of the 2FA package's own view (`@clocklimited/payload-2fa/rsc#TOTPVerify`). */
ipalTotpView: string
} & AdminViewServerProps
const originOf = (serverURL: unknown): string | undefined => {
if (typeof serverURL !== 'string' || !serverURL) {return undefined}
try {
return new URL(serverURL).origin
} catch {
return undefined
}
}
/**
* In front of the 2FA package's setup and verify views: replaces the `back` query value with a
* checked one (safeBack.ts) and renders the package's view with it. The package's form sends
* the browser to `back` after a valid code, so what it receives must be safe to navigate to.
*
* ipalKit registers it (twoFactor.safeBack, on by default). A project that builds its own 2FA
* screens can use it too: give it `ipalTotpView` and list that path in `admin.dependencies`.
*/
export function SafeTotpView(props: SafeTotpViewProps): React.ReactNode {
const { ipalSafeBack, ipalTotpView, ...rest } = props
const config = props.initPageResult?.req?.payload?.config ?? props.payload?.config
const raw = (props.searchParams as Record<string, unknown> | undefined)?.back
const back = (ipalSafeBack ?? safeAdminBack)(typeof raw === 'string' ? raw : undefined, {
adminRoute: config?.routes?.admin ?? '/admin',
origin: originOf(config?.serverURL),
})
const View = getFromImportMap<React.ComponentType<Record<string, unknown>>>({
importMap: props.importMap,
PayloadComponent: ipalTotpView,
schemaPath: '',
silent: true,
})
if (!View) {
throw new Error(
`[ipal] The admin import map has no "${ipalTotpView}" (the 2FA screen ipal-kit puts its back-link check in front of). ` +
'Run `pnpm payload generate:importmap` and commit the result.',
)
}
return <View {...rest} searchParams={{ ...props.searchParams, back }} />
}