Fix potentially cubic-time regex in parsePatch (#647) · kpdecker/jsdiff@de95cca · GitHub
Skip to content

Commit de95cca

Browse files
Fix potentially cubic-time regex in parsePatch (#647)
* Add a test for the existing handling of Mercurial patches (This is the feature involved in #644, and I want to try to avoid regressions. It previously had no automated tests.) * Fix ReDOS * Add release notes
1 parent b9aeede commit de95cca

3 files changed

Lines changed: 149 additions & 4 deletions

File tree

‎release-notes.md‎

Lines changed: 1 addition & 0 deletions

‎src/patch/parse.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,27 @@ export function parsePatch(uniDiff: string): StructuredPatch[] {
2323
break;
2424
}
2525

26-
// Diff index
27-
const header = (/^(?:Index:|diff(?: -r \w+)+)\s+(.+?)\s*$/).exec(line);
28-
if (header) {
29-
index.index = header[1];
26+
// Try to parse the line as a diff header, like
27+
// Index: README.md
28+
// or
29+
// diff -r 9117c6561b0b -r 273ce12ad8f1 .hgignore
30+
// or
31+
// Index: something with multiple words
32+
// and extract the filename (or whatever else is used as an index name)
33+
// from the end (i.e. 'README.md', '.hgignore', or
34+
// 'something with multiple words' in the examples above).
35+
//
36+
// TODO: It seems awkward that we indiscriminately trim off trailing
37+
// whitespace here. Theoretically, couldn't that be meaningful -
38+
// e.g. if the patch represents a diff of a file whose name ends
39+
// with a space? Seems wrong to nuke it.
40+
// But this behaviour has been around since v2.2.1 in 2015, so if
41+
// it's going to change, it should be done cautiously and in a new
42+
// major release, for backwards-compat reasons.
43+
// -- ExplodingCabbage
44+
const headerMatch = (/^(?:Index:|diff(?: -r \w+)+)\s+/).exec(line);
45+
if (headerMatch) {
46+
index.index = line.substring(headerMatch[0].length).trim();
3047
}
3148

3249
i++;

‎test/patch/parse.js‎

Lines changed: 127 additions & 0 deletions

0 commit comments

Comments
 (0)