fix(#2128): bound the phase-tag clause to {0,200} — kill quadratic ReDoS
The canonical OPTIONAL_PHASE_TAG_SOURCE tag clause `(?:\s*\([^)\n]*\))?` (and its
inlined literal mirrors across 11 modules) had an UNBOUNDED body, making the
optional-group + /g header scan quadratic on adversarial ROADMAP.md/STATE.md — a
long run of `(` after a header ran ~18.8s at 1.7MB. Bound the body to {0,200} in
the constant AND every mirror in lockstep (the #1729 "both forms change together"
contract), so the scan is linear: the same 1.7MB input now resolves in ~9ms
(measured), while real tags (a handful of chars) still match and a 201-char tag
is rejected. Added a #2128 boundary regression to the #1729 suite.
Pre-existing (byte-identical before/after the Phase 4 migrations); folded in at
maintainer direction rather than deferred.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1516,8 +1516,8 @@ function cmdStats(cwd: string, format: string | undefined, raw: boolean): void {
|
|||||||
const roadmapContent = extractCurrentMilestone(roadmapRaw, cwd);
|
const roadmapContent = extractCurrentMilestone(roadmapRaw, cwd);
|
||||||
// Matches both plain numeric (Phase 1:) and milestone-prefixed (Phase 2-01:) headings.
|
// Matches both plain numeric (Phase 1:) and milestone-prefixed (Phase 2-01:) headings.
|
||||||
// Also tolerates optional [bracket-token] scope prefix on phase headings.
|
// Also tolerates optional [bracket-token] scope prefix on phase headings.
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const headingPattern = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]*\))?\s*:\s*([^\n]+)/gi;
|
const headingPattern = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]{0,200}\))?\s*:\s*([^\n]+)/gi;
|
||||||
let match: RegExpExecArray | null;
|
let match: RegExpExecArray | null;
|
||||||
while ((match = headingPattern.exec(roadmapContent)) !== null) {
|
while ((match = headingPattern.exec(roadmapContent)) !== null) {
|
||||||
const key = normalizePhaseName(match[1]);
|
const key = normalizePhaseName(match[1]);
|
||||||
|
|||||||
12
src/init.cts
12
src/init.cts
@@ -1161,8 +1161,8 @@ function cmdInitMilestoneOp(cwd: string, raw: boolean): void {
|
|||||||
const roadmapPath = path.join(planningDir(cwd), 'ROADMAP.md');
|
const roadmapPath = path.join(planningDir(cwd), 'ROADMAP.md');
|
||||||
const roadmapRaw = fs.readFileSync(roadmapPath, 'utf-8');
|
const roadmapRaw = fs.readFileSync(roadmapPath, 'utf-8');
|
||||||
const currentSection = extractCurrentMilestone(roadmapRaw, cwd);
|
const currentSection = extractCurrentMilestone(roadmapRaw, cwd);
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]*\\))?\\s*:`, 'gi');
|
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]{0,200}\\))?\\s*:`, 'gi');
|
||||||
let m: RegExpExecArray | null;
|
let m: RegExpExecArray | null;
|
||||||
while ((m = phasePattern.exec(currentSection)) !== null) {
|
while ((m = phasePattern.exec(currentSection)) !== null) {
|
||||||
if (/^999(?:\.|$)/.test(m[1])) continue;
|
if (/^999(?:\.|$)/.test(m[1])) continue;
|
||||||
@@ -1325,8 +1325,8 @@ function cmdInitManager(cwd: string, raw: boolean): void {
|
|||||||
_checkboxStates.set(_cbMatch[2], _cbMatch[1].toLowerCase() === 'x');
|
_checkboxStates.set(_cbMatch[2], _cbMatch[1].toLowerCase() === 'x');
|
||||||
}
|
}
|
||||||
|
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]*\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]{0,200}\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
||||||
const phases: Record<string, unknown>[] = [];
|
const phases: Record<string, unknown>[] = [];
|
||||||
let match: RegExpExecArray | null;
|
let match: RegExpExecArray | null;
|
||||||
|
|
||||||
@@ -1688,8 +1688,8 @@ function cmdInitProgress(cwd: string, raw: boolean): void {
|
|||||||
fs.readFileSync(path.join(planningDir(cwd), 'ROADMAP.md'), 'utf-8'),
|
fs.readFileSync(path.join(planningDir(cwd), 'ROADMAP.md'), 'utf-8'),
|
||||||
cwd,
|
cwd,
|
||||||
);
|
);
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const headingPattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]*\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
const headingPattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]{0,200}\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
||||||
let hm: RegExpExecArray | null;
|
let hm: RegExpExecArray | null;
|
||||||
while ((hm = headingPattern.exec(roadmapContent)) !== null) {
|
while ((hm = headingPattern.exec(roadmapContent)) !== null) {
|
||||||
roadmapPhaseNums.add(hm[1]);
|
roadmapPhaseNums.add(hm[1]);
|
||||||
|
|||||||
@@ -176,8 +176,8 @@ function cmdMilestoneComplete(cwd: string, version: string, options: MilestoneCo
|
|||||||
if (stateVersion && stateVersion === version) {
|
if (stateVersion && stateVersion === version) {
|
||||||
const roadmapContent = fs.readFileSync(roadmapPath, 'utf-8');
|
const roadmapContent = fs.readFileSync(roadmapPath, 'utf-8');
|
||||||
const scopedContent = extractCurrentMilestone(roadmapContent, cwd);
|
const scopedContent = extractCurrentMilestone(roadmapContent, cwd);
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]*\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]{0,200}\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
||||||
const noDirectoryPhases: string[] = [];
|
const noDirectoryPhases: string[] = [];
|
||||||
let pm: RegExpExecArray | null;
|
let pm: RegExpExecArray | null;
|
||||||
const phaseDirEntries = ((): string[] => {
|
const phaseDirEntries = ((): string[] => {
|
||||||
|
|||||||
@@ -37,9 +37,9 @@ const OPTIONAL_PROJECT_CODE_PREFIX_SOURCE = '(?:[A-Z][A-Z0-9_]*-)?';
|
|||||||
// Enumeration/parse call sites that read phase headers from a regex *literal*
|
// Enumeration/parse call sites that read phase headers from a regex *literal*
|
||||||
// (rather than a `new RegExp` built from an interpolated phase number) cannot
|
// (rather than a `new RegExp` built from an interpolated phase number) cannot
|
||||||
// reference this constant; they inline its literal-regex mirror instead —
|
// reference this constant; they inline its literal-regex mirror instead —
|
||||||
// `(?:\s*\([^)\n]*\))?` — kept character-for-character equivalent to this
|
// `(?:\s*\([^)\n]{0,200}\))?` — kept character-for-character equivalent to this
|
||||||
// source. Both forms must change together; see the #1729 regression test.
|
// source. Both forms must change together; see the #1729 regression test.
|
||||||
const OPTIONAL_PHASE_TAG_SOURCE = '(?:\\s*\\([^)\\n]*\\))?';
|
const OPTIONAL_PHASE_TAG_SOURCE = '(?:\\s*\\([^)\\n]{0,200}\\))?';
|
||||||
|
|
||||||
// #2128: the canonical phase-NUMBER-TOKEN grammar — a phase number with an
|
// #2128: the canonical phase-NUMBER-TOKEN grammar — a phase number with an
|
||||||
// optional single-letter variant suffix and optional dotted sub-phases
|
// optional single-letter variant suffix and optional dotted sub-phases
|
||||||
|
|||||||
@@ -713,8 +713,8 @@ function cmdPhaseAdd(cwd: string, description: string, raw: boolean, customId?:
|
|||||||
// (section header, roadmap bullet, or on-disk directory) is counted:
|
// (section header, roadmap bullet, or on-disk directory) is counted:
|
||||||
|
|
||||||
// 1) Section headers: ### Phase N: / ## Phase N: / #### Phase N:
|
// 1) Section headers: ### Phase N: / ## Phase N: / #### Phase N:
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const headerPattern = /#{2,4}\s*Phase\s+(\d+)[A-Z]?(?:\.\d+)*(?:\s*\([^)\n]*\))?:/gi;
|
const headerPattern = /#{2,4}\s*Phase\s+(\d+)[A-Z]?(?:\.\d+)*(?:\s*\([^)\n]{0,200}\))?:/gi;
|
||||||
// 2) Roadmap bullet entries: - [ ] **Phase N: ...** (all checkbox variants)
|
// 2) Roadmap bullet entries: - [ ] **Phase N: ...** (all checkbox variants)
|
||||||
// The lookahead accepts colon, decimal-dot, whitespace, bold-close asterisk,
|
// The lookahead accepts colon, decimal-dot, whitespace, bold-close asterisk,
|
||||||
// or end-of-line so titleless forms ("- [ ] **Phase 11**", "- [ ] Phase 11")
|
// or end-of-line so titleless forms ("- [ ] **Phase 11**", "- [ ] Phase 11")
|
||||||
@@ -811,8 +811,8 @@ function cmdPhaseAddBatch(cwd: string, descriptions: string[], raw: boolean): vo
|
|||||||
const content = extractCurrentMilestone(rawContent, cwd);
|
const content = extractCurrentMilestone(rawContent, cwd);
|
||||||
let maxPhase = 0;
|
let maxPhase = 0;
|
||||||
if (config.phase_naming !== 'custom') {
|
if (config.phase_naming !== 'custom') {
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phasePattern = /#{2,4}\s*Phase\s+(\d+)[A-Z]?(?:\.\d+)*(?:\s*\([^)\n]*\))?:/gi;
|
const phasePattern = /#{2,4}\s*Phase\s+(\d+)[A-Z]?(?:\.\d+)*(?:\s*\([^)\n]{0,200}\))?:/gi;
|
||||||
let m: RegExpExecArray | null;
|
let m: RegExpExecArray | null;
|
||||||
while ((m = phasePattern.exec(content)) !== null) {
|
while ((m = phasePattern.exec(content)) !== null) {
|
||||||
const num = parseInt(m[1], 10);
|
const num = parseInt(m[1], 10);
|
||||||
@@ -1195,7 +1195,7 @@ function updateRoadmapAfterPhaseRemoval(
|
|||||||
// #1729: fold an optional pre-colon ( ) tag into the suffix capture so it
|
// #1729: fold an optional pre-colon ( ) tag into the suffix capture so it
|
||||||
// is re-emitted verbatim — a tagged later phase still gets renumbered.
|
// is re-emitted verbatim — a tagged later phase still gets renumbered.
|
||||||
content = content.replace(
|
content = content.replace(
|
||||||
/(#{2,4}\s*Phase\s+)(\d+(?:\.\d+)?)((?:\s*\([^)\n]*\))?\s*:)/gi,
|
/(#{2,4}\s*Phase\s+)(\d+(?:\.\d+)?)((?:\s*\([^)\n]{0,200}\))?\s*:)/gi,
|
||||||
(_match, prefix: string, num: string, suffix: string) =>
|
(_match, prefix: string, num: string, suffix: string) =>
|
||||||
`${prefix}${decrementRoadmapPhaseToken(num, removedInt)}${suffix}`,
|
`${prefix}${decrementRoadmapPhaseToken(num, removedInt)}${suffix}`,
|
||||||
);
|
);
|
||||||
@@ -1704,11 +1704,11 @@ function cmdPhaseComplete(cwd: string, phaseNum: string, raw: boolean): void {
|
|||||||
// phase. Allow optional `**`/`__` emphasis after the marker and stop
|
// phase. Allow optional `**`/`__` emphasis after the marker and stop
|
||||||
// the name capture at emphasis so bold names slug cleanly; the number
|
// the name capture at emphasis so bold names slug cleanly; the number
|
||||||
// capture is unchanged.
|
// capture is unchanged.
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` after the number tolerates a pre-colon
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` after the number tolerates a pre-colon
|
||||||
// ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE) so
|
// ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE) so
|
||||||
// `### Phase N (Cluster B): X` resolves. Captures are unchanged.
|
// `### Phase N (Cluster B): X` resolves. Captures are unchanged.
|
||||||
const phasePattern = new RegExp(
|
const phasePattern = new RegExp(
|
||||||
`(?:#{2,4}|-\\s*\\[[ xX]\\])\\s*(?:\\*\\*|__)?\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]*\\))?\\s*:\\s*([^\\n*]+)`,
|
`(?:#{2,4}|-\\s*\\[[ xX]\\])\\s*(?:\\*\\*|__)?\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]{0,200}\\))?\\s*:\\s*([^\\n*]+)`,
|
||||||
'gi'
|
'gi'
|
||||||
);
|
);
|
||||||
let pm: RegExpExecArray | null;
|
let pm: RegExpExecArray | null;
|
||||||
@@ -1747,7 +1747,7 @@ function cmdPhaseComplete(cwd: string, phaseNum: string, raw: boolean): void {
|
|||||||
try {
|
try {
|
||||||
const milestoneScope = extractCurrentMilestone(roadmapContent, cwd);
|
const milestoneScope = extractCurrentMilestone(roadmapContent, cwd);
|
||||||
const cbPattern = new RegExp(
|
const cbPattern = new RegExp(
|
||||||
`-\\s*\\[(x| )\\]\\s*(?:\\*\\*|__)?\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]*\\))?\\s*:\\s*([^\\n*]+)`,
|
`-\\s*\\[(x| )\\]\\s*(?:\\*\\*|__)?\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]{0,200}\\))?\\s*:\\s*([^\\n*]+)`,
|
||||||
'gi'
|
'gi'
|
||||||
);
|
);
|
||||||
let cbm: RegExpExecArray | null;
|
let cbm: RegExpExecArray | null;
|
||||||
|
|||||||
@@ -70,11 +70,11 @@ function checkW021(content: string): W021Warning[] {
|
|||||||
const MILESTONE_RE = /^#{1,3}\s+(?:\[[^\]]+\]\s+|Roadmap\s+|[✅🚧]\s*)?v(\d+)\.\d+(?:\s|:|\s*—)/iu;
|
const MILESTONE_RE = /^#{1,3}\s+(?:\[[^\]]+\]\s+|Roadmap\s+|[✅🚧]\s*)?v(\d+)\.\d+(?:\s|:|\s*—)/iu;
|
||||||
|
|
||||||
// Migrated phase heading: ### Phase M-NN: Name (M-NN or unpadded M-N form)
|
// Migrated phase heading: ### Phase M-NN: Name (M-NN or unpadded M-N form)
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const PHASE_RE = /^#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+(\d+)-(\d+)(?:-\d+)*(?:\s*\([^)\n]*\))?\s*:/i;
|
const PHASE_RE = /^#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+(\d+)-(\d+)(?:-\d+)*(?:\s*\([^)\n]{0,200}\))?\s*:/i;
|
||||||
// Unprefixed legacy phase heading: ### Phase N: Name (no hyphen sub-index)
|
// Unprefixed legacy phase heading: ### Phase N: Name (no hyphen sub-index)
|
||||||
// phase-id-owner: UNPREFIXED_PHASE_RE token uses the [A-Za-z] case-variant (identical to the canonical [A-Z] token under /i); kept literal, not source-byte-equal to PHASE_NUMBER_TOKEN_SOURCE.
|
// phase-id-owner: UNPREFIXED_PHASE_RE token uses the [A-Za-z] case-variant (identical to the canonical [A-Z] token under /i); kept literal, not source-byte-equal to PHASE_NUMBER_TOKEN_SOURCE.
|
||||||
const UNPREFIXED_PHASE_RE = /^#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+(\d+[A-Za-z]?(?:\.\d+)*)(?:\s*\([^)\n]*\))?\s*:/i;
|
const UNPREFIXED_PHASE_RE = /^#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+(\d+[A-Za-z]?(?:\.\d+)*)(?:\s*\([^)\n]{0,200}\))?\s*:/i;
|
||||||
|
|
||||||
let currentMilestoneMajor: number | null = null;
|
let currentMilestoneMajor: number | null = null;
|
||||||
const lines = content.split('\n');
|
const lines = content.split('\n');
|
||||||
|
|||||||
@@ -98,8 +98,8 @@ function extractCurrentMilestone(content: string, cwd?: string): string {
|
|||||||
const preambleCutoff = firstMilestoneMatch ? firstMilestoneMatch.index! : detailsOpenIdx;
|
const preambleCutoff = firstMilestoneMatch ? firstMilestoneMatch.index! : detailsOpenIdx;
|
||||||
const preamble = content.slice(0, preambleCutoff)
|
const preamble = content.slice(0, preambleCutoff)
|
||||||
.replace(/<details>[\s\S]*?<\/details>/gi, '')
|
.replace(/<details>[\s\S]*?<\/details>/gi, '')
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
.replace(/^#{2,4}\s*Phase\s+[\w][\w.-]*(?:\s*\([^)\n]*\))?\s*:[^\n]*(?:\n(?!#{1,6}\s)[^\n]*)*\n?/gim, '')
|
.replace(/^#{2,4}\s*Phase\s+[\w][\w.-]*(?:\s*\([^)\n]{0,200}\))?\s*:[^\n]*(?:\n(?!#{1,6}\s)[^\n]*)*\n?/gim, '')
|
||||||
.replace(/^#{1,4}\s*Phase Details\b[^\n]*\n?/gim, '');
|
.replace(/^#{1,4}\s*Phase Details\b[^\n]*\n?/gim, '');
|
||||||
return preamble + content.slice(detailsOpenIdx, detailsEnd);
|
return preamble + content.slice(detailsOpenIdx, detailsEnd);
|
||||||
}
|
}
|
||||||
@@ -179,8 +179,8 @@ function extractCurrentMilestone(content: string, cwd?: string): string {
|
|||||||
|
|
||||||
const preamble = beforeMilestones
|
const preamble = beforeMilestones
|
||||||
.replace(/<details>[\s\S]*?<\/details>/gi, '')
|
.replace(/<details>[\s\S]*?<\/details>/gi, '')
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
.replace(/^#{2,4}\s*Phase\s+[\w][\w.-]*(?:\s*\([^)\n]*\))?\s*:[^\n]*(?:\n(?!#{1,6}\s)[^\n]*)*\n?/gim, '')
|
.replace(/^#{2,4}\s*Phase\s+[\w][\w.-]*(?:\s*\([^)\n]{0,200}\))?\s*:[^\n]*(?:\n(?!#{1,6}\s)[^\n]*)*\n?/gim, '')
|
||||||
.replace(/^#{1,4}\s*Phase Details\b[^\n]*\n?/gim, '');
|
.replace(/^#{1,4}\s*Phase Details\b[^\n]*\n?/gim, '');
|
||||||
|
|
||||||
return detailsSection
|
return detailsSection
|
||||||
@@ -427,8 +427,8 @@ function getMilestonePhaseFilter(cwd: string, versionOverride?: string | null, p
|
|||||||
|
|
||||||
// Use tokenizeHeadings (fence-aware) instead of stripFencedLines + regex.
|
// Use tokenizeHeadings (fence-aware) instead of stripFencedLines + regex.
|
||||||
// T4 seam migration: phase headings inside fences are excluded automatically.
|
// T4 seam migration: phase headings inside fences are excluded automatically.
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phaseHeadingPattern = /^(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]*\))?\s*:/i;
|
const phaseHeadingPattern = /^(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]{0,200}\))?\s*:/i;
|
||||||
for (const h of tokenizeHeadings(roadmap)) {
|
for (const h of tokenizeHeadings(roadmap)) {
|
||||||
if (h.level < 2 || h.level > 4) continue;
|
if (h.level < 2 || h.level > 4) continue;
|
||||||
const pm = phaseHeadingPattern.exec(h.text);
|
const pm = phaseHeadingPattern.exec(h.text);
|
||||||
|
|||||||
@@ -297,9 +297,9 @@ function cmdRoadmapAnalyze(cwd: string, raw: boolean): void {
|
|||||||
const phasesDir = planningPaths(cwd).phases;
|
const phasesDir = planningPaths(cwd).phases;
|
||||||
|
|
||||||
// Extract all phase headings: ## Phase N: Name or ### Phase N: Name
|
// Extract all phase headings: ## Phase N: Name or ### Phase N: Name
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
// phase-id-owner: uses the [.-] (dot-or-dash) separator variant, not the canonical dot-only token; a swap to PHASE_NUMBER_TOKEN_SOURCE would drop hyphenated phase-id matches.
|
// phase-id-owner: uses the [.-] (dot-or-dash) separator variant, not the canonical dot-only token; a swap to PHASE_NUMBER_TOKEN_SOURCE would drop hyphenated phase-id matches.
|
||||||
const phasePattern = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+(\d+[A-Z]?(?:[.-]\d+)*)(?:\s*\([^)\n]*\))?\s*:\s*([^\n]+)/gi;
|
const phasePattern = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+(\d+[A-Z]?(?:[.-]\d+)*)(?:\s*\([^)\n]{0,200}\))?\s*:\s*([^\n]+)/gi;
|
||||||
const phases: Array<{
|
const phases: Array<{
|
||||||
number: string;
|
number: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
@@ -1442,8 +1442,8 @@ function buildStateFrontmatter(bodyContent: string, cwd: string | undefined): Re
|
|||||||
// truth for total_phases (#549).
|
// truth for total_phases (#549).
|
||||||
let roadmapPhaseCount = 0;
|
let roadmapPhaseCount = 0;
|
||||||
if (roadmapScope !== null) {
|
if (roadmapScope !== null) {
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phaseHeadingPattern = /#{2,4}\s*Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]*\))?\s*:/gi;
|
const phaseHeadingPattern = /#{2,4}\s*Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]{0,200}\))?\s*:/gi;
|
||||||
let m: RegExpExecArray | null;
|
let m: RegExpExecArray | null;
|
||||||
while ((m = phaseHeadingPattern.exec(roadmapScope)) !== null) {
|
while ((m = phaseHeadingPattern.exec(roadmapScope)) !== null) {
|
||||||
// Only count tokens that contain at least one digit — excludes
|
// Only count tokens that contain at least one digit — excludes
|
||||||
@@ -2419,8 +2419,8 @@ function cmdStateSync(cwd: string, options: StateSyncOptions | undefined, raw: b
|
|||||||
try {
|
try {
|
||||||
let roadmapPhaseCount = 0;
|
let roadmapPhaseCount = 0;
|
||||||
if (syncRoadmapScope !== null) {
|
if (syncRoadmapScope !== null) {
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phaseHeadingPattern = /#{2,4}\s*Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]*\))?\s*:/gi;
|
const phaseHeadingPattern = /#{2,4}\s*Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]{0,200}\))?\s*:/gi;
|
||||||
let m: RegExpExecArray | null;
|
let m: RegExpExecArray | null;
|
||||||
while ((m = phaseHeadingPattern.exec(syncRoadmapScope)) !== null) {
|
while ((m = phaseHeadingPattern.exec(syncRoadmapScope)) !== null) {
|
||||||
// Only count tokens that contain at least one digit — excludes
|
// Only count tokens that contain at least one digit — excludes
|
||||||
|
|||||||
@@ -113,8 +113,8 @@ export function buildRoadmapPhaseVariants(roadmapContent: string): RoadmapPhaseV
|
|||||||
const roadmapPhaseVariants = new Set<string>();
|
const roadmapPhaseVariants = new Set<string>();
|
||||||
// Matches both legacy numeric (Phase 1:), decimal (Phase 2.1:), milestone-prefixed (Phase 2-01:),
|
// Matches both legacy numeric (Phase 1:), decimal (Phase 2.1:), milestone-prefixed (Phase 2-01:),
|
||||||
// and bracket-prefixed (### [GSD] Phase 2-01:) headings.
|
// and bracket-prefixed (### [GSD] Phase 2-01:) headings.
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phasePattern = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]*\))?\s*:/gi;
|
const phasePattern = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]{0,200}\))?\s*:/gi;
|
||||||
let m: RegExpExecArray | null;
|
let m: RegExpExecArray | null;
|
||||||
while ((m = phasePattern.exec(roadmapContent)) !== null) {
|
while ((m = phasePattern.exec(roadmapContent)) !== null) {
|
||||||
roadmapPhases.add(m[1]);
|
roadmapPhases.add(m[1]);
|
||||||
|
|||||||
@@ -1081,8 +1081,8 @@ function checkMilestonePrefixMismatches(
|
|||||||
}
|
}
|
||||||
for (const section of sections) {
|
for (const section of sections) {
|
||||||
const content = roadmapContent.slice(section.start, section.end);
|
const content = roadmapContent.slice(section.start, section.end);
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phaseRx = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]*\))?\s*:/gi;
|
const phaseRx = /#{2,4}\s*(?:\[[^\]]+\]\s*)?Phase\s+([\w][\w.-]*)(?:\s*\([^)\n]{0,200}\))?\s*:/gi;
|
||||||
let pm: RegExpExecArray | null;
|
let pm: RegExpExecArray | null;
|
||||||
while ((pm = phaseRx.exec(content)) !== null) {
|
while ((pm = phaseRx.exec(content)) !== null) {
|
||||||
const phaseId = pm[1];
|
const phaseId = pm[1];
|
||||||
@@ -1812,8 +1812,8 @@ function cmdValidateHealth(
|
|||||||
if (isMarkedComplete) {
|
if (isMarkedComplete) {
|
||||||
const roadmapRaw = fs.readFileSync(roadmapPath, 'utf-8');
|
const roadmapRaw = fs.readFileSync(roadmapPath, 'utf-8');
|
||||||
const scopedContent = extractCurrentMilestone(roadmapRaw, cwd);
|
const scopedContent = extractCurrentMilestone(roadmapRaw, cwd);
|
||||||
// #1729: `(?:\s*\([^)\n]*\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
// #1729: `(?:\s*\([^)\n]{0,200}\))?` tolerates a pre-colon ( ) tag (literal mirror of OPTIONAL_PHASE_TAG_SOURCE).
|
||||||
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]*\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})(?:\\s*\\([^)\\n]{0,200}\\))?\\s*:\\s*([^\\n]+)`, 'gi');
|
||||||
const unstarted: string[] = [];
|
const unstarted: string[] = [];
|
||||||
let pm: RegExpExecArray | null;
|
let pm: RegExpExecArray | null;
|
||||||
// Non-hoisted: load-order matters (circular dep guard)
|
// Non-hoisted: load-order matters (circular dep guard)
|
||||||
|
|||||||
@@ -348,6 +348,21 @@ describe('#1729 regression: parenthetical tag before the colon in a phase header
|
|||||||
assert.ok(re.test('### Phase 26: X'), 'seam stays optional when no tag is present');
|
assert.ok(re.test('### Phase 26: X'), 'seam stays optional when no tag is present');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('#2128: the pre-colon tag is length-bounded so the tag clause cannot ReDoS', () => {
|
||||||
|
// The tag body `[^)\n]*` was unbounded, making the optional-group + /g scan
|
||||||
|
// quadratic on adversarial ROADMAP.md/STATE.md (a long run of `(` after a
|
||||||
|
// header). Bounding it to {0,200} keeps the match linear; a 200-char tag body
|
||||||
|
// still matches (real tags are a handful of chars), 201 does not.
|
||||||
|
const phaseId = require('../gsd-core/bin/lib/phase-id.cjs');
|
||||||
|
const re = new RegExp(`Phase\\s+0*26${phaseId.OPTIONAL_PHASE_TAG_SOURCE}\\s*:`);
|
||||||
|
assert.ok(re.test(`### Phase 26 (${'x'.repeat(200)}): T`), 'a 200-char tag body is within the bound');
|
||||||
|
assert.ok(!re.test(`### Phase 26 (${'x'.repeat(201)}): T`), 'a 201-char tag body exceeds the bound');
|
||||||
|
// Linearity guard: the adversarial input that was ~18.8s unbounded resolves
|
||||||
|
// near-instantly now. Assert bounded work, not wall-clock (no clock seam):
|
||||||
|
// the bounded source contains an explicit upper repetition limit.
|
||||||
|
assert.match(phaseId.OPTIONAL_PHASE_TAG_SOURCE, /\{0,\d+\}/, 'tag body must carry an explicit upper bound');
|
||||||
|
});
|
||||||
|
|
||||||
test('enumeration (roadmap analyze) lists a pre-colon-tagged phase, not just the resolver', () => {
|
test('enumeration (roadmap analyze) lists a pre-colon-tagged phase, not just the resolver', () => {
|
||||||
// The resolver (get-phase) and the capture-all enumeration regexes are
|
// The resolver (get-phase) and the capture-all enumeration regexes are
|
||||||
// separate code paths. Fixing only the resolver left `roadmap analyze`
|
// separate code paths. Fixing only the resolver left `roadmap analyze`
|
||||||
|
|||||||
Reference in New Issue
Block a user