/** * Static security rules (src/modules/doctor/rules/securityCode.ts): each pattern found and not found, the * accept marker, the config checks, the doctor wiring — and the GATE: the plugin's own code and the reference site * hold no place that matches a rule unless test/security-patterns.allow.json judges it. */ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' import { afterAll, describe, expect, test } from 'vitest' import { listCodeFiles, scanCode, scanConfigs } from '../src/modules/doctor/rules/securityCode.js' import { runDoctor } from '../src/modules/doctor/run.js' const root = path.resolve(import.meta.dirname, '..') const rules = (file: string, code: string) => scanCode(file, code).map((h) => h.rule) describe('static security rules — each pattern', () => { test('HTML and JSON printed into the page', () => { expect(rules('src/a.tsx', '
')).toEqual(['code-inner-html']) expect(rules('src/a.tsx', '')).toEqual(['code-inner-html']) expect(scanCode('src/a.tsx', '')[0].message).toMatch(/<\/script>/) expect(rules('src/a.tsx', "")).toEqual([]) expect(rules('src/a.tsx', '')).toEqual([]) expect(rules('src/a.ts', 'el.innerHTML = text')).toEqual(['code-inner-html']) expect(rules('src/a.ts', "el.innerHTML = ''")).toEqual([]) expect(rules('src/a.ts', 'el.insertAdjacentHTML("beforeend", x)')).toEqual(['code-inner-html']) }) test('code from strings and shell commands', () => { expect(rules('src/a.ts', 'eval(code)')).toEqual(['code-eval']) expect(rules('src/a.ts', 'const f = new Function("a", body)')).toEqual(['code-eval']) expect(rules('src/a.ts', 'setTimeout("go()", 10)')).toEqual(['code-eval']) expect(rules('src/a.ts', 'setTimeout(() => go(), 10); retrieval(x)')).toEqual([]) expect(rules('src/a.ts', 'execSync(`convert ${file} out.png`)')).toEqual(['code-shell']) expect(rules('src/a.ts', "execFileSync('convert', [file, 'out.png'])")).toEqual([]) }) test('secrets that reach the browser', () => { expect(rules('src/a.ts', 'const k = process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY')).toEqual(['code-public-secret']) expect(rules('src/a.ts', 'const k = process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY')).toEqual([]) expect(rules('src/a.tsx', "'use client'\nconst s = process.env.PAYLOAD_SECRET")).toEqual(['code-public-secret']) expect(rules('src/a.tsx', 'const s = process.env.PAYLOAD_SECRET')).toEqual([]) }) test('the Local API with access off where a request reaches it', () => { expect(rules('src/app/api/x/route.ts', "await payload.find({ collection: 'orders', overrideAccess: true })")).toEqual(['code-override-access']) expect(rules('src/seed/run.ts', "await payload.find({ collection: 'orders', overrideAccess: true })")).toEqual([]) expect(rules('src/app/page.tsx', "await payload.find({ collection: 'orders', user: req.user })")).toEqual(['code-local-api-user']) expect(rules('src/app/page.tsx', "await payload.find({ collection: 'orders', user, overrideAccess: false })")).toEqual([]) expect(rules('src/app/page.tsx', "await payload.find({ collection: 'pages', where: { slug: { equals: slug } } })")).toEqual([]) }) test('a where clause from the request', () => { expect(rules('src/app/api/x/route.ts', "payload.find({ collection: 'pages', where: req.query.where })")).toEqual(['code-raw-where']) expect(rules('src/app/api/x/route.ts', "payload.find({ collection: 'pages', where: JSON.parse(searchParams.get('w')) })")).toEqual(['code-raw-where']) expect(rules('src/app/api/x/route.ts', "payload.find({ collection: 'pages', where: { slug: { equals: String(params.slug) } } })")).toEqual([]) }) test('redirects and fetches to an address from the request', () => { expect(rules('src/app/x/page.tsx', "redirect(searchParams.get('next'))")).toEqual(['code-open-redirect']) expect(rules('src/app/x/route.ts', "return NextResponse.redirect(new URL(req.nextUrl.searchParams.get('to'), req.url))")).toEqual(['code-open-redirect']) expect(rules('src/app/x/page.tsx', "redirect('/pl')")).toEqual([]) expect(rules('src/app/x/route.ts', 'const r = await fetch(body.url)')).toEqual(['code-ssrf']) expect(rules('src/app/x/route.ts', "const r = await fetch(searchParams.get('u'))")).toEqual(['code-ssrf']) expect(rules('src/app/x/route.ts', "const r = await fetch('https://api.example.com/x', { body: JSON.stringify(body) })")).toEqual([]) }) test('a place accepted with its reason; comments do not count', () => { expect(rules('src/a.tsx', '// iapl-security-ok: escaped by toSafeHtml above\n')).toEqual([]) expect(rules('src/a.ts', 'eval(x) // iapl-security-ok: build-time only')).toEqual([]) expect(rules('src/a.ts', '// eval(x) in a comment\n/* new Function() */')).toEqual([]) }) test('config settings that open a door', () => { const next = (code: string) => scanConfigs({ code, file: 'next.config.ts' }, null).map((f) => f.severity) expect(next("images: { dangerouslyAllowSVG: true }")).toEqual(['error']) expect(next("images: { dangerouslyAllowSVG: true, contentSecurityPolicy: \"sandbox\" }")).toEqual([]) expect(next("images: { remotePatterns: [{ hostname: '**' }] }")).toEqual(['warning']) expect(next("experimental: { serverActions: { allowedOrigins: ['*'] } }")).toEqual(['error']) const payload = (code: string) => scanConfigs(null, { code, file: 'src/payload.config.ts' }).length expect(payload("cors: '*', debug: true, graphQL: { disableIntrospectionInProduction: false }, maxDepth: 50")).toBe(4) expect(payload('cors: [process.env.NEXT_PUBLIC_SERVER_URL], maxDepth: 10')).toBe(0) }) }) describe('static security rules — in the doctor', () => { const dirs: string[] = [] afterAll(() => {for (const d of dirs) {rmSync(d, { force: true, recursive: true })}}) test('iapl-doctor --only security reports a project\'s own unsafe code, and an iapl-doctor.json entry silences one file', async () => { const dir = mkdtempSync(path.join(tmpdir(), 'iapl-sec-')) dirs.push(dir) const files: Record