R2-1: the notice was rendered instead of the 2FA screen, and its flag is a text search of the importMap.js on disk — not the map in the build. A false alarm (map generated in the build with the repository's file in the running image, a re-exporting map) hid a working code form and locked everyone out. The notice is now rendered above the screen, the log line says "may be", and the flag goes to ipal-kit's own provider only. R2-2: views are compared against copies, so a factory that reuses its view objects is guarded too; copyViews, changedViewKeys, SAFE_TOTP_VIEW, totpViewImportKey, findImportMapFile and missingImportMapEntries are exported; ipal-doctor warns when the config registers the 2FA package's own view; docs wording and a misplaced comment fixed. Co-Authored-By: Claude Fable 5.2 <[email protected]>
148 lines
7.0 KiB
TypeScript
148 lines
7.0 KiB
TypeScript
/**
|
|
* The `back` query parameter of the 2FA setup and verify screens.
|
|
*
|
|
* @clocklimited/payload-2fa passes it, unchecked, to `location.replace()` once the code is
|
|
* accepted. Anyone can put it in a link in front of a panel user: `javascript:…` then runs in
|
|
* the admin origin with the session that has just passed the second factor, and an absolute
|
|
* URL sends the user off-site straight after a genuine login step (GHSA-rffw-fq79-g3ch in the
|
|
* original `payload-totp`, fixed there in 3.0.5; the fork does not have the fix).
|
|
*
|
|
* ipal-kit therefore cleans the value on the server before the package's view sees it
|
|
* (SafeTotpView.tsx): `twoFactor.safeBack` — on by default, a function replaces the rule,
|
|
* `false` turns it off.
|
|
*/
|
|
|
|
/** What a `back` rule is told about the panel. */
|
|
export type SafeBackContext = {
|
|
/** `routes.admin` of the Payload config, e.g. `/admin`. */
|
|
adminRoute: string
|
|
/**
|
|
* The site's own origin (from `serverURL`), when the config has one. An absolute `back` on
|
|
* this origin is accepted; without it only relative values are.
|
|
*/
|
|
origin?: string
|
|
}
|
|
|
|
/**
|
|
* A `back` rule: gets the raw query value (undefined when absent or repeated) and returns the
|
|
* URL the screen goes to after a valid code. Return a path on the site's own origin.
|
|
*/
|
|
export type SafeBackRule = (back: string | undefined, context: SafeBackContext) => string
|
|
|
|
/** Never a real site: a relative `back` is resolved against it, and anything that leaves it is refused. */
|
|
const PLACEHOLDER_ORIGIN = 'http://ipal-safe-back.invalid'
|
|
|
|
const trimSlashes = (path: string) => `/${path.replace(/^\/+|\/+$/g, '')}`
|
|
|
|
/**
|
|
* The default rule, the same as upstream's: `back` is resolved the way a browser resolves it
|
|
* and kept only when it stays on the site's origin, over HTTP(S), without credentials, inside
|
|
* the admin route. Anything else — `javascript:`, `data:`, `//host`, `/\host`, `\\host`,
|
|
* `https:host`, a path outside the panel, no value at all — gives the admin root.
|
|
*
|
|
* Returns a path (`/admin/collections/pages?page=2`), never an absolute URL, so the browser
|
|
* goes to exactly what was checked and cannot be taken off the origin.
|
|
*/
|
|
export const safeAdminBack: SafeBackRule = (back, { adminRoute, origin }) => {
|
|
const adminPath = trimSlashes(adminRoute || '/')
|
|
if (typeof back !== 'string' || !back) {return adminPath}
|
|
|
|
let base = PLACEHOLDER_ORIGIN
|
|
if (origin) {
|
|
try {
|
|
base = new URL(origin).origin
|
|
} catch {
|
|
/* not a URL: relative values only */
|
|
}
|
|
}
|
|
|
|
let url: URL
|
|
try {
|
|
url = new URL(back, base)
|
|
} catch {
|
|
return adminPath
|
|
}
|
|
// `blob:` URLs report the origin that made them, hence the protocol check.
|
|
if (url.origin !== base || !['http:', 'https:'].includes(url.protocol) || url.username || url.password) {
|
|
return adminPath
|
|
}
|
|
const inside = adminPath === '/' || url.pathname === adminPath || url.pathname.startsWith(`${adminPath}/`)
|
|
// The parser collapses `//`, but a path that still starts with two slashes would be read as a host.
|
|
if (!inside || url.pathname.startsWith('//')) {return adminPath}
|
|
return `${url.pathname}${url.search}${url.hash}`
|
|
}
|
|
|
|
/** The package's views that take `back` — matched by the route the package gives them. */
|
|
const TOTP_VIEW_PATH = /^\/?(?:setup|verify)-totp$/
|
|
|
|
/** Import-map path of the wrapper ipalKit registers. */
|
|
export const SAFE_TOTP_VIEW = '@intecion/ipal-kit/admin#SafeTotpView'
|
|
|
|
/** A view component as it stands in a Payload config. */
|
|
export type TotpViewComponent = { clientProps?: object; exportName?: string; path: string; serverProps?: object } | string
|
|
type ViewLike = { Component?: unknown; path?: string }
|
|
type Dependency = { exportName?: string; path: string; type: 'component' }
|
|
|
|
/** `path#export` of a component — the key it has in the import map. */
|
|
export const totpViewImportKey = (component: TotpViewComponent): string => {
|
|
const [path, hash] = (typeof component === 'string' ? component : component.path).split('#')
|
|
const exportName = (typeof component === 'object' ? component.exportName : undefined) ?? hash ?? 'default'
|
|
return `${path}#${exportName}`
|
|
}
|
|
|
|
/** Key of the `admin.dependencies` entry that keeps a wrapped view importable. */
|
|
export const totpViewDependencyKey = (component: TotpViewComponent) => `ipal-totp-view:${totpViewImportKey(component)}`
|
|
|
|
/**
|
|
* Puts ipal-kit's wrapper in front of the 2FA views for which `added(key)` is true and whose
|
|
* route is `setup-totp` / `verify-totp`: the wrapper cleans `back` and renders the original
|
|
* view, which it finds in the import map (`admin.dependencies` keeps it there). ipal-kit still
|
|
* never imports the optional package. The original's `serverProps` stay server props; its
|
|
* `clientProps` travel as `ipalTotpClientProps`, so the wrapper can hand them to a client view.
|
|
*/
|
|
export function wrapTotpViews<V extends ViewLike>(
|
|
views: Record<string, V>,
|
|
added: (key: string) => boolean,
|
|
rule: SafeBackRule | undefined,
|
|
wrapper = SAFE_TOTP_VIEW,
|
|
): { dependencies: Record<string, Dependency>; views: Record<string, V> } {
|
|
const dependencies: Record<string, Dependency> = {}
|
|
const out: Record<string, V> = {}
|
|
for (const [key, view] of Object.entries(views)) {
|
|
const component = view?.Component as TotpViewComponent | undefined
|
|
const path = typeof component === 'string' ? component : component?.path
|
|
if (!added(key) || !view || typeof view.path !== 'string' || !TOTP_VIEW_PATH.test(view.path) || !component || !path || path === wrapper) {
|
|
out[key] = view
|
|
continue
|
|
}
|
|
const own = typeof component === 'object' ? component : undefined
|
|
const original = own?.exportName ? { exportName: own.exportName, path } : path
|
|
dependencies[totpViewDependencyKey(component)] = { type: 'component', ...(own?.exportName ? { exportName: own.exportName } : {}), path }
|
|
out[key] = {
|
|
...view,
|
|
Component: {
|
|
path: wrapper,
|
|
serverProps: { ...own?.serverProps, ipalSafeBack: rule, ipalTotpClientProps: own?.clientProps, ipalTotpView: original },
|
|
},
|
|
}
|
|
}
|
|
return { dependencies, views: out }
|
|
}
|
|
|
|
/**
|
|
* Shallow copies of a config's views. Put these into the config BEFORE calling a 2FA factory and
|
|
* keep them as `before` for `changedViewKeys`: a view the factory registers is then never the
|
|
* same object as one that was there, also when the factory reuses its view objects between calls.
|
|
*/
|
|
export const copyViews = <V>(views: Record<string, V> | undefined): Record<string, V> =>
|
|
Object.fromEntries(Object.entries(views ?? {}).map(([key, view]) => [key, view && typeof view === 'object' ? { ...view } : view]))
|
|
|
|
/**
|
|
* Keys whose view object is not the one in `before` — what a plugin added or replaced.
|
|
* `added` for `wrapTotpViews`: `const before = copyViews(views)` → run the factory on a config
|
|
* holding `before` → `const keys = changedViewKeys(before, viewsAfter)` → `(key) => keys.has(key)`.
|
|
* A key alone says nothing: the 2FA package replaces a view that already stands under its keys.
|
|
*/
|
|
export const changedViewKeys = (before: Record<string, unknown>, after: Record<string, unknown>): Set<string> =>
|
|
new Set(Object.keys(after).filter((key) => after[key] !== before[key]))
|