Skip to content

fix: prevent ReDoS in hunk header pattern - #54

Merged
sergeyt merged 1 commit into
masterfrom
fix/hunk-header-redos
Nov 21, 2026
Merged

sergeyt merged 1 commit into
masterfrom
fix/hunk-header-redos

Conversation

@sergeyt

@sergeyt sergeyt commented Nov 21, 2026

Copy link
Copy Markdown
Owner

The hunk header regex used ,?(\d+)? after each range start. With the comma optional, a run of N digits could be split between (\d+) and (\d+)? in N ways on both the "-" and "+" sides. When the closing " @@" was missing, backtracking tried every combination (cubic time): a ~4 KB line took ~20 s to parse.

Make the comma and the line count a single optional group, (?:,(\d+))?, so each digit run has exactly one way to match. Capture group indexes are unchanged.

Add regression tests for the malformed header and for the parsed ranges of "-1,2 +3,4", "-1 +1", "-5 +5,2" and "-12,3 +34".

Rebuild index.js.

Reported-by: @YulmuFest

The hunk header regex used `,?(\d+)?` after each range start. With the
comma optional, a run of N digits could be split between `(\d+)` and
`(\d+)?` in N ways on both the "-" and "+" sides. When the closing " @@"
was missing, backtracking tried every combination (cubic time): a ~4 KB
line took ~20 s to parse.

Make the comma and the line count a single optional group,
`(?:,(\d+))?`, so each digit run has exactly one way to match. Capture
group indexes are unchanged.

Add regression tests for the malformed header and for the parsed ranges
of "-1,2 +3,4", "-1 +1", "-5 +5,2" and "-12,3 +34".

Rebuild index.js.

Reported-by: @YulmuFest
@sergeyt
sergeyt merged commit 9e7d996 into master Nov 21, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant