ipal-doctor: one command that checks a site against the team rules

bin ipal-doctor + @intecion/ipal-kit/doctor: static rules (versions, Node 22, AGENT.md §3
layout and naming, database names, env rules, headers, X-Powered-By, CSP, 2FA, cookie
scanner, adminMeta, AGENTS.md, skills, seeds, block library via blockLibraryManifest,
ipal-audit --quick), registry rules (latest plugin, pnpm audit --prod), live rules (--url,
ipal-seo-check), deep rules (--deep via payload run; GET /api/ipal/doctor, off until
IPAL_DOCTOR_TOKEN). Human / JSON output, --strict, exit codes 0/1/2/3, --sites drift table,
ipal-doctor.json with reasons, until, files, seen.

Fixes found on the way: sitemap leaves out the System pages 404 page; ipal-audit finds
array labels after fields, any form seed, runs grep in UTF-8; ipal-seo-check counts
duplicate titles per language. Reference site at zero findings (static), CI steps on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-29 16:03:00 +01:00
co-authored by Claude Opus 5.5
parent 0ab133fbd7
commit a4e8cfeae8
62 changed files with 2998 additions and 70 deletions
+162
View File
@@ -0,0 +1,162 @@
import type { DoctorContext } from '../project.js'
import type { DoctorRule, RuleFinding } from '../types.js'
import { configFile, nextConfigFile } from '../project.js'
import { lt, stripComments } from '../util.js'
const next = (ctx: DoctorContext) => stripComments(ctx.read(nextConfigFile(ctx) ?? ''))
const cfg = (ctx: DoctorContext) => stripComments(ctx.read(configFile(ctx) ?? ''))
const isLocal = (url: string) => /^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::|\/|$)/i.test(url)
const noindex = (html: string, robotsHeader: null | string) =>
/<meta[^>]+name=["']robots["'][^>]*content=["'][^"']*noindex/i.test(html) || /<meta[^>]+content=["'][^"']*noindex[^>]+name=["']robots["']/i.test(html) || /noindex/i.test(robotsHeader ?? '')
/** Days between two ISO dates. */
const daysBetween = (from: string, to: string) => Math.floor((Date.parse(to) - Date.parse(from)) / 86_400_000)
export const securityRules: DoctorRule[] = [
{
id: 'security-headers',
check: async (ctx) => {
const out: RuleFinding[] = []
const file = nextConfigFile(ctx)
if (!/buildSecurityHeaders\s*\(/.test(next(ctx))) {out.push({ file: file ?? 'next.config.ts', fix: 'headers(): [{ source: "/:path*", headers: buildSecurityHeaders({ csp }) }]', message: 'next.config does not use buildSecurityHeaders', severity: 'error' })}
if (ctx.live) {
const home = await ctx.live.fetch('/')
if (home.status) {
const h = home.headers
const missing = [
...(isLocal(ctx.live.url) || h.get('strict-transport-security') ? [] : ['Strict-Transport-Security']),
...(h.get('x-content-type-options') ? [] : ['X-Content-Type-Options']),
...(h.get('referrer-policy') ? [] : ['Referrer-Policy']),
...(h.get('permissions-policy') ? [] : ['Permissions-Policy']),
...(h.get('x-frame-options') || /frame-ancestors/.test(h.get('content-security-policy') ?? '') ? [] : ['X-Frame-Options / frame-ancestors']),
]
if (missing.length) {out.push({ fix: 'buildSecurityHeaders on /:path* (security.md)', message: `live / lacks ${missing.join(', ')}`, severity: 'error' })}
}
}
return out
},
docs: 'docs/security.md',
group: 'security',
source: 'static',
},
{
id: 'powered-by',
check: async (ctx) => {
const out: RuleFinding[] = []
if (!/withSecurityConfig\s*\(|poweredByHeader\s*:\s*false/.test(next(ctx))) {
out.push({ file: nextConfigFile(ctx) ?? 'next.config.ts', fix: 'export default withPayload(withSecurityConfig(nextConfig))', message: 'X-Powered-By is not hidden (withSecurityConfig)', severity: 'error' })
}
if (ctx.live) {
for (const path of ['/', `${ctx.live.adminRoute}/login`]) {
const res = await ctx.live.fetch(path)
const value = res.headers.get('x-powered-by')
if (value) {out.push({ fix: 'withSecurityConfig (security.md → X-Powered-By)', message: `live ${path} sends X-Powered-By: ${value}`, severity: 'error' })}
}
}
return out
},
docs: 'docs/security.md',
group: 'security',
source: 'static',
},
{
id: 'csp',
check: async (ctx) => {
const code = next(ctx) + stripComments(ctx.read(`${ctx.src}proxy.ts`))
if (!/\bcsp\s*[:,}]|buildCsp|Content-Security-Policy/i.test(code)) {
return [{ file: nextConfigFile(ctx) ?? 'next.config.ts', fix: "buildSecurityHeaders({ csp: { mode: 'report-only', reportUri: '/api/csp-report' } }) + buildAdminCspHeaders on /admin/:path*", message: 'no Content-Security-Policy', severity: 'warning' }]
}
const out: RuleFinding[] = []
if (!/buildAdminCspHeaders\s*\(/.test(code)) {out.push({ file: nextConfigFile(ctx) ?? 'next.config.ts', fix: "{ source: '/admin/:path*', headers: buildAdminCspHeaders(csp) }", message: 'no CSP for the admin (buildAdminCspHeaders)', severity: 'warning' })}
let reportOnly = /mode\s*:\s*['"]report-only['"]/.test(code) || !/mode\s*:\s*['"]enforce['"]/.test(code)
if (ctx.live) {
for (const path of ['/', `${ctx.live.adminRoute}/login`]) {
const h = (await ctx.live.fetch(path)).headers
if (h.get('content-security-policy')) {
if (path === '/') {reportOnly = false}
} else if (h.get('content-security-policy-report-only')) {
reportOnly = true
} else {
out.push({ fix: 'security.md → ROLL OUT CSP', message: `live ${path} has no CSP header`, severity: 'warning' })
}
}
}
if (reportOnly) {
const seen = ctx.config.seen?.['csp-report-only'] ?? ctx.today
const days = daysBetween(seen, ctx.today)
out.push({
fix: "switch to mode: 'enforce' once the reports are clean (security.md → ROLL OUT CSP)",
message: `CSP is report-only (since ${seen}${days ? `, ${days} days` : ''})`,
severity: days >= 28 ? 'warning' : 'info',
})
}
return out
},
docs: 'docs/security.md',
group: 'security',
source: 'static',
},
{
id: '2fa',
check: (ctx) => {
const out: RuleFinding[] = []
if (!/twoFactor\s*:\s*\{[\s\S]*?plugin\s*:\s*payloadTotp/.test(cfg(ctx))) {out.push({ file: configFile(ctx) ?? 'payload.config.ts', fix: "ipalKit({ twoFactor: { issuer: '<site>', plugin: payloadTotp } })", message: '2FA is not on (twoFactor with plugin: payloadTotp)', severity: 'error' })}
else if (!ctx.installed('@clocklimited/payload-2fa')) {out.push({ fix: 'pnpm add @clocklimited/payload-2fa', message: '@clocklimited/payload-2fa is not installed', severity: 'error' })}
if (ctx.deep?.usersWithout2fa) {out.push({ fix: 'each of them signs in and sets up TOTP (forceSetup)', message: `${ctx.deep.usersWithout2fa} panel user(s) without TOTP`, severity: 'error' })}
return out
},
docs: 'docs/security.md#2fa-totp--enforced-for-every-user',
group: 'security',
source: 'static',
},
{
id: 'cookie-scanner',
check: (ctx) => {
const code = cfg(ctx)
if (!/cookieScanner\s*:/.test(code) || /cookieScanner\s*:\s*false/.test(code)) {
return [{ file: configFile(ctx) ?? 'payload.config.ts', fix: 'ipalKit({ cookieScanner: { crawl: true } }) + generate:importmap', message: 'the cookie scanner is off', severity: 'error' }]
}
const out: RuleFinding[] = []
if (!/cookieScanner\s*:\s*\{[^}]*\bcrawl\s*:\s*(?!false)/.test(code)) {out.push({ fix: 'cookieScanner: { crawl: true } (shared cookie-crawler service)', message: 'cookie scanner without crawl — no scheduled site check', severity: 'info' })}
if (ctx.deep?.pendingCookies) {out.push({ fix: 'Privacy → Cookies → Review and approve → Approve and add', message: `${ctx.deep.pendingCookies} cookie(s) wait for approval`, severity: 'warning' })}
return out
},
docs: 'docs/cookie-scanner.md',
group: 'security',
source: 'static',
},
{
id: 'admin-meta',
check: async (ctx) => {
const out: RuleFinding[] = []
const code = cfg(ctx)
if (lt(ctx.pluginVersion, '1.14.5')) {out.push({ message: `admin link previews (adminMeta) need ipal-kit ≥ 1.14.5 (installed ${ctx.pluginVersion ?? 'none'})`, severity: 'warning' })}
if (/adminMeta\s*:\s*false/.test(code)) {out.push({ file: configFile(ctx) ?? undefined, message: 'adminMeta: false — the panel has no link preview and no noindex from the plugin', severity: 'warning' })}
if (/agency\s*:\s*false/.test(code)) {out.push({ file: configFile(ctx) ?? undefined, message: 'agency: false — no technology credit', severity: 'warning' })}
if (ctx.live && !out.length) {
const res = await ctx.live.fetch(`${ctx.live.adminRoute}/login`)
if (res.status && res.status < 400 && !/\/api\/ipal\/admin-og/.test(res.text)) {out.push({ fix: 'adminMeta on (default) and generate:importmap', message: 'the live admin has no ipal link preview (og:image /api/ipal/admin-og)', severity: 'warning' })}
}
return out
},
docs: 'docs/white-label.md',
group: 'admin',
source: 'static',
},
{
id: 'admin-noindex',
check: async (ctx) => {
if (!ctx.live) {return []}
const path = `${ctx.live.adminRoute}/login`
const res = await ctx.live.fetch(path)
if (!res.status || res.status >= 400) {return [{ message: `live ${path} → ${res.status || res.error}`, severity: 'warning' }]}
return noindex(res.text, res.headers.get('x-robots-tag')) ? [] : [{ fix: 'adminMeta robots (default noindex, nofollow)', message: `live ${path} is not noindex`, severity: 'error' }]
},
docs: 'docs/white-label.md',
group: 'admin',
source: 'live',
},
]
export { noindex }