diff --git a/src/exports/doctor.ts b/src/exports/doctor.ts index 7d80aa6..5e12cde 100644 --- a/src/exports/doctor.ts +++ b/src/exports/doctor.ts @@ -17,9 +17,6 @@ export { AUDIT_RULE_MAP, parseToolOutput } from '../modules/doctor/rules/tooling export { applySettings, AUDIT_SKIP_RULES, doctorRules, globToRegExp, runDeepProbe, runDoctor } from '../modules/doctor/run.js' export type { RunDoctorOptions } from '../modules/doctor/run.js' export { buildDoctorEndpoint, collectDoctorStatus } from '../modules/doctor/status.js' -// routes:check — the site's public routes against the judged snapshot (docs/security.md "Routes nobody has judged") -export { runRouteCheck } from '../modules/security/routeCheck.js' -export type { RunRouteCheckOptions } from '../modules/security/routeCheck.js' export type { DoctorEndpointOptions } from '../modules/doctor/status.js' export type { DoctorConfig, @@ -32,3 +29,6 @@ export type { DoctorStatus, RuleFinding, } from '../modules/doctor/types.js' +// routes:check — the site's public routes against the judged snapshot (docs/security.md "Routes nobody has judged") +export { runRouteCheck } from '../modules/security/routeCheck.js' +export type { RunRouteCheckOptions } from '../modules/security/routeCheck.js' diff --git a/src/modules/doctor/rules/security.ts b/src/modules/doctor/rules/security.ts index ff91d18..358e874 100644 --- a/src/modules/doctor/rules/security.ts +++ b/src/modules/doctor/rules/security.ts @@ -65,10 +65,10 @@ export const securityRules: DoctorRule[] = [ // first segment merely STARTS with those letters (/administracja, /apis): no locale redirect, and no CSP from the // proxy on a single-locale site (IAWL idea 36, fixed in the documented literal in 1.17.1). id: 'proxy-matcher', - check: async (ctx) => { + check: (ctx) => { const file = `${ctx.src}proxy.ts` const code = stripComments(ctx.read(file)) - const loose = [...code.matchAll(/\(\?!((?:[\w-]+\|)*[\w-]+)\|/g)].map((m) => m[1]!.split('|')).find((names) => names.some((n) => ['_next', 'admin', 'api'].includes(n))) + const loose = [...code.matchAll(/\(\?!((?:[\w-]+\|)*[\w-]+)\|/g)].map((m) => m[1].split('|')).find((names) => names.some((n) => ['_next', 'admin', 'api'].includes(n))) if (!loose) {return []} return [{ file, diff --git a/src/modules/security/routeCheck.ts b/src/modules/security/routeCheck.ts index adcdcf6..d4ba0c5 100644 --- a/src/modules/security/routeCheck.ts +++ b/src/modules/security/routeCheck.ts @@ -59,6 +59,7 @@ function walk(dir: string): string[] { */ export async function runRouteCheck(options: RunRouteCheckOptions): Promise { const cwd = options.cwd ?? process.cwd() + // eslint-disable-next-line no-console -- a command-line check prints its report const log = options.log ?? ((line: string) => console.log(line)) const appDir = join(cwd, options.appDir ?? 'src/app') const snapshotFile = join(cwd, options.snapshot ?? 'e2e/routes.snapshot.json') diff --git a/src/modules/security/routeInventory.ts b/src/modules/security/routeInventory.ts index 4a632d9..1290aba 100644 --- a/src/modules/security/routeInventory.ts +++ b/src/modules/security/routeInventory.ts @@ -80,7 +80,7 @@ export function listNextRouteFiles( for (const file of files) { const parts = file.split('/').filter((p) => p && !/^\(.*\)$/.test(p)) const name = parts.pop() ?? '' - const base = name.replace(/\.(?:[cm]?[jt]sx?)$/, '') + const base = name.replace(/\.[cm]?[jt]sx?$/, '') const path = `/${parts.join('/')}` if (base === 'route') { const code = read(file) @@ -99,12 +99,12 @@ export function listNextRouteFiles( export type RouteSnapshot = Record export type RouteSnapshotDiff = { + /** Routes the snapshot has and the config no longer opens (not a failure; `--update` drops them). */ + gone: string[] /** Entries whose reason is empty or still says TODO. */ unexplained: string[] /** Routes the config has and the snapshot does not. */ unjudged: string[] - /** Routes the snapshot has and the config no longer opens (not a failure; `--update` drops them). */ - gone: string[] } export function compareRouteSnapshot(current: string[], snapshot: RouteSnapshot): RouteSnapshotDiff { @@ -112,7 +112,7 @@ export function compareRouteSnapshot(current: string[], snapshot: RouteSnapshot) const now = new Set(current) return { gone: [...judged].filter((id) => !now.has(id)).sort(), - unexplained: [...judged].filter((id) => now.has(id) && (!snapshot[id]?.trim() || /^TODO\b/i.test(snapshot[id]!.trim()))).sort(), + unexplained: [...judged].filter((id) => now.has(id) && (!snapshot[id]?.trim() || /^TODO\b/i.test(snapshot[id].trim()))).sort(), unjudged: current.filter((id) => !judged.has(id)), } } diff --git a/test/formAttachments.test.ts b/test/formAttachments.test.ts index 9f62c36..415201e 100644 --- a/test/formAttachments.test.ts +++ b/test/formAttachments.test.ts @@ -369,7 +369,7 @@ describe('3 · refused files leave no submission and send no e-mail', () => { test('a program inside an Office file is found also with trailing dots or spaces (Windows drops them)', async () => { for (const entry of ['word/setup.exe', 'word/setup.exe.', 'word/setup.exe ', 'word/setup.EXE. .', 'word/vbaProject.bin.']) { - const data = zip({ '[Content_Types].xml': '', 'word/document.xml': '', [entry]: 'x' }) + const data = zip({ '[Content_Types].xml': '', [entry]: 'x', 'word/document.xml': '' }) await refused([{ name: 'cv.docx', data }], notAcceptable('cv.docx')) } }) diff --git a/test/proxyMatcher.test.ts b/test/proxyMatcher.test.ts index 5525c61..0407a7b 100644 --- a/test/proxyMatcher.test.ts +++ b/test/proxyMatcher.test.ts @@ -16,13 +16,13 @@ const { getMiddlewareMatchers } = createRequire(import.meta.url)(path.join(next, getMiddlewareMatchers: (matchers: string[], nextConfig: object) => { regexp: string }[] } const runs = (matcher: string) => { - const re = new RegExp(getMiddlewareMatchers([matcher], {})[0]!.regexp) + const re = new RegExp(getMiddlewareMatchers([matcher], {})[0].regexp) return (pathname: string) => re.test(pathname) } describe('the proxy matcher', () => { test('skips /api, /admin and /_next as whole segments and files; runs on pages that only START with those letters', () => { - const proxy = runs(DEFAULT_PROXY_MATCHER[0]!) + const proxy = runs(DEFAULT_PROXY_MATCHER[0]) for (const skipped of ['/api', '/api/users', '/api/ipal/hit', '/admin', '/admin/', '/admin/collections/users', '/_next/static/chunk.js', '/robots.txt', '/sitemap.xml']) { expect([skipped, proxy(skipped)]).toEqual([skipped, false]) } @@ -33,6 +33,6 @@ describe('the proxy matcher', () => { test('the reference site uses the same literal (Next reads it statically, it cannot be imported)', () => { const proxyFile = readFileSync(path.join(root, 'reference/src/proxy.ts'), 'utf8') - expect(proxyFile).toContain(`matcher: ['${DEFAULT_PROXY_MATCHER[0]!.replace(/\\/g, '\\\\')}']`) + expect(proxyFile).toContain(`matcher: ['${DEFAULT_PROXY_MATCHER[0].replace(/\\/g, '\\\\')}']`) }) }) diff --git a/test/routeInventory.test.ts b/test/routeInventory.test.ts index 67dba44..17d7cc3 100644 --- a/test/routeInventory.test.ts +++ b/test/routeInventory.test.ts @@ -16,8 +16,8 @@ import { describe, expect, test, vi } from 'vitest' import type { GraphqlSchemaLike } from '../src/modules/security/routeInventory.js' -import { iapl } from '../src/plugin.js' import { compareRouteSnapshot, listNextRouteFiles, listPublicRoutes, updateRouteSnapshot } from '../src/modules/security/routeInventory.js' +import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' vi.mock('server-only', () => ({})) @@ -67,7 +67,7 @@ describe('listPublicRoutes', () => { const config = await build(adapter()) lists[name] = listPublicRoutes(config, { graphqlSchema: await configToSchema(config) }) } - const sqlite = lists.sqlite! + const sqlite = lists.sqlite expect(lists.mongodb).toEqual(sqlite) expect(lists.postgres).toEqual(sqlite) for (const id of [ @@ -88,15 +88,15 @@ describe('listPublicRoutes', () => { test('Next route files: exported methods, route groups left out, metadata routes', () => { const code: Record = { + '(frontend)/[locale]/page.tsx': 'export default function Page() {}', '(payload)/api/[...slug]/route.ts': 'export const GET = a\nexport const POST = b\nexport const OPTIONS = c', 'api/health/route.ts': 'export async function GET() {}', 'api/mcp/route.ts': "export { mcpGET as GET, mcpPOST as POST } from '@/lib/content'", 'md/[locale]/[[...slug]]/route.ts': 'export const dynamic = "force-dynamic"\nexport function GET() {}', 'robots.ts': 'export default function robots() {}', 'sitemap.xml/route.ts': 'export const GET = handler', - '(frontend)/[locale]/page.tsx': 'export default function Page() {}', } - expect(listNextRouteFiles(Object.keys(code), (f) => code[f]!)).toEqual([ + expect(listNextRouteFiles(Object.keys(code), (f) => code[f])).toEqual([ { methods: ['GET', 'OPTIONS', 'POST'], path: '/api/[...slug]' }, { methods: ['GET'], path: '/api/health' }, { methods: ['GET', 'POST'], path: '/api/mcp' }, diff --git a/test/starter.test.ts b/test/starter.test.ts index e1b53fb..eca523a 100644 --- a/test/starter.test.ts +++ b/test/starter.test.ts @@ -294,7 +294,7 @@ describe('starter: links and paths (IAWL ideas 12, 13)', () => { const dir = path.join(parent, 'site') try { for (const bad of ['../escaped.txt', 'a/../../escaped.txt', path.join(parent, 'abs.txt')]) { - expect(() => writeFiles(dir, { 'ok.txt': 'fine', [bad]: 'no' }), bad).toThrow(/outside the new folder/) + expect(() => writeFiles(dir, { [bad]: 'no', 'ok.txt': 'fine' }), bad).toThrow(/outside the new folder/) } expect(readdirSync(parent)).toEqual([]) writeFiles(dir, { 'ok.txt': 'fine', 'src/a/b.ts': 'x' })