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:
co-authored by
Claude Opus 5.5
parent
0ab133fbd7
commit
a4e8cfeae8
@@ -0,0 +1,105 @@
|
||||
import type { DoctorRule, RuleFinding } from '../types.js'
|
||||
|
||||
import { databaseNameFromUri, databaseNamesFor } from '../../env/databaseName.js'
|
||||
import { configFile } from '../project.js'
|
||||
import { stripComments } from '../util.js'
|
||||
|
||||
/** The database name of a URI; also for placeholder URIs (`HOST:PORT`) that `new URL` rejects. */
|
||||
const dbName = (uri: string | undefined) => databaseNameFromUri(uri) ?? (uri?.match(/^[a-z+]+:\/\/[^/]*\/([^/?#]+)/i)?.[1] ?? null)
|
||||
|
||||
/** Placeholder hosts, users or names in .env.example — fine, they carry no secret. */
|
||||
const PLACEHOLDER = /[<>]|\b(?:HOST|PORT|USER|PASS(?:WORD)?|DATABASE|example)\b|\$\{/i
|
||||
|
||||
export const envRules: DoctorRule[] = [
|
||||
{
|
||||
id: 'db-name',
|
||||
check: (ctx) => {
|
||||
const out: RuleFinding[] = []
|
||||
if (!ctx.domain) {
|
||||
return [{ fix: 'add "domain": "<site domain>" to ipal-doctor.json', message: 'no site domain (ipal-doctor.json "domain", or a non-local NEXT_PUBLIC_SERVER_URL) — database names not checked', severity: 'warning' }]
|
||||
}
|
||||
const { dev, prod } = databaseNamesFor(ctx.domain)
|
||||
for (const [file, env] of [['.env', ctx.env.local], ['.env.example', ctx.env.example]] as const) {
|
||||
if (!env) {continue}
|
||||
const uri = env.DATABASE_URI
|
||||
if (!uri) {
|
||||
if (file === '.env.example') {out.push({ file, fix: `DATABASE_URI=mongodb://USER:PASS@HOST:PORT/${dev}?authSource=admin`, message: `${file} has no DATABASE_URI`, severity: 'warning' })}
|
||||
continue
|
||||
}
|
||||
if (/^file:/i.test(uri)) {
|
||||
out.push({ file, fix: `a MongoDB database named ${dev}`, message: `${file}: SQLite file — the team standard is a database named ${dev}`, severity: 'warning' })
|
||||
continue
|
||||
}
|
||||
const name = dbName(uri)
|
||||
if (name === prod) {
|
||||
out.push({ file, fix: `point it at ${dev}`, message: `${file} points at the production database ${prod} — a local run or seed writes to production`, severity: 'error' })
|
||||
} else if (name !== dev) {
|
||||
out.push({ file, fix: `rename the database to ${dev} (production: ${prod}) — deployment.md backup + restore`, message: `${file}: database "${name ?? '(none)'}", expected ${dev}`, severity: 'error' })
|
||||
}
|
||||
}
|
||||
const prodName = ctx.env.prod ? dbName(ctx.env.prod.DATABASE_URI) : undefined
|
||||
if (ctx.env.prod && prodName !== prod) {out.push({ fix: `name the production database ${prod}`, message: `--env-file: database "${prodName ?? '(none)'}", expected ${prod}`, severity: 'error' })}
|
||||
return out
|
||||
},
|
||||
docs: 'docs/deployment.md',
|
||||
group: 'env',
|
||||
source: 'static',
|
||||
},
|
||||
{
|
||||
id: 'env-rules',
|
||||
check: (ctx) => {
|
||||
const file = ctx.exists(`${ctx.src}instrumentation.ts`) ? `${ctx.src}instrumentation.ts` : 'instrumentation.ts'
|
||||
const code = stripComments(ctx.read(file))
|
||||
if (!/assertEnv\s*\(/.test(code)) {
|
||||
return [{ file, fix: "register(): assertEnv(ipalEnvRules({ storage: true, ipHeader: true })) from '@intecion/ipal-kit/env'", message: 'instrumentation.ts does not call assertEnv(ipalEnvRules(…))', severity: 'error' }]
|
||||
}
|
||||
const out: RuleFinding[] = []
|
||||
if (!/ipalEnvRules\s*\(/.test(code)) {out.push({ file, message: 'assertEnv without ipalEnvRules — the plugin variables are not checked', severity: 'warning' })}
|
||||
if (/from\s+['"]@intecion\/ipal-kit['"]/.test(code)) {out.push({ file, fix: "import from '@intecion/ipal-kit/env'", message: 'instrumentation.ts imports the package root (Edge build breaks)', severity: 'error' })}
|
||||
const cfg = stripComments(ctx.read(configFile(ctx) ?? ''))
|
||||
const flag = (name: string) => new RegExp(`\\b${name}\\s*:`).test(code)
|
||||
if (/buildR2Storage/.test(cfg) && !flag('storage')) {out.push({ file, fix: 'ipalEnvRules({ storage: true })', message: 'R2 is wired but ipalEnvRules has no storage flag', severity: 'warning' })}
|
||||
if (/cookieScanner\s*:\s*\{[^}]*\bcrawl\s*:\s*(?!false)/.test(cfg) && !flag('cookieCrawl')) {out.push({ file, fix: 'ipalEnvRules({ cookieCrawl: true })', message: 'cookieScanner.crawl is on but ipalEnvRules has no cookieCrawl flag', severity: 'warning' })}
|
||||
if (!flag('ipHeader')) {out.push({ file, fix: 'ipalEnvRules({ ipHeader: true })', message: 'ipalEnvRules has no ipHeader flag — rate limits trust no client-IP header', severity: 'warning' })}
|
||||
if (!flag('databaseName')) {out.push({ file, fix: "ipalEnvRules({ databaseName: { domain: '<domain>' } })", message: 'ipalEnvRules has no databaseName check', severity: 'info' })}
|
||||
return out
|
||||
},
|
||||
docs: 'docs/env.md',
|
||||
group: 'env',
|
||||
source: 'static',
|
||||
},
|
||||
{
|
||||
id: 'env-example',
|
||||
check: (ctx) => {
|
||||
if (!ctx.env.example) {return [{ fix: 'add .env.example (names, placeholder values)', message: 'no .env.example', severity: 'error' }]}
|
||||
const out: RuleFinding[] = []
|
||||
const cfg = stripComments(ctx.read(configFile(ctx) ?? ''))
|
||||
const want = ['DATABASE_URI', 'PAYLOAD_SECRET', 'NEXT_PUBLIC_SERVER_URL', 'IPAL_IP_HEADER']
|
||||
if (/buildR2Storage/.test(cfg) && !ctx.auditSkips.has('r2')) {want.push('R2_BUCKET', 'R2_ENDPOINT', 'R2_ACCESS_KEY_ID', 'R2_SECRET_ACCESS_KEY', 'R2_PUBLIC_URL')}
|
||||
if (/cookieScanner\s*:\s*\{[^}]*\bcrawl\s*:\s*(?!false)/.test(cfg)) {want.push('IPAL_COOKIE_SCAN_URL', 'IPAL_COOKIE_SCAN_TOKEN')}
|
||||
const missing = want.filter((v) => !(v in ctx.env.example!))
|
||||
if (missing.length) {out.push({ file: '.env.example', fix: 'add the names (empty or placeholder values)', message: `.env.example lacks ${missing.join(', ')}`, severity: 'warning' })}
|
||||
// A real-looking credential in a committed file.
|
||||
const creds = (ctx.env.example.DATABASE_URI ?? '').match(/^[a-z+]+:\/\/([^@/]+)@/i)?.[1]
|
||||
if (creds && !PLACEHOLDER.test(creds)) {out.push({ file: '.env.example', fix: 'replace with USER:PASS', message: 'DATABASE_URI in .env.example carries credentials', severity: 'error' })}
|
||||
for (const key of ['PAYLOAD_SECRET', 'R2_SECRET_ACCESS_KEY', 'GRAPH_CLIENT_SECRET', 'IPAL_DOCTOR_TOKEN']) {
|
||||
const value = ctx.env.example[key]
|
||||
if (value && value.length >= 16 && !PLACEHOLDER.test(value)) {out.push({ file: '.env.example', fix: `leave ${key} empty`, message: `${key} in .env.example has a value`, severity: 'error' })}
|
||||
}
|
||||
return out
|
||||
},
|
||||
docs: 'docs/env.md',
|
||||
group: 'env',
|
||||
source: 'static',
|
||||
},
|
||||
{
|
||||
id: 'env-live',
|
||||
check: (ctx) =>
|
||||
ctx.deep?.envProblems?.length
|
||||
? [{ fix: 'set the variable (Coolify → Environment Variables, or .env) and redeploy', message: `the project's env rules fail: ${ctx.deep.envProblems.join(', ')}`, severity: 'error' }]
|
||||
: [],
|
||||
docs: 'docs/env.md',
|
||||
group: 'env',
|
||||
source: 'deep',
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user