import type { DoctorRule, RuleFinding } from '../types.js' import { databaseNameFromUri, databaseNamesFor } from '../../env/databaseName.js' import { legacyEnvName } from '../../shared/envName.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": "" to iapl-doctor.json', message: 'no site domain (iapl-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(iaplEnvRules({ storage: true, ipHeader: true })) from '@intecion/ipal-kit/env'", message: 'instrumentation.ts does not call assertEnv(iaplEnvRules(…))', severity: 'error' }] } const out: RuleFinding[] = [] if (!/\b(?:iaplEnvRules|ipalEnvRules)\s*\(/.test(code)) {out.push({ file, message: 'assertEnv without iaplEnvRules — 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: 'iaplEnvRules({ storage: true })', message: 'R2 is wired but iaplEnvRules has no storage flag', severity: 'warning' })} if (/cookieScanner\s*:\s*\{[^}]*\bcrawl\s*:\s*(?!false)/.test(cfg) && !flag('cookieCrawl')) {out.push({ file, fix: 'iaplEnvRules({ cookieCrawl: true })', message: 'cookieScanner.crawl is on but iaplEnvRules has no cookieCrawl flag', severity: 'warning' })} if (!flag('ipHeader')) {out.push({ file, fix: 'iaplEnvRules({ ipHeader: true })', message: 'iaplEnvRules has no ipHeader flag — rate limits trust no client-IP header', severity: 'warning' })} if (!flag('databaseName')) {out.push({ file, fix: "iaplEnvRules({ databaseName: { domain: '' } })", message: 'iaplEnvRules 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', 'IAPL_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('IAPL_COOKIE_SCAN_URL', 'IAPL_COOKIE_SCAN_TOKEN')} // a plugin variable under its spelling from before 1.17.0 (IPAL_…) is there too const missing = want.filter((v) => !(v in ctx.env.example!) && !((legacyEnvName(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', 'IAPL_DOCTOR_TOKEN', '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', }, ]