diff --git a/docs/doctor.md b/docs/doctor.md index f7b14ee..b9d1f85 100644 --- a/docs/doctor.md +++ b/docs/doctor.md @@ -89,7 +89,7 @@ Turn off: `ipalKit({ doctor: false })`. Adapt: `ipalKit({ doctor: { tokenVar: 'M | | `layout-forbidden` | error | no `middleware.ts`, no `lib/{pages,locales,navigation}.ts`, `fields/{link,sectionHeader,sectionSettings}.ts` | | | `layout-extra` | info | `src/` entries outside the allow list (AGENT.md §3 + `seed/`, `design/`, `db.ts`, `env.rules.ts`, `content.config.ts`; extend with `layout.allow`) | | | `naming` | warning | block folders, collection and global files PascalCase, `scripts/*` kebab-case, no `index.ts` barrels | -| | `importmap` | error | the import map exists, holds the entries for what the config turns on (2FA, R2, cookie scanner, `adminMeta`), names no export the plugin no longer has; with `--deep`, equals a fresh one. Warning: the config registers the 2FA package's own setup / verify view directly (outside the `back` link check — security.md) | +| | `importmap` | error | the import map exists, holds the entries for what the config turns on (2FA, R2, cookie scanner, `adminMeta`), names no export the plugin no longer has; with `--deep`, equals a fresh one. Warning: the config uses the 2FA package's own setup / verify view as a view's `Component` (outside the `back` link check — security.md); not for that path as `ipalTotpView` of `SafeTotpView` or in `admin.dependencies` | | | `tsconfig-skills` | error | `tsconfig.json` excludes the skills folder | | env | `db-name` | error | `DATABASE_URI` in `.env` and `.env.example` names `_dev`; a local `.env` on `_prod` is an error; `--env-file` checks `_prod` (`databaseNamesFor`) | | | `env-rules` | error / warning | `instrumentation.ts` calls `assertEnv(ipalEnvRules(…))` from `/env`, with `storage` (R2), `cookieCrawl` (crawl), `ipHeader`; `databaseName` suggested (info) | diff --git a/docs/security.md b/docs/security.md index abca596..f1fee7d 100644 --- a/docs/security.md +++ b/docs/security.md @@ -610,8 +610,9 @@ guarded. The same holds when the project also lists `payloadTotp(...)` in `plugi your `clientProps`, and `back` plus `searchParams` — both checked; read `back` from the props, not from `useSearchParams()`, which still holds the raw query value. - From a plugin listed after `ipalKit`, or under another key or route: that view is yours and is not wrapped. Use - `wrapTotpViews` or `safeAdminBack` in it. `ipal-doctor` warns when the config names the package's own view - (`@clocklimited/payload-2fa/rsc#TOTPVerify` / `#TOTPSetup`) directly. + `wrapTotpViews` or `safeAdminBack` in it. `ipal-doctor` warns when the config uses the package's own view + (`@clocklimited/payload-2fa/rsc#TOTPVerify` / `#TOTPSetup`) as a view's `Component`; the same path as the + `ipalTotpView` of `SafeTotpView`, or in `admin.dependencies`, is fine. Wrapping views yourself, for a project that runs the 2FA package without `ipalKit`'s `twoFactor` (all from `@intecion/ipal-kit`): diff --git a/src/modules/doctor/rules/layout.ts b/src/modules/doctor/rules/layout.ts index 65f71a8..981f565 100644 --- a/src/modules/doctor/rules/layout.ts +++ b/src/modules/doctor/rules/layout.ts @@ -204,11 +204,13 @@ export const layoutRules: DoctorRule[] = [ } } // A view the project registers itself with the 2FA package's own component is not behind the back-link check. - if (/@clocklimited\/payload-2fa\/rsc#TOTP(?:Setup|Verify)/.test(cfg)) { + // Only as a view's component (`Component: ''` or `Component: { path: '' }`): the same path as the + // value of `ipalTotpView` or of an `admin.dependencies` entry is the fix, not the problem. + if (/\bComponent\s*:\s*(?:\{[^{}]*?\bpath\s*:\s*)?['"`]@clocklimited\/payload-2fa\/rsc#TOTP(?:Setup|Verify)\b/.test(cfg)) { out.push({ file: configFile(ctx) ?? undefined, fix: "register it through wrapTotpViews (or Component: { path: '@intecion/ipal-kit/admin#SafeTotpView', serverProps: { ipalTotpView: '' } } plus the path in admin.dependencies) — security.md \"Your own 2FA screen\"", - message: "the config names the 2FA package's own setup / verify view: a view registered that way sends the browser to an unchecked `back` link after the code", + message: "the config registers the 2FA package's own setup / verify view as a view component: it sends the browser to an unchecked `back` link after the code", severity: 'warning', }) } diff --git a/test/doctor.test.ts b/test/doctor.test.ts index 2b8eea2..b6bafc0 100644 --- a/test/doctor.test.ts +++ b/test/doctor.test.ts @@ -68,15 +68,34 @@ describe('importmap rule and the 2FA back-link check', () => { expect(messages(older).join()).not.toContain('SafeTotpView') }) - test("warns when the config registers the 2FA package's own view itself", async () => { + describe("the 2FA package's own view in the config", () => { const map = entries('@clocklimited/payload-2fa/rsc#TOTPSetup', '@intecion/ipal-kit/admin#TotpRedirectProvider', '@intecion/ipal-kit/admin#SafeTotpView') const admin = 'export { SafeTotpView } from "./x.js"; export { TotpRedirectProvider } from "./y.js"' - const own = "twoFactor: { plugin: payloadTotp }, admin: { components: { views: { Mine: { Component: '@clocklimited/payload-2fa/rsc#TOTPVerify', path: '/my-verify' } } } }" - const raw = await doctor(project({ ...withPlugin(admin), [MAP]: map, 'src/payload.config.ts': own }), ['importmap']) - const NOTE = /^warning: the config names the 2FA package's own setup \/ verify view/ - expect(messages(raw).filter((m) => NOTE.test(m))).toHaveLength(1) - const plain = await doctor(project({ ...withPlugin(admin), [MAP]: map, 'src/payload.config.ts': 'twoFactor: { plugin: payloadTotp }' }), ['importmap']) - expect(messages(plain).filter((m) => NOTE.test(m))).toEqual([]) + const NOTE = /^warning: the config registers the 2FA package's own setup \/ verify view/ + const notes = async (config: string) => + messages(await doctor(project({ ...withPlugin(admin), [MAP]: map, 'src/payload.config.ts': `twoFactor: { plugin: payloadTotp }, ${config}` }), ['importmap'])).filter((m) => NOTE.test(m)) + const RAW = '@clocklimited/payload-2fa/rsc#TOTPVerify' + + test.each([ + ['a string component', `admin: { components: { views: { Mine: { Component: '${RAW}', path: '/my-verify' } } } }`], + ['an object component', `admin: { components: { views: { Mine: { Component: { path: "${RAW}", serverProps: { a: 1 } }, path: '/my-verify' } } } }`], + ['an object component over several lines', `views: { Mine: { Component: {\n clientProps,\n path: \`${RAW}\`,\n}, path: '/my-verify' } }`], + ['the setup view', "views: { Mine: { Component: '@clocklimited/payload-2fa/rsc#TOTPSetup', path: '/my-setup' } }"], + ])('warns for %s', async (_name, config) => { + expect(await notes(config)).toHaveLength(1) + }) + + test.each([ + ['no such view', ''], + // the fix the warning itself suggests + ['the wrapper with the raw path as ipalTotpView, and the path in admin.dependencies', + `admin: { components: { views: { Mine: { Component: { path: '@intecion/ipal-kit/admin#SafeTotpView', serverProps: { ipalTotpView: '${RAW}' } }, path: '/my-verify' } } }, dependencies: { 'my-verify': { path: '${RAW}', type: 'component' } } }`], + ['the wrapper with serverProps written first', `views: { Mine: { Component: { serverProps: { ipalTotpView: '${RAW}' }, path: '@intecion/ipal-kit/admin#SafeTotpView' }, path: '/my-verify' } }`], + ['a dependencies entry alone', `admin: { dependencies: { keep: { type: 'component', path: '${RAW}' } } }`], + ['a comment', `// Component: '${RAW}'`], + ])('does not warn for %s', async (_name, config) => { + expect(await notes(config)).toEqual([]) + }) }) })