From 3a01ac45464f2397f67f88ff113dcdd704c219e7 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Mon, 26 Oct 2026 08:52:28 -0600 Subject: [PATCH] chore: test:redos -> test:recheck (#4167) * add remove only note * remove important redundant note * chore: add args to test:redos * test:recheck --- AGENTS.md | 2 +- package.json | 2 +- test/recheck.ts | 75 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 02103b2a..1869d7b9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -83,7 +83,7 @@ npm run build - `npm run test:specs`: Runs the spec test suite (CommonMark, GFM, new, original, redos). - `npm run test:lint` / `npm run lint`: Runs ESLint / auto-fixes formatting. - `npm run test:types`: Validates TypeScript types (`tsc` + `attw`). -- `npm run test:redos`: Runs `recheck` to scan regex patterns for exponential or polynomial backtracking (run when backtracking concerns are noted). +- `npm run test:recheck`: Runs `recheck` to scan regex patterns for exponential or polynomial backtracking (run when backtracking concerns are noted). Supports filtering with dot notation and an optional `--safe` flag (e.g. `npm run test:recheck -- other.endingHash`, `npm run test:recheck -- inline`, `npm run test:recheck -- --safe`). By default, safe regexps are omitted from output unless `--safe` is specified. - `npm run test`: Full test suite (cleans build, builds docs, runs spec, unit, umd, cjs, types, lint). - `npm run bench`: Runs performance benchmark comparisons against CommonMark and Markdown-it. - `npm run rules`: Outputs the compiled regex rules from `src/rules.ts` (e.g. `npm run rules -- block.gfm.item`). diff --git a/package.json b/package.json index 478a5ee1..c52d257d 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,7 @@ "test:cjs": "node test/cjs-test.cjs", "test:lint": "eslint", "test:only": "npm run build && npm run test:specs:only && npm run test:unit:only", - "test:redos": "node test/recheck.ts > vuln.js", + "test:recheck": "node test/recheck.ts > vuln.js", "test:specs:only": "node --test --test-only --test-reporter=spec test/run-spec-tests.js", "test:specs": "node --test --test-reporter=spec test/run-spec-tests.js", "test:types": "tsc --project tsconfig-type-test.json && attw -P --entrypoints . --profile esm-only", diff --git a/test/recheck.ts b/test/recheck.ts index 0f4c5e8c..571cc143 100644 --- a/test/recheck.ts +++ b/test/recheck.ts @@ -5,7 +5,7 @@ interface RegexpObj { [k: string]: RegExp | string | ((arg: number) => RegExp | string) | ((arg: string) => RegExp | string) | RegexpObj; } -async function checkRegexp(obj: RegexpObj, name: string) { +async function checkRegexp(obj: RegexpObj, name: string, showSafe = false) { await Promise.all(Object.keys(obj).map(async(prop: string) => { const item = obj[prop]; const itemName = `${name}.${prop}`; @@ -20,21 +20,26 @@ async function checkRegexp(obj: RegexpObj, name: string) { // TODO: skip functions for now return; } else { - return checkRegexp(item, itemName); + return checkRegexp(item, itemName, showSafe); } const gfm = itemName.includes('.gfm.'); const pedantic = itemName.includes('.pedantic.'); const recheckObj = await check(source, flags); try { - console.log(`// ${itemName}: /${recheckObj.source}/${recheckObj.flags}`); + const regExp = `// ${itemName}: /${recheckObj.source}/${recheckObj.flags}`; switch (recheckObj.status) { case 'safe': - console.log('// safe'); + if (showSafe) { + console.log(regExp); + console.log('// safe'); + } break; case 'vulnerable': + console.log(regExp); console.log(`// marked(${recheckObj.attack.pattern}, { pedantic: ${pedantic ? 'true' : 'false'}, gfm: ${gfm ? 'true' : 'false'} });`); break; default: + console.log(regExp); console.log('// error:', recheckObj.error); break; } @@ -45,17 +50,63 @@ async function checkRegexp(obj: RegexpObj, name: string) { })); } -console.log(` -import { marked } from '../lib/marked.esm.js'; +async function main() { + const allRules: RegexpObj = { + inline, + block, + other, + }; + + const rawArgs = process.argv.slice(2); + const showSafe = rawArgs.includes('--safe'); + const args = rawArgs.filter(arg => arg !== '--safe'); + let checks: Promise[] = []; + + if (args.length > 0) { + for (const arg of args) { + const parts = arg.split('.'); + let current: RegexpObj[string] | undefined = allRules; + for (const part of parts) { + if (current && typeof current === 'object' && !(current instanceof RegExp) && part in current) { + current = (current as RegexpObj)[part]; + } else { + current = undefined; + break; + } + } + + if (current === undefined) { + console.error(`Rule not found: ${arg}`); + continue; + } + + if (current instanceof RegExp || typeof current === 'string') { + const lastDot = arg.lastIndexOf('.'); + const parentName = lastDot === -1 ? '' : arg.slice(0, lastDot); + const prop = lastDot === -1 ? arg : arg.slice(lastDot + 1); + checks.push(checkRegexp({ [prop]: current }, parentName, showSafe)); + } else if (typeof current === 'object' && current !== null) { + checks.push(checkRegexp(current as RegexpObj, arg, showSafe)); + } + } + } else { + checks = [ + checkRegexp(inline, 'inline', showSafe), + checkRegexp(block, 'block', showSafe), + checkRegexp(other, 'other', showSafe), + ]; + } + + console.log(` +import { marked } from './lib/marked.esm.js'; const start = Date.now(); `); -await Promise.all([ - checkRegexp(inline, 'inline'), - checkRegexp(block, 'block'), - checkRegexp(other, 'other'), -]); + await Promise.all(checks); -console.log(` + console.log(` console.log(Date.now() - start);`); +} + +await main();